DSPRelated.com
Forums

How to write a NotchFilter procedure

Started by gpdsoft June 5, 2006
Hi,
I have do write a procedure in Delphi that take an array of samples and
filters it.

The filter I've to do is Notch to cut out the 50Hz from my signal.

My signal is sampled at 50Khz for a max of 250 millisec.

Do you know where I can find the code or something similar to quickly
implement this function in my software?

Thnx a lot


gpdsoft skrev:
> Hi, > I have do write a procedure in Delphi that take an array of samples and > filters it. > > The filter I've to do is Notch to cut out the 50Hz from my signal. > > My signal is sampled at 50Khz for a max of 250 millisec. > > Do you know where I can find the code or something similar to quickly > implement this function in my software?
A recipe for a general notch filter ought to be available in any textbook on DSP. As for your sampling parameters, it seems the notch needs to be at a relative frequency of 0.001, and you have some 12.5 cycles in your signal. The low relative frequency of the notch suggests that you probably have to use floating point arithmetics to get a good separation (narrow notch). Unfortunately, the narrow notch would almost certainly imply that you get long initial and terminal transients. If this is some academic exercise, go ahead. If this has some real-life purpose, you might want to think very carefully about how to proceed. Rune
Rune Allnor wrote:
> gpdsoft skrev: >> Hi, >> I have do write a procedure in Delphi that take an array of samples and >> filters it. >> >> The filter I've to do is Notch to cut out the 50Hz from my signal. >> >> My signal is sampled at 50Khz for a max of 250 millisec. >> >> Do you know where I can find the code or something similar to quickly >> implement this function in my software? > > A recipe for a general notch filter ought to be available in any > textbook on DSP. > > As for your sampling parameters, it seems the notch needs > to be at a relative frequency of 0.001, and you have some > 12.5 cycles in your signal. The low relative frequency of the > notch suggests that you probably have to use floating point > arithmetics to get a good separation (narrow notch). Unfortunately, > the narrow notch would almost certainly imply that you get long > initial and terminal transients. > > If this is some academic exercise, go ahead. If this has some > real-life purpose, you might want to think very carefully about > how to proceed. > > Rune >
Are you implying that it doesn't matter if an academic exercise works? :-) Steve
Rune Allnor wrote:

> gpdsoft skrev: > >>Hi, >>I have do write a procedure in Delphi that take an array of samples and >>filters it. >> >>The filter I've to do is Notch to cut out the 50Hz from my signal. >> >>My signal is sampled at 50Khz for a max of 250 millisec. >> >>Do you know where I can find the code or something similar to quickly >>implement this function in my software? > > > A recipe for a general notch filter ought to be available in any > textbook on DSP. > > As for your sampling parameters, it seems the notch needs > to be at a relative frequency of 0.001, and you have some > 12.5 cycles in your signal. The low relative frequency of the > notch suggests that you probably have to use floating point > arithmetics to get a good separation (narrow notch). Unfortunately, > the narrow notch would almost certainly imply that you get long > initial and terminal transients. > > If this is some academic exercise, go ahead. If this has some > real-life purpose, you might want to think very carefully about > how to proceed. > > Rune >
If your tone is exactly on frequency and you're collecting the data in chunks then processing it after the fact you may want to do a least-squares fit of a sine wave to the data, then subtract it out. If your data is always an integer number of cycles long a least-squares fit can by had simply by demodulating the data with sin(wt) and cos(wt) and using the two values to indicate phase and amplitude. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ "Applied Control Theory for Embedded Systems" came out in April. See details at http://www.wescottdesign.com/actfes/actfes.html
"Rune Allnor" <allnor@tele.ntnu.no> wrote in message
news:1149549851.844892.281900@u72g2000cwu.googlegroups.com...
> > gpdsoft skrev: > > Hi, > > I have do write a procedure in Delphi that take an array of samples and > > filters it. > > > > The filter I've to do is Notch to cut out the 50Hz from my signal. > > > > My signal is sampled at 50Khz for a max of 250 millisec. > > > > Do you know where I can find the code or something similar to quickly > > implement this function in my software?
One tip: Rather than try and design a notch at 50 Hz it should be easier to put the notch at dc and then mix the input signal down 50 Hz before the notch and back up after the notch. It will be easier to design the IIR filter for the notch at DC and this scheme allows you to adjust the placement of the notch easily. If you need more attenuation you can cascade multiple IIR filters. -Clark
Steve Underwood skrev:
> Rune Allnor wrote: > > gpdsoft skrev: > >> Hi, > >> I have do write a procedure in Delphi that take an array of samples and > >> filters it. > >> > >> The filter I've to do is Notch to cut out the 50Hz from my signal. > >> > >> My signal is sampled at 50Khz for a max of 250 millisec. > >> > >> Do you know where I can find the code or something similar to quickly > >> implement this function in my software? > > > > A recipe for a general notch filter ought to be available in any > > textbook on DSP. > > > > As for your sampling parameters, it seems the notch needs > > to be at a relative frequency of 0.001, and you have some > > 12.5 cycles in your signal. The low relative frequency of the > > notch suggests that you probably have to use floating point > > arithmetics to get a good separation (narrow notch). Unfortunately, > > the narrow notch would almost certainly imply that you get long > > initial and terminal transients. > > > > If this is some academic exercise, go ahead. If this has some > > real-life purpose, you might want to think very carefully about > > how to proceed. > > > > Rune > > > Are you implying that it doesn't matter if an academic exercise works? :-)
Yes. Hence the term "only of academic interest". Academics (me being one of them) are not always very aware of practical aspects of the exercises and projects they concoct. Rune
Anonymous skrev:
> "Rune Allnor" <allnor@tele.ntnu.no> wrote in message > news:1149549851.844892.281900@u72g2000cwu.googlegroups.com... > > > > gpdsoft skrev: > > > Hi, > > > I have do write a procedure in Delphi that take an array of samples and > > > filters it. > > > > > > The filter I've to do is Notch to cut out the 50Hz from my signal. > > > > > > My signal is sampled at 50Khz for a max of 250 millisec. > > > > > > Do you know where I can find the code or something similar to quickly > > > implement this function in my software? > > > One tip: Rather than try and design a notch at 50 Hz it should be easier to > put the notch at dc and then mix the input signal down 50 Hz before the > notch and back up after the notch.
Hmmmm.... I am not sure this would be a good idea. Using a notch filter, not a high pass filter, implies that there is useful data in the band DC -- 50 Hz. Mixing down by 50 Hz would cause aliasing, which you would not be able to recover from.
> It will be easier to design the IIR > filter for the notch at DC and this scheme allows you to adjust the > placement of the notch easily. If you need more attenuation you can cascade > multiple IIR filters.
There is a recipe for the notch filter in the book by Proakis & Manolakis where you can do that with a 2nd order IIR filter. The problem with very narrow notches is the numerical accuracy of the coefficients. Rune
Rune Allnor wrote:
> Anonymous skrev: > > There is a recipe for the notch filter in the book by Proakis & > Manolakis > where you can do that with a 2nd order IIR filter. The problem with > very narrow notches is the numerical accuracy of the coefficients. > > Rune
If the calculations are done in Delphi then why worry about the numerical accuracy of the coefficients? I posted two links to a 50 Hz notch filter example a few weeks ago. I just modified my Notch filter work sheet to try the OPs needs. The coeffiicients and the ploted results were fine. Peter Nachtwey
> > One tip: Rather than try and design a notch at 50 Hz it should be easier
to
> > put the notch at dc and then mix the input signal down 50 Hz before the > > notch and back up after the notch. > > Hmmmm.... > > I am not sure this would be a good idea. Using a notch filter, not a > high > pass filter, implies that there is useful data in the band DC -- 50 Hz. > Mixing down by 50 Hz would cause aliasing, which you would not > be able to recover from. > >
I'm assuming that the processing/mixer would be complex so there should be no alias problem.
Anonymous skrev:
> > > One tip: Rather than try and design a notch at 50 Hz it should be easier > to > > > put the notch at dc and then mix the input signal down 50 Hz before the > > > notch and back up after the notch. > > > > Hmmmm.... > > > > I am not sure this would be a good idea. Using a notch filter, not a > > high > > pass filter, implies that there is useful data in the band DC -- 50 Hz. > > Mixing down by 50 Hz would cause aliasing, which you would not > > be able to recover from. > > > > > > I'm assuming that the processing/mixer would be complex so there should be > no alias problem.
You didn't state that assumption (or at least I didn't note it), but I think you are right, it might work. However, you would need a single sideband, complex-valued signal to start with. If your goal is to get a simpler system than a regular notch filter... Rune