Reputation: 143
I'm looking for a way to extract data from a WAV file that will be useful for an FFT algorithm I'm trying to implement. So far what I have are a bunch of hex values for left and right audio channels, but I am a little lost on how to translate this over to time and frequency domains for an FFT.
Here's what I need for example:
3.6 2.6
2.9 6.3
5.6 4.0
4.8 9.1
3.3 0.4
5.9 4.8
5.0 2.6
4.3 4.1
And this is the prototype of the function taking in the data for the FFT:
void fft(int N, double (*x)[2], double (*y)[2])
Where N is the number of points for the FFT, x is a pointer to the time-domain samples, y is a pointer to the frequency-domain samples.
Thanks!
Upvotes: 1
Views: 5133
Reputation: 70673
The most commonly found WAVE/RIFF file format has a 44 byte header followed by 16-bit or 2-byte little-endian signed integer samples, interleaved for stereo. So if you know how to skip bytes, and read short ints into doubles, you should be good to go.
Just feed your desired length of time domain data to your FFT as the real component vector; the result of the FFT will be a complex frequency domain vector.
Upvotes: 0
Reputation: 62048
For testing purposes you don't need to extract waveform data from WAV files. You can just generate a few signals in memory (e.g. 0, non-zero constant, sinusoid, 2 superimposed sinusoids, white noise) and then test your FFT function on them and see whether or not you're getting what you should (0 for 0, peak at zero frequency for non-zero constant signal, 2 peaks for every sinusoid, uniform non-zero magnitude across all frequencies for white noise).
If you really want to parse WAV files, see Wikipedia on the format (follow the links). Use either raw PCM encoding or A/µ-law PCM encoding (AKA G.711).
FFT is usually implemented using an in-place algorithm, meaning that the output replaces the input. If you do the same, you don't really need the second pointer.
Upvotes: 3