Introduction to signals and systems: part II

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

Filtering definitions

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

Filter[n, sampleRate, {lowFreq, highFreq}] generates an ideal discrete filter for an FFT list of length 'n' (sampled at 'sampleRate'), with a bandpass from 'lowFreq' to 'highFreq' (in Hz).

Filter[n_, 
          samplingRate_, {lowF_, 
            highF_}] := 
Module[{coeff, l, 
            h}, 
coeff = 
            Table[0, {n}]; 
l = Floor[lowF*n/samplingRate]; 
          h = Ceiling[highF*n/samplingRate]; 
If[
            l  ==  0,  l  =  1;  
            coeff = ReplacePart[coeff, 1, 1]]; 
coeff = 
            ReplacePart[coeff, 1, Table[{i}, {i, l + 1, h + 1}]]; 
          coeff = ReplacePart[coeff, 1, 
              Table[{i}, {i, n + 1 - h, 
                  n + 1 - l}]]
];

Time domain ⟺ Frequency domain: time-varying example...

Example #1: sum of sinusoids

Continuous-time waveform

x1[t_] := 
        Cos[2  π 10  t]  +  Cos[2  π 25 t]  +  
          Cos[2  π 50 t]  +  
          Cos[2  π 100 t];
gx1 = 
        Plot[x1[t], {t, 0, 1},  PlotPoints -> 100, 
          PlotStyle -> {Blue},  
          ImageSize -> 500];
x1[t]  //  TraditionalForm

Fast Fourier Transform (FFT)

sampleRate = 1000;

signal  =  
        Table[x1[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  
          N;

fft  =  Fourier[signal];
gf1 = 
        ListPlot[FourierRearrange[Abs[fft], sampleRate], 
          PlotStyle -> {Red}, 
          PlotRange -> {{-125, 125}, All},  Frame -> True,  
          ImageSize -> 500];

Example #2: time-varying sinusoid

Analog waveform

u[t_] := UnitStep[t];

x2[
          t_] := (u[t] - u[t -  .3]) 
            Cos[2  π 10  t]  +  (u[t -  .3] - u[t -  .6]) 
            Cos[2  π 25 t]  + (u[t -  .6] - u[t -  .8]) Cos[
              2  π 50 t]  +  (u[t -  .8] - u[t - 1. ]) 
            Cos[2  π 100 t];
gx2 = 
        Plot[x2[t], {t, 0, 1},  PlotPoints -> 100, 
          PlotStyle -> {Blue},  
          ImageSize -> 500];
x2[t]  //  TraditionalForm

Fast Fourier Transform (FFT)

sampleRate = 1000;

signal  =  
        Table[x2[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  
          N;

fft  =  Fourier[signal];
gf2 = 
        ListPlot[FourierRearrange[Abs[fft], sampleRate], 
          PlotStyle -> {Red}, 
          PlotRange -> {{-125, 125}, All},  Frame -> True,  
          ImageSize -> 500];

Display all

Show[GraphicsArray[{{gx1, gx2}, {gf1, gf2}}], 
        ImageSize -> 640];

Filter example: noisy sinusoid

Noisy, discrete-time waveform

sampleRate = 1000;

f1[t_] := Sin[2  π 10  t]  +  Random[] - 1/2;
signal  =  
        Table[f1[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  N;
ListPlot[signal,  PlotStyle -> Blue];

Discrete spectrum: Fast Fourier Transform (FFT)

fft  =  Fourier[signal];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft], sampleRate], Red, 
          PlotRange -> All,  Frame -> True];

Show[g1,  PlotRange -> {{-20, 20}, All}];

Filter (DC to 15Hz)

Define filter

filter  =  Filter[Length[signal], sampleRate, {0, 15}];
ListPlot[FourierRearrange[filter,  sampleRate], 
        PlotRange -> All,  PlotStyle -> {Blue}];

Low-pass filtered signal

ListPlot[InverseFourier[filter*fft]  //  Chop,  
        PlotStyle -> Blue];

Filter (15Hz to 500Hz)

Define filter

filter  =  
        Filter[Length[signal], sampleRate, {15, 500}];
ListPlot[FourierRearrange[filter,  sampleRate], 
        PlotRange -> All,  PlotStyle -> {Blue}];

High-pass filtered signal

ListPlot[noise = InverseFourier[filter*fft]  //  Chop, 
        PlotStyle -> Blue];

fft  =  Fourier[noise];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft], sampleRate], Red, 
          PlotRange -> All,  Frame -> True];

Speech filtering

Import wav file

Adjust the directory setting and input file below to your system configuration.

dir = "/Users/nechyba/public_html/eel3135/data";
file = "Speech.wav";

SetDirectory[dir];
wav1 = ReadSoundFile[file, PrintHeader -> True];

sampleRate1 = 11127;

Listen to wav file

ListPlay[wav1, SampleRate -> sampleRate1];

Click on the sound plot above to hear the speech signal.

Discrete spectrum: Fast Fourier Transform (FFT)

fft  =  Fourier[wav1];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft], sampleRate1], Red, 
          PlotRange -> All,  Frame -> True];

Filter

Define filter

filter  =  
        Filter[Length[wav1], sampleRate1, {0, 1000}];
ListPlot[FourierRearrange[filter,  sampleRate1], 
        PlotRange -> All,  PlotStyle -> {Blue}];

Filtered signal

ListPlay[reconstructed = InverseFourier[filter*fft]  //  Chop,  
        SampleRate -> sampleRate1];

Amplitude modulation (AM)

Sample discrete signal

sampleRate = 8000;

f1[t_] := Sin[2  π 20  t];
signal  =  
        Table[f1[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  N;
ListPlot[signal,  
        PlotStyle -> Blue];
ƒ[t]  ==  f1[t]  //  TraditionalForm

Discrete spectrum: Fast Fourier Transform (FFT)

fft  =  Fourier[signal];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft], sampleRate], Red, 
          PlotRange -> All,  Frame -> True];

Show[g1,  PlotRange -> {{-40, 40}, All}];

Carrier signal

fc[t_] := Sin[2  π 200 t];
Plot[fc[t], {t, 0, 1},  PlotPoints -> 100,  
        PlotStyle -> Blue];

Amplitude modulation

f2[t_] := (1 + f1[t]) fc[t];
signal2  =  
        Table[f2[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  N;
g2 = 
        ListPlot[signal2,  
          PlotStyle -> Blue];
ƒ[t]  ==  f2[t]  //  TraditionalForm

Show[g2, PlotRange -> {{0, 800}, All}];

Discrete spectrum: Fast Fourier Transform (FFT)

fft2  =  Fourier[signal2];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft2], sampleRate], Red, 
          PlotRange -> All,  Frame -> True];

Show[g1,  PlotRange -> {{-250, 250}, All}];

Amplitude demodulation

f3[t_] := f2[t] fc[t];
signal3  =  
        Table[f3[t], {t, 0, 1, 1/(sampleRate - 1)}]  //  N;
g3 = 
        ListPlot[signal3,  
          PlotStyle -> Blue];
ƒ[t]  ==  f3[t]  //  TraditionalForm

Show[g3, PlotRange -> {{0, 800}, All}];

Discrete spectrum: Fast Fourier Transform (FFT)

fft3  =  Fourier[signal3];
g1 = 
        DiscreteListPlot[FourierRearrange[Abs[fft3], sampleRate], Red, 
          PlotRange -> All,  Frame -> True];

Show[g1,  PlotRange -> {{-500, 500}, All}];

Filter (10Hz to 30Hz)

Define filter

filter  =  
        Filter[Length[signal3], sampleRate, {10, 30}];
ListPlot[FourierRearrange[filter,  sampleRate], 
        PlotRange -> All,  PlotStyle -> {Blue}];

Filtered signal

ListPlot[reconstructed = InverseFourier[filter*fft]  //  Chop,  
        PlotStyle -> {Blue}];