You are on page 1of 13

Music 421

Spring 2004-2005
Homework #6
Spectral Smoothing, Window Method
48 points
Due in one week (5/18/2005)

1. (5 pts) Derive the ideal impulse response corresponding to the desired amplitude re-
sponse (
1, 0 ≤ ω1 ≤ |ω| ≤ ω2 ≤ π/T
H(ejωT ) =
0, otherwise
[Hint: Use a Fourier theorem to make use of the result given for the ideal lowpass filter
in the lecture overheads and/or text.]
Solution:

h(n) = DT F T −1 H(ejωT )
¡ ¢
Z π
1 T
= H(ejωT )ejωT n dω
2π/T − T π
Z ωc
T
= (1)ejωT n dω
2π −ωc
T 1 jωT n ¯¯ωc
= e
2π jT n −ωc

1
= sin(ωc T n)
πn

2. (10 pts) For the ideal bandpass filter of the previous problem, with ω1 T = ω2 T = π/2
(i.e., the pass-band width is zero), design a length M = 100 real FIR bandpass filter
using the window method. Define the transition bandwidth to be the width of the
main lobe for the window used. Plot an overlay of the ideal and obtained amplitude
responses (in dB) for the following window types (please group appropriately to save
paper) :

• Hamming
• Kaiser with β set to give a worst-case stopband rejection of 42 dB
• Hann
• Kaiser with β set to give 31 dB stopband rejection
• Blackman
• Kaiser with β set to give 58 dB stopband rejection

1
• Kaiser with β = 10

Describe how the results for the first three Kaiser windows compare with those for the
respective first three Blackman-Harris windows. What stopband rejection does β = 10
yield? In general, how does the stop-band rejection compare to the side-lobe level of
the window functions used?
Solution:
This is meant to be a zero-width bandpass filter, ideally passing a frequency only at
ω = π/2. One may argue that the filter width is zero, with a zero width impulse in the
DTFT domain at π/2. By this reasoning, when we take the integral to go from the
DTFT domain to the discrete time domain, we in fact get zero. So, mathematically,
the answer really is that the filter impulse response is 0 for all time. However, any form
of limiting construction shows the “engineering solution” which indicates a need for
some kind of renormalization leading to an impulse in the frequency domain. We will
accept the “mathematical” answer if the above is shown, but the engineer’s answer is
obviously preferred. The code used to design and plot the frequency response of the
filter using various windows is given below.

M=100;
%N=8*(M-1);
%n=-(M-1)/2:(M-1)/2;
N = 8*M;
n = 0:M-1;
hid=2*cos(pi*n/2);

w=hamming(M)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(1);
plot([0:1/N:0.5-1/N],20*log10(max(0.001,abs(Hw(1:N/2))/max(abs(Hw)))),’b--’);
grid
hold on

w=hanning(M)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(1);
hold on;
plot([0:1/N:0.5-1/N],20*log10(max(0.0001,abs(Hw(1:N/2))/max(abs(Hw)))),’k’);
grid
xlabel(’normalized frequency’);ylabel(’Normalized gain(dB)’);

w=blackman(M)’;
hw=hid.*w;

2
Hw=fft(hw,N);
figure(1);hold on;
plot([0:1/N:0.5-1/N],20*log10(max(0.0001,abs(Hw(1:N/2))/max(abs(Hw)))),’r-.’);
grid
xlabel(’normalized frequency’);ylabel(’Normalized gain(dB)’);
hold on;
stem(0.25,0);
legend(’hamming’,’hann’,’blackman’,’ideal’);
hold off

% Second set (Kaiser)


w=kaiser(M,5.7)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(2);
plot([0:1/N:0.5-1/N],20*log10(abs(Hw(1:N/2))/max(abs(Hw))));grid

w=kaiser(M,4.1)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(2);hold on;
plot([0:1/N:0.5-1/N],20*log10(abs(Hw(1:N/2))/max(abs(Hw))),’r--’);grid

w=kaiser(M,8)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(2);hold on;
plot([0:1/N:0.5-1/N],20*log10(abs(Hw(1:N/2))/max(abs(Hw))),’c-.’);grid

w=kaiser(M,10)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(2);hold on;
plot([0:1/N:0.5-1/N],20*log10(abs(Hw(1:N/2))/max(abs(Hw))),’k:’);grid
xlabel(’normalized frequency’);ylabel(’Normalized gain(dB)’);
hold on
stem(0.25,0);
legend(’\beta=5.7’,’\beta=4.1’,’\beta=8’,’\beta=10’,’ideal’);
hold off

The overlaid plots are shown in Figure 1 for the Hamming, Hann and Classic Blackman
and in Figure 2 for Kaiser windows. In particular, β = 5.7, 4.1 and 8 approximately
give the worst-case stopband rejection of 42, 31 and 58 dB respectively. Also shown

3
0
hamming
hann
blackman
−10 ideal

−20

−30

Normalized gain(dB)
−40

−50

−60

−70

−80
0 0.1 0.2 0.3 0.4 0.5
normalized frequency

Figure 1: An overlay of magnitude frequency responses of Hamming, Hann and Blackman


window filter design.
Kaiser window bandpass filter design
0
β=5.7
β=4.1
β=8
−20 β=10
ideal

−40
Normalized gain(dB)

−60

−80

−100

−120

−140
0 0.1 0.2 0.3 0.4 0.5
normalized frequency

Figure 2: An overlay of magnitude frequency responses of Kaiser window filter design with
various values of β.

in Figure 3 are the overlaid plots of Hamming, Hann and Blackman window filter fre-
quency response on comparable filters using Kaiser window.

The differences among windows used in the filter design are exactly what we learned
about the window transform. Kaiser window offers a compact energy within the main-
lobe so its sidelobe level is small but at the expense of wider transition bandwidth. We
have the control of the mainlobe width, hence the transition bandwidth, through β.
Hamming window has equal-level sidelobes but the worst-case sidelobe is lower than
that using Hann. Blackman window gives a response similar to Hann but with lower
sidelob levels and slightly wider mainlobe.
Finally, the stopband rejection in the case of Kaiser window with β = 10 is about -75
dB, taking half the mainlobe as a transition band.
As a continuation of the initial discussion about mathemtical answers, recall the Hilbert
transform discussion from lecture. We obtained inferior results when we simply win-

4
0

Normalized gain(dB)
−100

−200

−300 hamming
kaiser(5.7)
−400
0 0.1 0.2 0.3 0.4 0.5
normalized frequency
0

Normalized gain(dB)
−100

−200

−300 hann
kaiser(4.1)
−400
0 0.1 0.2 0.3 0.4 0.5
normalized frequency
0

Normalized gain(dB)
−100

−200

−300 blackman
kaiser(8)
−400
0 0.1 0.2 0.3 0.4 0.5
normalized frequency

Figure 3: A comparison between using Hamming, Hann and Blackman window with their
comparable Kaiser windows.

dowed the sampled, ideal Hilbert transform kernel. That’s when we went back and
modified the “ideal” specification to be compatible with a “bandlimited environment”,
and thereby avoided aliasing (and halved the necessary computations, since every other
sample became zero in the desired impulse response). In this situation, the ideal re-
sponse can be modified to become compatible with a “time-limited” environment. This
is just another way to approach it.

3. (10 pts) For ωc T = π/2, design a length M = 100 real FIR lowpass filter using
the window method. Plot the amplitude response (dB gain versus frequency) for the
following windows:

• Hamming
• Hann
• Blackman
• Kaiser with β = 10

Explain why the stopband rejection is so different from that in the previous problem.
Solution:
The plots of the frequency responses are shown in Figure 9. The sidelobe levels in
this problem are smoother and lower than the previous one. This is because the
sidelobes of the windowspectrum gets smoothed out by the convolution process in the
frequency domain with the ideal lowpass filter frequency response which is a rectangular
function. This may be counterintuitive, but we recall that sidelobes alternate positive
and negative, thus when we sum over more of them under the ideal rect function, we
get closer to zero. (This frequency domain convolution is due to multiplication in time
domain of the ideal impulse response by the window.)
Relevant code:

M = 100;

5
N = 8*M;
n = -M/2+1:M/2;
hid=sin(pi*0.5*n)./(pi*0.5*n+eps);
hid(n==0) = 1; % due to sinc exception

w=hamming(M)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(3);
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’b--’);
grid
figure(2);
subplot(411);
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’b’);
grid

w=hanning(M)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(3);
hold on;
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’k’);
grid
xlabel(’normalized frequency’);ylabel(’Normalized gain(dB)’);
figure(2);
subplot(412);
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’b’);
grid

w=blackman(M)’;
hw=hid.*w;
Hw=fft(hw,N);
figure(3);hold on;
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’r-.’);
grid
xlabel(’normalized frequency’);ylabel(’Normalized gain(dB)’);
hold on;
figure(2);
subplot(413);
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’b’);
grid

% Kaiser
w=kaiser(M,10)’;

6
Hamming
0

−50

−100

−150
0 0.1 0.2 Hann 0.3 0.4 0.5
0 normalized frequency

−50

Normalized gain(dB)
−100

−150
0 0.1 0.2 Blackman 0.3 0.4 0.5
0 normalized frequency

−100

−200
0 0.1 0.2 Kaiser 0.3 0.4 0.5
0 normalized frequency

−100

−200
0 0.1 0.2 0.3 0.4 0.5
normalized frequency

Figure 4: The magnitude frequency responses of Hamming, Hann, Blackman and Kaiser(10)
window filter design.

hw=hid.*w;
Hw=fft(hw,N);
figure(3);
hold on;
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’g:’);
grid
legend(’hamming’,’hann’,’blackman’,’kaiser’);
hold off
figure(2);
subplot(414);
plot([0:1/N:0.5-1/N],20*log10(max(0.000001,abs(Hw(1:N/2))/max(abs(Hw)))),’b’);
grid

4. (13 pts) Download the sound file noisypeaches.wav1 containing speech embedded in
white noise.

(a) (5 pts) Plot the spectrogram of noisypeaches.wav to help you understand its
spectral content but there is no need to submit it. Design a low pass filter using
the window method with a Kaiser window of length 100 and β=10. The cut-off
frequency of the filter should be 4 kHz. Plot its impulse response and magnitude
of frequency response.
(b) (3 pts) Apply this filter to the noisy speech signal either by the FFT method of
simple filtering. Listen and describe the result compared to the original.
(c) (5 pts) Now downsample the original noisy speech signal by a factor of two by
simply throwing away every other sample. Listen to the result and compare it to
the original higher sampling rate. Repeat the same downsampling scheme on the
lowpass filtered speech signal and again, compare with its higher sampling rate
1
http://www-ccrma.stanford.edu/˜jos/hw421/hw6/noisypeaches.wav

7
version. Why does the latter pair (lowpass filtered) sound more similar than the
first pair (unfiltered)?

Solution: Possible code for this problem, which also generates the required output
files, is:

% HW6q6.m
%[signal,fs] = wavread(’noisyoboe.wav’); % read in signal
[signal,fs] = wavread(’noisypeaches.wav’); % read in signal
%%%%% filter design %%%%%%%%%%%%%%%%%%%%%
M = 100; % time length of window
N = 8*M; % FFT length
n = -M/2+1:M/2; % time indices
%w_c = 6000; % choose cutoff of 6kHz for oboe lowpass filter
w_c = 4000; % choose cutoff of 6kHz for speech lowpass filter
norm_rad_w_c = w_c/(fs/2)*pi; % convert to normalized rad freq
hid=sin(norm_rad_w_c*n)./(norm_rad_w_c*n+eps); % ideal response
hid(n==0) = 1; % due to sinc exception
w=kaiser(M,10)’;
hw=hid.*w; % window ideal response
Hw=fft(hw,N); % frequency response
%%%% part a/c: plots %%%%%%%%%%%%%%%%%%%%
subplot(211); plot(n,hw); xlabel(’n’); ylabel(’amplitude’);
title...
(’lowpass filter using Kaiser(100,10) window: impulse and frequency response’)
subplot(212);
plot(-fs/2:fs/(N-1):fs/2,...
fftshift(20*log10(max(0.000001,abs(Hw)/max(abs(Hw))))));
grid
ylabel(’normalized dB magnitude’); xlabel(’Freq in Hz’);
%%%%%% parts b/c/d: filter signal %%%%%%%
filtered_signal = conv(signal,hw); % simple filtering
wavwrite(filtered_signal./max(filtered_signal),fs,’hw6q6out.wav’);
%%%%%% part e: downsample %%%%%%%%%%%%%%%
ds_signal = signal(mod(1:length(signal),2)==0); % throw away odd samples
ds_filtered_signal = filtered_signal(mod(1:length(signal),2)==0);
ds_fs = fs/2; % downsampled sampling rate
wavwrite(ds_signal./max(filtered_signal),ds_fs,’hw6q6ds.wav’);
wavwrite(ds_filtered_signal./max(filtered_signal),ds_fs,’hw6q6dsf.wav’);

(a) (10 pts) The spectrogram (Figure 6) shows rather obviously that most of the non-
noise signal energy is concentrated the band of frequency up to about 6 kHz. We
can then design a lowpass filter with a cut-off frequency here to retain most of
the oboe signal while getting rid of (white) noise which spreads out through the

8
Lowpass filter using Kaiser(100,10) window : impulse response

0.8

0.6

Magnitude
0.4

0.2

−40 −30 −20 −10 0 10 20 30 40


n

Magnitude frequency response


0

−50

dB
−100

−150
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2
frequency (Hz) x 10
4

Figure 5: The impulse response and the magnitude frequency response of the designed
lowpass filter with a cut-off frequency of 5 kHz.

whole spectrum. The plot of the filter’s impulse response and magnitude of the
frequency response are shown in Figure 5.
(b) (5 pts) By using either FFT or convolution the original signal with the filter just
obtained, the output clearly has lower level of noise while the oboe sound still
sounds very much the same. The spectrogram is shown in Figure 6.
(c) (10 pts) Now with the speech signal, it is not clear-cut where we should place
the cut-off of the lowpass filter having sibilants like “c” and “s” (Figure 8). A
compromise needs to be made but a good choice may be a cut-off at 4 kHz. The
impulse and frequency response of the filter are shown in Figure 7.
(d) (5 pts) The output of the filter clearly shows that we lost some high frequency
components in the speech along with the noise, especially “c” in “juicy” (Figure
8).
(e) (10 pts) If the signal is simply downsampled by throwing away every other sample,
the resulting spectrum is a stretched version of the original and aliasing can
occur. This is evident in listening, and even though it may sound brighter, having
more high frequency content, the aliasing can be a cause of annoyance and is
irrecoverable. On the other hand, if the signal is first bandlimited by lowpass
filtering to make sure that, after stretching, the spectrum will not fold over the
sampling frequency, the resulting downsampled sound will sound very much like
the one before the conversion. The process is also reversible.

5. (5 pts) Figure 9 shows the impulse response and the corresponding magnitude spectrum
of a lowpass filter.

(a) (3 pts) Without using MATLAB, sketch the magnitude spectrum of the impulse
response shown in Figure 10, which was obtained from that in Fig. 9 by negating
the odd-numbered samples. Explain how you obtained your answer.

9
4 Original
x 10

1.5

Frequency
1

0.5

0
0 0.1 0.2 0.3 0.4 0.5 0.6
Time

4 Filtered
x 10

1.5

Frequency
1

0.5

0
0 0.1 0.2 0.3 0.4 0.5 0.6
Time

Figure 6: The spectrogram of the original noisy oboe sound and the lowpass filtered version
using cut-off frequency of 5 kHz.

Lowpass filter using Kaiser(100,10) window : impulse response


1

0.5
Magnitude

−0.5
−50 −40 −30 −20 −10 0 10 20 30 40 50
n

Magnitude frequency response


0

−50
dB

−100

−150
−8000 −6000 −4000 −2000 0 2000 4000 6000 8000
frequency (Hz)

Figure 7: The impulse response and the magnitude frequency response of the designed
lowpass filter with a cut-off frequency of 4 kHz.

Original
8000

6000
Frequency

4000

2000

0
0 0.5 1 1.5 2 2.5 3
Time

Filtered
8000

6000
Frequency

4000

2000

0
0 0.5 1 1.5 2 2.5 3
Time

Figure 8: The spectrogram of the original noisy speech sound and the lowpass filtered version
using cut-off frequency of 4 kHz.

10
Impulse response
1

0.8

0.6

amplitude
0.4

0.2

−0.2

−0.4
−50 −40 −30 −20 −10 0 10 20 30 40 50
time (samples)

Magnitude Spectrum

0
amplitude (dB)

−20

−40

−60

−80
0 0.5 1 1.5 2 2.5 3
frequency (rad/samples)

Figure 9: Impulse response of a lowpass filter and its magnitude spectrum

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−50 −40 −30 −20 −10 0 10 20 30 40 50

Figure 10: Impulse response

11
10

−10

−20

−30

−40

−50

−60

−70

−80
0 0.5 1 1.5 2 2.5 3

Figure 11: Magnitude spectrum of a highpass filter

(b) (2 pts) Verify your answer by plotting the magnitude spectrum of the above
impulse response using MATLAB. You can download a mat file ir.mat2 which
contains an impulse response vector h(n) shown in Fig. 10.

Solution:

(a) (3 pts) Negating odd-numbered samples of h(n) is equivalent to multiplying h(n)


by (−1)n = ejπn ; i.e., it has the same effect of shifting the spectrum H(ω) by
π in the frequency domain. Therefore, the resulting spectrum will be that of a
highpass filter with the cutoff frequency ωc = π − π/4 = 3π/4.
(b) (2 pts) Figure 11 shows the spectrum of the impulse response hhp (n) = (−1)n h(n).

6. (5 pts) Design a real, linear-phase, FIR bandpass filter using remez() in Matlab with
the following specifications: Sampling rate fs = 100 Hz, pass-band from 20 Hz to 30
Hz, stop-band from 0 to 10 Hz and 40 to 50 Hz, δs = 0.01 (−40 dB) ripple in the
stop-band, and δp = 0.02 ripple in the pass-band, which is unity gain. The filter thus
has transition bands from 10 to 20 Hz, and from 30 to 40 Hz. Turn in a listing of your
Matlab code, and the result of its execution (e.g., using the diary command), which
should include a print-out of the filter length, a listing of the filter coefficients, and a
plot of the filter amplitude response on a dB vertical scale. [Hint: Start with ’help
remezord’ in Matlab.]
Solution:

>> diary ’remezproblem.txt’


>> [N,Fo,Ao,W] = remezord([10 20 30 40],[0 1 0],[.01 .02 .01],100)
2
http://ccrma.stanford.edu/˜jos/hw421/hw6/ir.mat

12
N =
17

Fo =
0
0.2000
0.4000
0.6000
0.8000
1.0000

Ao =
0
0
1
1
0
0

W =
2
1
2

>> h = remez(N,Fo,Ao,W)

h =
-0.0179 -0.0229 0.0324 0.0108 0.0295 0.0881 -0.1709
-0.2296 0.2743 0.2743 -0.2296 -0.1709 0.0881 0.0295
0.0108 0.0324 -0.0229 -0.0179

>> freqz(h,1,512,100);
>> print -deps ’ampresponse.eps’

3 extra points for recognizing that ripple specs are not met using remezord output
(N = 17), and increasing N until they are (N = 19).

13

You might also like