MATLAB: Impulse response for difference equation

difference equationdtsp

I'm trying to derive the impulse response for the following:
y[n] = ay[n − 1] + x[n] − (a^N)x[n − N]
I am using two different methods.
  1. using the filter() function in Matlab.
  2. using pencil and paper, as per Exercise 2.25 in DTSP by Oppenheim
But the results don't agree with each other:
My code:
close all
clear
% solve the following:
% y[n] = ay[n − 1] + x[n] − (a^N)x[n − N]
% first with the fitler() function
% next with direct calculation of h(n), as per ex. 2.25 in the textbook
% using the filter() function
a=0.8;
N=5;
aa=zeros(1,50);
aa(1:2)=[1 -a];
bb=zeros(1,50);
bb(1)=1;
bb(N+1)=power(a,N);
myImpulse=zeros(1,50);
myImpulse(1)=1;
myImpulseResponse=filter(bb,aa,myImpulse);
t=0:49;
figure(1);
subplot(2,1,1)
stem(t,myImpulseResponse)
title('using filter()');
% next with direct calculation of h(n), as per ex. 2.25 in DTSP Oppenheim
% h[n] = (a^n)u[n] - (a^(n))*u[n-N]
a1 = power(a,t);
a2 = power(a,t);
a2(1:N)=0;
calculatedImpulseResponse = a1-a2;
subplot(2,1,2)
stem(t,calculatedImpulseResponse)
title('calculated as per DTSP Oppenheim')
I can explain my derivation (as per the example 2.25 in DTSP) if you need it.
I would like to know if I am using filter() correctly. Any observations would be appreciated.

Best Answer

Hi Roger,
try
bb(N+1) = -power(a,N);
which is what you actually have. I think you'll lilke the resulting plot.
Related Question