notch filter in matlab

To implement a notch filter in MATLAB, you can use the iirnotch function from the signal processing toolbox. This function designs a second-order IIR notch filter to remove a particular frequency from the input signal.

Here is an example of how to implement a notch filter in MATLAB:

main.m
% Define the sampling frequency and signal parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector (seconds)
f = 50; % Frequency of sinusoid (Hz)

% Generate a noisy signal with a 50 Hz sinusoid
x = cos(2*pi*f*t) + randn(size(t));

% Design the notch filter to remove the 50 Hz frequency
wo = 50/(fs/2); % Normalized frequency
bw = wo/35; % Bandwidth
[b,a] = iirnotch(wo,bw);

% Apply the notch filter to the input signal
y = filter(b,a,x);

% Plot the original and filtered signals
figure;
subplot(2,1,1);
plot(t,x);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t,y);
title('Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
699 chars
29 lines

In this example, we define the sampling frequency fs, time vector t, and the frequency of the sinusoid f. We generate a noisy signal with a 50 Hz sinusoid added to it. We then use the iirnotch function to design a notch filter with a center frequency of 50 Hz and a bandwidth of 1.43 Hz. We apply the resulting filter coefficients to the input signal using the filter function, and plot both the original and filtered signals.

gistlibby LogSnag