HESC686   Mathematics and Signal Processing for Biomechanics

 

Windowing  

 

When frequency content of a signal is computed, errors can and do arise when we take a limited-duration snapshot of a signal that actually lasts for a longer time.  Windowing is a way to reduce these errors, though it cannot eliminate them completely.

 

Why errors happen

The mathematical process of frequency analysis assumes that the snapshot we have taken will immediately repeat. This is not really true.  And it often the case that the end of our snapshot does not smoothly mesh with the start of our snapshot.  This means that if our snapshot really were one “repeat” of the signal, it would have to be a signal which was discontinuous at the edge of the recording period.  The only way to create a repeating signal with a discontinuity is to add high frequency components to the signal to mimic the “jump” at the discontinuity.  This occurs in frequency analysis.  But the true signal did not have a discontinuity, so the high frequencies that appear in the power spectrum are in a sense artifacta that arises because we could not sample the signal forever.  (Real signals never repeat, so their period is “forever”.)

 

How “windowing” can help

We can suppress the discontinuity, and the resulting spurious high frequencies in the frequency analysis, by “tapering” the recorded signal smoothly to zero at the start and end of the recording period.  This is the basic idea of windowing.  How to do it?

1.      Remove the mean value from the signal whose spectrum you wish to compute by subtracting the mean from all the points

2.      Compute the coefficients for a window which is as long as the number of data points in your signal.

3.      Multiply each data point in the de-meaned signal by the corresponding window coefficient.

4.      Compute the Fourier transform or power spectrum of the tapered signal.

 

When not to use a window

Do not use a window if your signal is an integer number of cycles of a periodic signal.  The reason is that in such a case, the end of the signal will mesh smoothly with the beginning (i.e. it wraps around smoothly), and therefore there is no need for a window (see discussion above).  Examples of this situation include:

1.      Biomechanical gait data that is averaged across multiple strides to create one full stride’s worth of data

2.      Blood pressure or flow waves that are averaged across multiple heart beats to create a wave corresponding to one full beat.

 

Examples of widely used windows

Many suggestions exist for how to smoothly taper a signal at its edges.  Well-known and widely used windows include the Hanning, Hamming, and Blackman windows.  They differ slightly in how they taper near the edges.  In the formulas below, we assume the recording period extends from t=0 to t=T.

 

Hanning window

This is also known as a cosine taper.  It starts at 0, rises to 1 in the middle of the period, and then goes smoothly back down to zero at the end.

w1(t) = 0.5  – 0.5 * cos(2pt/T)

If x(t) is the data (presumed to have zero mean value), then the windowed version of the data,y(t), is computed as follows:

y1(t) = x(t) * w1(t)

 

Hamming window

This modified cosine taper starts at 0.08, rises to 1 in the middle of the period, and then goes smoothly back down to 0.08 at the end.

w2(t) = 0.54  – 0.46 * cos(2pt/T)

y2(t) = x(t) * w2(t)

 

Blackman window

This modified cosine taper uses two cosines. 

w3(t) * 0.42  – 0.5 * cos(2pt/T) + 0.08 * cos(4pt/T)

y3(t) = x(t) * w3(t)

 

Figure 1 shows Hannning, Hamming, and Blackman windows of length 100.

Figure 1. Window comparison.  File windowing_fig1.jpg.

Matlab code to make Figure 1:

>> w1=hanning(100);

>> w2=hamming(100);

>> w3=blackman(100);

>> t=1:100;

>> plot(t,w1,'.r-',t,w2,'.g-',t,w3,'.b-');

>> xlabel('Time'); ylabel('Amplitude'); title('Window Comparison');

>> legend('Hanning','Hamming','Blackman'); grid on;

 

Kaiser, or Kaiser-Bessel, window:

This tapering function has an adjustable parameter b.  b can vary from 0 to +infinity, but the range 3 ≤ b ≤ 10 is typical.  When b = 0, the Kaiser window becomes a rectangular window (no tapering).  As b rises, tapering increases, and the main lobe width increases and sideband attenuation increases.

The exact formula for the Kaiser window is complicated.  Figure 2 shows Kaiser windows with b=3, 5, and 10.

Figure 2.  Kaiser window comparison.  File windowing_fig2.jpg.

Matlab code to make Figure 2:

>> w4=kaiser(100,3);

>> w5=kaiser(100,5);

>> w6=kaiser(100,10);

>> t=1:100;

>> plot(t,w4,'.r-',t,w5,'.g-',t,w6,'.b-');

>> xlabel('Time'); ylabel('Amplitude'); title('Kaiser Window Comparison');

>> legend('b=3','b=5','b=10'); grid on;

 

Rectangular window: this is really no window, i.e. no tapering.  It is mentioned for completeness and because the rectangular window is one of the window options in many window-related VIs in Labview.

w0(t)=1 for 0<=t<T.

y(t) = x(t)*w0(t) = x(t)

 

Figure 3 shows an EMG before and after windowing.

Figure 3.  EMG before and after windowing with a Hamming window.  File windowing_fig3.jpg.

Matlab code for Figure 3:

>> d=load('wcr0301164.emg');

>> N=length(d);

>> t=(1:N)/10000;

>> x=d-mean(d);

>> w1=hamming(N);

>> y=x.*w1;

>> plot(t,x,'k',t,y,'r');

>> xlabel('Time (s)'); ylabel('Amplitude (V)'); title('wcr0301164.emg');

>> legend('Raw (zero mean)','After Hamming Window');

 

Which window should I use?

In general, the different windows trade off two competing goals which can be seen in the frequency domain: having a narrow “main lobe width” and having high attenuation of the side lobes.  The Blackman window has a wider main lobe and more side lobe attenuation than Hanning or Hamming.  As b increases for the Kaiser window, the main lobe width (in the frequency domain) increases and the side lobe attenuation increases.

Figure 4 shows the frequency domain behavior of the Hanning, Hamming, Blackman, and rectangular (no taper) windows.  The first side lobe of the Hamming is lower (i.e. Hamming is better) than the first side lobe of the Hanning, but the “distant” side lobes of the Hanning are lower than the Hamming (thus the Hanning is better in that regard).  The Blackman has a wider main lobe width (which is undesirable) compared to the Hanning and Hamming, but it has more side lobe attenuation (which is desirable).  The rectangular window has minimal side lobe attenuation, which is why it is a poor choice.

Figure 4.  Frequency domain comparison of Hanning, Hamming, Blackman, and rectangular windows.  File windowing_fig4.jpg.

Matlab code for Figure 4:

>> w0=ones(1,100);

>> wvtool(w1,w2,w3,w0);

 

In most biomedical applications, any one of the windows considered above, except the rectangular (no taper) window, will give acceptable results.  The Hamming window is preferred by many due to its relatively narrow main lobe width and good attenuation of the first few side lobes.

 

Figure 5 shows the amplitude spectra of an EMG signal, computed with and without a Hamming window.  The differences between the two spectra do not seem to be significant. The Labview window VIs used in this example automatically amplify the window coefficients to approximately conserve total power.  This is why the peak power of the windowed signal is slightly greater than the peak power without windowing.

 

Figure 5.  Comparison of amplitude spectra of a signal, with and without a Hamming window.  File windowing_fig5.jpg.

This was created with spectrumwithavg+window.vi, data files wcr0301164.emg (sampled at 10 kHz). 

 

Spectra may be estimated by dividing a signal into segments, computing the spectrum for each segment, then averaging the spectral results.  Windows can and usually should be used in this process: each segment should be multiplied by a window.  Figure 6 shows how this is done, using the same data as in Figure 5, divided into 4 segments.  The left panel shows the data after applying a Hamming window to each of the four segments.  The right panel shows the amplitude spectrum of the segmented, windowed data. (The segmenting technique for spectrum estimation is discussed elsewhere.  In Labview we specify the “vector averaging” technique.  In Matlab, vector averaging is assumed.)

 

Figure 6.  Computation of a spectrum estimate by segmenting and windowing the data.  File windowing_fig6.jpg.

This was created with spectrumwithavg+window.vi, data files wcr0301164.emg (sampled at 10 kHz). 

 

 

Copyright © 2011 W.C. Rose