IIR Filter for loop Code

4 views (last 30 days)
Robert Manalo
Robert Manalo on 11 Oct 2021
Commented: Mathieu NOE on 12 Oct 2021
Can anyone help to code this one using for loop code?
the given filter coefficient
b0 = 0.05
b1 = 0.03
b2 = 0.02
a1 = 0.5
a2 = 0.5

Accepted Answer

Mathieu NOE
Mathieu NOE on 11 Oct 2021
hello
if you want to simply plot the bode diagram :
Fs = 1000; % assumed for now ...sampling frequency
b0 = 0.05
b1 = 0.03
b2 = 0.02
a1 = 0.5
a2 = 0.5
numd = [b0 b1 b2];
dend = [1 a1 a2];
dbode(numd,dend,1/Fs);
  5 Comments
Robert Manalo
Robert Manalo on 12 Oct 2021
thank you mate for this one but what i need is the iir filter using "for loop" code and not about the plotting can you still help me?
Mathieu NOE
Mathieu NOE on 12 Oct 2021
sure
here you are .
BTW I noticed I made a sign error in the beginning :
dend = [1 -a1 -a2]; % instead of dend = [1 a1 a2];
code updated - see at the end the demo of for loop coding
you can see the 3 methods do show the same result (fortunately !)
clc
clearvars
b0 = 0.05;
b1 = 0.03;
b2 = 0.02;
a1 = 0.5;
a2 = 0.5;
numd = [b0 b1 b2];
dend = [1 -a1 -a2];
x = [1 0 0 0 0 0 0 0 0 0 0 0 0 0]; % looks like you're looking for the impulse response ?
y = filter(numd,dend,x);
figure(1)
plot(y)
% the same impulse response can be obtainde by
figure(2)
dimpulse(numd,dend);
% manual for loop coding
x = [1 0 0 0 0 0 0 0 0 0 0 0 0 0];
samples = length(x);
y(1) = b0*x(1) + 0 + 0 + 0 + 0;
y(2) = b0*x(2) + b1*x(1) + 0 + a1*y(1) + 0;
for k = 3:samples
y(k) = b0*x(k) + b1*x(k-1) + b2*x(k-2) + a1*y(k-1) + a2*y(k-2);
end
figure(3)
plot(y)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!