From 9a883f50462f22474601204b0052ca9ef4fce676 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 13 Sep 2020 00:58:05 -0700 Subject: [PATCH] Partly simplify FFT bit-reversal This can almost certainly be improved further, as less than half of the indices really need their reversed bit-pattern calculated and elements swapped (any symetrical bit pattern would just swap with itself, and indices whose reversed bit-pattern has already been traversed is already swapped). It may also prove beneficial to provide the base-2 log of the fft buffer size (number of bits to represent the indices), as that could help make the reversal more efficient with a known bit/loop count. --- common/alcomplex.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/common/alcomplex.cpp b/common/alcomplex.cpp index e5cbe8a0..0af9fc98 100644 --- a/common/alcomplex.cpp +++ b/common/alcomplex.cpp @@ -18,12 +18,8 @@ void complex_fft(const al::span> buffer, const double sign) for(size_t i{1u};i < fftsize-1;i++) { size_t j{0u}; - for(size_t mask{1u};mask < fftsize;mask <<= 1) - { - if((i&mask) != 0) - j++; - j <<= 1; - } + for(size_t imask{i + fftsize};imask;imask >>= 1) + j = (j<<1) + (imask&1); j >>= 1; if(i < j) @@ -35,9 +31,9 @@ void complex_fft(const al::span> buffer, const double sign) for(size_t i{1u};i < fftsize;i<<=1, step<<=1) { const size_t step2{step >> 1}; - double arg{al::MathDefs::Pi() / static_cast(step2)}; + const double arg{al::MathDefs::Pi() / static_cast(step2)}; - std::complex w{std::cos(arg), std::sin(arg)*sign}; + const std::complex w{std::cos(arg), std::sin(arg)*sign}; std::complex u{1.0, 0.0}; for(size_t j{0};j < step2;j++) {