Error: Index exceeds matrix dimensions. Error in Edesign8_2 (line 21) plot(real(z(i)),imag(z(i)),'o')
2 views (last 30 days)
Show older comments
I cannot for the life of me figure out why my Matlab code keeps breaking. For some reason when the stopband frequency Ws exceeds 1.2, the code breaks due to the extra pole.
%Using Matlab for lowpass Elliptic filter design
close all; clear all;
Wp=1; %passband frequency
Ws=1.2; %stopband frequency
Rp=1; %maximum attenuation in passband (in dB), alpha_max
Rs=30; %minimum attenuation in stopband (in dB), alpha_min
[n,Wn]=ellipord(Wp,Ws,Rp,Rs,'s') %n is filter order, Wn is cutoff
% frequency, 's' denotes an analog filter
% denormalize to Ws for InvCheby
[b,a]=ellip(n,Rp,Rs,1,'s') %b is coefficients of numerator, a is coefficients of
% denominator where 1st entry is highorder
% coefficient. This is the transfer function of a
% normalized inverse Cheby filter.
% Pass the stopband attenuation Rs for InvCheby
sys=tf(b,a) %calculates the transfer function of the normalized butterworth filter
bode(sys) %plots the bode plot of the transfer function
[z,p,k]=ellip(n,Rp,Rs,Wp,'s') %returns zeros (z) and poles (p) and gain (k) of filter
figure; hold on;
for i=1:n
plot(real(p(i)),imag(p(i)),'x')
plot(real(z(i)),imag(z(i)),'o')
end
%axis([-1 1 -1 1])
%calculate stages from poles
poles=p(1);
zeros=z(1);
for i=3:2:n%n-mod(n,2)
poles=[poles,p(i)];
zeros=[zeros,z(i)];
end
%poles/zeros matching
d11=abs(zeros(1)-poles(1))+abs(zeros(2)-poles(2))
d12=abs(zeros(1)-poles(2))+abs(zeros(2)-poles(1))
% for j=1:n/2
% d(i+j-1)=abs(poles(i)-zeros(i))+abs(imag(poles(i)))
w0=sqrt(real(poles).^2+imag(poles).^2)
Q=-w0./(2*real(poles))
for i=1:(n-mod(n,2))/2
den(i,:)=[1 w0(i)/Q(i) w0(i)^2];
num(i,:)=[1 0 abs(zeros(i))^2];
end
if mod(n,2) %odd order polynomial
den(i+1,:)=[0 1 w0(i+1)];
num(i+1,:)=[0 0 1];
end
%order the stages
[Qs,ind]=sort(Q);
w0s=w0(ind);
nums=num(ind,:);
dens=den(ind,:);
for i=1:(n+mod(n,2))/2
t(i)=tf(nums(i,:),dens(i,:));
end
%K=(dens(1,3)*dens(2,3))/(nums(1,3)*nums(2,3))
K=k
M2=1/K
[m,w]=bode(t(1));
M1=max(m)
k1=K*M2/M1
k2=M1/M2
0 Comments
Answers (1)
Ishu
on 26 Mar 2024
Hi Jerrod,
I understand that the issue you're encountering with the code breaking when the stopband frequency "Ws" exceeds 1.2 is due to the calculation and handling of poles and zeros.
Like for "ellipord" function where the filter order and the distribution of poles and zeros can significantly change with the specifications. Elliptic filters can have complex pole-zero placements and can generate different number of poles and zeros. As your "n" and "p" are 5 but the value of "z" is 4, so when you plot the real and imaginary part of poles and zeros, the loop breaks and you get the error.
In order to plot the poles and zeros when the number of poles and zeros might not match exactly, you can refer to the below approach:
figure; hold on; grid on;
plot(real(p), imag(p), 'x', 'MarkerSize', 10); % Poles
plot(real(z), imag(z), 'o', 'MarkerSize', 10); % Zeros
For more information on "ellipord" you can refer the below MathWorks documentation;
See Also
Categories
Find more on Digital Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!