Notch Filter (wiki)

Do we want to a) update the notch.ny filter and b) include it with Audacity.

http://audacityteam.org/nyquist/notch.ny

I find the UI a bit confusing (you have to select “alternative” for the “Notch choice” in order for the “Notch frequency” entry to be used). The UI could be updated to use version 3 controls. The maximum Q is a bit limited at 5, and the minimum Q is 0.01!

On the wiki noise removal page a recommendation is made to use a notch filter to deal with whistle or hum, then users are directed to the Nyquist prompt. Wouldn’t it be better to include this simple plug-in instead?

– Bill

[Update]
The Notch filter is now available on the Audacity wiki
http://wiki.audacityteam.org/wiki/Download_Nyquist_Plug-ins

Yes.
(except for “minimum Q is 0.01” which is far too low for any practical application that I can think of.)

50/60 Hz presets can be useful, though I’m open to suggestions of what Q to use for these.
Q > 1 affects a more narrow band but can cause ringing.
A single notch filter can be a considerable benefit for removing the fundamental “mains hum” prior to noise reduction, but if there is a noticeable hum on a recording then there are invariably many harmonics contributing to the noise.

Any other useful presets?

For recent/current versions of Audacity the “(if (arrayp s)(vector…” business is not required as the (notch2 …) function now supports stereo tracks.

What would your “ideal” notch filter effect be like?

Plain and simple, such as:

;nyquist plug-in
;version 3
;type process
;name "Notch Filter 2..."
;action "Applying Notch Filter..."
;info "Version 0.1 by BillW"

;control freq "Notch frequency" real " " 60.0 10.0 20000.0
;control q "Notch q" real " " 1.00 0.1 20.00

(notch2 s freq q)

– Bill

Personally I’d prefer a default of 50 Hz :smiley:

I really like the plain and simple approach, but…
there needs to be some error checking;

Generate or import a track with a 8 kHz sample rate and apply a notch filter with a centre frequency of 5000 Hz and you’ll see what I mean.

Agreed. I leave that to you. :slight_smile:

– Bill

(setq freq (min (max freq 10) (/ *sound-srate* 2.0)))

will constrain the frequency value between 10 Hz and the Nyquist frequency, but perhaps it would be preferable to raise an error message?

Perhaps something like:

;control freq "frequencyt" real ""  50 10 10000
;control q "Q" real "" 2 0.1 20

(if (or (> freq  (/ *sound-srate* 2.0))(< freq 10))
   (format nil "Error.nFrequency out of range.")
   (notch2 s freq q))

Steve:
If we want an effect dedicated to hum removal, how about something like this:

;nyquist plug-in
;version 3
;type process
;name "Hum Removal..."
;action "Performing Hum Removal..."
;info "V0.1 by BillW"

;control ntch " Notch filter frequency" choice "60 Hz,60 and 120 Hz,50 Hz,50 and 100 Hz" 0

(case ntch ((0 1) (setq fund 60))((2 3) (setq fund 50)))
(setq scnd (+ fund fund))

(case ntch
    ((0 2) ; fundamental only
        (notch2 s fund 15)
    )
    ((1 3) ; fundamental and second harmonic
        (notch2 (notch2 s fund 15) scnd 20)
    )
  )
)

I did some experiments a long time ago cascading notch filters to the 3rd, 4th, 5th, etc. harmonics of the fundamental hum frequency. Got comb filtering/ringing pretty quickly, so decided to concentrate on the worst offenders. If you’ve got “buzz”, then I think you need to attack it with Noise Removal, or apply the “re-shoot” filter.

– Bill

There’s a really long thread somewhere in the forum on this question, and yes, comb/ringing effects tend to mount up pretty fast.
The approach that I generally take to removing mains hum is usually to zap the fundamental with a notch filter, then use Noise Removal on the rest.

If we are to have a dedicated hum removal effect, then I think we need to have one that works (which will be a lot more complex than a simple notch filter) otherwise it’s wide open to all of the kind of criticism that Noise Removal gets.

A “general purpose” notch filter is a really useful tool, so even if the default frequency is 60 Hz (or 50) I don’t think that we should be advertising it as a “mains hum removal” effect. Just keep it as a general purpose tool (as the low-pass/high-pass filters are).

Agreed. You had asked about presets and they’re useful mostly for hum removal. Keep it simple and general. Just a notch filter.

– Bill

Keep it simple +1

Do you have a preference for just constraining the input values or generating an error message?
My preference is for an error message generated if the input is outside of the maximum possible range of 0 to sample-rate/2

A notch filter with a centre frequency of zero does nothing and produces no error, which I think is the correct action.
I propose that if the frequency is set to zero the effect should be skipped as this will produce the same result but faster. (It would also protect against possible changes to Nyquist that might generate an error for a notch at 0Hz).

Suggestions for the frequency slider range?
How about 0 to 10000 Hz - that gives a short slider movement from the cursor keys of +/- 10 Hz, mouse wheel steps of +/- 20 Hz and large steps from Ctrl+cursor key of 1000 Hz. I think that this range should also be adequate for the vast majority of practical applications.

I think an error message is better - this lets the user know what’s going on. Constraining the input values could be confusing.

0 Hz notch filter? I suppose someone might try that. Maybe by accident. Maybe thinking it will remove DC? I think we need an error message there as well.

As for slider range (you can input values in the text box outside the range, right?), 1 - 10,000 is probably reasonable. Not too many people these days will want to notch out the 19 kHz FM pilot tone, and if they do they can input the value explicitly.

I now leave this in your capable hands. You could probably bang this out in 10 minutes, whereas it would likely take me hours to get up to speed again on Nyquist with error checking.

– Bill

I’m not so sure about that.

Theoretically, does a notch filter with a centre frequency of 0 Hz make sense? Is it like asking the filter to remove nothing (which would be valid, but pointless), or should it act as a high pass filter (as a notch filter can be thought of as a combined high-pass and low-pass filter), or is it meaningless?

Practically, the notch2 function does nothing. There is also a minor inconvenience in having the lower end of the slider scale pegged at 1 which is that adjusting the slider with the cursor keys gives values of 71, 81, 91, 101,… so setting a value of say 200 Hz is a bit less convenient unless the value is typed. On the other hand, if the slider has a minimum setting of zero, then we cannot then throw up an error message for a value of zero.

Here’s both versions - my preference is for a slider range of 0 to 10000.

;nyquist plug-in
;version 1
;type process
;categories "http://lv2plug.in/ns/lv2core/#FilterPlugin"
;name "Notch Filter"
;action "Performing Notch Filter..."
;info "Released under GPL v2."

;control freq "frequencyt" real "" 60 1 10000
;control q "Q (high value for narrow notch)" real "" 1 0.1 20

;; By Steve Daulton and Bill W. September 2010

(cond
  ((or (> freq (/ *sound-srate* 2.0))(<= freq 0))(format nil "Error.nFrequency out of range."))
  ((< q 0.01)(format nil "Error.nQ must be greater than 0.01"))
  (T (notch2 s freq q)))



;nyquist plug-in
;version 1
;type process
;categories "http://lv2plug.in/ns/lv2core/#FilterPlugin"
;name "Notch Filter"
;action "Performing Notch Filter..."
;info "Released under GPL v2."

;control freq "frequencyt" real "" 60 1 10000
;control q "Q (high value for narrow notch)" real "" 1 0.1 20

;; By Steve Daulton and Bill W. September 2010

(cond
  ((> freq (/ *sound-srate* 2.0))(format nil "Error.nFilter frequency too high for track sample rate."))
  ((< freq 0)(format nil "Error.nFilter frequency must be greater than 0 Hz"))
  ((< q 0.01)(format nil "Error.nQ must be greater than 0.01"))
  ((= freq 0) s)
  (T (notch2 s freq q)))

I thought about slider behaviour as well, and agree that it’s “nicer” to see zeros instead of ones appear in the text box in response to the slider. So let’s go that way.

I prefer the second version of the error checking as it gives a more helpful error message when the chosen frequency is too high, and when the frequency is less than zero (truly a “sanity check”). So the only change I’d make is set the slider range to 0 to 10000.

– Bill

Oops again - I intended to put the range of 0 to 10000 in that second one but copied it into the forum wrong.

Just a couple of minor changes in here - if you’d care to check it over I think this is now ready.
notch2.ny (646 Bytes)

Almost. Shouldn’t the effect name have “…” on the end since it invokes a dialog? I guess we have to keep “2” in the file name to differentiate it from the original? And you can spell my last name out in full in the comment line.

– Bill

Is that what it means? I never knew that :smiley:

Not necessarily (if it replaces the other version).
I called the file notch2.ny because it is virtually a “straight” implementation of the Nyquist function “notch2”.

No I can’t. I don’t know what your last name is :smiley:

Help yourself to adding your last name and three dots…

Steve:
OK, here it is.
notch2.ny (655 Bytes)

Cool :slight_smile:
I found your name on an e-mail and added it to my copy.
notch2bw.ny and notch2sd.ny are identical.

Thanks both, for this improved version. I’ve got slight reservations about restricting the slider maximum to 10000 Hz, because some people may still want to use this as a quick “just type it in” EQ attenuator rather than specifically a noise tool. But 20000 Hz makes the keyboard increments 20 Hz which isn’t so convenient, so OK…

I don’t like “high value for narrow notch” much and never liked “Q” as a name for a control so I suggest “Width (higher value reduces width)” in the attached as more user-friendly.

Strictly it seems to me the errors should be “Frequency must be at least 0 Hz” (not “greater than”, since the slider shows 0 and rightly doesn’t give an error at that value); and “Width must be at least 0.01” (again not “greater than” because 0.01 doesn’t give an error). Also one error had a period (full stop) at the end and the others didn’t. I assume a proper sentence should end with the period, but the word “Error” itself should not have one.

Looks like this will be included in Audacity so the attached names Steve and Bill on the info line. I assume we will call it just “notch.ny” as it will replace the previous one.

;nyquist plug-in
;version 1
;type process
;categories "http://lv2plug.in/ns/lv2core/#FilterPlugin"
;name "Notch Filter..."
;action "Performing Notch Filter..."
;info "By Steve Daulton and Bill Wharrie. Released under GPL v2."

;control freq "Frequency" real "Hz" 60 0 10000
;control q "Width (higher value reduces width)" real "" 1 0.1 20

;; notch.ny by Steve Daulton and Bill Wharrie, September 2010.

(cond
  ((> freq (/ *sound-srate* 2.0))(format nil "ErrornFrequency too high for track sample rate."))
  ((< freq 0)(format nil "ErrornFrequency must be at least 0 Hz."))
  ((< q 0.01)(format nil "ErrornWidth must be at least 0.01."))
  ((= freq 0) s)
  (T (notch2 s freq q)))

Gale
notch3.ny (684 Bytes)

The choice of slider range is a balance for what will be most useful / convenient. Using notch filters above 10 kHz is probably not very common, but when used it will probably need an exact value that would be much more easily entered by typing. The plug-in will accept typed values up to the Nyquist frequency.

I agree that “width” is more friendly, but “Q” is the correct name and has been universally used for decades. The term “width” is sometimes used for band pass/stop filters where it refers to the number of octaves and a higher number is a wider frequency band.

I think that “Frequency must be greater than 0” (or perhaps Frequency should be…") is acceptable since a frequency of zero does nothing.
I agree that “Width must be at least 0.01”

If this is included in Audacity there should perhaps be a brief mention in the manual regarding typed values greater than 10000 Hz.