diff options
Diffstat (limited to 'src/audio/fft.cc')
| -rw-r--r-- | src/audio/fft.cc | 91 |
1 files changed, 42 insertions, 49 deletions
diff --git a/src/audio/fft.cc b/src/audio/fft.cc index 25477b9..3f8e706 100644 --- a/src/audio/fft.cc +++ b/src/audio/fft.cc @@ -97,47 +97,42 @@ void fft_inverse(float* real, float* imag, size_t N) { } } -// DCT-II via FFT using double-and-mirror method -// This is a more robust algorithm that avoids reordering issues -// Reference: Numerical Recipes, Press et al. +// DCT-II via FFT using reordering method +// Reference: Numerical Recipes Chapter 12.3, Press et al. void dct_fft(const float* input, float* output, size_t N) { const float PI = 3.14159265358979323846f; - // Allocate temporary arrays for 2N-point FFT - const size_t M = 2 * N; - float* real = new float[M]; - float* imag = new float[M]; + // Allocate temporary arrays for N-point FFT + float* real = new float[N]; + float* imag = new float[N]; - // Pack input: [x[0], x[1], ..., x[N-1], x[N-1], x[N-2], ..., x[1]] - // This creates even symmetry for real-valued DCT - for (size_t i = 0; i < N; i++) { - real[i] = input[i]; - } - for (size_t i = 0; i < N; i++) { - real[N + i] = input[N - 1 - i]; + // Reorder input: even indices first, then odd indices reversed + // [x[0], x[2], x[4], ...] followed by [x[N-1], x[N-3], x[N-5], ...] + for (size_t i = 0; i < N / 2; i++) { + real[i] = input[2 * i]; // Even indices: 0, 2, 4, ... + real[N - 1 - i] = input[2 * i + 1]; // Odd indices reversed: N-1, N-3, ... } - memset(imag, 0, M * sizeof(float)); + memset(imag, 0, N * sizeof(float)); - // Apply 2N-point FFT - fft_forward(real, imag, M); + // Apply N-point FFT + fft_forward(real, imag, N); - // Extract DCT coefficients + // Extract DCT coefficients with phase correction // DCT[k] = Re{FFT[k] * exp(-j*pi*k/(2*N))} * normalization - // Note: Need to divide by 2 because we doubled the signal length for (size_t k = 0; k < N; k++) { const float angle = -PI * k / (2.0f * N); const float wr = cosf(angle); const float wi = sinf(angle); - // Complex multiplication: (real + j*imag) * (wr + j*wi) + // Complex multiplication: (real[k] + j*imag[k]) * (wr + j*wi) // Real part: real*wr - imag*wi const float dct_value = real[k] * wr - imag[k] * wi; - // Apply DCT-II normalization (divide by 2 for double-length FFT) + // Apply DCT-II normalization if (k == 0) { - output[k] = dct_value * sqrtf(1.0f / N) / 2.0f; + output[k] = dct_value * sqrtf(1.0f / N); } else { - output[k] = dct_value * sqrtf(2.0f / N) / 2.0f; + output[k] = dct_value * sqrtf(2.0f / N); } } @@ -145,48 +140,46 @@ void dct_fft(const float* input, float* output, size_t N) { delete[] imag; } -// IDCT (Inverse DCT-II) via FFT using double-and-mirror method -// This is the inverse of the DCT-II (used for synthesis) +// IDCT (DCT-III) via FFT - inverse of the DCT-II reordering method +// Reference: Numerical Recipes Chapter 12.3, Press et al. void idct_fft(const float* input, float* output, size_t N) { const float PI = 3.14159265358979323846f; - // Allocate temporary arrays for 2N-point FFT - const size_t M = 2 * N; - float* real = new float[M]; - float* imag = new float[M]; + // Allocate temporary arrays for N-point FFT + float* real = new float[N]; + float* imag = new float[N]; - // Prepare FFT input from DCT coefficients - // IDCT = Re{IFFT[DCT * exp(j*pi*k/(2*N))]} * 2 + // Prepare FFT input with inverse phase correction + // FFT[k] = DCT[k] * exp(+j*pi*k/(2*N)) / normalization + // Note: DCT-III (inverse of DCT-II) requires factor of 2 for AC terms for (size_t k = 0; k < N; k++) { - const float angle = PI * k / (2.0f * N); // Positive for inverse + const float angle = PI * k / (2.0f * N); // Positive angle for inverse const float wr = cosf(angle); const float wi = sinf(angle); - // Apply inverse normalization - float scaled_input; + // Inverse of DCT-II normalization with correct DCT-III scaling + float scaled; if (k == 0) { - scaled_input = input[k] * sqrtf(N) * 2.0f; + scaled = input[k] / sqrtf(1.0f / N); } else { - scaled_input = input[k] * sqrtf(N / 2.0f) * 2.0f; + // DCT-III needs factor of 2 for AC terms + scaled = input[k] / sqrtf(2.0f / N) * 2.0f; } - // Complex multiplication: DCT[k] * exp(j*theta) - real[k] = scaled_input * wr; - imag[k] = scaled_input * wi; - } - - // Fill second half with conjugate symmetry (for real output) - for (size_t k = 1; k < N; k++) { - real[M - k] = real[k]; - imag[M - k] = -imag[k]; + // Complex multiplication: scaled * (wr + j*wi) + real[k] = scaled * wr; + imag[k] = scaled * wi; } // Apply inverse FFT - fft_inverse(real, imag, M); + fft_inverse(real, imag, N); - // Extract first N samples (real part only, imag should be ~0) - for (size_t i = 0; i < N; i++) { - output[i] = real[i]; + // Unpack: reverse the reordering from DCT + // Even output indices come from first half of FFT output + // Odd output indices come from second half (reversed) + for (size_t i = 0; i < N / 2; i++) { + output[2 * i] = real[i]; // Even positions + output[2 * i + 1] = real[N - 1 - i]; // Odd positions (reversed) } delete[] real; |
