Fourier series examples

Mathematica definitions

Load needed packages for this notebook

<< Graphics`Colors`;
<< Calculus`FourierTransform`;

Set up defaults

Turn off annoying warning messages

Off[General::spell];
Off[General::spell1];

Set default options for specific functions

SetOptions[Plot, Frame -> True];
SetOptions[ListPlot, Frame -> True,  PlotJoined -> True,  
        Axes -> {True, False}];
SetOptions[Fourier, 
        FourierParameters -> {1, -1}];
SetOptions[InverseFourier, 
        FourierParameters -> {1, -1}];
SetOptions[FourierTransform,  
        FourierParameters -> {0, -2 π}];
SetOptions[FourierTransform,  
        FourierParameters -> {0, -2 π}];
SetOptions[InverseFourierTransform,  
        FourierParameters -> {0, -2 π}];

Visualization functions

Fix time variable for sampled signals

FixTime[signal_, 
          sampleRate_] := 
Module[{time, 
            data}, 
time = 
            Table[i/sampleRate, {i, 1, 
                Length[signal]}]; 
data  =  {time, 
                signal} // Transpose
];

Generate periodic waveform from one period

PeriodicSignal[f, {dmin, dmax}, {t, min, max}] generates a periodic waveform for the interval [min, max]. It is assumed that one period of the function f(t) is defined over the interval [dmin, dmax].

PeriodicSignal[
          f_, {dmin_, dmax_}, {t_, min_, 
            max_}] := 
Module[{T, imin, 
            imax}, 
T  =  
            dmax  -  dmin; 
imin = 
            Floor[(min - dmin)/T]; 
imax = 
            Ceiling[(max - dmax)/T];  
Sum[
            f[t - i T] (UnitStep[t - dmin - i T]  -  
                  UnitStep[t - dmax - i T]), {i, imin, 
              imax}]
];

Plot periodic waveforms

PeriodicPlot[f, {dmin, dmax}, {t, min, max}, opts] plots a a periodic waveform for the interval [min, max]. It is assumed that one period of the function f(t) is defined over the interval [dmin, dmax]. The 'opts' argument passed plotting options to the Mathematica function Plot[ ].

PeriodicPlot[f_, {dmin_, dmax_}, {t_, min_, max_}, 
          opts___] := 
Module[{fnc}, 
fnc = PeriodicSignal[f, {dmin, dmax}, {t, min, max}]; 
Plot[
            fnc, {t, min, max}, opts]
];

Plot discrete-time signal as sequence of amplitude lines

DiscreteListPlot[signal_, color_:  Black, 
          opts___] := 
Module[{data, g1, 
            g2}, 
If[
            Length[signal[[1]]]  ==  2, 
data = 
              signal, 
data = 
              FixTime[signal, 
                1]
]; 
g1 = Line[{{#[[1]], 0}, {#[[1]], #[[2]]}}] &  /@  
              data; 
g2 = {PointSize[ .015], 
                  Point[#]} &  /@  data; 
Show[
            Graphics[{color, g1, g2}], 
            opts]
];

Fourier rearrange

FourierRearrange[fft] rearranges the result of an FFT (Mathematica function Fourier[ ]), to a easier to visualize the spectrum represented by 'fft'. The length of the list 'fft' is assumed to be even; if it is not, the last term in 'fft' is dropped.

FourierRearrange[fft_,  
          samplingRate_] := 
Module[{fft2, n, 
            n2}, 
If[EvenQ[Length[fft]],  
            fft2  =  fft,  
            fft2  =  Drop[fft, -1]];  
n = 
            Length[fft]; 
          n2 = n/2; 
{Table[ 
                samplingRate i/n, {i, -n2 + 1, n2}], 
              Take[fft2, {n2 + 2, n2*2}]~Join~Take[fft2, {1, n2 + 1}]} // 
            Transpose
];

Short-cut functions

Dirac delta function

δ[t_] := DiracDelta[t];

Unit step function

u[t_] := UnitStep[t];

Pulse of width w

p[t_, w_] := u[t + w/2] - u[t - w/2];

Hack to plot δ(t)

PlotConvert[X_, 
        w_] := 
X  /.  {δ[a_] -> 
            p[a, w]}

Sampler (CT to DT converter)

sampler[xc_, fs_, 
          range_: {0, 5}] := 
Table[{n, xc[n/fs]}, {n, 
            range[[1]]*fs, range[[2]]*fs}];

Continuous-time Fourier series (CTFS)

Example #1: triangle wave

Define and plot triangle wave

Triangle1[
          t_] := (2  t + 1) (u[t + 1/2]  -  
                u[t])  +  (-2 t + 1) (u[t] - 
                u[t - 1/2]);
fnc1  =  
        PeriodicSignal[
          Triangle1, {-1/2, 1/2}, {t, -2, 
            2}];
g1 = 
        Plot[fnc1, {t, -2, 2},  PlotStyle -> {Blue}, 
          PlotRange -> {-1.01, 2.01}];

Complex Fourier series

series  =  
        FourierSeries[Triangle1[t], t, 5];
series  //  TraditionalForm

Real representation

series2  = series  //  ComplexExpand;

    series2  // TraditionalForm

Plot result

g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 2.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plot error between truncated Fourier series and triangle wave

Plot[fnc1  -  series,  {t, -2, 2},  
        PlotStyle -> {Green},  PlotPoints -> 250, 
        PlotRange -> {- .1,  .1}];

Frequency domain plots

ftbl  =  
        Table[{k, FourierCoefficient[Triangle1[t], t, k]}, {k, -10, 
            10}];

DiscreteListPlot[ftbl,  Red,  Frame -> True,  
        PlotRange -> All];

Example #2: sawtooth wave

Define and plot square wave

SawTooth1[t_] := (t);
fnc1  =  
        PeriodicSignal[
          SawTooth1, {-1/2, 1/2}, {t, -2, 
            2}];
g1 = 
        Plot[fnc1, {t, -2, 2},  PlotPoints -> 100, 
          PlotStyle -> {Blue}, 
          PlotRange -> {-1.01, 1.01}];

Complex Fourier series

series  =  
        FourierSeries[SawTooth1[t], t, 5];
series  //  Simplify  // TraditionalForm

Real representation

series2  = series  //  ComplexExpand;

    series2  // TraditionalForm

Plot result

g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 500];

More terms

series  =  
        FourierSeries[SawTooth1[t], t, 10];
g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], 
        ImageSize -> 500];
series  //  ComplexExpand  // TraditionalForm

Even more terms

series  =  
        FourierSeries[SawTooth1[t], t, 100];
g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], 
        ImageSize -> 500];
series  //  ComplexExpand  // TraditionalForm

Frequency domain plots

ftbl  =  
        Table[{k, 
            FourierCoefficient[SawTooth1[t], t, k,  
              FourierParameters -> {1, -1}]}, {k, -50, 
            50}];

DiscreteListPlot[{#[[1]], Abs[#[[2]]]} &  /@  ftbl,  
        Red,  Frame -> True,  PlotRange -> All];

DiscreteListPlot[{#[[1]], 
              Arg[#[[2]] +  .000001]} &  /@  ftbl,  Red,  
        Frame -> True,  PlotRange -> All];

Example #3: square wave

Define and plot square wave

Pulse1[t_] := (UnitStep[t]  - 1/2);
fnc1  =  
        PeriodicSignal[
          Pulse1, {-1/2, 1/2}, {t, -2, 
            2}];
g1 = 
        Plot[fnc1, {t, -2, 2},  PlotPoints -> 100, 
          PlotStyle -> {Blue}, 
          PlotRange -> {-1.01, 1.01}];

Complex Fourier series

series  =  
        FourierSeries[Pulse1[t], t, 5];
series  //  TraditionalForm

Real representation

series2  = series  //  ComplexExpand;

    series2  // TraditionalForm

Plot result

g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 500];

More terms

series  =  
        FourierSeries[Pulse1[t], t, 10];
g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], 
        ImageSize -> 500];
series  //  ComplexExpand  // TraditionalForm

Even more terms

series  =  
        FourierSeries[Pulse1[t], t, 100];
g2 = 
        Plot[series, {t, -2, 2}, 
          PlotStyle -> {Red,  Thickness[ .01]}, 
          PlotRange -> {-1.01, 1.01},  
          DisplayFunction -> Identity,  
          PlotPoints -> 1000];
Show[GraphicsArray[{{g1, g2}}], 
        ImageSize -> 500];
series  //  ComplexExpand  // TraditionalForm

Frequency domain plots

ftbl  =  
        Table[{k, 
            FourierCoefficient[Pulse1[t], t, k,  
              FourierParameters -> {1, -1}]}, {k, -50, 
            50}];

DiscreteListPlot[{#[[1]], Abs[#[[2]]]} &  /@  ftbl,  
        Red,  Frame -> True,  PlotRange -> All];

DiscreteListPlot[{#[[1]], 
              Arg[#[[2]] +  .000001]} &  /@  ftbl,  Red,  
        Frame -> True,  PlotRange -> All];