Frequency response of FIR filters: examples

Mathematica definitions

Load needed packages for this notebook

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

(1) Simple low-pass FIR filter examples

y[n]  =  
        1/L (x[n]  +  x[n - 1]  +  x[n - 2]  +   ...   +  
              x[n - L + 1])
 y[n]  =  
        1/L (∑_(k = 0)^(L - 1))x[n - k]

Sampling frequency for experiments

fs = 500;

Filter length L = 10

Define impulse response sequence h(n)

L = 10;
hn  =  Table[{i, 1/L}  //  N,  {i, 0, L - 1}]

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -1];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

Filter length L = 30

Define impulse response sequence h(n)

L = 30;
hn  =  Table[{i, 1/L}  //  N,  {i, 0, L - 1}]

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -1];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

Filter length L = 100

Define impulse response sequence h(n)

L = 100;
hn  =  Table[{i, 1/L}  //  N,  {i, 0, L - 1}]

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -1];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

(2) Simple high-pass FIR filter examples

Filter length L = 2

y[n]  =  
        1/2  x[n]  -  1/2 x[n - 1]

Define impulse response sequence h(n)

L = 2;
hn  =  {Table[i - 1, {i, 1, L}], {1/2, -1/2}}  // 
          Transpose //  N

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -1];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

Filter length L = 3

y[n]  =  
        1/4  x[n]  -  
          1/2  x[n - 1]  +  1/4 x[n - 2]

Define impulse response sequence h(n)

L = 3;
hn  =  {Table[i - 1, {i, 1, L}], {1/4, -1/2, 1/4}}  // 
          Transpose //  N

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -2];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

Filter length L = 4

y[n]  =  
        1/8  x[n]  -  3/8  x[n - 1]  +  
          3/8  x[n - 2] - 1/8 x[n - 3]

Define impulse response sequence h(n)

L = 4;
hn  =  {Table[i - 1, {i, 1, L}], {1/8, -3/8, 3/8, -1/8}}  // 
          Transpose //  N

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -3];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];

Filter length L = 5

y[n]  =  
        1/16  x[n]  -  1/4  x[n - 1]  +  3/8  x[n - 2] - 
          1/4  x[n - 3] + 1/16 x[n - 4]

Define impulse response sequence h(n)

L = 5;
hn  =  {Table[i - 1, {i, 1, L}], {1/16, -1/4, 3/8, -1/4, 
              1/16}}  // Transpose //  N

Frequency response function H((e^j)θ)

Hθ  = DTFT[hn, θ];

Plots of frequency response function H((e^j)θ)

g1 = 
        Plot[Abs[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[Hθ], {θ, -π, π},  
          PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Plots of frequency response function H((e^j)θ) as a function of f

g1 = 
        Plot[Abs[
            Hθ  /.  θ ->  2  π f/fs], {f, -fs/
              2, fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
g2 = 
        Plot[Arg[
            Hθ /.  θ ->  2  π f/fs], {f, -fs/2, 
            fs/2},  PlotStyle -> {Red},  PlotRange -> All, 
          PlotPoints -> 200,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}}], ImageSize -> 504];

Output of system with noisy sinusoidal input

f1[t_] := Cos[2  π 4  t]  +  Random[] - 1/2;
xn  =  
        Table[f1[t], {t, 0, 1, 1/(fs - 1)}]  //  N;
g1 = 
        ListPlot[xn,  PlotStyle -> {Blue},  
          DisplayFunction -> Identity];
yn  = 
        Drop[ListConvolve[Last  /@  hn, xn, {1, -1}, 
            0], -4];
g2 = 
        ListPlot[yn,  PlotStyle -> {Red},  
          DisplayFunction -> Identity];
g3  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[xn]], fs], Blue,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
g4  =  
        DiscreteListPlot[FourierRearrange[Abs[Fourier[yn]], fs], Red,  
          PlotRange -> {0, 40}, Frame -> True,  
          DisplayFunction -> Identity];
Show[GraphicsArray[{{g1, g2}, {g3, g4}}],  
        ImageSize -> 504];