MATLAB: Design band pass filter

bandpassfiltersignalwhite noise

Hello,
I have to design bandpass filter of 2-5kHz band, filter length 100. Then I have to filter with it the white noise and one other signal (triangle, sawtooth, square, f=500Hz).
Previously I designed a low pass filter and filter through it white noise (code below), I wonder if I could modify the code to fit the band pass or should I design it other way. Maybe filter designer? But how to later filter with it the signals? Please help me, I'm struggling with Matlab 🙁
rng default
Fs = 1000;
t = linspace(0,1,Fs);
x = randn(size(t));
fc = 150;
Wn = (2/Fs)*fc;
b = fir1(20,Wn,'low',kaiser(21,3));
fvtool(b,1,'Fs',Fs)
y = filter(b,1,x);
plot(t,x,t,y)
xlim([0 0.1])
xlabel('Time (s)')
ylabel('Amplitude')
legend('Original Signal','Filtered Data')

Best Answer

Here is prototype code I used for a different project that you can adapt, if you want an IIR filter:
Fs = 250; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
Ts = 1/Fs; % Estimate Sampling Interval
t = linspace(0, numel(EKG), numel(EKG))/Fs;
Wp = [2 90]/Fn; % Normalised Passband (Passband = 2 Hz To 90 Hz)
Ws = [1 100]/Fn; % Normalised Stopband (Passband = 1 Hz To 100 Hz)
Rp = 1; % Passband Ripple/Attenuation
Rs = 50; % Stopband Ripple/Attenuation
[n,Wp] = ellipord(Wp, Ws, Rp, Rs); % Calculate Elliptic Filter Optimum Order
[z,p,k] = ellip(n, Rp, Rs, Wp,'bandpass'); % Elliptic Filter
[sos,g] = zp2sos(z,p,k); % Convert To Second-Order-Sections For Stability
figure
freqz(sos,2^16,Fs)
filtered_signal = filtfilt(sos,g,signal);
There are a number of FIR designs on Answers as well (I have posted many of them), although I usually reserve those for filters with multiple passbands and stopbands.