We introduce FFT (Fast Fourier transform) using DX11 GPGPU, also implement FT without DX11. FT is commonly used to transform function in time domain to a function in frequency domain, this further allows us to analyze signals in frequency space and add our own changes (digital equalizers), these changes are further reconverted to time domain using inverse FT.
We implement FT without DX11 to verify the output.
This article does not hope to introduce to the readers Fourier transform (FT), it only show cases use of GPU to perform FFTs, it would be a good idea to read up on FT. Also you would be required to go through my previous article http://www.codeproject.com/Articles/42612/DirectX-11-Compute-Shaders, since this article requires you to know about DX11 resource and views. Thankfully DX11 already provides FFT related functionality so we are not required to write a compute shader.
Advantage of running FT on GPU is to gain performance from the parallel stream processors.
Using the code
As with my previous article, the attached code must be referred to at all times.
We must start by creating a DX11 device, remember that CS is not available on all hardware, for the purpose of this tutorial we will use the reference driver, this will force FFT to be computed on CPU.
The next thing we need is the buffer on which we shall perform FT, remember what we perform here are strictly discrete FTs, so we work on buffer of fix size, please refer to http://en.wikipedia.org/wiki/Discrete_Fourier_transform
In our example, we create input of real numbers (no imaginary component is being used), but the outcome of an FT may contain an imaginary component.
FT always has a cosine (real) and sine (imaginary) component, we implement our own variation using an exp (e^x) function where e=2.7182818 refer: http://en.wikipedia.org/wiki/E_(mathematical_constant)
Now comes the best part
we need to create the buffer and fill it up with the data generated by the above code (please refer to my previous article related to compute shader), the difference between my previous article is that we use buffers that allow raw views as oppose to structured buffer. These buffers once created are made available to the GPU, the GPU can access them to perform the required computation.
desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS;
Lets create the FFT object
HRESULT hr=D3DX11CreateFFT(pContextOut,&fftdesc,1 ,&fftbufferinfo,&pFFT)
The variable fftbufferinfo returned will tell us the resource requirements to complete FFT, in the attached code ,we need 2 temporary buffers. We must create 2 temporary buffers and attach them, these temporary buffers are used internally to carry out the operation. Look up http://msdn.microsoft.com/en-us/library/windows/desktop/ff476847(v=vs.85).aspx
We must also set the forward scale to 1, every FT point of output will be multiplied by this value.
The next thing is to call
This will tell the DX11 runtime to perform FFT on the input data, it will return a resource view from which output data can be accessed. Further transforms and be performed on the output buffer.
Then pull out the output buffer, this is explained in my previous article on compute shader and in attached code. Remember the output will be twice the size of the input buffer since we get two outputs for every input point, one real and one imaginary value (as discussed earlier).
Now lets verify this by writing out own FT. The below code is straight from my college books
b[k] will hold the FT output at point k
The first for loop does the summation, the next one moves ahead to find the FT of the next point. (This part is self explanatory to people who have worked on FTs)
The 2 for loops are used (hence a time complexity of n^2), not to mention exp function is added to work with complex numbers and is compute heavy, i =sqrt(-1), j,k are used in for loop. The output of our FT closely matches the output of DX11 FFT.
Since b[k] is calculated for every value of k, these operations can be carried out on every stream individually, hence FFT/FT implemented on GPU will greatly improve performance.
As an exercise, you may implement Inverse FT (IFT):
Points of Interest
FFT are very computational intensive tasks, and to implement them on the GPU can highly optimize your code. You can try implementing your own version of a sound equalizer or a new winamp visualization using the attached code (may be my next project).