Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameMLVisitors.h> template<typename T, typename I = unsigned long> struct FastFourierTransVisitor; // ------------------------------------- template<typename T, typename I = unsigned long> using fft_v = FastFourierTransVisitor<T, I>; |
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface. This visitor calculates Fast Fourier Transform on a column. If column length is a power of 2, the algorithm will be more efficient in terms of time and memory. The input could be either a column of floating-point values or a column of complex values. The result is always a vector of complex values with same number of items as the given column. get_result() returns the FFT vector in complex values get_magnitude() returns vector of magnitudes get_angle() returns vector of phase angles A fast Fourier transform (FFT) is an algorithm that computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa. The DFT is obtained by decomposing a sequence of values into components of different frequencies. This operation is useful in many fields, but computing it directly from the definition is often too slow to be practical. This process has many engineering applications including decomposing a time-series and finding seasonality in a time-series. explicit FastFourierTransVisitor(bool inverse = false); inverse: Calculate the inverse. |
T: Column data type I: Index type |
static void test_FastFourierTransVisitor() { std::cout << "\nTesting FastFourierTransVisitor{ } ..." << std::endl; using cx = std::complex<double>; std::vector<unsigned long> idxvec = { 1UL, 2UL, 3UL, 10UL, 5UL, 7UL, 8UL, 12UL }; std::vector<double> dblvec = { 1, 1, 1, 1, 0, 0, 0, 0 }; std::vector<std::string> strvec = { "11", "22", "33", "44", "55", "66", "-77", "88" }; std::vector<cx> cplxvec = { cx(0, 0), cx(1, 1), cx(3, 3), cx(4, 4), cx(4, 4), cx(3, 3), cx(1, 1) }; MyDataFrame df; df.load_data(std::move(idxvec), std::make_pair("str_col", strvec), std::make_pair("dbl_col", dblvec)); df.load_column("cplx_col", cplxvec, nan_policy::dont_pad_with_nans); fft_v<double> fft; df.single_act_visit<double>("dbl_col", fft); for (auto citer : fft.get_result()) std::cout << citer << " | "; std::cout << std::endl; df.load_column("FFT int col", fft.get_result(), nan_policy::dont_pad_with_nans); fft_v<std::complex<double>> i_fft (true); df.single_act_visit<std::complex<double>>("FFT int col", i_fft); for (auto citer : i_fft.get_result()) std::cout << citer << " | "; std::cout << std::endl; // The case of size is not a power of 2 // fft_v<cx> fft_cx; df.single_act_visit<cx>("cplx_col", fft_cx); for (auto citer : fft_cx.get_result()) std::cout << citer << " | "; std::cout << std::endl; df.load_column("FFT int col 2", fft_cx.get_result(), nan_policy::dont_pad_with_nans); fft_v<cx> i_fft2 (true); df.single_act_visit<std::complex<double>>("FFT int col 2", i_fft2); for (auto citer : i_fft2.get_result()) std::cout << citer << " | "; std::cout << std::endl; try { typedef StdDataFrame<std::string> StrDataFrame; StrDataFrame df2; df2.read("data/SHORT_IBM.csv", io_format::csv2); fft_v<double, std::string> fft2; df2.single_act_visit<double>("IBM_Close", fft2); df2.load_column("FFT Close", fft2.get_result()); fft_v<cx> i_fft2 (true); df2.single_act_visit<cx>("FFT Close", i_fft2); /* for (auto citer : i_fft2.get_result()) std::cout << citer << ", "; std::cout << std::endl; */ } catch (const DataFrameError &ex) { std::cout << ex.what() << std::endl; } }