Audacity Nyquist Programming: Removing a 60hz hum

My friend Flavio and I record a Spanish-language podcast dedicated to Apple products called Puromac. Since he currently lives in Puerto Rico and I'm in New York City, we talk over Skype and record the conversation using either Audio Hijack Pro or Call Recorder for Skype.

In our most recent episode, Flavio's mic produced an audible 60hz hum throughout the whole podcast. When he ran the audio file through Levelator (an excellent utility to adjust the levels of all audio within a file) the hum was made even louder. Before doing any post work on the audio file, I needed to remove the hum.

I use Audacity for my audio editing and while it has a very good Noise Removal tool, it was failing to pull out the hum completely, leaving a "thumping" effect over the audio. My guess is that the harmonics of the electrical hum (the frequency isn't a solid 60hz) were a problem for the Noise Removal tool.

What I needed was a notch filter--a filter that removes a specific frequency from an audio signal. My plan was to use a notch filter to remove the bulk of the hum and then clean up the rest using Audacity's Noise Removal tool.

Searching around the web, I found a Nyquist script that removed a 60hz hum along with an additional 4 of its harmonics.

Audio samples (You will need headphones to hear the differences):
  1. Original audio file with a noticeable hum.

  2. An attempt to remove the hum using solely Audacity's Noise Removal tool. You will notice there is still a hum in parts of the dialogue which fades in and out.

  3. The stereo Nyquist script below applied to the original file.

  4. The stereo Nyquist script applied followed by Audacity's Noise Removal tool.



What is Nyquist?

Nyquist is a programming language for use in audio and music applications developed at Carnegie Mellon University (my alma matter). Version 2.x of the language (which is what Audacity uses) is written in Lisp. Audacity's help page has a very brief but informative introduction into Lisp. I recommend reading it as it makes the Nyquist script below easier to understand. You can also visit the Nyquist page at CMU for more information.

Removing a 60hz hum with a Nyquist script:

Audacity doesn't have a full Nyquist development environment, but it does have a plugin that allows you enter a Nyquist script to run and debug.

In Audacity, select a portion of the audio you want to clean. (You could select the whole thing, but since there is no preview function for Nyquist scripts, and my audio was more than a hour long, I selected a representative portion of the audio, ran the script, made sure it worked, pressed Command-Z  (on a Mac) to undo the script, selected the whole file then ran the script again. )

If you're working with a mono audio tractk, select Effect:Nyquist Prompt... then in the window that pops up, enter the following script.

(notch2
(notch2
(notch2
(notch2
(notch2
s
300 10)
240 8)
180 4)
120 2)
60 1)


... Then click "OK".

This runs a recursive set of notch filters (the notch2 function) on the audio signal, filtering out 60, 120, 180, 240 and 300 hz frequencies.

For a stereo track, the script is more involved:

(vector
(notch2
(notch2
(notch2
(notch2
(notch2
(aref s 0)
300 10)
240 8)
180 4)
120 2)
60 1)
(notch2
(notch2
(notch2
(notch2
(notch2
(aref s 1)
300 10)
240 8)
180 4)
120 2)
60 1) )


This script is the same as the first, except that it handles 2 inputs via a vector (basically an array).

If you still hear a little bit of hum (I did, though it was barely audible), use the Noise Removal filter in Audacity for that last bit of noise.

Examining the Nyquist script:

As I understand it, in Lisp everything is a function call. The format for all calls is:

([FUNC_NAME] [ARGS...])

The notch2 function is called like this:

(notch2 s 60 1)

Where "notch2" is the name of the function, "s" is the first argument referring to the audio signal (I guess Audacity calls it "s" and makes it available globally), "60" is the value in Hertz of the frequency to extract and "1" is the Q factor.

The Nyquist and Audacity documentation don't talk much about what a Q factor is, except that it's the "depth and width" of the function, or the "fudge factor" around the frequency. My friend Taylor sent me this Wikipedia article on Q factors after seeing the original version of this blog posts in which I conceded ignorance on the subject. Essentially, a value below 1 creates a wider notch, while a value above it creates a narrower one.

The script above could be written as:

(notch2 (notch2 (notch2 (notch2 (notch2 s 300 10) 240 8) 180 4) 120 2) 60 1)

Though it's a little harder to read, you get a better sense of the recursive nature of the call. The inner notch2 function call filters out 300hz with the signal "s" as the input. The output of that call is fed to the second notch2 call, which filters out 240hz. Then comes 180hz, 120hz and finally the outer call filters out the remaining 60hz signal from the audio.

You can read more about noise removal in Audacity on its Wiki page.

Prebuilt Nyquist filters:

After writing this blog entry I discovered that Audacity already has a notch filter routine already written as part of their downloadable library, so that you can add it to Audacity's plug-ins folder. It's not clear to me why these plugins don't come installed by default.

Looking at the code for Audacity's notch.ny, I don't think it handles harmonics like the script above, but it's certainly easier to use and knows the difference between a mono and stereo file.

[UPDATED: More information about Q factors]