Multiple notches with Nyquist prompt?

Effects, Recipes, Interfacing with other software, etc.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Devadaru
Posts: 8
Joined: Tue Feb 03, 2009 10:53 pm
Operating System: Please select

Multiple notches with Nyquist prompt?

Post by Devadaru » Tue Feb 03, 2009 10:59 pm

I am removing a hum from some voice recordings. I find that if I notch the file at 60, 120, 180, 300, 420 .... all the way to 1740 hz or so, the hum is fairly fully removed, without much loss of quality. How can I do all of them at one go? If I make a list in the Nyquist Prompt window, it seems to handle only the first one... what I want is as follows:

Code: Select all

(notch2 s 60 25)
(notch2 s 120 10)
(notch2 s 173 25)
(notch2 s 180 25)
(notch2 s 300 40)
(notch2 s 420 40)
(notch2 s 540 50)
(notch2 s 660 50)
(notch2 s 780 60)
(notch2 s 900 60)
(notch2 s 1020 60)
(notch2 s 1140 60)
(notch2 s 1260 60)
(notch2 s 1380 60)
(notch2 s 1500 60)
(notch2 s 1620 60)
(notch2 s 1740 60)
(notch2 s 1860 60)
I trust there's a simple way to do this... Thanks for your help. (don't know why the 173 shows up; in the plot spectrum it is there.)

kozikowski
Forum Staff
Posts: 68906
Joined: Thu Aug 02, 2007 5:57 pm
Operating System: macOS 10.13 High Sierra

Re: Multiple notches with Nyquist prompt?

Post by kozikowski » Thu Feb 05, 2009 12:34 am

That's using the shortcut tools. You could do it manually by defining three points. Instead of notch=400, you define 399=-0db, 400=-40dB 401= 0dB. You can put as many of those in a formula as you wish.

Make a sloppy notch filter with the graphic tools, save the filter, open it up and see what the code reads like. I'm no programmer and that's how I did a simple curve once, and you can put as many of those points in as you want.

It's possible that there is a "Q" restriction to filters like that, so they don't really come out notches, but it's worth a go.

You know that's how Noise Removal works right? You expose the Noise Removal Tool to hum by itself and it will build the filter for you. It will try to remove everything in that hum sample (profile) from the following show.

And I would do this in Audacity 1.3. The 1.2 Noise Removal tools were pretty dreadful.

Koz

steve
Site Admin
Posts: 80717
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Multiple notches with Nyquist prompt?

Post by steve » Thu Feb 05, 2009 1:40 am

The Nyquist notch filter provides a very deep and steep notch. When you remove hum with a notch filter, you are removing that frequency from everything, the music as well as the noise. For this reason you should avoid using it more than necessary. If you need to remove harmonics all the way up to 1.8 kHz, then you have a serious hum problem and you should address it by using shielded cable (shielded twisted pair and balanced circuits if necessary). Going up to 240 Hz should be more than sufficient.

(I'm curious how you worked out what Q values to use).

Back to the question in hand:

Looking at this as an iteration (in this example I will use increasingly narrow notches for higher harmonics):
You want to remove 60 Hz from the sound "s"

Code: Select all

(notch2 s 60 25)
And you want to remove 120 Hz from the sound that has had 60 Hz removed:

Code: Select all

(notch2 (notch2 s 60 10) 120 20)
And you want to remove 180 Hz from that:

Code: Select all

(notch2 (notch2 (notch2 s 60 10) 120 20) 180 30)
Let me write that more clearly:

Code: Select all

(notch2 
   (notch2 
      (notch2 
         (notch2 s 60 10) 
      120 20) 
   180 30) 
240 40)
Of course for dealing with harmonics you could always use a loop construct such as:

Code: Select all

(setq mysound s)
(setq q 2)         ; set the base Q for the filter
(setq iter 4)     ; set the number of iterations
(setq freq 60)  ; set base frequency

; start the DO loop
(dotimes (i iter mysound)   
(setf mysound (notch2 mysound (* freq (1+ i)) (* q (1+ i))))
)                     ;end of loop
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Devadaru
Posts: 8
Joined: Tue Feb 03, 2009 10:53 pm
Operating System: Please select

Re: Multiple notches with Nyquist prompt?

Post by Devadaru » Thu Feb 05, 2009 1:50 pm

Thanks to both of you.

I set the Q values by trial and error, checking the plotted spectrum to see if a too-deep notch was being cut, or not enough. It is approximate, of course.

Well, unfortunately neither of the code pieces you so kindly wrote could succeed; the first returns no audio, the second, only "returns the value: 60". And having no experience with scripting, I cannot debug. I thought one could write a filter that could apply various actions to a selection successively--that is, first notch the whole selection at 60, then when that is finished, start again and notch the whole selection at 120, etc. So I can run it, walk away, and come back to have the hum gone.

Umm, I am still using 1.2.6. I gather from Koz's comment that I should probably upgrade to 1.3. Yes, I haven't been using the built-in sound removal, because it simply leaves too many artifacts, even at the low setting. If the 1.3 version does well, then perhaps that is the easiest solution. But there is probably some additional room noise, in addition to the hum, that may vary from part to part.

The hummy recordings are voice recordings, of 8 lectures delivered at an outside facility. Our own recordings are generally hum-free; this place had an old hummy system, and I'm just trying to salvage the recordings we have...

Thanks again for your help. In the meantime, I shall upgrade to 1.3.

--D

Devadaru
Posts: 8
Joined: Tue Feb 03, 2009 10:53 pm
Operating System: Please select

Re: Multiple notches with Nyquist prompt?

Post by Devadaru » Thu Feb 05, 2009 2:57 pm

Installed--the 1.3.7 noise removal is 100x better than 1.2.6. It leaves the empty portions silent.
Still, I'd like to know if it's possible to do what I wanted--that is, to have a filter do repeated actions as I described above... The hum is still audible as a background to the actual voice portions.

Any tips on the settings of noise removal? We have noise reduction, frequency smoothing, and attack time.

Thanks, D.

steve
Site Admin
Posts: 80717
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Multiple notches with Nyquist prompt?

Post by steve » Thu Feb 05, 2009 3:31 pm

Devadaru wrote: Well, unfortunately neither of the code pieces you so kindly wrote could succeed; the first returns no audio, the second, only "returns the value: 60".
I've not tested on Audacity 1.2 (I've not got it installed), but on Audacity 1.3 they definitely do work (I've just tested them all).
To use them, select some audio, then from the effects menu select "Nyquist Prompt". Then copy and paste the code from any one of the examples into the Nyquist Prompt dialogue box and click OK.

To get a debugging output, click on "Debug" instead of "OK".
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Devadaru
Posts: 8
Joined: Tue Feb 03, 2009 10:53 pm
Operating System: Please select

Re: Multiple notches with Nyquist prompt?

Post by Devadaru » Thu Feb 05, 2009 3:49 pm

Brilliant!

Oh dear, the problem is with 1.2.

I hereby recommend to all users:

UPGRADE TO 1.3! It is head and shoulders above 1.2.

Thanks again for your help. This is exactly what I need to process our files satisfactorily.

--D

Trebor
Posts: 9849
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows 8 or 8.1

Re: Multiple notches with Nyquist prompt?

Post by Trebor » Wed Mar 04, 2009 5:48 am

I have written a (multi-notch) mains-hum remover (dehummer) plug-in for Audacity, “inspired by” the code posted above.

If you anyone wants to try my dehummer plug-in you can download a copy from this link and place it in Audacity's plug-in folder and restart Audacity,
the plug-in should appear in the (unsorted) effects list as "50/60Hz dehummer 1.0"

Pros:
It completely removes 50Hz or 60Hz mains-hum harmonics:
(the results are very good when extracting audio buried in hum).

Cons:
Mono only: (have to split stereo tracks and apply to each track separately).
It is slow: takes approximately 1/5th actual duration of track to remove hum.
Adds unintentional reverberation effect, (not unpleasant), and Gibbs ringing.
Last edited by Trebor on Sun Mar 08, 2009 9:56 pm, edited 1 time in total.

steve
Site Admin
Posts: 80717
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Multiple notches with Nyquist prompt?

Post by steve » Wed Mar 04, 2009 8:40 pm

Nice one Trebor, good to see some interest in Nyquist.

A few comments on the code - looking at the business end of it (I've replaced the controls with your default values):

Code: Select all

(setq choice 0)
(setq a 12)
(setq v 20)
(setf freq (cond((= choice 0) 50) ((= choice 1) 60)))

(setq que (/ 10000 (* a a)))
(setq anti (/ 10000 (* v v)))
(setq mysound s)
(setq r *sound-srate*)
(setq iter (truncate (/ (/ r freq) 2)))
(setq d (/ iter anti))

(dotimes (i iter mysound) 
(setf mysound (notch2 mysound (* freq (1+ i)) (* que (1+ (/ i d))))))
That's a really complicated equation for calculating "Q"
(with a sample rate of 48kHz...)

Did you realise that since "que", "i" and "d" are all integers, "(* que (1+ (/ i d)))" jumps up in steps of 69?

Also, 480 iterations seems rather excessive and takes the harmonics right up to 24kHz. I can see the logic in going up to the Nyquist frequency, but I would argue that in practice the last few hundred iterations are removing a much greater percentage of the "music" than the "noise". Also, reducing the number of frequency bands will speed up processing and reduce ringing. (and yes, the removal of all of those harmonics is audible - if you process a section of audio that does not have hum, then listen through good speakers or headphones you can hear the difference between the processed and unprocessed sections)

The other thing that leads to ringing is that you are using such high Q values. While it is desirable to keep the notch filter tight, high Q values cause ringing, so there is a trade off to be made. You probably notice that if you test the plug-in with the default values on "pure hum", it takes a while before the filter "kicks in". Lower Q values will cause less ringing, but the notches will be wider, but if you limit the effect to lower frequencies where the hum is likely to be more noticeable, then lower Q values should not present a problem.

To make the plug-in work with stereo...
Stereo tracks in Audacity consist of an array with two elements (the left and right channels).
You can test for an array with (arrayp sound)
The two elements (left and right) are accessed with (aref sound 0) and (aref sound 1)
To return a stereo sound to Audacity, use "vector"

You will commonly see code like this:

(if (arrayp sound) ;; if sound is an array
(vector (aref function() 0) (aref function() 1)) ;; create an array
(function()) ;; else

If you turn your code into a function, then it can be called once for a mono track, or twice for a stereo track.
(One more thought - mains hum can often cause some amount of DC off-set, so adding a low frequency high pass filter could be a good idea)
Something like this:

Code: Select all

(setq setfreq 50)
(setq iter 40)

(defun dehum (mysound freq itr)
   (dotimes (i itr mysound)
   (setf mysound (notch2 (hp mysound 20) (* freq (1+ i)) (* 2 (1+ i))))))

(if (arrayp s)
   (vector
      (dehum (aref s 0) setfreq iter)
      (dehum (aref s 1) setfreq iter))
(dehum s setfreq iter))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Trebor
Posts: 9849
Joined: Sat Dec 27, 2008 5:22 pm
Operating System: Windows 8 or 8.1

Re: Multiple notches with Nyquist prompt?

Post by Trebor » Wed Mar 04, 2009 10:18 pm

Despite my unfamiliarity with LISP, (I now know why they named it after a speech impediment :D ),
and this being my first attempt at a plug-in, my Dehummer does exactly what is says on the tin :
here is a link to a before and after example (mp3) … http://www.sendspace.com/file/nxvejc

I did try reducing the number of loops, to the first 100 harmonics (5KHz), but I could still clearly hear the high frequency harmonics of the mains hum, so unfortunately the 480 loops (at 48000 sample rate) are necessary. (I’m in the UK these numbers relate to 50Hz mains).

Thank you for the tips on Nyquist stereo, I will try make dehummer 2.0 a stereo version.
Attachments
Mains dehummer 2-0.zip
My Mains dehummer 2.0 plug-in for Audacity, (zip file: "extract" to audacity plug-in folder)
(658 Bytes) Downloaded 515 times
Last edited by Trebor on Wed Apr 08, 2009 5:06 pm, edited 1 time in total.

Post Reply