Periodic extension

Below is an implementation of the concept of periodic extension in Mathematica.  The function  PerExt is a function of five variables:  x  this is an arbitrary real number, ff is a function defined on a half-open interval [aa,bb) (here we assume that   aa<bb) and we consider  aa and  bb as variable in this function.  It is important to note that  ff must be given as a Pure Function function

Clear[PerExt,ff,aa,bb,x];PerExt[x_,ff_,aa_,bb_]:=ff[x-(Floor[((x-aa)/(bb-aa))])(bb-aa)]

Example of a pure function

(((#)^2))&[4]

The square function defined on the interval [-2,1] and then periodically extended:

Plot[PerExt[x,(((#)^2))&,-2,1],{x,-4,4},PlotRange->{-1,5},Exclusions->Range[-32,10,3]]

Or, with a continuous periodic extension, the square function defined on [-1,1], and periodically extended:

Plot[PerExt[x,(((#)^2))&,-1,1],{x,-4,4},PlotRange->{-0.5,4},Exclusions->Range[-32,10,3]]

In the above example the given function is (x^2). Below we show this function and its periodic extension with period 2:

Show[Plot[(((#)^2))&[x],{x,-2,2},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],Plot[PerExt[x,(((#)^2))&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{-0.5,4},AspectRatio->Automatic,ImageSize->600]

More examples:

Show[Plot[(Abs[#])&[x],{x,-5,5},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],Plot[PerExt[x,(Abs[#])&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{-0.5,3},AspectRatio->Automatic,ImageSize->600]

Show[Plot[(#)&[x],{x,-5,5},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],Plot[PerExt[x,(#)&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{-2,2},AspectRatio->Automatic,ImageSize->600]

Show[Plot[(Sin[#])&[x],{x,-5 Pi,5 Pi},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],Plot[PerExt[x,(Sin[#])&,-(Pi/2),(Pi/2)],{x,-4 Pi,4 Pi},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{{-3 Pi,3 Pi},{-2,2}},Ticks->{Range[-6 Pi,6 Pi,Pi/2],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

Show[Plot[(Cos[#])&[x],{x,-5 Pi,5 Pi},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],Plot[PerExt[x,(Cos[#])&,-(Pi/2),(Pi/2)],{x,-4 Pi,4 Pi},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{{-3 Pi,3 Pi},{-2,2}},Ticks->{Range[-6 Pi,6 Pi,Pi/2],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

Show[Plot[(UnitStep[#])&[x],{x,-6,6},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}},Exclusions->Automatic],Plot[PerExt[x,(UnitStep[#])&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}],PlotRange->{{-3 ,3},{-1,2}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

Fourier series

Example 1, the unit step function on the interval (-1,1)

Let us find the Fourier series of the function

Show[Plot[(UnitStep[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}},Exclusions->{0}],PlotRange->{{-3 ,3},{-1,2}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

The coefficient (Subscript[a, 0]) is

(1/2)Integrate[1,{x,0,1}]

The coefficients (Subscript[a, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Cos[k Pi x],{x,0,1}],And[k∈Integers,k>0]]

The coefficients (Subscript[b, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Sin[k Pi x],{x,0,1}],And[k∈Integers,k>0]]

Thus the partial sum with 40 terms of the Fourier Series is

nn=20;(1/2)+Sum[(2/((2 k -1) Pi))Sin[(2 k -1) Pi x],{k,1,nn}]

Verify this with graphs

Show[Plot[(UnitStep[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}},Exclusions->{0}],Plot[(1/2)+((2 Sin[π x])/π)+((2 Sin[3 π x])/(3 π))+((2 Sin[5 π x])/(5 π))+((2 Sin[7 π x])/(7 π))+((2 Sin[9 π x])/(9 π))+((2 Sin[11 π x])/(11 π))+((2 Sin[13 π x])/(13 π))+((2 Sin[15 π x])/(15 π))+((2 Sin[17 π x])/(17 π))+((2 Sin[19 π x])/(19 π))+((2 Sin[21 π x])/(21 π))+((2 Sin[23 π x])/(23 π))+((2 Sin[25 π x])/(25 π))+((2 Sin[27 π x])/(27 π))+((2 Sin[29 π x])/(29 π))+((2 Sin[31 π x])/(31 π))+((2 Sin[33 π x])/(33 π))+((2 Sin[35 π x])/(35 π))+((2 Sin[37 π x])/(37 π))+((2 Sin[39 π x])/(39 π)),{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

It is nice to include the periodic extension

Plot[PerExt[x,(UnitStep[#])&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[1,0,0],Thickness[0.005]}}]

Show[Plot[PerExt[x,(UnitStep[#])&,-1,1],{x,-6,6},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}},Exclusions->Range[-10,10,1]],Plot[(1/2)+((2 Sin[π x])/π)+((2 Sin[3 π x])/(3 π))+((2 Sin[5 π x])/(5 π))+((2 Sin[7 π x])/(7 π))+((2 Sin[9 π x])/(9 π))+((2 Sin[11 π x])/(11 π))+((2 Sin[13 π x])/(13 π))+((2 Sin[15 π x])/(15 π))+((2 Sin[17 π x])/(17 π))+((2 Sin[19 π x])/(19 π))+((2 Sin[21 π x])/(21 π))+((2 Sin[23 π x])/(23 π))+((2 Sin[25 π x])/(25 π))+((2 Sin[27 π x])/(27 π))+((2 Sin[29 π x])/(29 π))+((2 Sin[31 π x])/(31 π))+((2 Sin[33 π x])/(33 π))+((2 Sin[35 π x])/(35 π))+((2 Sin[37 π x])/(37 π))+((2 Sin[39 π x])/(39 π)),{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

It might be interesting to include different partial sums of the Fourier series:

Manipulate[Show[Plot[PerExt[x,(UnitStep[#])&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}},Exclusions->Range[-10,10,1]],Plot[(1/2)+Sum[(2/((2 k -1) Pi))Sin[(2 k -1) Pi x],{k,1,nn}],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600],{{nn,3},Range[0,20],Setter},ControlPlacement->Top]

Example 2, the absolute value function on the interval (-1,1)

Let us find the Fourier series of the function

Show[Plot[(Abs[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

The coefficient (Subscript[a, 0]) is

(1/2)Integrate[Abs[x],{x,-1,1}]

The coefficients (Subscript[a, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Abs[x] Cos[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

The coefficients (Subscript[b, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Abs[x] Sin[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

Thus the partial sum with 40 terms of the Fourier Series is

nn=20;(1/2)+Sum[((-4)/((((2 k -1))^2) (Pi^2)))Cos[(2 k -1) Pi x],{k,1,nn}]

Verify this with graphs

Show[Plot[(Abs[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[(1/2)-((4 Cos[π x])/(π^2))-((4 Cos[3 π x])/(9 (π^2)))-((4 Cos[5 π x])/(25 (π^2)))-((4 Cos[7 π x])/(49 (π^2)))-((4 Cos[9 π x])/(81 (π^2)))-((4 Cos[11 π x])/(121 (π^2)))-((4 Cos[13 π x])/(169 (π^2)))-((4 Cos[15 π x])/(225 (π^2)))-((4 Cos[17 π x])/(289 (π^2)))-((4 Cos[19 π x])/(361 (π^2)))-((4 Cos[21 π x])/(441 (π^2)))-((4 Cos[23 π x])/(529 (π^2)))-((4 Cos[25 π x])/(625 (π^2)))-((4 Cos[27 π x])/(729 (π^2)))-((4 Cos[29 π x])/(841 (π^2)))-((4 Cos[31 π x])/(961 (π^2)))-((4 Cos[33 π x])/(1089 (π^2)))-((4 Cos[35 π x])/(1225 (π^2)))-((4 Cos[37 π x])/(1369 (π^2)))-((4 Cos[39 π x])/(1521 (π^2))),{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

It is nice to include the periodic extension

Show[Plot[PerExt[x,(Abs[#])&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[(1/2)-((4 Cos[π x])/(π^2))-((4 Cos[3 π x])/(9 (π^2)))-((4 Cos[5 π x])/(25 (π^2)))-((4 Cos[7 π x])/(49 (π^2)))-((4 Cos[9 π x])/(81 (π^2)))-((4 Cos[11 π x])/(121 (π^2)))-((4 Cos[13 π x])/(169 (π^2)))-((4 Cos[15 π x])/(225 (π^2)))-((4 Cos[17 π x])/(289 (π^2)))-((4 Cos[19 π x])/(361 (π^2)))-((4 Cos[21 π x])/(441 (π^2)))-((4 Cos[23 π x])/(529 (π^2)))-((4 Cos[25 π x])/(625 (π^2)))-((4 Cos[27 π x])/(729 (π^2)))-((4 Cos[29 π x])/(841 (π^2)))-((4 Cos[31 π x])/(961 (π^2)))-((4 Cos[33 π x])/(1089 (π^2)))-((4 Cos[35 π x])/(1225 (π^2)))-((4 Cos[37 π x])/(1369 (π^2)))-((4 Cos[39 π x])/(1521 (π^2))),{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600]

It might be interesting to include different partial sums of the Fourier series:

Manipulate[Show[Plot[PerExt[x,(Abs[#])&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[(1/2)+Sum[((-4)/((((2 k -1))^2) (Pi^2)))Cos[(2 k -1) Pi x],{k,1,nn}],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-0.5,1.5}},Ticks->{Range[-6,6,1],Range[-2,2,1]},AspectRatio->Automatic,ImageSize->600],{{nn,3},Range[0,20],Setter},ControlPlacement->Top]

Example 3, the exponential function on the interval (-1,1)

Let us find the Fourier series of the function

Show[Plot[(Exp[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],AxesOrigin->0, PlotRange->{{-3 ,3},{-0.2,3}},AspectRatio->Automatic,ImageSize->600]

The coefficient (Subscript[a, 0]) is

FullSimplify[(1/2)Integrate[Exp[x],{x,-1,1}]]

FullSimplify[(1/2)Integrate[Exp[x],{x,-1,1}]==Sinh[1]]

The coefficients (Subscript[a, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Exp[x] Cos[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

FullSimplify[(((((-1))^k) (-1+(E^2)))/(E+E (k^2) (π^2)))==2 Sinh[1]*((((-1))^k)/(1+(((k Pi))^2)))]

The coefficients (Subscript[b, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[Exp[x] Sin[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

FullSimplify[(((((-1))^(1+k)) (-1+(E^2)) k π)/(E+E (k^2) (π^2)))==-2 Pi Sinh[1]*(((((-1))^k) k)/(1+(((k Pi))^2)))]

Thus the partial sum with 40 terms of the Fourier Series is

nn=20;Clear[FS3];FS3[x_]=Sinh[1]+2 Sinh[1]Sum[((((-1))^k)/(1+(((k Pi))^2)))Cos[k Pi x],{k,1,nn}]-2 Pi Sinh[1]Sum[(((((-1))^k) k)/(1+(((k Pi))^2)))Sin[k Pi x],{k,1,nn}]

Verify this with graphs

Show[Plot[(Exp[#])&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS3[x],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],AxesOrigin->0, PlotRange->{{-3 ,3},{-0.2,3}},AspectRatio->Automatic,ImageSize->600]

It is nice to include the periodic extension

Show[Plot[PerExt[x,(Exp[#])&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS3[x],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],AxesOrigin->0, PlotRange->{{-3 ,3},{-0.2,3}},AspectRatio->Automatic,ImageSize->600]

It might be interesting to include different partial sums of the Fourier series:

Manipulate[Show[Plot[PerExt[x,(Exp[#])&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[Sinh[1]+2 Sinh[1]Sum[((((-1))^k)/(1+(((k Pi))^2)))Cos[k Pi x],{k,1,nn}]-2 Pi Sinh[1]Sum[(((((-1))^k) k)/(1+(((k Pi))^2)))Sin[k Pi x],{k,1,nn}],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],AxesOrigin->0, PlotRange->{{-3 ,3},{-0.2,3}},AspectRatio->Automatic,ImageSize->600],{{nn,3},Range[0,20],Setter},ControlPlacement->Top]

Example 4, the identity function on the interval (-1,1)

Let us find the Fourier series of the function

Show[Plot[(#)&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],PlotRange->{{-3 ,3},{-1.5,1.5}}, AspectRatio->Automatic,ImageSize->600]

The coefficient (Subscript[a, 0]) is

(1/2)Integrate[x,{x,-1,1}]

The coefficients (Subscript[a, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[x Cos[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

The coefficients (Subscript[b, k]), k∈ℕ  are

FullSimplify[(1/1)Integrate[x Sin[k Pi x],{x,-1,1}],And[k∈Integers,k>0]]

Thus the partial sum with 40 terms of the Fourier Series is

nn=20;Clear[FS4]; FS4[x_]=Sum[-((2 (((-1))^k))/(k π))Sin[k  Pi x],{k,1,nn}]

Verify this with graphs

Show[Plot[(#)&[x],{x,-1,1},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS4[x],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]

It is nice to include the periodic extension

Show[Plot[PerExt[x,( #)&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS4[x],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]

It might be interesting to include different partial sums of the Fourier series:

Manipulate[Show[Plot[PerExt[x,( #)&,-1,1],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[Sum[-((2 (((-1))^k))/(k π))Sin[k  Pi x],{k,1,nn}],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-3 ,3},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600],{{nn,3},Range[0,20],Setter},ControlPlacement->Top]

Example 5, the sine function on the interval (-π/2,π/2)

Let us find the Fourier series of the function

Show[Plot[(Sin[#])&[x],{x,-Pi/2,Pi/2},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.012]}}],PlotRange->{{-3 ,3},{-1.5,1.5}}, AspectRatio->Automatic,ImageSize->600]

The coefficient (Subscript[a, 0]) is

(1/(2Pi/2))Integrate[Sin[x],{x,-Pi/2,Pi/2}]

The coefficients (Subscript[a, k]), k∈ℕ  are

FullSimplify[(1/(Pi/2))Integrate[Sin[x] Cos[2 k x],{x,-Pi/2,Pi/2}],And[k∈Integers,k>0]]

The coefficients (Subscript[b, k]), k∈ℕ  are

FullSimplify[(1/(Pi/2))Integrate[Sin[x] Sin[2 k x],{x,-Pi/2,Pi/2}],And[k∈Integers,k>0]]

Thus the partial sum with 40 terms of the Fourier Series is

nn=20;Clear[FS5]; FS5[x_]=Sum[((8 (((-1))^k) k)/(π-4 (k^2) π))Sin[2 k x],{k,1,nn}]

Verify this with graphs

Show[Plot[(Sin[#])&[x],{x,-Pi/2,Pi/2},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS5[x],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-5,5},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]

It is nice to include the periodic extension

Show[Plot[PerExt[x,(Sin[#])&,-Pi/2,Pi/2],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[FS5[x],{x,-16,16},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-6,6},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600]

It might be interesting to include different partial sums of the Fourier series:

Manipulate[Show[Plot[PerExt[x,(Sin[#])&,-Pi/2,Pi/2],{x,-10,10},PlotStyle->{{RGBColor[0,0,0.6],Thickness[0.01]}}],Plot[Sum[((8 (((-1))^k) k)/(π-4 (k^2) π))Sin[2 k x],{k,1,nn}],{x,-6,6},PlotStyle->{{RGBColor[1,0.5,0],Thickness[0.004]}}],
PlotRange->{{-6 ,6},{-1.5,1.5}},AspectRatio->Automatic,ImageSize->600],{{nn,3},Range[0,20],Setter},ControlPlacement->Top]