# Applying FFT to PCM stream

I would like to use a .WAV file and convert it into a spectogram. Any idea how to do this with Naudio? Basically the flow should be like this, I think:

1. Convert WAV file to PCM stream
2. Convert PCM stream to double-array and applying FFT
3. Using the result from (2) and convert it to a spectrogram

Your description of how to create a spectrogram is correct. Here's an explanation with a little more detail:

First, create a WaveChannel32, using a WaveFileReader. This will output a PCM byte stream. Then read 4 times the size of your FFT bytes from the WaveChannel32 into a buffer. Convert every four bits in the buffer into an integer.

It's important at this point to use a window function on our interval, to emphasise amplitudes closer to the current time stamp.

I'd then suggest using the Exocortex FFT library to calculate the FFT. The Fourier.FFT method will convert an array of complex numbers to its corresponding Fourier transform. Then ignore the second half of the array produced, to prevent aliasing.

The first half of the array corresponds to the column of the spectrogram at our current time stamp. Each number in the array corresponds to a single pixel in the spectrogram, the frequency of which is determined by the index of the array (equally spaced throughout 0 to 22050Hz). The darkness of each pixel is determined by the magnitude squared of the complex number corresponding to said pixel.

Here's some sample code for producing one column:

var waveChannel = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader("file path"));

var buffer = new byte[4096*4];

var samples = new ComplexF;

for (var i = 0; i < 4096, i++)
{
samples[i].Re = BitConverter.ToSingle(buffer,i*4);

// Window should be pre-populated with values from your window function
samples[i].Re *= Window[i];
}

Fourier.FFT(samples, FourierDirection.Forward);

• Hi thanks for your answer. I would like to ask if i would like to get the whole spectrogram out and display it, would it be easily achievable using a metro app project as it does not support WinForm? – user2431088 Jun 22 '13 at 3:38
• I think that should be fairly easily achievable, but I have zero experience with Metro. – laurie Jun 24 '13 at 9:17