HW#4
HW#4
Problem 1. The file ECG_60HZ_data.mat contains an ECG signal in variable x that was sampled at fs =
250 Hz and has been corrupted by 60-Hz noise. The 60-Hz noise is at a high frequency compared to the
ECG signal, so it appears as a thick line superimposed on the signal. Construct a 127-coefficient FIR
rectangular bandstop filter with a center frequency of 60 Hz and a bandwidth of 10 Hz (i.e., ±5 Hz) and
apply it to the noisy signal. Implement the filter using either filter or conv with the 'same' option, but note
the time shift if you use the former. Plot the signal before and after filtering and also plot the filter’s
magnitude spectrum to ensure the filter spectrum is what you want.
Problem 2. This problem compares causal and noncausal FIR filter implementation. Generate the filter
coefficients of a 65th-order rectangular window filter with a cutoff frequency of 40 Hz. Apply a Blackman–
Harris window to the filter. Then apply the filter to the noisy sawtooth wave, x, in file sawth.mat. This
waveform was sampled at fs = 1000 Hz. Implement the filter in two ways. Use the causal filter routine and
noncausal conv with the 'same' option. (Without this option, conv is like filter except that it produces extra
points.) Plot the two waveforms along with the original, superimposed for comparison. Note the obvious
differences. Also note that while the filter removes much of the noise, it also reduces the sharpness of the
transitions.
Problem 3. Given the advantage of a noncausal filter with regard to the time shift shown in Problem 4.20,
why not use noncausal filters routinely? This problem shows the downsides of noncausal FIR filtering.
Generate the filter coefficients of a 33rd-order rectangular window filter with a cutoff frequency of 100 Hz,
assuming fs = 1 kHz. Use a Blackman–Harris window on the truncated filter coefficients. Generate an
impulse function consisting of a 1 followed by 255 zeros. Now apply the filter to the impulse function in
two ways: causally using the MATLAB filter routine, and noncausally using the conv routine with the
'same' option. (The latter generates a noncausal filter since it performs symmetrical convolution.) Plot the
two time responses separately, limiting the x axis to 0–0.05 s to better visualize the responses. Then take
the Fourier transform of each output and plot the magnitude and phase. (For a change, you can plot the
phase in radians.) Use the MATLAB unwrap routine on the phase data before plotting. Note the strange
spectrum produced by the noncausal filter (i.e., conv with the ‘same’ option). This is because the noncausal
filter has truncated the initial portion of the impulse response. To confirm this, rerun the program using an
impulse that is delayed by 10 sample intervals (i.e., impulse = [zeros(1,10) 1 zeros(1,245)];). Note that the
magnitude spectra of the two filters are now the same, although the phase curves are different due to the
different delays produced by the two filters. The phase spectrum of the noncausal filter shows reduced
phase shift with frequency as would be expected. This problem demonstrates that noncausal filters can
create artifact with the initial portion of an input signal because of the way it compensates for the time shift
of causal filters.
Problem 4. Compare the step response of an 8th-order Butterworth filter and a 44th-order (i.e., L = 44 +
1) rectangular window (i.e., FIR) filter, both having a cutoff frequency of 0.2 fs. Assume a sampling
frequency of 2000 Hz for plotting. (Recall that MATLAB routines normalize frequencies to fs/2, while the
rectangular window filters equations are normalized to fs). Use a Blackman–Harris window with the FIR
filter, then implement both filters using MATLAB’s filter routine. Use a step of 256 samples but offset the
step by 20 samples for better visualization (i.e., the step change should occur at the 20th sample). Plot the
time responses of both filters. Also plot the magnitude spectra of both filters. Use Equation 4.13 to find the
Instructor: Dr. Ngo Thanh Hoan
spectrum of both filters. (For the FIR filter, the denominator of Equation 4.13 is 1.0.) Note that although
the magnitude spectra have about the same slope, the step response of the IIR filter has more overshoot—
another possible downside to IIR filters.
Problem 5. Repeat Problem 1, but use an eighth-order Butterworth bandstop filter to remove the 60-Hz
noise. Load the file ECG_60HZ_data.mat containing the noisy ECG signal in variable x (fs = 250 Hz).
Apply the filter to the noisy signal using either filter or filtfilt, but note the time shift if you use the former.
Plot the signal before and after filtering. Also plot the filter spectrum to ensure the filter is correct. [Recall
that with the Signal Processing Toolbox, cutoff frequencies are specified relative to fs/2.] Note how
effective the low-order IIR filter is at removing the 60-Hz noise.
Problem 6. This problem demonstrates a comparison of a causal and a noncausal IIR filter implementation.
Load file Resp_noise1.mat containing a noisy respiration signal in variable resp_noise1. Assume a sample
frequency of 125 Hz. Construct a 14thorder Butterworth filter with a cutoff frequency of 0.15 fs/2. Filter
that signal using both filter and filtfilt and plot the original and both filtered signals. Plot the signals offset
on the same graph to allow for easy comparison. Also plot the noise-free signal found as resp in file
Resp_noise1.mat below the other signals. Note how the original signal compares with the two filtered
signals, in terms of the restoration of features in the original signal and the time shift.