Clear Filters
Clear Filters

Freqz Command Resulting in Position 1 Invalid Index

1 view (last 30 days)
Hi, I was wondering if someone could review my code an explain what is causing the error message in my freqz command. The error message is claiming that index in position 1 is invalid and that array indices must be positive or local, but I don't see what's causing the issue. If someone could help me figure out what the error is, I would be very appreciattive
clc
w1 = 0.35;
w2 = 0.65;
n = 1024;
freqz = linspace(0,1,n);
for order = [16 32 64 128 256];
bp = fir1(order, [w1 w2], 'bandpass');
f = freqz(bp, 1, n);
magn_dB = 20*log10(abs(f));
plot [freqz, magn_dB, DisplayName, ['Order' num2str(order)];
hold on ;
end
Index in position 1 is invalid. Array indices must be positive integers or logical values.

'freqz' appears to be both a function and a variable. If this is unintentional, use 'clear freqz' to remove the variable 'freqz' from the workspace.
legend('Location', 'South');
grid on
xlabel('Normalized Frequency, \omega')
ylabel ('Magnitude, dB')
title('Effect of Increasing Filter Order')

Answers (1)

Jan
Jan on 25 Oct 2022
Edited: Jan on 25 Oct 2022
The problem is mentioned in the error message already:
"'freqz' appears to be both a function and a variable. If this is unintentional, use 'clear freqz' to remove the variable 'freqz' from the workspace."
After this line:
freqz = linspace(0,1,n);
freqz is a vector, not a function anyore.
What is the purpose of this line:
plot [freqz, magn_dB, DisplayName, ['Order' num2str(order)];
Do you mean:
plot(freqz, magn_dB, 'DisplayName', ['Order' num2str(order)]);
% ^^^^^ see above

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!