design a band pass filter in matlab

Here's a sample code for designing a band pass filter in MATLAB using the fir1 function.

main.m
% Band pass filter parameters
Fs = 1000;      % Sampling frequency
Fpass1 = 50;    % First passband frequency (Hz)
Fpass2 = 200;   % Second passband frequency (Hz)
Fstop1 = 25;    % First stopband frequency (Hz)
Fstop2 = 225;   % Second stopband frequency (Hz)
Ap = 1;         % Passband ripple (dB)
Ast = 50;       % Stopband attenuation (dB)

% Design the filter using the fir1 function
Nyquist = Fs/2;
Fpass1_norm = Fpass1/Nyquist;
Fpass2_norm = Fpass2/Nyquist;
Fstop1_norm = Fstop1/Nyquist;
Fstop2_norm = Fstop2/Nyquist;
N = 100; % Filter order
b = fir1(N, [Fpass1_norm, Fpass2_norm], 'bandpass', kaiser(N+1, Ap), 'noscale');

% Plot the filter frequency response
freqz(b, 1, 1024, Fs)

% Apply the filter to a signal
t = 0:1/Fs:0.5;
x = sin(2*pi*100*t) + sin(2*pi*300*t); % Signal with two frequency components
y = filter(b, 1, x); % Filtered signal
855 chars
26 lines

The fir1 function is used to design a finite impulse response (FIR) filter with the desired passband and stopband frequencies. The freqz function is used to plot the frequency response of the filter. Finally, the filter function is used to apply the filter to a sample signal.

gistlibby LogSnag