From 42d259462a4cadab0db3751c08a741e7e762523a Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 6 May 2021 22:21:03 +0200 Subject: [PATCH] Eliminate unneeded computation --- sources/dsp/MultirateSTFT.cpp | 5 +++++ sources/dsp/STFT.cpp | 8 +++++--- sources/dsp/SpectralAnalyzer.cpp | 19 ++++++++++++++++++- sources/dsp/SpectralAnalyzer.h | 7 +++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/sources/dsp/MultirateSTFT.cpp b/sources/dsp/MultirateSTFT.cpp index 3a47d2d..06fce31 100644 --- a/sources/dsp/MultirateSTFT.cpp +++ b/sources/dsp/MultirateSTFT.cpp @@ -20,6 +20,11 @@ void MultirateSTFT::configure(const Configuration &config) rateConfig[r].stepSize = config.stepSize / (1u << r); rateConfig[r].sampleRate = config.sampleRate / (1u << r); fStft[r].configure(rateConfig[r]); + + if (r == Rates - 1) + fStft[r].configureBinRange(0, specSize); + else + fStft[r].configureBinRange(specSize / 2, specSize); } // diff --git a/sources/dsp/STFT.cpp b/sources/dsp/STFT.cpp index 0aa8ea9..eb4515f 100644 --- a/sources/dsp/STFT.cpp +++ b/sources/dsp/STFT.cpp @@ -2,8 +2,6 @@ #include "FFTPlanner.h" #include "AnalyzerDefs.h" #include -#include -#include #include void STFT::configure(const Configuration &config) @@ -31,8 +29,12 @@ void STFT::processNewBlock(float *input) std::complex *cpx = _cpx.data(); fftwf_execute_dft_r2c(plan, input, (fftwf_complex *)cpx); + const uint32_t *binRange = getBinRange(); + uint32_t start = binRange[0]; + uint32_t end = std::min(binRange[1], numBins); + float *mag = getMagnitudes(); - for (uint32_t i = 0; i < numBins; ++i) { + for (uint32_t i = start; i < end; ++i) { double linear = std::abs(cpx[i]) * (2.0f / windowSize); double decibel = 20.0 * std::log10(std::max(kStftFloorMagnitude, linear)); mag[i] = decibel; diff --git a/sources/dsp/SpectralAnalyzer.cpp b/sources/dsp/SpectralAnalyzer.cpp index baa0527..6adfe2f 100644 --- a/sources/dsp/SpectralAnalyzer.cpp +++ b/sources/dsp/SpectralAnalyzer.cpp @@ -36,6 +36,13 @@ void SteppingAnalyzer::configureStepping(uint32_t numBins, const Configuration & _smoother.configure(numBins, config.stepSize, config.attackTime, config.releaseTime, config.sampleRate); } +void SteppingAnalyzer::configureBinRange(uint32_t start, uint32_t end) +{ + _binRange[0] = start; + _binRange[1] = end; + _smoother.configureBinRange(start, end); +} + void SteppingAnalyzer::setAttackAndRelease(float attack, float release) { _smoother.setAttackAndRelease(attack, release); @@ -92,6 +99,12 @@ void SteppingAnalyzer::Smoother::configure(uint32_t numBins, uint32_t stepSize, setAttackAndRelease(attackTime, releaseTime); } +void SteppingAnalyzer::Smoother::configureBinRange(uint32_t start, uint32_t end) +{ + _binRange[0] = start; + _binRange[1] = end; +} + void SteppingAnalyzer::Smoother::setAttackAndRelease(float attack, float release) { ARFollower *ar = _ar.data(); @@ -115,6 +128,10 @@ void SteppingAnalyzer::Smoother::process(float *stepData) { ARFollower *ar = _ar.data(); uint32_t numBins = (uint32_t)_ar.size(); - for (uint32_t i = 0; i < numBins; ++i) + + uint32_t start = _binRange[0]; + uint32_t end = std::min(_binRange[1], numBins); + + for (uint32_t i = start; i < end; ++i) stepData[i] = ar[i].compute(stepData[i]); } diff --git a/sources/dsp/SpectralAnalyzer.h b/sources/dsp/SpectralAnalyzer.h index ab247de..b33ad8e 100644 --- a/sources/dsp/SpectralAnalyzer.h +++ b/sources/dsp/SpectralAnalyzer.h @@ -47,11 +47,13 @@ class SteppingAnalyzer : public BasicAnalyzer { public: uint32_t getWindowSize() const { return _windowSize; } + const uint32_t *getBinRange() const { return _binRange; } protected: void configureStepping(uint32_t numBins, const Configuration &config); public: + virtual void configureBinRange(uint32_t start, uint32_t end); virtual void setAttackAndRelease(float attack, float release) override; virtual void clear() override; virtual void process(const float *input, uint32_t numFrames) override; @@ -72,6 +74,9 @@ class SteppingAnalyzer : public BasicAnalyzer { uint32_t _ringIndex {}; std::vector _ring; + // range + uint32_t _binRange[2] = { 0u, ~0u }; + // temporary std::vector _input; @@ -79,12 +84,14 @@ class SteppingAnalyzer : public BasicAnalyzer { class Smoother { public: void configure(uint32_t numBins, uint32_t stepSize, double attackTime, double releaseTime, double sampleRate); + void configureBinRange(uint32_t start, uint32_t end); void setAttackAndRelease(float attack, float release); void clear(); void process(float *stepData); private: std::vector _ar; uint32_t _stepSize = 0; + uint32_t _binRange[2] = { 0u, ~0u }; }; Smoother _smoother; };