IIR Filter for loop Code

5 次查看(过去 30 天)
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

采纳的回答

Mathieu NOE
Mathieu NOE 2021-10-11
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 个评论
Robert Manalo
Robert Manalo 2021-10-12
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 2021-10-12
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)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Digital Filter Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by