6
votes

So, I've been working on a little visualizer for sound files, just for fun. I basically wanted to imitate the "Scope" and "Ocean Mist" visualizers in Windows Media Player. Scope was easy enough, but I'm having problems with Ocean Mist. I'm pretty sure that it is some kind of frequency spectrum, but when I do an FFT on my waveform data, I'm not getting the data that corresponds to what Ocean Mist displays. The spectrum actually looks correct, so I knew there was nothing wrong with the FFT. I'm assuming that the visualizer runs the spectrum through some kind of filter, but I have no idea what it might be. Any ideas?

EDIT2: I posted an edited version of my code here (editor's note: link doesn't work anymore). By edited, I mean that I removed all the experimental comments everywhere, and left only the active code. I also added some descriptive comments. The visualizer now looks like this.

EDIT: Here are images. The first is my visualizer, and the second is Ocean Mist.

my visualizer

ocean mist

4
It might help if you posted a link to a screenshot of what you're trying to achieve (e.g., an example of the ocean mist visualization) for the lazy\non WMP users.davidtbernal
@Bevin - I made some changes to your code. THEY ARE UNTESTED so I can't guarantee syntax, but I hope the spirit of them make sense. I'm about to head out for a while, but will check for updates later. Also, it would be helpful if you could post the documentation for the FFT you're using.mtrw
Well, you should have copied the link in the address bar after saving, because pastebin doesn't actually change the existing code, it makes a new "pad". I can wait :)Bevin
Well, getting late for me. Anyway, here's the place where I got the FFT. It isn't as big as say, FFTW, but it seems to work. The original page can't be reached, so here is a Google cache page. 74.125.77.132/search?hl=en&q=cache:http://www.librow.com/…Bevin
@Bevin - that was very silly of me, sorry. Anyway, I reconstructed the changes. See pastebin.com/8WgaaAMY. Make sure that when you pass a sine wave in, you get something like the green line in the loglog graph I posted earlier. Yours should be smoother due to no random noise, but the spike should be about the same width and at roughly the same horizontal place.mtrw

4 Answers

6
votes

Here's some Octave code that shows what I think should happen. I hope the syntax is self-explanatory:

%# First generate some test data
%# make a time domain waveform of sin + low level noise
N = 1024;
x = sin(2*pi*200.5*((0:1:(N-1))')/N) + 0.01*randn(N,1);

%# Now do the processing the way the visualizer should
%# first apply Hann window = 0.5*(1+cos)
xw = x.*hann(N, 'periodic');
%# Calculate FFT.  Octave returns double sided spectrum
Sw = fft(xw);
%# Calculate the magnitude of the first half of the spectrum
Sw = abs(Sw(1:(1+N/2))); %# abs is sqrt(real^2 + imag^2)

%# For comparison, also calculate the unwindowed spectrum
Sx = fft(x)
Sx = abs(Sx(1:(1+N/2)));

subplot(2,1,1);
plot([Sx Sw]); %# linear axes, blue is unwindowed version
subplot(2,1,2);
loglog([Sx Sw]); %# both axes logarithmic

which results in the following graph: top: regular spectral plot, bottom: loglog spectral plot (blue is unwindowed) http://img710.imageshack.us/img710/3994/spectralplots.png

I'm letting Octave handle the scaling from linear to log x and y axes. Do you get something similar for a simple waveform like a sine wave?

OLD ANSWER

I'm not familiar with the visualizer you mention, but in general:

  • Spectra are often displayed using a log y-axis (or colormap for spectrograms).
  • Your FFT might be returning a double-sided spectrum, but you probably want to use only the first half (looks like you're doing already).
  • Applying a window function to your time data makes the spectral peaks narrower by reducing leakage (looks like you're doing this too).
  • You might need to divide by the transform blocksize if you're concerned with absolute magnitudes (I guess not important in your case).
  • It looks like the Ocean Mist visualizer is using a log x-axis too. It might also be smoothing adjacent frequency bins in sets or something.
3
votes

Normally for this kind of thing you want to convert your FFT output to a power spectrum, usually with a log (dB) amplitude scale, e.g. for a given output bin:

p = 10.0 * log10 (re * re + im * im);

1
votes

It definitely looks like the ocean mist Y-Axis is logarithmic.

1
votes

It seems to that not only the y axis, but the x axis also is logarithmic. The distance between peaks seems to lower at higher frequencies.