0

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:

See trace

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

Jdip
  • 5,980
  • 3
  • 7
  • 29
  • 3
    Does this answer your question? 50/60Hz suppression Filter Implementation This has the block diagram to implement a recursive notch filter and how to compute the coefficients for any notch frequency and bandwidth for a given sample rate. – Dan Boschen Feb 17 '23 at 04:52
  • Hi Not really, they are not using the recursive method, so I guess that method wont help me calculate my coefficiens correctly. I might try it anyway to see it it help me but im still puzzeled by how to calculate with a cycle time (sampling) of 5ms – user1745759 Feb 17 '23 at 11:20
  • I am not sure what you mean by “recursive method”… as an IIR filter it’s a recursive filter in that the subsequent outputs are dependent on the prior outputs as the block diagram depicts (the blocks with the inverse z’s in them are unit sample delays) – Dan Boschen Feb 17 '23 at 13:23
  • 1
    It looks like, @DanBoschen that the OP was dabbling with the cookbook but they're not dividing by $a_0$ (which you really have to do) and they're missing $b_2$ (maybe calling it "$b_1$" instead). – robert bristow-johnson Feb 17 '23 at 22:15
  • Please edit your question to include the sampling rate, the frequency that you wish to filter out, and the values of your coefficients (a0:a2, b0 and b1). Also, you are retaining the state variables (x1:x3, y1, y2) between iterations, yes? – TimWescott Feb 17 '23 at 22:31
  • Editted to add a bit more info – user1745759 Feb 18 '23 at 04:12
  • @robertbristow-johnson could you explain to me a bit more about that cookbook, in the link I posted the math is a bit different, see http://www.dspguide.com/ch19/1.htm equation 19-1 there is no division there – user1745759 Feb 18 '23 at 04:15
  • 1
    Just a general remark. Honestly you could make an effort at formatting. I'm not talking about grammatical errors that are completely excusable, but indents, typos (don't you get a little red scribble under "coefficien"?) and code-styling appropriately... we have to be able to read your question to be able to answer it. I've done it this time but if you edit or ask again please be mindful of my poor eyes! – Jdip Feb 18 '23 at 05:15
  • The link I gave you gives you the formulas for the a and b coefficients given the frequency of the notch and sampling rate, as well as the bandwidth. Did you read my posted response at that link? It sounds exactly what you are looking for, did you try it? – Dan Boschen Feb 18 '23 at 11:50

0 Answers0