commit 0543232bc6919838340b5c93e4888e27fc7d9625
parent 7f0867e780cd424b6bf2b3128989da724b112b2c
Author: Matt Demanett <matt@demanett.net>
Date: Mon, 28 Sep 2020 22:07:52 -0400
Analyzers: limit peak readouts to displayed range.
Diffstat:
3 files changed, 16 insertions(+), 26 deletions(-)
diff --git a/src/Ranalyzer.cpp b/src/Ranalyzer.cpp
@@ -293,7 +293,7 @@ struct RanalyzerDisplay : AnalyzerDisplay, ChannelDisplayListener {
displayChannel(2, c2);
}
- void drawHeader(const DrawArgs& args) override {
+ void drawHeader(const DrawArgs& args, float rangeMinHz, float rangeMaxHz) override {
nvgSave(args.vg);
const int textY = -4;
diff --git a/src/analyzer_base.cpp b/src/analyzer_base.cpp
@@ -175,32 +175,22 @@ SpectrumAnalyzer::WindowType AnalyzerCore::window() {
}
}
-float AnalyzerCore::getPeak(int channel) {
+float AnalyzerCore::getPeak(int channel, float minHz, float maxHz) {
assert(channel >= 0 && channel < _nChannels);
+ const float* bins = getBins(channel);
+ const int bandsPerBin = _size / _binsN;
+ const float fWidth = (APP->engine->getSampleRate() / 2.0f) / (float)(_size / bandsPerBin);
float max = 0.0f;
int maxBin = 0;
- const float* bins = getBins(channel);
- for (int bin = 0; bin < _binsN; ++bin) {
- if (bins[bin] > max) {
- max = bins[bin];
- maxBin = bin;
+ int i = std::max(0, (int)(minHz / fWidth));
+ int n = std::min(_binsN, 1 + (int)(maxHz / fWidth));
+ for (; i < n; ++i) {
+ if (bins[i] > max) {
+ max = bins[i];
+ maxBin = i;
}
}
-
- const int bandsPerBin = _size / _binsN;
- const float fWidth = (APP->engine->getSampleRate() / 2.0f) / (float)(_size / bandsPerBin);
-
return (maxBin + 0.5f)*fWidth;
- // ??
- // float sum = 0.0f;
- // float sumWeights = 0.0f;
- // int i = std::max(0, maxBin - 1);
- // int j = std::max(_binsN - 1, maxBin + 1);
- // for (; i <= j; ++i) {
- // sum += bins[i] * fWidth * i;
- // sumWeights += bins[i];
- // }
- // return sum / sumWeights;
}
void AnalyzerCore::stepChannel(int channelIndex, Input& input) {
@@ -467,7 +457,7 @@ void AnalyzerDisplay::draw(const DrawArgs& args) {
nvgSave(args.vg);
nvgScissor(args.vg, _insetAround, _insetAround, _size.x - _insetAround, _size.y - _insetAround);
if (_module) {
- drawHeader(args);
+ drawHeader(args, rangeMinHz, rangeMaxHz);
}
drawYAxis(args, strokeWidth, amplitudePlot);
drawXAxis(args, strokeWidth, frequencyPlot, rangeMinHz, rangeMaxHz);
@@ -516,7 +506,7 @@ void AnalyzerDisplay::drawBackground(const DrawArgs& args) {
nvgRestore(args.vg);
}
-void AnalyzerDisplay::drawHeader(const DrawArgs& args) {
+void AnalyzerDisplay::drawHeader(const DrawArgs& args, float rangeMinHz, float rangeMaxHz) {
nvgSave(args.vg);
const int textY = -4;
@@ -536,7 +526,7 @@ void AnalyzerDisplay::drawHeader(const DrawArgs& args) {
}
for (int i = 0; i < _module->_core._nChannels; ++i) {
if (_module->_core._channels[i]) {
- snprintf(s, sLen, "%c:%7.1f", 'A' + i, _module->_core.getPeak(i));
+ snprintf(s, sLen, "%c:%7.1f", 'A' + i, _module->_core.getPeak(i, rangeMinHz, rangeMaxHz));
drawText(args, s, x, _insetTop + textY, 0.0, &_channelColors[i % channelColorsN]);
}
x += 9 * charPx + spacing;
diff --git a/src/analyzer_base.hpp b/src/analyzer_base.hpp
@@ -122,7 +122,7 @@ struct AnalyzerCore {
assert(i >= 0 && i < _nChannels);
return _currentOutBufs[i];
}
- float getPeak(int channel);
+ float getPeak(int channel, float minHz, float maxHz);
void stepChannel(int channelIndex, Input& input);
void stepChannelSample(int channelIndex, float sample);
};
@@ -267,7 +267,7 @@ struct AnalyzerDisplay : TransparentWidget, AnalyzerTypes {
void channelLabel(int channel, std::string label);
void draw(const DrawArgs& args) override;
void drawBackground(const DrawArgs& args);
- virtual void drawHeader(const DrawArgs& args);
+ virtual void drawHeader(const DrawArgs& args, float rangeMinHz, float rangeMaxHz);
void drawYAxis(const DrawArgs& args, float strokeWidth, AmplitudePlot plot);
void drawXAxis(const DrawArgs& args, float strokeWidth, FrequencyPlot plot, float rangeMinHz, float rangeMaxHz);
void drawXAxisLine(const DrawArgs& args, float hz, float rangeMinHz, float rangeMaxHz);