Notch Filter in Matlab

Status
Not open for further replies.

Eowe

Newbie level 2
Joined
Aug 3, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
12
I try to get a first order notch filter at 50hz and Sampling frequency(Fs) 200 Hz in Matlab I wrote some code but didn't work

Code:
dt=1/200;      //1/fs
t=0:dt:0.2;
x=cos(2*pi*25*t)+cos(2*pi*50*t);
y=filter(x,------);     //I dont know what I must write here
plot(Y(w));       //I must write here laplace of y(w)

How can do that
 

Before you can use the filter() function, you need to design your filter coefficients. One way of doing this in Matlab is to use the fir1() function. You just need to specify the lower and upper bounds of the stopband (in Hz) and also specify the filter type as 'stop'.

Here is an example:


Code Matlab M - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
close all; clear all; clc;
 
% Input signal
fsamp = 500;
dt = 1/fsamp;
t = 0:dt:1;
x = cos(2*pi*25*t) + cos(2*pi*50*t);
 
% Design N-tap bandstop filter
N = 256;
notch_min_hz = 45;
notch_max_hz = 55;
b = fir1(N, 2 * [notch_min_hz, notch_max_hz] / fsamp, 'stop');
 
% Apply filter and plot
y = filter(b, 1, x);
plot(t, y);
grid on;
title('Filter Output');
xlabel('Time (secs)');
ylabel('Amplitude');
 
% Plot frequency response of filter
[H, W] = freqz(b);
figure();
plot(fsamp * W/(2*pi), 20*log10(abs(H)));
grid on;
title('Filter Frequency Response');
xlabel('Frequency (Hz)');
ylabel('(dB)');



In my example, I increased the sample rate so we can see the shape of the cosine more clearly, but you can change it back to 200Hz if required.

Of course, you should modify the parameters to fit your needs. In particular, increasing the number of filter taps will improve the attenuation of the notch. Also, for a given number of taps, increasing the width of the notch will improve the rejection inside the notch. (However, depending on your application, you may need to keep the notch as narrow as possible).
 
Last edited:
Reactions: Eowe

    Eowe

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…