2

How would I go about implementing a spectrum analyser like the ones in WinAmp below?

Example of 'equaliser bars' from Winamp

Just by looking at it, I think that these bars are rendered to display the 'volume level' of a specific frequency band of the incoming audio data; however, I'm not sure how to actually calculate this data needed for the rather easy task of drawing the bars.

From what I've been told and understand, calculating these values can be done by using an FFT — however, I'm not exactly sure how to calculate those, given a buffer of input data — am I on the right track about FFTs? How would I apply an FFT on the input data and get, say, an integer out of the FFT that represents the 'volume' of a specific frequency band?

The drawing part isn't a problem, since I can just draw directly to my framebuffer and render that out. I'm doing this as a project on an FPGA, using a Nios II soft-CPU, in case anyone's wondering about potential hardware limitations. Audio data comes in as 24-bit data at 96kHz.

Tristan
  • 3,058
  • 6
  • 40
  • 68
  • 3
    The length of the bars is proportional to the logarithm of the power peak of a particular channel. –  Mar 10 '13 at 07:34

2 Answers2

6

You're probably looking for FFTw.

edit:

To elaborate on your question:

calculating these values can be done by using an FFT — however, I'm not exactly sure how to calculate those, given a buffer of input data: yes, you're right; that's exactly how it's done. You take a (necssarily small, due to the time-frequency uncertainty principle) sample segment out of the currently playing audio data, and feed it to a (typically) discrete, real-only FFT (one of the best known, most widely used and fastest being the DCT family of DFTs - in fact there are highly optimized versions of most DCTs in FFTw). Then you take out the next sample segment and repeat the process. The output of the FFT will be the freqeuncy decomposition of the audio signal that has been fed in - you then need to decide how to display it (i.e. which function to use on the outputs of the FFT, common candidates being f(x) = x; f(x) = sqrt(x); f(x) = log(x)) and also how to present/animate the following readings (e.g. you could average each band in the temporal direction or you could have the maximums "fall off" slowly).


rage-edit: Additional links since it appears somebody knows how to downvote but not how to use google:

CAFxX
  • 28,060
  • 6
  • 41
  • 66
  • 1
    I fail to see why a downtime of a the official site of the arguably most famous FFT library is my responsibility. Do you know how to use google? Then you should be able to access the cached version of the page, instead of giving out pointless -1s. – CAFxX Mar 10 '13 at 08:54
  • @Zeta that's better than the (IMHO braindead) original reason you gave. I'll elaborate. – CAFxX Mar 10 '13 at 08:57
  • 1
    You have to admit that an answer pointing to a resource that's offline (or might be offline in a certain future) is basically (IMHO) useless. See also http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259; however, since you already started to elaborate I removed my downvote. And by the way, _I_ know how to use caching sites, but others who might visit this page in the future might not. An answer is not only for the current OP but also for future readers! – Zeta Mar 10 '13 at 09:02
0

It's pretty straight forward - just use one of the many FFT algorithms! Most of them require floating point calculations, but a google search brings up methods with just integers. You are spot on though, FFT's are what you want.

To understand how to apply a FFT, you should have a read of this page on discrete fourier transforms, although it's quite heavy on maths:

To implement it on your FPGA, I'd have a look at the source code of this project:

Here's a previous SO question that gives a summary of how it works (in any language).

There's an immense amount of information on creating what by the way is called a "Spectrum Analyser", there must be dozens of complete implementations where the source code is freely available. Just page through a google search for "Spectrum analyser source code C" for example.

Community
  • 1
  • 1
Jodes
  • 14,118
  • 26
  • 97
  • 156