signal filtering in matlab in matlab

To perform signal filtering in MATLAB, you can use the built-in function filter. The filter function applies a digital filter to a sequence of input data.

Here is an example on how to use filter function for signal filtering in MATLAB:

main.m
% Generate a noisy signal
Fs = 1000;              % Sampling frequency
t = 0:1/Fs:1;           % Time vector
f1 = 10;                % Frequency of the signal
f2 = 150;               % Frequency of the noise
signal = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t) + 0.1*randn(size(t));

% Design the filter
order = 5;              % Filter order
cutoffFreq = 30;        % Desired cutoff frequency
normalizedCutoffFreq = cutoffFreq / (Fs/2); % Normalize cutoff frequency
b = fir1(order, normalizedCutoffFreq);      % FIR filter coefficients

% Apply the filter to the signal
filteredSignal = filter(b,1, signal);

% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t, signal);
title('Original Signal');
subplot(2,1,2);
plot(t, filteredSignal);
title('Filtered Signal');
777 chars
25 lines

In this example, the fir1 function is used to design a lowpass FIR filter with a cutoff frequency of 30 Hz. The filter function is then used to apply the filter to the signal variable. The filtered signal is stored in the filteredSignal variable and can be plotted to visualize the effect of the filtering.

Note that the specific filter design and parameters can vary depending on your specific requirements and the characteristics of your signal.

related categories

gistlibby LogSnag