Skip to content

Commit

Permalink
Don't make a float version of complex_fft
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Oct 25, 2023
1 parent 62e0d91 commit 5f81366
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
29 changes: 12 additions & 17 deletions common/alcomplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace {

using ushort = unsigned short;
using ushort2 = std::pair<ushort,ushort>;
using complex_d = std::complex<double>;

constexpr size_t BitReverseCounter(size_t log2_size) noexcept
{
Expand Down Expand Up @@ -110,9 +111,7 @@ constexpr std::array<std::complex<T>,gBitReverses.size()-1> gArgAngle{{

} // namespace

template<typename Real>
std::enable_if_t<std::is_floating_point<Real>::value>
complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t<Real> sign)
void complex_fft(const al::span<std::complex<double>> buffer, const double sign)
{
const size_t fftsize{buffer.size()};
/* Get the number of bits used for indexing. Simplifies bit-reversal and
Expand All @@ -135,18 +134,18 @@ complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t
*/
for(size_t k{0};k < fftsize;k+=step)
{
std::complex<Real> temp{buffer[k+step2]};
const complex_d temp{buffer[k+step2]};
buffer[k+step2] = buffer[k] - temp;
buffer[k] += temp;
}

const std::complex<Real> w{gArgAngle<Real>[i].real(), gArgAngle<Real>[i].imag()*sign};
std::complex<Real> u{w};
const complex_d w{gArgAngle<double>[i].real(), gArgAngle<double>[i].imag()*sign};
complex_d u{w};
for(size_t j{1};j < step2;j++)
{
for(size_t k{j};k < fftsize;k+=step)
{
std::complex<Real> temp{buffer[k+step2] * u};
const complex_d temp{buffer[k+step2] * u};
buffer[k+step2] = buffer[k] - temp;
buffer[k] += temp;
}
Expand All @@ -170,26 +169,26 @@ complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t
std::swap(buffer[idx], buffer[revidx]);
}

const Real pi{al::numbers::pi_v<Real> * sign};
const double pi{al::numbers::pi * sign};
for(size_t i{0};i < log2_size;++i)
{
const size_t step2{1_uz << i};
const size_t step{2_uz << i};
for(size_t k{0};k < fftsize;k+=step)
{
std::complex<Real> temp{buffer[k+step2]};
const complex_d temp{buffer[k+step2]};
buffer[k+step2] = buffer[k] - temp;
buffer[k] += temp;
}

const Real arg{pi / static_cast<Real>(step2)};
const std::complex<Real> w{std::polar(Real{1}, arg)};
std::complex<Real> u{w};
const double arg{pi / static_cast<double>(step2)};
const complex_d w{std::polar(1.0, arg)};
complex_d u{w};
for(size_t j{1};j < step2;j++)
{
for(size_t k{j};k < fftsize;k+=step)
{
std::complex<Real> temp{buffer[k+step2] * u};
const complex_d temp{buffer[k+step2] * u};
buffer[k+step2] = buffer[k] - temp;
buffer[k] += temp;
}
Expand Down Expand Up @@ -218,7 +217,3 @@ void complex_hilbert(const al::span<std::complex<double>> buffer)

forward_fft(buffer);
}


template void complex_fft<>(const al::span<std::complex<float>> buffer, const float sign);
template void complex_fft<>(const al::span<std::complex<double>> buffer, const double sign);
16 changes: 7 additions & 9 deletions common/alcomplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
* FFT and 1 is inverse FFT. Applies the Discrete Fourier Transform (DFT) to
* the data supplied in the buffer, which MUST BE power of two.
*/
template<typename Real>
std::enable_if_t<std::is_floating_point<Real>::value>
complex_fft(const al::span<std::complex<Real>> buffer, const al::type_identity_t<Real> sign);
void complex_fft(const al::span<std::complex<double>> buffer, const double sign);

/**
* Calculate the frequency-domain response of the time-domain signal in the
* provided buffer, which MUST BE power of two.
*/
template<typename T, size_t N>
void forward_fft(const al::span<T,N> buffer)
{ complex_fft(al::span<T>{buffer}, -1); }
template<size_t N>
void forward_fft(const al::span<std::complex<double>,N> buffer)
{ complex_fft(al::span<std::complex<double>>{buffer}, -1.0); }

/**
* Calculate the time-domain signal of the frequency-domain response in the
* provided buffer, which MUST BE power of two.
*/
template<typename T, size_t N>
void inverse_fft(const al::span<T,N> buffer)
{ complex_fft(al::span<T>{buffer}, 1); }
template<size_t N>
void inverse_fft(const al::span<std::complex<double>,N> buffer)
{ complex_fft(al::span<std::complex<double>>{buffer}, +1.0); }

/**
* Calculate the complex helical sequence (discrete-time analytical signal) of
Expand Down

0 comments on commit 5f81366

Please sign in to comment.