Reputation: 331
Given a differential equation:
y[n] - 0.9y[n-1] + 0.81y[n-2] = x[n] - x[n-2]
a. Find the impulse response for h[n], n=0,1,2
using recursion.
b. Find the impulse response using MATLAB command filter.
Upvotes: 0
Views: 12025
Reputation: 32930
I understand that this is homework, so I will try to give you guidelines without actually giving away the answer completely:
This is actually quite simple, because the differential equation contains the body of the recursive function almost entirely: y[n] = 0.9y[n-1] - 0.81y[n-2] + x[n] - x[n-2]
The parts in bold are actually the recursive calls! What you need to do is to build a function (let's call it func
) that receives x
and n
, and calculates y[n]
:
function y = func(x, n)
if (n < 0)
%# Handling for edge case n<0
return 0
else if (n == 0)
%# Handling for edge case n=0
return x(0)
else
%# The recursive loop
return 0.9 * func(x, n-1) - 0.81 * func(x, n-2) + x(n) - x(n-2)
end
Note that it's pseudo-code, so you still have to check the edge cases and deal with the indexation (indices in MATLAB start with 1 and not 0!).
The response of a digital filter is actually the y[n] that you're looking for. As you probably know from lesson, the coefficients of that filter would be the coefficients specified in the differential equation. MATLAB has a built-in function filter
that emulates just that, so if you write:
B = [1, 0, 1]; %# Coefficients for x
A = [1, 0.9, -0.81]; %# Coefficients for y
y = filter(B, A, x);
You'd get an output vector which holds all the values of y[n].
Upvotes: 5