Skip to content

Commit

Permalink
Eliminate unneeded computation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed May 6, 2021
1 parent 08a2d5e commit 42d2594
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions sources/dsp/MultirateSTFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ void MultirateSTFT<Rates>::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);
}

//
Expand Down
8 changes: 5 additions & 3 deletions sources/dsp/STFT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "FFTPlanner.h"
#include "AnalyzerDefs.h"
#include <algorithm>
#include <unordered_map>
#include <mutex>
#include <cmath>

void STFT::configure(const Configuration &config)
Expand Down Expand Up @@ -31,8 +29,12 @@ void STFT::processNewBlock(float *input)
std::complex<float> *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;
Expand Down
19 changes: 18 additions & 1 deletion sources/dsp/SpectralAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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]);
}
7 changes: 7 additions & 0 deletions sources/dsp/SpectralAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -72,19 +74,24 @@ class SteppingAnalyzer : public BasicAnalyzer {
uint32_t _ringIndex {};
std::vector<float> _ring;

// range
uint32_t _binRange[2] = { 0u, ~0u };

// temporary
std::vector<float> _input;

// step-by-step smoother
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<ARFollower> _ar;
uint32_t _stepSize = 0;
uint32_t _binRange[2] = { 0u, ~0u };
};
Smoother _smoother;
};

0 comments on commit 42d2594

Please sign in to comment.