Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F68061279
test_fft.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Jun 25, 17:28
Size
2 KB
Mime Type
text/x-c
Expires
Thu, Jun 27, 17:28 (2 d)
Engine
blob
Format
Raw Data
Handle
18520733
Attached To
R7571 SP4E-TB-TL-FR
test_fft.cc
View Options
#include <gtest/gtest.h>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include "my_types.hh"
#include "fft.hh"
/*****************************************************************/
TEST(FFT, transform) {
UInt N = 512;
Matrix<complex> m(N);
Real k = 2 * M_PI / N;
for (auto&& entry : index(m)) {
int i = std::get<0>(entry);
int j = std::get<1>(entry);
auto& val = std::get<2>(entry);
val = cos(k * i);
}
Matrix<complex> res = FFT::transform(m);
for (auto&& entry : index(res)) {
int i = std::get<0>(entry);
int j = std::get<1>(entry);
auto& val = std::get<2>(entry);
if (std::abs(val) > 1e-10)
std::cout << i << "," << j << " = " << val << std::endl;
if (i == 1 && j == 0)
ASSERT_NEAR(std::abs(val), N * N / 2, 1e-10);
else if (i == N - 1 && j == 0)
ASSERT_NEAR(std::abs(val), N * N / 2, 1e-10);
else
ASSERT_NEAR(std::abs(val), 0, 1e-10);
}
}
/*****************************************************************/
TEST(FFT, inverse_transform) {
UInt N = 512;
Matrix<complex> m(N);
Real k = 2 * M_PI / N;
for (auto&& entry : index(m)) {
int i = std::get<0>(entry);
int j = std::get<1>(entry);
auto& val = std::get<2>(entry);
if ((i == 1 && j == 0) || (i == N - 1 && j == 0)) {
val = N * N / 2;
} else {
val = 0;
}
}
Matrix<complex> res = FFT::itransform(m);
for (auto&& entry : index(res)) {
int i = std::get<0>(entry);
int j = std::get<1>(entry);
auto& val = std::get<2>(entry);
ASSERT_NEAR(std::abs(val), std::abs(cos(k * i)), 1e-10);
}
}
TEST(FFT, compute_frequencies) {
UInt N = 8;
// Populate reference frequency matrix from numpy generated file
Matrix<std::complex<int>> freqs_ref(N);
std::ifstream is("test_computeFrequencies.txt");
if (is.is_open() == false) {
std::cerr << "cannot open file" << std::endl;
throw;
}
std::string line;
double tmp;
for (int i=0; i < N; ++i) {
getline(is, line);
std::stringstream sstr(line);
for (int j = 0; j < N; ++j) {
sstr >> tmp;
freqs_ref(i, j) = std::complex<int>(tmp, 0);
}
}
is.close();
// Create frequency matrix with our own implementation
Matrix<std::complex<int>> freqs = FFT::computeFrequencies(N);
// Compare the two matrices
for (auto&& entry : index(freqs)) {
int i = std::get<0>(entry);
int j = std::get<1>(entry);
auto& val = std::get<2>(entry);
auto& ref = freqs_ref(i, j);
ASSERT_NEAR(std::abs(val), std::abs(ref), 1e-10);
}
}
Event Timeline
Log In to Comment