Skip to content

Commit

Permalink
FFT regression testing
Browse files Browse the repository at this point in the history
  • Loading branch information
djowel committed Aug 30, 2024
1 parent 6dee9ee commit 0d25623
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions test/fft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Distributed under the MIT License [ https://opensource.org/licenses/MIT ]
=============================================================================*/
#define CATCH_CONFIG_MAIN
#include <infra/catch.hpp>

#include <q/fft/fft.hpp>
#include <q_io/audio_file.hpp>
#include <array>
Expand All @@ -12,7 +15,7 @@ using namespace q::literals;

constexpr auto sps = 48000;

int main()
TEST_CASE("TEST_FFT")
{
constexpr std::size_t p = 7;
constexpr std::size_t n = 1<<p;
Expand All @@ -23,7 +26,7 @@ int main()
////////////////////////////////////////////////////////////////////////////
// sample data. A composite signal by summing three sine waves with
// different frequencies and amplitudes.
std::array<float, _2n> data;
std::array<double, _2n> data;
for (int i = 0; i < _2n; ++i)
{
data[i] =
Expand All @@ -36,20 +39,40 @@ int main()
out[pos] = data[i];
}

// Make a copy of the data
auto copy = data;

////////////////////////////////////////////////////////////////////////////
// compute FFT
q::fft<n>(data.data());
auto norm =
[](double val)
{
return val / (n/2);
};

for (int i = 0; i < _2n; ++i)
{
auto pos = i * n_channels;
auto ch2 = pos+1;
auto ch3 = pos+2;

out[ch2] = data[i] / (n/2);
out[ch3] = data[i+1] / (n/2);
out[ch2] = norm(data[i]);
out[ch3] = norm(data[i+1]);
}

REQUIRE_THAT(norm(data[0]),
Catch::Matchers::WithinAbs(0, 1e-15));

REQUIRE_THAT(norm(data[10*2]),
Catch::Matchers::WithinRel(0.388, 0.01));

REQUIRE_THAT(norm(data[20*2]),
Catch::Matchers::WithinRel(0.44, 0.01));

REQUIRE_THAT(norm(data[30*2]),
Catch::Matchers::WithinRel(0.074, 0.01));

////////////////////////////////////////////////////////////////////////////
// compute Inverse FFT
q::ifft<n>(data.data());
Expand All @@ -62,13 +85,17 @@ int main()
out[ch4] = data[i];
}

for (size_t i = 0; i < data.size(); ++i)
Catch::Matchers::WithinAbs(
std::abs(data[i] - copy[i]),
std::numeric_limits<double>::epsilon()
);

////////////////////////////////////////////////////////////////////////////
// Write to a wav file

q::wav_writer wav(
"results/fft.wav", n_channels, sps
);
wav.write(out);

return 0;
}

0 comments on commit 0d25623

Please sign in to comment.