quote:
but what data is in windowWindowFunction
It depends on what window function you choose. A Hann window is a simple window formula.
const int length = 512;double windowFunction[length];for(int n = 0; n < length; ++n) windowFunction[n] = 0.5*(1-cos(2*pi*n/length))
quote:
what kind of structure is Frequencies
You should no look at the code as something you just plug into the compiler as it is. Frequencies in this case would be an array of the same length as the sample buffer. Each entry in the array represents the amplitude of a frequency (the fourier transform actually results in complex values, but it's the absolute value of these complex values you're generally interested in).
quote:
why is the length divided by 2
I assume you mean '/2' in 'sound[at - length / 2 + i]'
That is because the function getFrequencies takes a parameter 'at', which is a point in the sample buffer where you want to extract frequencies, and you pick a window of size 'length' around this point. '/2' is just to get half the window size on each side of 'at'. Don't worry about that.
quote:
what operations does doFourierTransform do
Now this is where all magic happens. The discrete fourier transform is defined as
X(k) = sum from n=0 to n=length-1, x(n)*exp(-j*2*pi*k*n/length)
Where x(n) is the sound buffer, and X(n) is the frequency components of the sound buffer. doFourierTransform could look something like this, in pseudo-code.
complex X[length];double F[length];for(k = 0; k < length; ++k){ X[k] = 0; for(n = 0; n < length; ++k) { X[k] += x[n]*exp(complex(0, 2*pi*k*n/length)); } F[k] = abs(X[k]);}return F;
complex is a, say, class for complex numbers.
F is the frequency spectrum of the buffer.
As you can see, the discrete fourier transform (DFT) is an O(n
2) operation. As the buffer grows, the time taken to get the frequencies grows squared. The fast fourier transform (FFT) is another way to get the DFT, which uses symmetries in the different values of n and k, and the corresponding values of the exponential function, to greatly reduce the number of calculations needed, but on the other hand, it requires the buffer to be of power of two length. The main idea behind FFT is to break the buffer down into smaller parts and perform the DFT on these instead. Since splitting the buffer into two reduces the DFT-time by four, we have reduced the time by one half. But we don't stop here, so we keep on splitting all the way down to a length of 2 samples. The FFT is a O(n*logn) operation.
[edited by - Brother Bob on January 30, 2003 8:57:42 AM]