MATLAB: Bandpassing of a signal

bandpassbandpass filtering

Can anyone provide the error free full matlab code for output like in the attached picture taken from the link https://www.mathworks.com/help/signal/ref/bandpass.html?searchHighlight=bandpassing&s_tid=doc_srchtitle

Best Answer

An equivalent elliptic filter (essentially the same one the bandpass function would design):
Fs = 1000; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Wp = [100 200]/Fn; % Passband Normalised
Ws = [ 90 210]/Fn; % Stopband Normalised
Rp = 1; % Passband Ripple (Irrelevant in Butterworth)
Rs = 50; % Stopband Attenuation
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Zero-Pole-Gain
[sos,g] = zp2sos(z,p,k); % Second-Order Section For Stability
figure
freqz(sos, 2^16, Fs) % Filter Bode Plot
x_filt = filtfilt(sos, g, x); % Filter Signal
Here, ‘x’ is the signal vector. To reproduce the plot, create an equivalent signal (this one from the bandpass documentation page):
fs = 1e3;
t = 0:1/fs:1;
x = [2 1 2]*sin(2*pi*[50 150 250]'.*t) + randn(size(t))/10;
then apply my filter design to it.
To create the second plot, divide the fft of ‘x_filt’, by the fft of ‘x’ using element-wise division, then plot as you normally would plot the fft. See fft for details.