Contents

Demonstration of the operation of a Difference Equation

format compact;

N=25;n=0:N-1;

Different Inputs

xi=[1 zeros(1,N-1)];
%xi=[1 ones(1,N-1)];
%xi=cos(2*pi*.05*n);

Initialize the Filter: Program memory : a and b Coefficients

a1=-1.7;a2=.8;b0=1;b1=0;b2=0;
%a1=-1.7;a2=.8;b0=1;b1=0;b2=0;

Initialize the Filter: data memory : Variable data, x and y

y1=0;y2=0;x1=0;x2=0;

Time starts

n=0;

Run The difference Equation

figure(1);plot(0,0);hold on;%axis([0 N -5 5]);
grid on
% At each sampling instant repeat the following loop ('forever')
    fprintf('%s %s %s %s %s %s \n','         y      ','   y1    ','    y2    ','    x     ','    x1    ','    x2')
while(n<N)
    % Read  input x
    x=xi(n+1);
    % Calculate next value of output
    y = -a1*y1 -a2*y2 + b0*x + b1*x1 + b2*x2;
    fprintf('%10.3g %10.3g %10.3g %10.3g %10.3g %10.3g\n',y,y1,y2,x,x1,x2)
%     data=['y=' num2str(y) ';   y1='  num2str(y1) ';   y2='   num2str(y2) ';   x='   num2str(x) ';   x1='   num2str(x1) ';   x2='   num2str(x2)];
%     disp(data)
    % Update Memory
    stem(n,y,'markersize',6,'markerfacecolor','r');drawnow;hold on;%pause
    y2=y1;y1=y;
    n=n+1;
    end
hold off
         y          y1         y2         x          x1         x2 
         1          0          0          1          0          0
       1.7          1          0          0          0          0
      2.09        1.7          1          0          0          0
      2.19       2.09        1.7          0          0          0
      2.06       2.19       2.09          0          0          0
      1.74       2.06       2.19          0          0          0
      1.31       1.74       2.06          0          0          0
     0.842       1.31       1.74          0          0          0
      0.38      0.842       1.31          0          0          0
   -0.0276       0.38      0.842          0          0          0
    -0.351    -0.0276       0.38          0          0          0
    -0.575     -0.351    -0.0276          0          0          0
    -0.696     -0.575     -0.351          0          0          0
    -0.724     -0.696     -0.575          0          0          0
    -0.673     -0.724     -0.696          0          0          0
    -0.566     -0.673     -0.724          0          0          0
    -0.423     -0.566     -0.673          0          0          0
    -0.267     -0.423     -0.566          0          0          0
    -0.115     -0.267     -0.423          0          0          0
    0.0181     -0.115     -0.267          0          0          0
     0.123     0.0181     -0.115          0          0          0
     0.194      0.123     0.0181          0          0          0
     0.232      0.194      0.123          0          0          0
     0.239      0.232      0.194          0          0          0
      0.22      0.239      0.232          0          0          0