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

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 11 de Oct. de 2021

0 votos

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 comentarios

Mathieu NOE
Mathieu NOE el 12 de Oct. de 2021
Hi
if my contribution has helped you, do you mind accepting it ?
tx
Robert Manalo
Robert Manalo el 12 de Oct. de 2021
Hi sorry for the delay but this one really this means a lot and thank you but im refering to this kind of code:
b0 = 0.05;
b1 = 0.03;
b2 = 0.02;
a1 = 0.5;
a2 = a1;
x = [1 0 0 0 0 0 0 0];
xsize = size(x);
x1 = [0 0 x];
for i=3:xsize(2)
y(i-2) = (b0*x1(i) + b1*x1(i-1) + b2*x1(i-2)) ;
yn = [0 0 y];
end
can you still help continuing....? i dont know if im in the right path
Mathieu NOE
Mathieu NOE el 12 de Oct. de 2021
ok , so it's about doing a time simulation
you can use filter to do it - you are not obliged to write the recursion yourself (unless it is required)
as it seems you are looking for an impulse response see also dimpulse
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);
Robert Manalo
Robert Manalo el 12 de Oct. de 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 el 12 de Oct. de 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)

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 11 de Oct. de 2021

Comentada:

el 12 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by