|
| 1 | +// DCT-IV / half-shift (BDCT) benchmark and validation. |
| 2 | +// |
| 3 | +// Validates the paired radix-4 half-shift kernel and the DCT-IV built on it, |
| 4 | +// in both double and single precision, without any long double (there is no |
| 5 | +// extended precision on the NEON target). Correctness is checked three ways: |
| 6 | +// |
| 7 | +// 1. analytic exact: half-shift of a unit impulse has |H[k]| == 1 with a |
| 8 | +// closed-form phase, and DCT-IV of a single basis vector is a clean spike; |
| 9 | +// 2. independent cross-check against a direct same-precision DFT; |
| 10 | +// 3. exact inverse roundtrip. |
| 11 | +// |
| 12 | +// Build (from repo root): |
| 13 | +// c++ -O3 -std=c++17 -Iinclude examples/dctiv_benchmark.cpp -lm -o build/examples/dctiv_benchmark |
| 14 | + |
| 15 | +#include "../src/detail/bdct_kernel.hpp" |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#include <cmath> |
| 19 | +#include <cstdio> |
| 20 | +#include <cstdlib> |
| 21 | +#include <random> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +constexpr double PI = 3.141592653589793238462643383279502884; |
| 27 | + |
| 28 | +template <class RT> |
| 29 | +std::vector<RT> make_input(int n, std::mt19937_64& rng) { |
| 30 | + std::vector<RT> x(static_cast<std::size_t>(n)); |
| 31 | + std::uniform_real_distribution<double> dist(-1.0, 1.0); |
| 32 | + for (int i = 0; i < n; ++i) x[static_cast<std::size_t>(i)] = static_cast<RT>(dist(rng)); |
| 33 | + return x; |
| 34 | +} |
| 35 | + |
| 36 | +// --- correctness for one precision ----------------------------------------- |
| 37 | + |
| 38 | +template <class HS, class DCT, class CT, class RT> |
| 39 | +void run_correctness(int N, std::mt19937_64& rng, const char* tag) { |
| 40 | + HS hs(N); |
| 41 | + DCT dct(N); |
| 42 | + const int bins = N / 2; |
| 43 | + |
| 44 | + // (1a) half-shift of a unit impulse at n0: H[k] = exp(-2*pi*i*(k+1/2)*n0/N). |
| 45 | + std::vector<RT> imp(static_cast<std::size_t>(N), static_cast<RT>(0)); |
| 46 | + const int n0 = 1; |
| 47 | + imp[static_cast<std::size_t>(n0)] = static_cast<RT>(1); |
| 48 | + std::vector<CT> Himp(static_cast<std::size_t>(bins)); |
| 49 | + hs.forward(imp.data(), Himp.data()); |
| 50 | + double imp_err = 0.0; |
| 51 | + for (int k = 0; k < bins; ++k) { |
| 52 | + const double ang = -2.0 * PI * (static_cast<double>(k) + 0.5) * |
| 53 | + static_cast<double>(n0) / static_cast<double>(N); |
| 54 | + const double dr = static_cast<double>(Himp[static_cast<std::size_t>(k)].re) - std::cos(ang); |
| 55 | + const double di = static_cast<double>(Himp[static_cast<std::size_t>(k)].im) - std::sin(ang); |
| 56 | + imp_err = std::max(imp_err, std::sqrt(dr * dr + di * di)); |
| 57 | + } |
| 58 | + |
| 59 | + // (1b) DCT-IV of basis vector m: x[n]=cos(pi*(m+1/2)*(n+1/2)/N) -> (N/2)*delta_km. |
| 60 | + const int m = (N >= 8) ? 3 : 1; |
| 61 | + std::vector<RT> basis(static_cast<std::size_t>(N)); |
| 62 | + for (int n = 0; n < N; ++n) { |
| 63 | + basis[static_cast<std::size_t>(n)] = static_cast<RT>( |
| 64 | + std::cos(PI * (m + 0.5) * (n + 0.5) / N)); |
| 65 | + } |
| 66 | + std::vector<RT> c(static_cast<std::size_t>(N)); |
| 67 | + dct.forward(basis.data(), c.data()); |
| 68 | + double dct_err = 0.0; |
| 69 | + for (int k = 0; k < N; ++k) { |
| 70 | + const double expect = (k == m) ? 0.5 * N : 0.0; |
| 71 | + dct_err = std::max(dct_err, std::fabs(static_cast<double>(c[static_cast<std::size_t>(k)]) - expect)); |
| 72 | + } |
| 73 | + |
| 74 | + // (2) random signal vs direct same-precision DFT, and (3) roundtrip. |
| 75 | + std::vector<RT> x = make_input<RT>(N, rng); |
| 76 | + std::vector<CT> H(static_cast<std::size_t>(bins)); |
| 77 | + hs.forward(x.data(), H.data()); |
| 78 | + double xcheck = 0.0, ref_mag = 0.0; |
| 79 | + for (int k = 0; k < bins; ++k) { |
| 80 | + double sr = 0.0, si = 0.0; |
| 81 | + for (int n = 0; n < N; ++n) { |
| 82 | + const double ang = -2.0 * PI * (k + 0.5) * n / N; |
| 83 | + sr += static_cast<double>(x[static_cast<std::size_t>(n)]) * std::cos(ang); |
| 84 | + si += static_cast<double>(x[static_cast<std::size_t>(n)]) * std::sin(ang); |
| 85 | + } |
| 86 | + const double dr = static_cast<double>(H[static_cast<std::size_t>(k)].re) - sr; |
| 87 | + const double di = static_cast<double>(H[static_cast<std::size_t>(k)].im) - si; |
| 88 | + xcheck = std::max(xcheck, std::sqrt(dr * dr + di * di)); |
| 89 | + ref_mag = std::max(ref_mag, std::sqrt(sr * sr + si * si)); |
| 90 | + } |
| 91 | + |
| 92 | + std::vector<RT> xr(static_cast<std::size_t>(N)); |
| 93 | + hs.inverse(H.data(), xr.data()); |
| 94 | + double round_err = 0.0; |
| 95 | + for (int n = 0; n < N; ++n) { |
| 96 | + round_err = std::max(round_err, |
| 97 | + std::fabs(static_cast<double>(xr[static_cast<std::size_t>(n)] - |
| 98 | + x[static_cast<std::size_t>(n)]))); |
| 99 | + } |
| 100 | + |
| 101 | + std::printf("%-6s N %7d hs_impulse %.3e dctiv_basis %.3e " |
| 102 | + "hs_vs_dft(rel) %.3e roundtrip %.3e\n", |
| 103 | + tag, N, imp_err, dct_err, |
| 104 | + ref_mag > 0 ? xcheck / ref_mag : 0.0, round_err); |
| 105 | +} |
| 106 | + |
| 107 | +// --- timing ---------------------------------------------------------------- |
| 108 | + |
| 109 | +template <class HS, class DCT, class CT, class RT> |
| 110 | +void run_timing(int N, std::mt19937_64& rng, const char* tag) { |
| 111 | + std::vector<RT> x = make_input<RT>(N, rng); |
| 112 | + HS hs(N); |
| 113 | + DCT dct(N); |
| 114 | + std::vector<CT> H(static_cast<std::size_t>(N / 2)); |
| 115 | + std::vector<RT> c(static_cast<std::size_t>(N)); |
| 116 | + |
| 117 | + long iters = 1; |
| 118 | + { |
| 119 | + const double per = static_cast<double>(N) * std::log2(static_cast<double>(N)); |
| 120 | + iters = static_cast<long>(2.0e8 / per); |
| 121 | + if (iters < 1) iters = 1; |
| 122 | + } |
| 123 | + |
| 124 | + using clock = std::chrono::steady_clock; |
| 125 | + hs.forward(x.data(), H.data()); |
| 126 | + dct.forward(x.data(), c.data()); |
| 127 | + |
| 128 | + auto t0 = clock::now(); |
| 129 | + for (long i = 0; i < iters; ++i) hs.forward(x.data(), H.data()); |
| 130 | + auto t1 = clock::now(); |
| 131 | + for (long i = 0; i < iters; ++i) dct.forward(x.data(), c.data()); |
| 132 | + auto t2 = clock::now(); |
| 133 | + |
| 134 | + const double hs_ns = std::chrono::duration<double, std::nano>(t1 - t0).count() / iters; |
| 135 | + const double dct_ns = std::chrono::duration<double, std::nano>(t2 - t1).count() / iters; |
| 136 | + std::printf("%-6s N %7d halfshift %10.1f ns dctiv %10.1f ns (%ld iters)\n", |
| 137 | + tag, N, hs_ns, dct_ns, iters); |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace |
| 141 | + |
| 142 | +int main(int argc, char** argv) { |
| 143 | + std::mt19937_64 rng(12345); |
| 144 | + |
| 145 | + std::printf("backend: %s\n", bruun::simd_backend_name()); |
| 146 | + |
| 147 | + std::printf("\n== correctness (double) ==\n"); |
| 148 | + for (int N = 4; N <= 8192; N <<= 1) |
| 149 | + run_correctness<bdct::half_shift_plan, bdct::dctiv_plan, bdct::complex_t, double>(N, rng, "f64"); |
| 150 | + |
| 151 | + std::printf("\n== correctness (float) ==\n"); |
| 152 | + for (int N = 4; N <= 8192; N <<= 1) |
| 153 | + run_correctness<bdct::half_shift_plan_f32, bdct::dctiv_plan_f32, bdct::complex_f32_t, float>(N, rng, "f32"); |
| 154 | + |
| 155 | + std::printf("\n== timing (double) ==\n"); |
| 156 | + for (int N = 64; N <= (1 << 20); N <<= 1) |
| 157 | + run_timing<bdct::half_shift_plan, bdct::dctiv_plan, bdct::complex_t, double>(N, rng, "f64"); |
| 158 | + |
| 159 | + std::printf("\n== timing (float) ==\n"); |
| 160 | + for (int N = 64; N <= (1 << 20); N <<= 1) |
| 161 | + run_timing<bdct::half_shift_plan_f32, bdct::dctiv_plan_f32, bdct::complex_f32_t, float>(N, rng, "f32"); |
| 162 | + |
| 163 | + (void)argc; |
| 164 | + (void)argv; |
| 165 | + return 0; |
| 166 | +} |
0 commit comments