Author Topic: Notch filter in matlab  (Read 3097 times)

0 Members and 1 Guest are viewing this topic.

Offline harrysmithTopic starter

  • Contributor
  • Posts: 39
  • Country: dz
Notch filter in matlab
« on: January 23, 2021, 04:01:17 pm »
Hi everyone

I tried with this code but I did not have the template of a notch filter

a=20;          %order
fs=1000;      %sampling frequency
f1=400/fe;
f2=600/fe;
wn1=2*f1;
wn2=1-2*f2;
N1=(a-1)/2;
n=-a/2 : a/2;
hn=wn1*sinc(wn1*(n-N1))+wn2*sinc(wn2*(n-N1)).*cos(pi*(n-N1));
[H,f]=freqz(hn,1,250,fs);
figure(1)
plot(f,abs(H));

is there any other code to have an ideal notch filter

THanks
 

Offline Mau5

  • Newbie
  • Posts: 2
  • Country: ch
Re: Notch filter in matlab
« Reply #1 on: January 31, 2021, 09:32:49 am »
Hi,

This is my first post on this forum :)

You can create a notch filter based on an IIR-Filter type.
The transfer function is:

H(z) = (1 - 2 * cos(w) * z^(-1) + z^(-2)) / (1 - 2 * p * cos(w) * z^(-1) + p^2 * z^(-2))

Whereas w is the normalized frequency that you want to remove (w = 2 * pi * f / fs).
p is a parameter in the range of [0,1]. It determines the radius of the pole location on the unit circle. The closer it is to 1, the narrower the notch bandwidth.

For your problem, you can now put two single notch filters in series.

Here an example code: I hope it is formatted correctly :)

Code: [Select]
%% Notch Filter Test
clear;clc;

fs = 1000; % Sampling frequency [Hz]
f1 = 100;  % Notch filter frequency 1 [Hz]
f2 = 200;  % Notch filter frequency 2 [Hz]

%% Design Notch filter for first frequency
%H(z) = (1 - 2 * cos(w1) * z^(-1) + z^(-2)) / (1 - 2 * p * cos(w1) * z^(-1) + p^2 * z^(-2))

p = 0.99; % The closer to one, the narrower the notch bandwidth

w1 = 2*pi*f1 / fs; % Convert f1 to normalized frequency [+-2*pi]

num1 = [1 , -2 * cos(w1), 1];        %IIR-Filter coefficients for numerator 1
denum1 = [1. -2 * p * cos(w1), p^2]; %IIR-Filter coefficients for denumerator 1

%% Design Notch filter for second frequency

w2 = 2*pi*f2 / fs; % Convert f2 to normalized frequency [+-2*pi]

num2 = [1 , -2 * cos(w2), 1];        %IIR-Filter coefficients for numerator 2
denum2 = [1. -2 * p * cos(w2), p^2]; %IIR-Filter coefficients for denumerator 2

%% Connect filter parameters in series
num = conv(num1,num2);              %IIR-Filter coefficients numerator
denum = conv(denum1,denum2);        %IIR-Filter coefficients denumerator


%% Create Filter object and plot
iir = dsp.IIRFilter('Numerator',num,'Denominator',denum);
freqz(iir)


Note: The vectors num and denum are the IIR-Filter coefficients. You can also plot the filter with freqz(num, denum)
With the iir-object, you can directly apply the filter to some measurement data.

The frequency response of this example is attatched.
I hope this example code helps you

« Last Edit: January 31, 2021, 01:24:10 pm by Mau5 »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9894
  • Country: us
Re: Notch filter in matlab
« Reply #2 on: February 04, 2021, 04:12:38 pm »
Absolutely brilliant use of MATLAB, my favorite tool.

And welcome to the forum!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf