Additional sampling examples

Mathematica definitions

Load needed packages for this notebook

<< Graphics`Colors`;
<< Miscellaneous`Audio`;

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[FourierTransform,  
        FourierParameters -> {0, -2 π}];
SetOptions[InverseFourierTransform,  
        FourierParameters -> {0, -2 π}];

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]}

Plot negative and positive frequencies different colors

PlotSpecial[X_, f_, frange_, c1_:  Blue, c2_:  Red, 
          opts___] := 
Module[{g1, 
            g2}, 
g1 = 
            Plot[X, {f, frange[[1]], frange[[2]]}, 
              PlotStyle -> {c1},  DisplayFunction -> Identity, 
              opts]; 
g2 = 
            Plot[X, {f, frange[[2]], frange[[3]]}, 
              PlotStyle -> {c2}, DisplayFunction -> Identity, 
              opts]; 
Show[g1, g2,  
            DisplayFunction -> $DisplayFunction]
];

Sampled frequency spectrum

SampledSpectrum[X_, f_, fs_, n_:  5] := 
Sum[
          X[f - k fs], {k, -n, n}];

Sampling from a frequency-domain perspective: simple cosine

Oversampled (Subscript[f, s]) > 2  (Subscript[f, max])

Sampling frequency

fs = 5   ;

Original waveform

x[t_] := Cos[2  π t];

range = {0, 5};

gx = 
        Plot[x[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 100];

Frequency spectrum (CT)

X[f_] = 
      FourierTransform[x[t], t, 
          f]  /. {δ[a_ + f_ a_] -> δ[1 + f]/
              a, δ[-a_ + f_ a_] -> δ[-1 + f]/
              a}

gX = 
        Plot[Evaluate[PlotConvert[X[f],  .05]], {f, -12, 12}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

Frequency spectrum of sampled signal

Xs[f_, fs_, n_:  5] := SampledSpectrum[X, f, fs, n];

gXs = 
        Plot[Evaluate[PlotConvert[Xs[f, fs],  .05]], {f, -12, 12}, 
          PlotStyle -> {Red, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

gXXs = Show[gXs, gX];

Ideal filter

Filter[f_, fs_] := u[f + fs/2] - u[f - fs/2];

gF = 
        Plot[Filter[f, fs], {f, -12, 12}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotRange -> {- .1, 2.1}];

Sampled, filtered spectrum

XsFiltered[f_, fs_] := Filter[f, fs] Xs[f, fs];

gXsf = 
        Plot[Evaluate[PlotConvert[XsFiltered[f, fs],  .05]], {f, -12, 
            12}, PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

Ideally reconstructed signal

xr[t_] = 
      InverseFourierTransform[XsFiltered[f, fs], f, t] //  
        FullSimplify

gxr = 
        Plot[xr[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Orange, Thickness[ .01]},  
          PlotPoints -> 100];

Show[GraphicsArray[{gx, gxr}],  ImageSize -> 640];

Show[GraphicsArray[{{gx, gX}, {gXXs, gF}, {gXsf, gxr}}],  
        GraphicsSpacing -> { .1,  .6}, 
        ImageSize -> 432];

Undersampled (Subscript[f, s]) <  2  (Subscript[f, max])

Sampling frequency

fs = 1.4   ;

Original waveform

x[t_] := Cos[2  π t];

range = {0, 5};

gx = 
        Plot[x[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 100];

Frequency spectrum (CT)

X[f_] = 
      FourierTransform[x[t], t, 
          f]  /. {δ[a_ + f_ a_] -> δ[1 + f]/
              a, δ[-a_ + f_ a_] -> δ[-1 + f]/
              a}

gX = 
        Plot[Evaluate[PlotConvert[X[f],  .05]], {f, -4, 4}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

Frequency spectrum of sampled signal

Xs[f_, fs_, n_:  5] := SampledSpectrum[X, f, fs, n];

gXs = 
        Plot[Evaluate[PlotConvert[Xs[f, fs, 10],  .05]], {f, -4, 4}, 
          PlotStyle -> {Red, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

gXXs = Show[gXs, gX];

Ideal filter

Filter[f_, fs_] := u[f + fs/2] - u[f - fs/2];

gF = 
        Plot[Filter[f, fs], {f, -4, 4}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotRange -> {- .1, 2.1}];

Sampled, filtered spectrum

XsFiltered[f_, fs_] := Filter[f, fs] Xs[f, fs];

gXsf = 
        Plot[Evaluate[PlotConvert[XsFiltered[f, fs],  .05]], {f, -4, 4}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotPoints -> 500,  
          PlotRange -> {- .1, 1.1}];

Ideally reconstructed signal

xr[t_] = 
      InverseFourierTransform[XsFiltered[f, fs], f, t] //  
        FullSimplify

gxr = 
        Plot[xr[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Orange, Thickness[ .01]},  
          PlotPoints -> 100];

gxxr = Show[gx, gxr];

Show[GraphicsArray[{{gx, gX}, {gXXs, gF}, {gXsf, gxxr}}],  
        GraphicsSpacing -> { .1,  .6}, 
        ImageSize -> 432];

Sampling from a frequency-domain perspective: continuous frequency spectrum

Oversampled (Subscript[f, s]) >  2  (Subscript[f, max])

Sampling frequency

fs = 5.0   ;

Frequency domain representation of signal

X[
          f_] := (f + 2) (u[f + 1] - u[f])  +  (-f + 
                2) (u[f]  -  u[f - 1]);

gX = 
        Plot[X[f], {f, -12, 12},  
          PlotStyle -> {Blue, Thickness[ .01]}, 
          PlotRange -> {- .1, 4}];

Time-domain representation of signal

x[t_] = 
      InverseFourierTransform[X[f], f, t]  //  FullSimplify

range = {-5, 5};

gx = 
        Plot[x[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 100,  PlotRange -> All];

Frequency spectrum of sampled signal

Xs[f_, fs_, n_:  5] := SampledSpectrum[X, f, fs, n];

gXs = 
        Plot[Evaluate[PlotConvert[Xs[f, fs, 10],  .05]], {f, -12, 12}, 
          PlotStyle -> {Red, Thickness[ .01]},  PlotPoints -> 500, 
          PlotRange -> {- .1, 4}];

gXXs = Show[gXs, gX];

Ideal filter

Filter[f_, fs_] := u[f + fs/2] - u[f - fs/2];

gF = 
        Plot[Filter[f, fs], {f, -12, 12}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotRange -> {- .1, 2.1}];

Sampled, filtered spectrum

XsFiltered[f_, fs_] := Filter[f, fs] Xs[f, fs];

gXsf = 
        Plot[Evaluate[PlotConvert[XsFiltered[f, fs],  .05]], {f, -12, 
            12}, PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotPoints -> 500, 
          PlotRange -> {- .1, 4}];

Ideally reconstructed signal

xr[t_] = 
        InverseFourierTransform[XsFiltered[f, fs], f, t] //  
          FullSimplify;

gxr = 
        Plot[xr[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Orange, Thickness[ .01]},  
          PlotPoints -> 100,  PlotRange -> All];

gxxr = Show[gx, gxr];

Show[GraphicsArray[{{gx, gX}, {gXXs, gF}, {gXsf, gxxr}}],  
        GraphicsSpacing -> { .1,  .6}, 
        ImageSize -> 432];

Undersampled (Subscript[f, s]) <  2  (Subscript[f, max])

Sampling frequency

fs = 1.5   ;

Frequency domain representation of signal

X[
          f_] := (f + 2) (u[f + 1] - u[f])  +  (-f + 
                2) (u[f]  -  u[f - 1]);

gX = 
        Plot[X[f], {f, -4, 4},  
          PlotStyle -> {Blue, Thickness[ .01]}, 
          PlotRange -> {- .1, 4}];

Time-domain representation of signal

x[t_] = 
      InverseFourierTransform[X[f], f, t]  //  FullSimplify

range = {-5, 5};

gx = 
        Plot[x[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Blue, Thickness[ .01]},  
          PlotPoints -> 100,  PlotRange -> All];

Frequency spectrum of sampled signal

Xs[f_, fs_, n_:  5] := SampledSpectrum[X, f, fs, n];

gXs = 
        Plot[Evaluate[PlotConvert[Xs[f, fs, 10],  .05]], {f, -4, 4}, 
          PlotStyle -> {Red, Thickness[ .01]},  PlotPoints -> 500, 
          PlotRange -> {- .1, 4}];

gXXs = Show[gXs, gX];

Ideal filter

Filter[f_, fs_] := u[f + fs/2] - u[f - fs/2];

gF = 
        Plot[Filter[f, fs], {f, -4, 4}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotRange -> {- .1, 2.1}];

Sampled, filtered spectrum

XsFiltered[f_, fs_] := Filter[f, fs] Xs[f, fs];

gXsf = 
        Plot[Evaluate[PlotConvert[XsFiltered[f, fs],  .05]], {f, -4, 4}, 
          PlotStyle -> {Magenta, Thickness[ .01]},  
          PlotPoints -> 500, 
          PlotRange -> {- .1, 4}];

Ideally reconstructed signal

xr[t_] = 
        InverseFourierTransform[XsFiltered[f, fs], f, t] //  
          FullSimplify;

gxr = 
        Plot[xr[t], {t, range[[1]], range[[2]]}, 
          PlotStyle -> {Orange, Thickness[ .01]},  
          PlotPoints -> 100,  PlotRange -> All];

gxxr = Show[gx, gxr];

Show[GraphicsArray[{{gx, gX}, {gXXs, gF}, {gXsf, gxxr}}],  
        GraphicsSpacing -> { .1,  .6}, 
        ImageSize -> 432];