Rectangular, Hanning, and Hamming Window Transforms

Music 420 / EE 265
Winter 1992
   Julius O. Smith III
   CCRMA, Stanford University

This is a Mathematica Notebook which investigates frequency-domain properties of the Rectangular, Hanning, and Hamming windows.  It also illustrates some different kinds of plots possible in Mathematica.   This notebook can be found online at CCRMA in the file /dist/Mathematica/SignalProcessing/GenHamming.ma.   It is instructive to explore the effect of changing the various parameters such as window length M, FFT size NN, and the ªalphaº parameter of the generalized Hamming window.

The SincM function on the UnitCircle

M=17; 	(* Window Length = 2 Pi / sidelobe_width *)

SincM[wk_, M_] := If[N[Abs[Mod[wk,2 Pi]]] < 0.00001, M,
	 N[Sin[wk M/2]/Sin[wk/2]]];

Plot [SincM[wk, M], {wk, -Pi, Pi},
	PlotLabel->"SincM",
	AxesLabel->{" Rad/Sec", ""},
	PlotRange->{-M/4,M}];

(* Translate to Pi to get vertical axis off to the left *)
Plot [SincM[wk-Pi, M], {wk, 0, 2Pi},
	PlotLabel->"SincM at Pi",
	AxesLabel->{" Rad/Sec", ""},
	PlotRange->{-M/4,M}];

Evidence that SincM is the DTFT of the Rectangular Window

IDFT[W,NN] - Inverse DFT.

Input function W must be defined on [-1/2, 1/2).
	NN = DFT length and must be EVEN.
	A table is returned for speed.
	Time range in the returned list is -NN/2 to NN/2-1.

IDFT[W_,NN_] := Table[{n,
	(1/NN) Sum[
		N[ W[k/NN] E^(I 2 Pi k n/NN)],
		{k,-NN/2,NN/2-1}]},
		{n,-NN/2,NN/2-1}]

Sample SincM and take Inverse DFT to get Rectangular Window

NN = 32; (* Number of samples around unit circle *)

W[f_] := SincM[2 Pi f,M];

w = IDFT[W,NN];

ListPlot[Re[w], PlotRange->{0,1.2},
	AxesLabel->{"Time", ""},
	PlotLabel->"Time domain window function"];

ListPlot[Re[w], PlotRange->{0,1.2},
	AxesLabel->{"Time", ""}, PlotJoined->True,
	PlotLabel->"Time domain window function"];

Hanning and Hamming Window Transforms

(* The Generalized Hamming window transform, SincM-normalized *)
GenHammingT[wk_, M_, alpha_] := (SincM[wk, M]
	+ ((1-alpha) SincM[(wk - 2 Pi/M), M] 
	+ (1-alpha) SincM[(wk + 2 Pi/M), M])/(2 alpha))

(* The Hanning window transform - maximal roll-off rate *)
HanningT[wk_, M_] := GenHammingT[wk, M, 0.5]

(* The Hamming window transform - maximal sidelobe rejection *)
HammingT[wk_, M_] := GenHammingT[wk, M, 0.53836]

Plot [	HanningT[wk - Pi, M], {wk, 0, 2 Pi},
	PlotLabel->"Hanning Transform at Pi",
	AxesLabel->{" Rad/Sec", ""},
	PlotRange->{-M/4,M}];

Plot [	HammingT[wk - Pi, M], {wk, 0, 2 Pi},
	PlotLabel->"Hamming Transform at Pi",
	AxesLabel->{" Rad/Sec", ""},
	PlotRange->{-M/4,M}];

Window Transforms on a Decibel Scale

RectPlot = Plot [20 Log[10,(1/M)Abs[SincM[wk-Pi,M]]], 
	{wk, 0, 2 Pi},
	PlotLabel->"DFT[Rect](dB)",
	AxesLabel->{"Rad/Sec", ""},
	PlotRange->{-60, 10}];

HanningPlot = Plot [20 Log[10,(1/M)Abs[HanningT[wk - Pi, M]]], 
	{wk, 0, 2 Pi},
	PlotLabel->"DFT[Hanning](dB)",
	AxesLabel->{"Rad/Sec", ""},
	PlotRange->{-60,10}];

(* The anomaly between the first two lobes on the left
   (above) looks like a bug to me. *)

HammingPlot = Plot [20 Log[10,(1/M)Abs[HammingT[wk - Pi, M]]], 
	{wk, 0, 2 Pi},
	PlotLabel->"DFT[Hamming](dB)",
	AxesLabel->{"Rad/Sec", ""},
	PlotRange->{-60,10}];

Show[{RectPlot,HanningPlot,HammingPlot}];

Window Transforms Plotted over the Unit Circle

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{Cos[wk],Sin[wk],(1/M)SincM[wk, M]}], 
	{wk, -Pi, Pi, 2Pi/200}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]
	},
	(* Boxed->False, *)
        BoxRatios->{1,1,.25},
	ViewPoint->{2,-0.7,1},
	PlotLabel->"DFT[Rect] over the Unit Circle",
	AxesLabel->{"Re","Im","Amp"}]];

(* To illustrate another kind of plot, introduce an M/2-sample
   right-shift in the Hanning window in the time domain.  In 
   the frequency domain, this is a linear phase term with slope
   equal to -M/2. *)

ShiftedRectT[wk_,M_] := N[Exp[-I wk M/2] SincM[wk,M]];

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{
    	(1+Im[(1/M)ShiftedRectT[wk,M]])Cos[wk],
    	(1+Im[(1/M)ShiftedRectT[wk,M]])Sin[wk],
    	Re[(1/M)ShiftedRectT[wk,M]]}],
	{wk, -Pi, Pi, 2Pi/400}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]},
    Boxed->False,
    (* BoxRatios->{1,1,.25}, AxesLabel->{"Re","Im","Amp"}, *)
    ViewPoint->{2,-0.7,1},
    PlotLabel->"DFT[CausalRect] along the Unit Circle"]];

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{Cos[wk],Sin[wk], (1/M) HanningT[wk, M]}], 
	{wk, -Pi, Pi, 2Pi/200}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]},
    Boxed->False,
    (* BoxRatios->{1,1,.25}, AxesLabel->{"Re","Im","Amp"}, *)
    ViewPoint->{2,-0.7,1},
    PlotLabel->"DFT[Hanning] over the Unit Circle"]];

ShiftedHanningT[wk_,M_] := N[Exp[-I wk M/2] HanningT[wk,M]];

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{
    	(1+Im[(1/M)ShiftedHanningT[wk,M]])Cos[wk],
    	(1+Im[(1/M)ShiftedHanningT[wk,M]])Sin[wk],
    	Re[(1/M)ShiftedHanningT[wk,M]]}],
	{wk, -Pi, Pi, 2Pi/400}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]},
    Boxed->False,
    (* BoxRatios->{1,1,.25}, AxesLabel->{"Re","Im","Amp"}, *)
    ViewPoint->{2,-0.7,1},
    PlotLabel->"DFT[CausalHanning] along the Unit Circle"]];

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{Cos[wk],Sin[wk], (1/M) HammingT[wk, M]}], 
	{wk, -Pi, Pi, 2Pi/200}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]},
    Boxed->False,
    (* BoxRatios->{1,1,.25}, AxesLabel->{"Re","Im","Amp"}, *)
    ViewPoint->{2,-0.7,1},
    PlotLabel->"DFT[Hamming] over the Unit Circle"]];

ShiftedHammingT[wk_,M_] := N[Exp[-I wk M/2] HammingT[wk,M]];

Show[ Graphics3D[ { 
    PointSize[0.01],
    Table[Point[{
    	(1+Im[(1/M)ShiftedHammingT[wk,M]])Cos[wk],
    	(1+Im[(1/M)ShiftedHammingT[wk,M]])Sin[wk],
    	Re[(1/M)ShiftedHanningT[wk,M]]}],
	{wk, -Pi, Pi, 2Pi/400}],
    PointSize[0.003],
    Table[Point[{Cos[wk],Sin[wk],0}],{wk, -Pi, Pi, 2Pi/200}]},
    Boxed->False,
    (* BoxRatios->{1,1,.25}, AxesLabel->{"Re","Im","Amp"}, *)
    ViewPoint->{2,-0.7,1},
    PlotLabel->"DFT[CausalHamming] on the Unit Circle"]];