Transformations on Discrete Signals

Mathematica definitions

Load needed packages for this notebook

<< Graphics`Colors`;

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

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

Basic discrete signal transformations: example #1

Sample signal

Remove[Global`x];
Remove[Global`xc];
Remove[Global`Ts];

xc[t_] := (UnitStep[t + 2] - UnitStep[t]) (t + 2) + 
          2*(UnitStep[t] - UnitStep[t - 2]);

Ts  =  1/5;

x[n_Integer] := xc[n*Ts];

g1 = 
        DiscreteListPlot[Table[{n, x[n]}, {n, -20, 20}], Blue,  
          Frame -> True, Axes -> True, 
          PlotRange -> {- .1, 4.1}];

FrontEndTokenExecute["Copy"]

Reflection

g2 = 
        DiscreteListPlot[Table[{n, x[-n]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-scaling (compression)

g2 = 
        DiscreteListPlot[Table[{n, x[2  n]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

DC offset

g2 = 
        DiscreteListPlot[Table[{n, x[n] + 1}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-shifting (to the right)

g2 = 
        DiscreteListPlot[Table[{n, x[n - 5]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-shifting (to the left)

g2 = 
        DiscreteListPlot[Table[{n, x[n + 5]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Amplitude scaling

g2 = 
        DiscreteListPlot[Table[{n, 2  x[n]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Compound transformation

g2 = 
        DiscreteListPlot[Table[{n, x[2  n - 10]}, {n, -20, 20}], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Basic discrete signal transformations: example #2

Sample signal

Remove[Global`x];
Remove[Global`xc];
Remove[Global`Ts];

x1[t_] := 
        D[1/(1 + Exp[-tt]), {tt, 2}]  /.  tt -> t;
x2[t_] := 
        D[1/(1 + Exp[-tt]), {tt, 2}]  /.  tt -> t;
xc[t_] := x1[t] t/3  +  x2[t] (2 - t);

Ts  =  1/5;

x[n_Integer] := xc[n*Ts];

range = {n, -50, 50};

g1 = 
        DiscreteListPlot[Table[{n, x[n]}, Evaluate[range]], Blue,  
          Frame -> True, Axes -> True, 
          PlotRange -> All];

Reflection

g2 = 
        DiscreteListPlot[Table[{n, x[-n]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-scaling (compression)

g2 = 
        DiscreteListPlot[Table[{n, x[2  n]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

DC offset

g2 = 
        DiscreteListPlot[Table[{n, x[n] - 1/10}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-shifting (to the right)

g2 = 
        DiscreteListPlot[Table[{n, x[n - 20]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Time-shifting (to the left)

g2 = 
        DiscreteListPlot[Table[{n, x[n + 20]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Amplitude scaling

g2 = 
        DiscreteListPlot[Table[{n, 1/2  x[n]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];

Compound transformation

g2 = 
        DiscreteListPlot[Table[{n, x[-n + 20]}, Evaluate[range]], Red,  
          Frame -> True, 
          Axes -> 
            True, 
Evaluate[AbsoluteOptions[g1, 
                PlotRange][[1]]]];