11 #include <type_traits> 19 #include <gtest/gtest.h> 22 #include "../jPlot/jplot.h" 25 typename std::enable_if<!std::numeric_limits<T>::is_integer,
bool>::type
26 almost_equal(T x, T y,
int ulp) {
29 return std::abs(x-y) < std::numeric_limits<T>::epsilon() * std::abs(x+y) * ulp
31 || std::abs(x-y) < std::numeric_limits<T>::min();
34 double sqr_sum(
const std::vector< double >& signal ) {
38 for (
const auto& val : signal ) {
45 double sum(
const std::vector< double >& signal ) {
49 for (
const auto& val : signal ) {
56 std::vector< double > build_test_signal( uint N ) {
58 std::vector< double > sin_vect;
59 sin_vect.reserve( N );
61 double N_f =
static_cast<double>( N );
63 for ( uint i = 0; i < N ; i++ ) {
65 double x =
static_cast< double >( i )/N_f;
67 sin_vect.push_back( 0.25*std::sin( (5.0)*(2.0*M_PI)*x ) );
75 void TestJFFT(
const uint signal_size ) {
77 JFFT< std::vector< double > > fft_er(
true );
79 std::vector< double > signal = build_test_signal( signal_size );
81 double time_series_tot_pow = sqr_sum( signal );
83 plot( signal,
"Times Series" );
85 fft_er.SetUp( signal_size );
87 std::vector< double > power_spectrum = fft_er.PowerSpectrum( signal );
89 double power_spectrum_tot_pow = 2.0*sum( power_spectrum );
91 std::for_each(power_spectrum.begin(), power_spectrum.end(), [=](
double &x){std::sqrt(x); });
93 plot( power_spectrum,
"Power Spectrum" );
96 if( !almost_equal( time_series_tot_pow, power_spectrum_tot_pow, 2 ) ) {
98 std::cout <<
"Parseval's theorem has been violated." << std::endl;
100 std::cout <<
"Square sum of time series values: " 101 << time_series_tot_pow
102 <<
" Square sum of power spectrum values: " 103 << power_spectrum_tot_pow
107 std::cout <<
"Power spectrum differs by multiplicative factor of: " 108 << power_spectrum_tot_pow/time_series_tot_pow
113 std::cout <<
"Parseval's theorem holds." << std::endl;
115 std::cout <<
"Square sum of time series values: " 116 << time_series_tot_pow
117 <<
" Square sum of power spectrum values: " 118 << power_spectrum_tot_pow
127 for ( uint i = 2 ; i < std::pow( 2, 12 ); i *= 2 ) {
129 std::cout <<
"Testing JFFT for signal of size " 139 #endif // TEST_JFFT_H