Bit over my head in math here I want to do a notch filter in real-time programmation with codesys (the structure text here look alot like C)
so I have this: Linear phase Notch filter / Band reject filter implementation in C++ and this: http://www.dspguide.com/ch19/3.htm as reference
which looks like
lR := 1 - 3 * lrBandwidth;
K := (1 - 2 * lR * COS(2 * MATH.PI * lrFreq) + lR * lR) / (2 - 2 * COS(2 * MATH.PI * lrFreq));
a0 := K;
a1 := -(2 * K * COS(2 * MATH.PI * lrFreq));
a2 := K;
b0 := 2 * lR * COS(2 * MATH.PI * lrFreq);
b1 := -(lR * lR);
//Filtre récursif
x3 := x2;
x2 := x1;
x1 := REAL_TO_LREAL(rSignal);
rNotchFilter := a0 * x1 + a1 * x2 + a2 * x3 + b0 * y1 + b1 * y2;
y2 := y1;
y1 := rNotchFilter;
In codesys
I think my coefficients are calculated correctly but I'm not sure of the value in lrBandwidth and lrFreq according to dspguide they are fraction of the frequency and should between 0 and 0.5
That code is executed every 5ms and I'm looking to filter a signal at 2.5Hz (400ms period).
I have generated some signal with noise and as you can see I do not achieve what I wish:
Anyone can help me out here?
EDIT
Thanks for the help.
Here are more info that @TimWescott requested:
- The sampling rate is 5ms
- The trace for test are 0.1Hz triangle with a 2.5Hz sine added to it
- I wish to filter out 2.5Hz out of this signal so the sine is lot less visible.
With bandwidth at 0.06 and frequency at 0.5 I get:
a0 = 0.982081
a1 = 1.964162
a2 = 0.982081
b0 = -1.964
b1 = -0.964324
and yes x3, x2, x1, y2 and y2 are retained between each cycle.
I'm very unsure the value of bandwidth and frequency to use to calculate the coefficients
