Frequency Analyzer

Back home.

# The Real Time Frequency Analyzer

## So, how does it work?

First, the microphone converts sounds into voltage. Then, your sound card acts as a very fast digital voltmeter. It measures the voltage as often as 11,025 -- up to 44,100 times per second. You can set this number-of-measurement-per-second parameter using a drop-down combo box "Sampling Frequency" (here it shows 11 kHz) Each measurement is converted into an 8- or 16-bit number called a sample. 16-bit numbers allow for more accuracy in measuring subtle effects. You can select this paramter by clicking on one of the "Bits per sample" radio buttons.

The result of sampling is a series of numbers. This series is displayed in the upper right pane of the Frequency Analyzer. That's where you can see the sine wave produced by your whistling. The bottom pane shows the spectrum of the sound (see explanation below).

Every sound can be represented as a combination of sine waves of various frequencies. Mathematically, this splitting into component frequencies is called a Fourier transform. There are many ways of calculating the Fourier transform-- some of them faster, some of them slower. The best algorithm invented so far is called the Fast Fourier Transform (FFT). It calculates a Fourier transform of a digital signal using the divide-and-conquer method. It takes a certain number of samples (has to be a power of 2) and calculates the "spectrum," or the intensities of various sine waves that are the components of that sound. You can select the number of samples that go into a single transform using the "Points per Transform" combo box. The more points, the finer the transform, i.e., the more frequencies can be detected. If you look at the scale on the left of the main display pane, you'll see that it changes when you change the "Points per transform" paramter. By the way, the shorter marks are spaced every 100 Hz, the longer ones every 1000 Hz (1 kHz).

Since the sound usually changes with time, the Fourier transform is calculated many times per second. You can adjust this parameter using the "Speed (FFT's per sec)" slider (in the picture, it shows 10 FFT's per second). If you have a slower computer, be careful changing this parameter, as your processor might not be able to catch up with the speed with which the sound card is sending samples (the system might suddenly become sluggish and you might have to kill the Frequency Analyzer using the Windows 95 application bar).

Here's how you interpret the main pane of the Frequency Analyzer: There is a white vertical line moving from left to right. It leaves behind a colorful trace. The colors correspond to the intensities of various components of the sound. For instance, if you whistle into the microphone a pure sine wave of the frequency 1 kHz (you can easily learn to do it using the feedback from the Frequency Analyzer), the Fourier transform will have a sharp peak at 1 kHz. You'll see the program draw a thin line, probably red or yellow, depending on the volume, at the height corresponding to 1 kHz. If you lower the pitch of your whistle, the line will move downwards. If you icrease the pitch, it will turn upwards. When you say "aaah," you'll see several horizontal lines corresponding to the harmonics of your voice. You'll probably find it fascinating to be able to "look" at various sounds. You'll need at least a 486 computer, although a Pentium would be better. Only a few years ago only the fastest computers or dedicated hardware systems were able to perform a real time FFT. And now you can have a gadget on your desktop that does it for free. Computers are amazing, aren't they?

*) The multimedia system creates a high priority thread to intercept data from the sound card. This thread is supposed to yield from time to time to let other threads run. But if your computer is too slow and you ask this thread to perform too much work, it never yields and the app looks dead. On Windows 95 the whole system hangs. It's not a bug in the Frequency Analyzer--it's a "feature" of Windows.

Source code and tutorials.

For those of you who'd like to do some programming of their own, we are making availbable the C++ source (7k) of the Fast Fourier Transform algorithm. You may use it freely in your commercial software. The only thing we are asking in return is to display the acknowledgment to Reliable Software and the address of our web site in the About box of your program. (Being associated with Reliable Software is by itself good advertisment for your program. Please, don't overdo it though. If your product is an HTML editor, don't acknowledge the use of Reliable Software's FFT class in it. Nobody will believe you.)

By the way, this is the formula we are using

By popular demand we have included a tutorial on how to sample input from the microphone using Win32 API. It's really not that difficult, and Windows provides pretty good abstraction on top of the sound hardware.

Finally, after many requests for the complete source code of the Frequency Analyzer, we have decided to publish it here for downloading (26 KB zipped).

Important! If you get the following compilation error

" CallWindowProcA' : cannot convert parameter 1 from 'long (__stdcall *)(void *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(void)' controls.cpp

it means you haven't set the STRICT flag during compilation. Microsoft VC++ does not set this flag by default, assuming that you don't care about strict type checking and you'd rather produce buggy code than let the compiler discover your mistakes. Go to the menu Project/Settings, select the C/C++ tab and in Preprocessor Definitions add STRICT.