This tutorial showcasts FFT functionality.
- Note
- The FFT module is experimental in ViennaCL. The API might change in future versions.
We start with including the respective headers:
#include <iostream>
#include <vector>
#include <cmath>
#include <complex>
#include <fstream>
In the main()-routine we create a few vectors and matrices and then run FFT on them.
{
for (std::size_t i = 0; i < m.
size1(); i++)
for (std::size_t s = 0; s < m.
size2(); s++)
Fill the vectors and matrices with values by using operator(). Use viennacl::copy() for larger data!
for (std::size_t i = 0; i < input_vec.
size(); ++i)
{
if (i % 2 == 0)
{
} else
input_vec(i) = 0;
}
Compute the FFT and store result in 'output_vec'
std::cout << "Computing FFT Matrix" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "Done" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "Transpose" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "---------------------" << std::endl;
Compute the FFT using the Bluestein algorithm (usually faster, but higher memory footprint)
std::cout << "Computing FFT bluestein" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
Computing the standard radix-FFT for a vector
std::cout << "Computing FFT " << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
Computing the standard inverse radix-FFT for a vector
std::cout << "Computing inverse FFT..." << std::endl;
viennacl::inplace_ifft(output_vec);
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
Convert a real vector to an interleaved complex vector and back. Entries with even indices represent real parts, odd indices imaginary parts.
std::cout << "Computing real to complex..." << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing complex to real..." << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
Point-wise multiplication of two complex vectors.
std::cout << "Computing multiply complex" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "input2_vec: " << input2_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
That's it.
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#include <vector>
#include <cmath>
#include <complex>
#include <fstream>
{
for (std::size_t i = 0; i < m.
size1(); i++)
for (std::size_t s = 0; s < m.
size2(); s++)
for (std::size_t i = 0; i < input_vec.
size(); ++i)
{
if (i % 2 == 0)
{
} else
input_vec(i) = 0;
}
std::cout << "Computing FFT Matrix" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "Done" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "Transpose" << std::endl;
std::cout << "m: " << m << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing FFT bluestein" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing FFT " << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing inverse FFT..." << std::endl;
viennacl::inplace_ifft(output_vec);
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing real to complex..." << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing complex to real..." << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "Computing multiply complex" << std::endl;
std::cout << "input_vec: " << input_vec << std::endl;
std::cout << "input2_vec: " << input2_vec << std::endl;
std::cout << "Done" << std::endl;
std::cout << "output_vec: " << output_vec << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << "!!!! TUTORIAL COMPLETED SUCCESSFULLY !!!!" << std::endl;
return EXIT_SUCCESS;
}