design a low-pass filter in matlab

To design a low-pass filter in MATLAB, you can use the fir1 function, which designs a finite impulse response (FIR) filter with the given specifications. Here's an example code:

main.m
% Define filter parameters
cutoff_freq = 1000;  % Hz
sampling_freq = 8000;  % Hz
filter_order = 50;

% Compute filter coefficients
nyquist_freq = sampling_freq / 2;
cutoff_norm = cutoff_freq / nyquist_freq;
filter_coeffs = fir1(filter_order, cutoff_norm);

% Plot frequency response
freqz(filter_coeffs, 1, 1024, sampling_freq);
329 chars
13 lines

In this code, cutoff_freq is the desired cutoff frequency of the filter, sampling_freq is the sampling frequency of the signal, and filter_order is the desired order of the filter. The function nyquist_freq computes the Nyquist frequency, which is half of the sampling frequency, and cutoff_norm converts the cutoff frequency to a normalized frequency between 0 and 1.

The fir1 function is then called with the filter order and cutoff frequency as arguments, and it returns the filter coefficients. Finally, the freqz function is used to plot the frequency response of the filter.

Note that this example code designs a low-pass filter with a cutoff frequency of 1000 Hz, but you can modify the parameters to fit your specific application.

gistlibby LogSnag