Difference Equations and Impulse Responses

This project will help you to become more familiar with difference equations by exploring their characteristics in both the time and frequency domains.

Contents

1. Find filter

Let

This difference equation can be implemented using the filter command. Find the vectors a and b that represent the difference equation above for the filter command.

a = [1 -0.5];
b = [0.1 0.8 0.8];

2. Calculate h[n]

Calculate h[n] analytically for the difference equation above. Hint: You may find the residuez function useful.

3. Find h[n] by filtering an impulse

impulse = [1 zeros(1,99)];
h = filter(b,a,impulse);
stem([0:length(h)-1],h)

4. Filter pulse by filter and by conv

pulse = [ones(1,9) zeros(1,91)];
pulse_filt = filter(b,a,pulse);
pulse_conv = conv(h,pulse);

stem([0:length(pulse_filt)-1],pulse_filt)
stem([0:length(pulse_conv)-1],pulse_conv)

Visible difference is only in length. However, there are very tiny differences due to truncating the IIR response with filter

stem([0:length(pulse_filt)-1],pulse_filt-pulse_conv(1:length(pulse_filt)))

5. Frequency response

% See #2.

6. Plot mag/phase response

freqz(b,a)

The system is lowpass.

7. Filter two cosines

x1 = cos(0.1*pi*[0:99]);
x2 = cos(0.8*pi*[0:99]);

x1filt = filter(b,a,x1);
x2filt = filter(b,a,x2);

stem([0:length(x1filt)-1],x1filt)
stem([0:length(x2filt)-1],x2filt)

x1 is amplified, but x2 is attenuated

8. Demonstrate differentiation-in-frequency property of DTFT

% generate sequence

n = [0:100];
z = 0.5.^n;
zn = n.*z; % time-domain effect
Zf = fft(z); % sampled DTFT of z, where $$\omega = \frac{2\pi}{101}$$
Znf = fft(zn); % sampled DTFT of zn
Zfdiff = j*diff([Zf(end) Zf]); % approximating derivative by difference
Zfdiff = Zfdiff*101/(2*pi); % scaling due to omega vs. k
subplot(2,1,1)
plot(abs(Znf))
subplot(2,1,2)
plot(abs(Zfdiff))

These are approximately the same, as the theory tells us