MATLAB: How to Butterworth Filter with Bandpass [10 500] with sampling rate 1000

bandpass [10 500]butterworth filter

Hi,
I want to butterworth filter with bandpass [10 500] with sampling frequency 1000 but there is an error stating "The cutoff frequencies must be within the interval of (0,1)." How do I solve this problem?

Best Answer

You have to divide your passband and stopband frequencies by the Nyquist frequency to normalise them:
Fs = 1000; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Wp = [10 495]/Fn; % Norrmalised Passband Frequencies
Ws = [5 499]/Fn; % Normalised Stopband Frequencies
Rp = 1; % Passband Ripple (dB)
Rs = 25; % Stopband Ripple (dB)
[n,Wn] = buttord(Wp, Ws, Rp, Rs); % Optimal Filter Order
[b,a] = butter(n, Wn); % Calculate Filter Coefficients
[sos,g] = tf2sos(b,a); % Convert To Second-Order Sections For Stability
figure(1)
freqz(sos, 4096, Fs) % Filter Bode Plot
You cannot have any frequency exactly at zero or exactly at the Nyquist frequency, so I designed your passband to be close enough to them to be stable. In your filter, you can also use a high-pass design with a lower cutoff of 10 Hz, since your upper passband is at the Nyquist frequency. The passband and stopband ripple values are necessary to specify in the design but irrelevant to a Butterworth filter. These are acceptable values for most filter designs (for example Chebyshev) that require them.
You would use the ‘sos’ and ‘g’ variables in your actual filter:
filtered_signal = filtfilt(sos, g, signal);
(This design works well in R2016a and should also work in earlier releases.)