From 92e2d12b8936550444bed5f85eb33fed7c259740 Mon Sep 17 00:00:00 2001 From: zsliu98 Date: Fri, 27 Sep 2024 15:23:50 -0400 Subject: [PATCH] fix(multiple fft analyzer): fix incorrect array size --- .../fft_analyzer/multiple_fft_analyzer.hpp | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/source/dsp/fft_analyzer/multiple_fft_analyzer.hpp b/source/dsp/fft_analyzer/multiple_fft_analyzer.hpp index 0b4ccad8..825fd096 100644 --- a/source/dsp/fft_analyzer/multiple_fft_analyzer.hpp +++ b/source/dsp/fft_analyzer/multiple_fft_analyzer.hpp @@ -23,6 +23,7 @@ namespace zlFFT { class MultipleFFTAnalyzer final { public: explicit MultipleFFTAnalyzer() { + static_assert(PointNum % 2 == 1); tiltSlope.store(zlState::ffTTilt::slopes[static_cast(zlState::ffTTilt::defaultI)]); interplotFreqs[0] = minFreq; for (size_t i = 1; i < PointNum; ++i) { @@ -71,7 +72,7 @@ namespace zlFFT { decayRate.store(zlState::ffTSpeed::speeds[static_cast(zlState::ffTSpeed::defaultI)]); const auto currentDeltaT = deltaT.load(); - smoothedFreqs[0] = currentDeltaT * (-.5f); + smoothedFreqs[0] = 0.f; for (size_t idx = 1; idx < smoothedFreqs.size(); ++idx) { smoothedFreqs[idx] = smoothedFreqs[idx - 1] + currentDeltaT; } @@ -170,24 +171,20 @@ namespace zlFFT { fft->performFrequencyOnlyForwardTransform(fftBuffer.data()); const auto decay = actualDecayRate[i].load(); auto &smoothedDB{smoothedDBs[i]}; + const auto ampScale = 2.f / static_cast(fftBuffer.size()); for (size_t j = 0; j < smoothedFreqs.size(); ++j) { - const auto currentDB = juce::Decibels::gainToDecibels( - 2.f * fftBuffer[j] / static_cast(fftBuffer.size()), -240.f); - smoothedDB[j + 1] = currentDB < smoothedDB[j + 1] - ? smoothedDB[j + 1] * decay + currentDB * (1 - decay) - : currentDB; + const auto currentDB = juce::Decibels::gainToDecibels(ampScale * fftBuffer[j], -240.f); + smoothedDB[j] = currentDB < smoothedDB[j] + ? smoothedDB[j] * decay + currentDB * (1 - decay) + : currentDB; } - smoothedDB[0] = smoothedDB[1] * 1.1f; - smoothedDB[smoothedDB.size() - 1] = smoothedDB[smoothedDB.size() - 2] * 1.1f; std::vector x{smoothedFreqs.begin(), smoothedFreqs.end()}; std::vector y{smoothedDB.begin(), smoothedDB.end()}; using boost::math::interpolators::makima; - const auto spline = makima(std::move(x), std::move(y), 1.f, -1.f); + const auto spline = makima(std::move(x), std::move(y), 0.f, 0.f); - preInterplotDBs[i].front() = spline(minFreq); - preInterplotDBs[i].back() = spline(maxFreq); - for (size_t j = 1; j < preInterplotDBs[i].size() - 1; ++j) { + for (size_t j = 0; j < preInterplotDBs[i].size(); ++j) { preInterplotDBs[i][j] = spline(interplotFreqs[j * 2]); } } @@ -277,8 +274,8 @@ namespace zlFFT { juce::AbstractFifo abstractFIFO{1}; std::vector fftBuffer; - std::array smoothedFreqs{}; - std::array, FFTNum> smoothedDBs{}; + std::array smoothedFreqs{}; + std::array, FFTNum> smoothedDBs{}; std::array interplotFreqs{}; std::array, FFTNum> preInterplotDBs{}; std::array, PointNum>, FFTNum> interplotDBs{};