From 45e2e7ef67825d0428963114ffc61bc95d58743a Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Sat, 4 Jun 2016 14:46:28 +0100 Subject: [PATCH] spectrogram: Pull some constants out of the loop --- spectrogramplot.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spectrogramplot.cpp b/spectrogramplot.cpp index fb947f9..291c3fc 100644 --- a/spectrogramplot.cpp +++ b/spectrogramplot.cpp @@ -142,13 +142,15 @@ void SpectrogramPlot::getLine(float *dest, off_t sample) } fft->process(buffer.get(), buffer.get()); + const float invFFTSize = 1.0f / fftSize; + const float dbMult = 10.0f / log2f(10.0f); for (int i = 0; i < fftSize; i++) { // Start from the middle of the FFTW array and wrap // to rearrange the data int k = (i + fftSize / 2) & (fftSize - 1); - auto s = buffer[k] / (float)fftSize; + auto s = buffer[k] * invFFTSize; float mag = sqrt(s.real() * s.real() + s.imag() * s.imag()); - float magdb = 10 * log2f(mag) / log2f(10); + float magdb = log2f(mag) * dbMult; *dest = magdb; dest++; }