band pass filter in matlab

To implement a band-pass filter in MATLAB using finite impulse response (FIR) design, follow these steps:

  1. Define the filter specifications, such as the passband frequency range, stopband frequency range, and desired attenuation in both the passband and stopband.
main.m
Fs = 1000; % Sampling frequency
Fpass1 = 50; % First passband frequency
Fpass2 = 150; % Second passband frequency
Fstop1 = 30; % First stopband frequency
Fstop2 = 170; % Second stopband frequency
Apass = 1; % Maximum passband attenuation
Astop = 60; % Minimum stopband attenuation
281 chars
8 lines
  1. Use MATLAB's fdesign.bandpass function to create a filter specification object.
main.m
% Create filter specification object
d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2', Fstop1/Fs, Fpass1/Fs, Fpass2/Fs, Fstop2/Fs, Astop, Apass, Astop);
158 chars
3 lines
  1. Use the design function along with the filter specification object to obtain the filter coefficients.
main.m
% Obtain filter coefficients using the design function
Hd = design(d, 'window');
81 chars
3 lines
  1. Apply the filter to the input signal using MATLAB's filter function with the obtained filter coefficients.
main.m
% Apply the band-pass filter to the input signal
y = filter(Hd, x);
68 chars
3 lines

Note: x is the input signal to be filtered.

Here's the complete code:

main.m
Fs = 1000; % Sampling frequency
Fpass1 = 50; % First passband frequency
Fpass2 = 150; % Second passband frequency
Fstop1 = 30; % First stopband frequency
Fstop2 = 170; % Second stopband frequency
Apass = 1; % Maximum passband attenuation
Astop = 60; % Minimum stopband attenuation

% Create filter specification object
d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2', Fstop1/Fs, Fpass1/Fs, Fpass2/Fs, Fstop2/Fs, Astop, Apass, Astop);

% Obtain filter coefficients using the design function
Hd = design(d, 'window');

% Apply the band-pass filter to the input signal
y = filter(Hd, x);
591 chars
17 lines

This should produce the output signal y which is the filtered version of the input signal x.

gistlibby LogSnag