Time-frequency plots (spectrogram)

Mathematica definitions

Load needed packages for this notebook

<< Graphics`Colors`;
<< Calculus`FourierTransform`;
<< Graphics`Graphics`;
<< 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}];
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]
];

Plot discrete-time signal as sequence of amplitude lines

DiscreteListPlot2[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[ .005], 
                  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}];

Sampled frequency spectrum

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

Definition of DTFT

DTFT[
          xd_, θ_] := 
Module[{n, xn, n1, 
            n2}, 
{n, xn}  =  
            xd  //  Transpose; 
n1  =  First[n];  
          n2  =  Last[n]; 
Sum[
            xn[[k - n1 + 1]] Exp[-I k θ], {k, n1, 
              n2}]
];

Definition of DFT

DFT[
          x_] := 
Module[{x2,  k}, 
If[
            Length[First[x]]  >  1,  x2  =  Last  /@  x,  
            x2  =  x]; 
{Table[
                k, {k, 0, Length[x2] - 1}], Fourier[x2]}  //  
            Transpose];

Definition of spectral leakage window

Window[t_,  
        interval_] := (0.54  -   .46* 
            Cos[2  π/interval t])

Analysis of signals over time

Example #1: sum of sinusoids

Analog 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 -> 504,  
        GraphicsSpacing -> {0.1, 0.2}];

Spectrogram example #1

Example #1: sum of sinusoids

Analog waveform

x1[t_] := 
        Cos[2  π 100  t]  +  Cos[2  π 300  t]  +  
          Cos[2  π 500 t]  +  
          Cos[2  π 1000 t];
gx1 = 
        Plot[x1[t], {t, 0,  .05},  PlotPoints -> 100, 
          PlotStyle -> {Blue},  
          ImageSize -> 504];
x1[t]  //  TraditionalForm

Fast Fourier Transform (FFT)

sampleRate = 10000;

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

n = 500;

spectrogram  =  
        Transpose[Take[-Abs[Fourier[#]], {1, n/5}] &  /@  
            Partition[signal, n, n/2]];

ListDensityPlot[spectrogram, Mesh -> False,  
        PlotRange -> All,  MeshRange -> {{0, 1}, {0, 2000}},  
        AspectRatio -> 1/GoldenRatio,  
        ImageSize -> 504];

Example #2: time-varying sinusoid

Analog waveform

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

Fast Fourier Transform (FFT)

sampleRate = 10000;

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

n = 500;

spectrogram  =  
        Transpose[Take[-Abs[Fourier[#]], {1, n/5}] &  /@  
            Partition[signal, n, n/2]];

ListDensityPlot[spectrogram, Mesh -> False,  
        PlotRange -> All,  MeshRange -> {{0, 1}, {0, 2000}},  
        AspectRatio -> 1/GoldenRatio,  
        ImageSize -> 504];

Spectrogram example #2

Speech example

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

Fast Fourier Transform (FFT)

signal  =  wav1;

n = 500;

spectrogram  =  
        Transpose[Take[-Abs[Fourier[#]], {1, n/2}] &  /@  
            Partition[signal, n, n/2]];

ListDensityPlot[spectrogram, Mesh -> False,  
        PlotRange -> Automatic,  
        MeshRange -> {{0, 3.833}, {0, sampleRate1/2}},  
        AspectRatio -> 1/GoldenRatio,  
        ImageSize -> 504];