# How to filter out unwanted sounds via Fourier Transform

In this post about The Fast Fourier Transform, I described how simple it was to create a filter to remove unwanted sound from a WAV file. In order to do that, I needed some code to read and write a WAV file. Then I used the FFT to convert to the frequency domain, do a simple convolution (which in this case was trivial: multiply by 1 or 0 if the frequency was outside/inside the desired range), then do an inverse FFT.

The format of a WAV file is fairly simple. There is a header with some waveform information and then the raw data, typically in Pulse Code Modulated form. PCM is just a digitized representation of the waveform, like an amplitude vs time graph. The number of bits per sample indicates the number of discrete levels of amplitude, also known as the dynamic range

I remembered that Sound Recorder allows you to do primitive editing of WAV files, and it also has a feature to mix WAV files together. I used these features to record my voice and mix it with a 2048 hz sine wave to make a “noisy” sample input wave form. The goal was to write code to filter out the “noise”. Creating a sine wave WAV file is easy: just modify the code that writes the WAV file to output a sine wave.

Making your voice sound like the chipmunks is simple: just change the sampling frequency

You can do tricks like

• remove every other sample and halve the sample frequency to compress the data. Windows Media Player can speed up the video playback without making the audio sound like the chipmunks.

• Change 16 bit sampling to 8 bit sampling for compression

• Convolve the data with a filter (such as low pass, high pass, or notch filter).

• The convolution can be done as a simple multiply: do a Fourier Transform on the data, multiply by a filter, then do an inverse Fourier Transform

• A low-pass filter allows only frequencies below a particular frequency. Conversely for a high-pass filter. A bandpass filter allows only frequencies within a particular band. A notch filter is the inverse of a bandpass, and is what my sample does to eliminate the sine wave.

• A digital Treble or Bass control can use exactly the same technique, except that the filter shape might have a gentle attenuation or amplification of higher or lower frequencies.

(I was trying to find the built in microphone on my Toshiba Portege M200, but couldn’t find it in the documentation or the interactive tour. So I started trusty old Windows Sound Recorder (sndrec32.exe) and blew on some of the candidate holes I could find. Sure enough, there are a couple tiny holes at the bottom (when in non-tablet mode) of the screen )

Experiment with changing the filter parameters: frequency (nMid) and range (nRange).

Also, experiment with changing the FFT code itself: change a “+” to a “-“ to hear the result!

Here is a link to the sample FoxRocks.Wav file (It will open in whatever app is associated with the WAV file extension on your machine: Windows Media Player, for example. Just choose “File->Save As” to save it locally. Or you can right click on the link and choose File->Save Target as)

91206

Tags

1. MobyDikc says:

Calvin, you are a cool dude!

I <a href="http://www.cosmik-debris.net/?p=75">blogged about electronically created music</a> not too long ago, you might be interested in some of those projects.

2. James Topf says:

I need to subtract instrumental music (mostly violin) from a music + speaking-voice recording. I have original music on CD and saved as WAV file. Music/voice file is also WAV. For this ‘demixing’ need to subtract from about 5 secs of audio material. Any help? Don’t know how to ‘run’ FFT program but copied text file. How would one synchronize the files? …just edit them to same length/startpoint? Any help? Thanks

3. rape stories says:

Best of the text i read about a problem.

4. Sirmai Jean says:

Bonjour, Hi,

I’m from Paris (near), and not a professionnal programmer but I’m able to do some work in Java

(and I’ve done some Pascal in old times)

I would like to help a friend – student –

who tries to do the following manip:

He explores various cavities / resonnators

using the same input (a "pink" noise, may be ?)

and would like to analyse the output

– recorded in a .wav file –

with a FFT, in order to find some relations with the morphology of the cavity.

He is supposed to give the results in about one month.

So, here are the problems:

– Is the way: [.wav file –> extract data with Java –> use FFT and Java to see the results] a possible solution, or must I learn C++ quickly… ?

– are all the pieces of the puzzle already present, or must I build some of them or use some "glue" or patches here and there ?

How would you do it ?

Thanks for help !

Jean

5. Ray Howard says:

I recently removed a program that I think was running my streaming audio’s performance.  I now have every streaming video sounding like chipmunks.  I’m using windows 2000 by the way.

6. Toby Andersson says:

Hi Calvin,

I was surfing the Net for a solution something along the lines in your blog entry of 20 September 2005, but the link to the source code is dead (http://www.calvinhsia.com/slides.asp)!

I realize it was posted a long time ago, and space isn't unlimited, so it has perhaps been removed?

It would be great if you could make it available again.