Continuous-time Fourier transform (CTFT) 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}];

Fourier series to Fourier transform

Square wave, T = 2

Define and plot square wave

T0 = 2;

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

Frequency spectrum

spectrum =  
        Table[{k/T0, 
            FourierCoefficient[Pulse1[t], t, k, 
              FourierParameters -> {-1, -1/T0}]}, {k, -4*T0, 
            4*T0}];
ga = 
        DiscreteListPlot[spectrum,  Red,  Frame -> True,  
          PlotRange -> All,  Axes -> {True, False}];

Square wave, T = 4

Define and plot square wave

T0 = 4;

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

Frequency spectrum

spectrum =  
        Table[{k/T0, 
            FourierCoefficient[Pulse1[t], t, k, 
              FourierParameters -> {-1, -1/T0}]}, {k, -4*T0, 
            4*T0}];
gb = 
        DiscreteListPlot[spectrum,  Red,  Frame -> True,  
          PlotRange -> All,  Axes -> {True, False}];

Square wave, T = 6

Define and plot square wave

T0 = 6;

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

Frequency spectrum

spectrum =  
        Table[{k/T0, 
            FourierCoefficient[Pulse1[t], t, k, 
              FourierParameters -> {-1, -1/T0}]}, {k, -4*T0, 
            4*T0}];
gc = 
        DiscreteListPlot[spectrum,  Red,  Frame -> True,  
          PlotRange -> All,  Axes -> {True, False}];

Square wave, T = 8

Define and plot square wave

T0 = 8;

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

Frequency spectrum

spectrum =  
        Table[{k/T0, 
            FourierCoefficient[Pulse1[t], t, k, 
              FourierParameters -> {-1, -1/T0}]}, {k, -4*T0, 
            4*T0}];
gd = 
        DiscreteListPlot[spectrum,  Red,  Frame -> True,  
          PlotRange -> All,  Axes -> {True, False}];

Show results of previous four subsections

Show[GraphicsArray[{{g1, g2}, {g3, g4}}], 
        ImageSize -> 504];

Show[GraphicsArray[{{ga, gb}, {gc, gd}}], 
        ImageSize -> 504];

Pulse (T -> infinity)

Define and plot single pulse

Pulse1[
          t_] := (UnitStep[t + 1/2]  - 
            UnitStep[t - 1/2]);
g5 = 
        Plot[Pulse1[t], {t, -10, 10},  PlotPoints -> 100, 
          PlotStyle -> {Blue}, 
          PlotRange -> {-1.01, 3.01}];

Frequency spectrum

spectrum =  
        FourierTransform[Pulse1[t],  t,  f]  //  
          FullSimplify;
spectrum  //  TraditionalForm

ge = 
        Plot[spectrum, {f, -4, 4}, PlotStyle -> Red,  
          PlotRange -> All];

Compare FS and FT

Show[GraphicsArray[{{ga, gb}, {gd, ge}}], 
        ImageSize -> 504];

Continuous-time Fourier transform (CTFT) example

Widest pulse

x[t_] := u[t + 1]  -  u[t - 1];

X[f_] = 
        FourierTransform[x[t], t, 
              f]  /. {δ[b_ a_ + f a_] -> δ[b + f]/
                  a, δ[-b_ a_ + 
                    f_ a_] -> δ[-b + f]/a}  //  
          ComplexExpand;
{{x[t]}, {X[f]}}  // FullSimplify  //  
      TraditionalForm

gx1 = 
      Plot[Evaluate[x[t]], {t, -2, 2}, PlotStyle -> {Blue},  
        PlotPoints -> 500,  PlotRange -> {- .1, 2.1}]; 
    gX1 = Plot[Evaluate[X[f]], {f, -10, 10}, PlotStyle -> {Blue},  
        PlotPoints -> 500,  PlotRange -> All];

Narrower pulse

x[t_] := u[t + 1/2]  -  u[t - 1/2];

X[f_] = 
        FourierTransform[x[t], t, 
              f]  /. {δ[b_ a_ + f a_] -> δ[b + f]/
                  a, δ[-b_ a_ + 
                    f_ a_] -> δ[-b + f]/a}  //  
          ComplexExpand;
{{x[t]}, {X[f]}}  // FullSimplify  //  
      TraditionalForm

gx2 = 
      Plot[Evaluate[x[t]], {t, -2, 2}, PlotStyle -> {Red},  
        PlotPoints -> 500,  PlotRange -> {- .1, 2.1}]; 
    gX2 = Plot[Evaluate[X[f]], {f, -10, 10}, PlotStyle -> {Red},  
        PlotPoints -> 500,  PlotRange -> All];

Narrowest pulse

x[t_] := u[t + 1/10]  -  u[t - 1/10];

X[f_] = 
        FourierTransform[x[t], t, 
              f]  /. {δ[b_ a_ + f a_] -> δ[b + f]/
                  a, δ[-b_ a_ + 
                    f_ a_] -> δ[-b + f]/a}  //  
          ComplexExpand;
{{x[t]}, {X[f]}}  // FullSimplify  //  
      TraditionalForm

gx3 = 
      Plot[Evaluate[x[t]], {t, -2, 2}, 
        PlotStyle -> {Magenta,  Thickness[ .01]},  
        PlotPoints -> 500,  PlotRange -> {- .1, 2.1}]; 
    gX3 = Plot[Evaluate[X[f]], {f, -10, 10}, 
        PlotStyle -> {Magenta, Thickness[ .01]},  
        PlotPoints -> 500,  PlotRange -> All];

Compare frequency spectra of three pulses

Show[GraphicsArray[{{gx1, gX1}, {gx2, gX2}, {gx3, gX3}}], 
        ImageSize -> 504];