diff options
Diffstat (limited to 'tools/spectral_editor')
| -rw-r--r-- | tools/spectral_editor/dct.js | 101 | ||||
| -rw-r--r-- | tools/spectral_editor/script.js | 14 |
2 files changed, 45 insertions, 70 deletions
diff --git a/tools/spectral_editor/dct.js b/tools/spectral_editor/dct.js index deff8a9..435a7e8 100644 --- a/tools/spectral_editor/dct.js +++ b/tools/spectral_editor/dct.js @@ -1,20 +1,10 @@ const dctSize = 512; // Default DCT size, read from header // --- Utility Functions for Audio Processing --- +// Fast O(N log N) IDCT using FFT // JavaScript equivalent of C++ idct_512 function javascript_idct_512(input) { - const output = new Float32Array(dctSize); - const PI = Math.PI; - const N = dctSize; - - for (let n = 0; n < N; ++n) { - let sum = input[0] / 2.0; - for (let k = 1; k < N; ++k) { - sum += input[k] * Math.cos((PI / N) * k * (n + 0.5)); - } - output[n] = sum * (2.0 / N); - } - return output; + return javascript_idct_512_fft(input); } // Hanning window for smooth audio transitions (JavaScript equivalent) @@ -127,95 +117,90 @@ function fftInverse(real, imag, N) { } } -// DCT-II via FFT using double-and-mirror method (matches C++ dct_fft) -// This is a more robust algorithm that avoids reordering issues +// DCT-II via FFT using reordering method (matches C++ dct_fft) +// Reference: Numerical Recipes Chapter 12.3 function javascript_dct_fft(input, N) { const PI = Math.PI; - // Allocate arrays for 2N-point FFT - const M = 2 * N; - const real = new Float32Array(M); - const imag = new Float32Array(M); + // Allocate arrays for N-point FFT + const real = new Float32Array(N); + const imag = new Float32Array(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 (let i = 0; i < N; i++) { - real[i] = input[i]; - } - for (let 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 (let 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, ... } // imag is already zeros (Float32Array default) - // Apply 2N-point FFT - fftForward(real, imag, M); + // Apply N-point FFT + fftForward(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 const output = new Float32Array(N); for (let k = 0; k < N; k++) { const angle = -PI * k / (2.0 * N); const wr = Math.cos(angle); const wi = Math.sin(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 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 * Math.sqrt(1.0 / N) / 2.0; + output[k] = dct_value * Math.sqrt(1.0 / N); } else { - output[k] = dct_value * Math.sqrt(2.0 / N) / 2.0; + output[k] = dct_value * Math.sqrt(2.0 / N); } } return output; } -// IDCT (Inverse DCT-II) via FFT using double-and-mirror method (matches C++ idct_fft) +// IDCT (DCT-III) via FFT using reordering method (matches C++ idct_fft) +// Reference: Numerical Recipes Chapter 12.3 function javascript_idct_fft(input, N) { const PI = Math.PI; - // Allocate arrays for 2N-point FFT - const M = 2 * N; - const real = new Float32Array(M); - const imag = new Float32Array(M); + // Allocate arrays for N-point FFT + const real = new Float32Array(N); + const imag = new Float32Array(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 needs factor of 2 for AC terms for (let k = 0; k < N; k++) { - const angle = PI * k / (2.0 * N); // Positive for inverse + const angle = PI * k / (2.0 * N); // Positive angle for inverse const wr = Math.cos(angle); const wi = Math.sin(angle); - // Apply inverse normalization - let scaled_input; + // Inverse of DCT-II normalization with correct DCT-III scaling + let scaled; if (k === 0) { - scaled_input = input[k] * Math.sqrt(N) * 2.0; + scaled = input[k] / Math.sqrt(1.0 / N); } else { - scaled_input = input[k] * Math.sqrt(N / 2.0) * 2.0; + // DCT-III needs factor of 2 for AC terms + scaled = input[k] / Math.sqrt(2.0 / N) * 2.0; } - // 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 (let 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 - fftInverse(real, imag, M); + fftInverse(real, imag, N); - // Extract first N samples (real part only, imag should be ~0) + // Unpack: reverse the reordering from DCT + // Even output indices come from first half of FFT output + // Odd output indices come from second half (reversed) const output = new Float32Array(N); - for (let i = 0; i < N; i++) { - output[i] = real[i]; + for (let i = 0; i < N / 2; i++) { + output[2 * i] = real[i]; // Even positions + output[2 * i + 1] = real[N - 1 - i]; // Odd positions (reversed) } return output; diff --git a/tools/spectral_editor/script.js b/tools/spectral_editor/script.js index 48b0661..4053aef 100644 --- a/tools/spectral_editor/script.js +++ b/tools/spectral_editor/script.js @@ -378,19 +378,9 @@ function audioToSpectrogram(audioData) { } // Forward DCT (not in dct.js, add here) +// Fast O(N log N) DCT using FFT (delegates to dct.js implementation) function javascript_dct_512(input) { - const output = new Float32Array(DCT_SIZE); - const PI = Math.PI; - const N = DCT_SIZE; - - for (let k = 0; k < N; k++) { - let sum = 0; - for (let n = 0; n < N; n++) { - sum += input[n] * Math.cos((PI / N) * k * (n + 0.5)); - } - output[k] = sum * (k === 0 ? Math.sqrt(1 / N) : Math.sqrt(2 / N)); - } - return output; + return javascript_dct_512_fft(input); } function onReferenceLoaded(fileName) { |
