Hi Hassan,
The error you're encountering is due to the cutoff frequencies being outside the expected range for the buttord function, which expects values within (0,1). This range corresponds to normalized frequencies, where 1 represents the Nyquist frequency. In the initial approach, frequencies were divided by π, but for this specific application, the normalization should be based on the Nyquist frequency instead. Here's how to adjust your code for better alignment with MATLAB's expectations:
clc;
clear;
% Define the Nyquist frequency
F_nyquist = 50;
alphap = 2; % Passband attenuation in dB
alphas = 20; % Stopband attenuation in dB
%%
% Normalize the passband and stopband frequencies
wp = [0.2, 5] / F_nyquist; % Normalized passband frequencies
ws = [0.1, 6] / F_nyquist; % Normalized stopband frequencies
%%
% Determine the cutoff frequency and order of the filter
[n, wn] = buttord(wp, ws, alphap, alphas); % The buttord command determines the filter order
% Generate the system function of the filter
[b, a] = butter(n, wn);
% Define the frequency range for analysis
w = 0:0.01:6; % Frequency range
% Convert the system function into the frequency domain
[h, ph] = freqz(b, a, w);
% Compute the magnitude in dB and the phase of the frequency response
m = 20 * log10(abs(h)); % Magnitude in dB
an = angle(h); % Phase in radians
% Plot the magnitude response
subplot(2,1,1);
plot(ph / pi, m);
grid on;
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Gain in dB');
% Plot the phase response
subplot(2,1,2);
plot(ph / pi, an);
grid on;
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Phase (radians)');
I've assumed the Nyquist as 'F_nyquist = 50;'.
For more information and examples on filter design using buttord, check out the MATLAB documentation here: https://www.mathworks.com/help/signal/ref/buttord.html
This page will also shows how to plot and understand the filter’s behavior.
Hope this helps.