Digital Butterworth Bandpass Filter
15 views (last 30 days)
Show older comments
Hello, I am trying to create a butterworth bandpass filter in Matlab, and since it is my first time doing so, I am having issues. I am having a buttord error which I am having difficult understanding how to fix. I will copy paste my code under, so if you could help me resolve error it would be helpful. The error is "Error using buttord (line 78) The cutoff frequencies must be within the interval of (0,1)." Also in the next step I am trying to find the phase of the dominant frequency, so if you have any input on how to do that part, it will be greatly appreciated too. Thank you!
% Designing a Butterworth Bandpass Filter
alphap=2; %passband attenuation in db
alphas=20; %stopband attenuation in db
wp=[0.2,5]; %first and second passband Frequencies(The range of frequencies that pass)
ws=[0.1,6] %first and second stopband frequencies (right before and after the passband)
%to find cutoff frequency and order of the filter
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas) %the buttord command gives the order of filter
%system function of the filter
[b,a]=butter(n,wn)
w=0:0.01:6 ; % taken frequency
[h,ph]=freqz(b,a,w); %taken command frequency, With the freqz command you can convert the system function into the z domain
m=20*log10(abs(h)); % find the absolute value of the output from the freq domain system
an=angle(h); %find the phase of the z function
subplot(2,1,1)
plot(ph/pi,m)
grid on;
xlabel('noramalized frquency')
ylabel('gain in DB')
subplot(2,1,2)
plot(ph/pi,an) %here you use the phase
grid on;
xlabel('normalized frequency')
ylabel('phase in radians')
0 Comments
Answers (1)
vidyesh
on 12 Apr 2024
Edited: vidyesh
on 12 Apr 2024
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.
0 Comments
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!