commit no. 5
This commit is contained in:
@@ -85,15 +85,13 @@ void FFTProcessor::processComplex(const float* inputIQ,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience overloads (no complex output).
|
// Convenience overloads (no complex output) — reuse scratch buffer.
|
||||||
void FFTProcessor::processReal(const float* input, std::vector<float>& outputDB) {
|
void FFTProcessor::processReal(const float* input, std::vector<float>& outputDB) {
|
||||||
std::vector<std::complex<float>> dummy;
|
processReal(input, outputDB, scratchCplx_);
|
||||||
processReal(input, outputDB, dummy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FFTProcessor::processComplex(const float* inputIQ, std::vector<float>& outputDB) {
|
void FFTProcessor::processComplex(const float* inputIQ, std::vector<float>& outputDB) {
|
||||||
std::vector<std::complex<float>> dummy;
|
processComplex(inputIQ, outputDB, scratchCplx_);
|
||||||
processComplex(inputIQ, outputDB, dummy);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
@@ -50,6 +50,9 @@ private:
|
|||||||
fftwf_plan cplxPlan_ = nullptr;
|
fftwf_plan cplxPlan_ = nullptr;
|
||||||
|
|
||||||
void destroyPlans();
|
void destroyPlans();
|
||||||
|
|
||||||
|
// Scratch buffer for convenience overloads (avoids per-call allocation).
|
||||||
|
std::vector<std::complex<float>> scratchCplx_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ void SpectrumAnalyzer::configure(const AnalyzerSettings& settings) {
|
|||||||
hopSize_ = static_cast<size_t>(settings_.fftSize * (1.0f - settings_.overlap));
|
hopSize_ = static_cast<size_t>(settings_.fftSize * (1.0f - settings_.overlap));
|
||||||
if (hopSize_ < 1) hopSize_ = 1;
|
if (hopSize_ < 1) hopSize_ = 1;
|
||||||
|
|
||||||
|
// Cache window gain correction (avoid recomputing per block).
|
||||||
|
windowCorrection_ = -20.0f * std::log10(windowGain_ > 0 ? windowGain_ : 1.0f);
|
||||||
|
|
||||||
if (sizeChanged) {
|
if (sizeChanged) {
|
||||||
accumBuf_.assign(settings_.fftSize * inCh, 0.0f);
|
accumBuf_.assign(settings_.fftSize * inCh, 0.0f);
|
||||||
accumPos_ = 0;
|
accumPos_ = 0;
|
||||||
@@ -37,6 +40,13 @@ void SpectrumAnalyzer::configure(const AnalyzerSettings& settings) {
|
|||||||
channelComplex_.assign(nSpec, std::vector<std::complex<float>>(specSz, {0,0}));
|
channelComplex_.assign(nSpec, std::vector<std::complex<float>>(specSz, {0,0}));
|
||||||
channelWaterfalls_.assign(nSpec, {});
|
channelWaterfalls_.assign(nSpec, {});
|
||||||
|
|
||||||
|
// Pre-allocate scratch buffers.
|
||||||
|
if (settings_.isIQ) {
|
||||||
|
windowedBuf_.resize(settings_.fftSize * 2);
|
||||||
|
} else {
|
||||||
|
chanBuf_.resize(settings_.fftSize);
|
||||||
|
}
|
||||||
|
|
||||||
newSpectrumReady_ = false;
|
newSpectrumReady_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,38 +84,27 @@ void SpectrumAnalyzer::processBlock() {
|
|||||||
int N = settings_.fftSize;
|
int N = settings_.fftSize;
|
||||||
int inCh = settings_.inputChannels();
|
int inCh = settings_.inputChannels();
|
||||||
int nSpec = static_cast<int>(channelSpectra_.size());
|
int nSpec = static_cast<int>(channelSpectra_.size());
|
||||||
int specSz = fft_.spectrumSize();
|
|
||||||
|
|
||||||
std::vector<std::vector<float>> tempDBs(nSpec);
|
|
||||||
std::vector<std::vector<std::complex<float>>> tempCplx(nSpec);
|
|
||||||
|
|
||||||
if (settings_.isIQ) {
|
if (settings_.isIQ) {
|
||||||
std::vector<float> windowed(N * 2);
|
|
||||||
for (int i = 0; i < N; ++i) {
|
for (int i = 0; i < N; ++i) {
|
||||||
windowed[2 * i] = accumBuf_[2 * i] * window_[i];
|
windowedBuf_[2 * i] = accumBuf_[2 * i] * window_[i];
|
||||||
windowed[2 * i + 1] = accumBuf_[2 * i + 1] * window_[i];
|
windowedBuf_[2 * i + 1] = accumBuf_[2 * i + 1] * window_[i];
|
||||||
}
|
}
|
||||||
fft_.processComplex(windowed.data(), tempDBs[0], tempCplx[0]);
|
fft_.processComplex(windowedBuf_.data(), channelSpectra_[0], channelComplex_[0]);
|
||||||
} else {
|
} else {
|
||||||
std::vector<float> chanBuf(N);
|
|
||||||
for (int ch = 0; ch < nSpec; ++ch) {
|
for (int ch = 0; ch < nSpec; ++ch) {
|
||||||
for (int i = 0; i < N; ++i)
|
for (int i = 0; i < N; ++i)
|
||||||
chanBuf[i] = accumBuf_[i * inCh + ch];
|
chanBuf_[i] = accumBuf_[i * inCh + ch];
|
||||||
WindowFunctions::apply(window_, chanBuf.data(), N);
|
WindowFunctions::apply(window_, chanBuf_.data(), N);
|
||||||
fft_.processReal(chanBuf.data(), tempDBs[ch], tempCplx[ch]);
|
fft_.processReal(chanBuf_.data(), channelSpectra_[ch], channelComplex_[ch]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Window gain correction.
|
// Apply cached window gain correction.
|
||||||
float correction = -20.0f * std::log10(windowGain_ > 0 ? windowGain_ : 1.0f);
|
|
||||||
for (auto& db : tempDBs)
|
|
||||||
for (float& v : db)
|
|
||||||
v += correction;
|
|
||||||
|
|
||||||
for (int ch = 0; ch < nSpec; ++ch) {
|
for (int ch = 0; ch < nSpec; ++ch) {
|
||||||
channelSpectra_[ch] = tempDBs[ch];
|
for (float& v : channelSpectra_[ch])
|
||||||
channelComplex_[ch] = tempCplx[ch];
|
v += windowCorrection_;
|
||||||
channelWaterfalls_[ch].push_back(tempDBs[ch]);
|
channelWaterfalls_[ch].push_back(channelSpectra_[ch]);
|
||||||
if (channelWaterfalls_[ch].size() > kWaterfallHistory)
|
if (channelWaterfalls_[ch].size() > kWaterfallHistory)
|
||||||
channelWaterfalls_[ch].pop_front();
|
channelWaterfalls_[ch].pop_front();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,11 @@ private:
|
|||||||
std::vector<std::vector<std::complex<float>>> channelComplex_;
|
std::vector<std::vector<std::complex<float>>> channelComplex_;
|
||||||
std::vector<std::deque<std::vector<float>>> channelWaterfalls_;
|
std::vector<std::deque<std::vector<float>>> channelWaterfalls_;
|
||||||
bool newSpectrumReady_ = false;
|
bool newSpectrumReady_ = false;
|
||||||
|
|
||||||
|
// Pre-allocated scratch buffers for processBlock (avoid per-frame heap alloc)
|
||||||
|
std::vector<float> windowedBuf_; // IQ mode: windowed interleaved samples
|
||||||
|
std::vector<float> chanBuf_; // real mode: single-channel scratch
|
||||||
|
float windowCorrection_ = 0.0f;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
@@ -183,24 +183,24 @@ void Application::processAudio() {
|
|||||||
int nSpec = analyzer_.numSpectra();
|
int nSpec = analyzer_.numSpectra();
|
||||||
if (waterfallMultiCh_ && nSpec > 1) {
|
if (waterfallMultiCh_ && nSpec > 1) {
|
||||||
// Multi-channel overlay waterfall: physical + math channels.
|
// Multi-channel overlay waterfall: physical + math channels.
|
||||||
std::vector<std::vector<float>> wfSpectra;
|
wfSpectraScratch_.clear();
|
||||||
std::vector<WaterfallChannelInfo> wfChInfo;
|
wfChInfoScratch_.clear();
|
||||||
|
|
||||||
for (int ch = 0; ch < nSpec; ++ch) {
|
for (int ch = 0; ch < nSpec; ++ch) {
|
||||||
const auto& c = channelColors_[ch % kMaxChannels];
|
const auto& c = channelColors_[ch % kMaxChannels];
|
||||||
wfSpectra.push_back(analyzer_.channelSpectrum(ch));
|
wfSpectraScratch_.push_back(analyzer_.channelSpectrum(ch));
|
||||||
wfChInfo.push_back({c.x, c.y, c.z,
|
wfChInfoScratch_.push_back({c.x, c.y, c.z,
|
||||||
channelEnabled_[ch % kMaxChannels]});
|
channelEnabled_[ch % kMaxChannels]});
|
||||||
}
|
}
|
||||||
for (size_t mi = 0; mi < mathChannels_.size(); ++mi) {
|
for (size_t mi = 0; mi < mathChannels_.size(); ++mi) {
|
||||||
if (mathChannels_[mi].enabled && mathChannels_[mi].waterfall &&
|
if (mathChannels_[mi].enabled && mathChannels_[mi].waterfall &&
|
||||||
mi < mathSpectra_.size()) {
|
mi < mathSpectra_.size()) {
|
||||||
const auto& c = mathChannels_[mi].color;
|
const auto& c = mathChannels_[mi].color;
|
||||||
wfSpectra.push_back(mathSpectra_[mi]);
|
wfSpectraScratch_.push_back(mathSpectra_[mi]);
|
||||||
wfChInfo.push_back({c.x, c.y, c.z, true});
|
wfChInfoScratch_.push_back({c.x, c.y, c.z, true});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
waterfall_.pushLineMulti(wfSpectra, wfChInfo, minDB_, maxDB_);
|
waterfall_.pushLineMulti(wfSpectraScratch_, wfChInfoScratch_, minDB_, maxDB_);
|
||||||
} else {
|
} else {
|
||||||
int wfCh = std::clamp(waterfallChannel_, 0, nSpec - 1);
|
int wfCh = std::clamp(waterfallChannel_, 0, nSpec - 1);
|
||||||
waterfall_.pushLine(analyzer_.channelSpectrum(wfCh),
|
waterfall_.pushLine(analyzer_.channelSpectrum(wfCh),
|
||||||
@@ -601,37 +601,34 @@ void Application::renderSpectrumPanel() {
|
|||||||
// Build per-channel styles and combine physical + math spectra.
|
// Build per-channel styles and combine physical + math spectra.
|
||||||
int nPhys = analyzer_.numSpectra();
|
int nPhys = analyzer_.numSpectra();
|
||||||
int nMath = static_cast<int>(mathSpectra_.size());
|
int nMath = static_cast<int>(mathSpectra_.size());
|
||||||
int nTotal = nPhys + nMath;
|
|
||||||
|
|
||||||
std::vector<std::vector<float>> allSpectra;
|
allSpectraScratch_.clear();
|
||||||
std::vector<ChannelStyle> styles;
|
stylesScratch_.clear();
|
||||||
allSpectra.reserve(nTotal);
|
|
||||||
styles.reserve(nTotal);
|
|
||||||
|
|
||||||
// Physical channels.
|
// Physical channels.
|
||||||
for (int ch = 0; ch < nPhys; ++ch) {
|
for (int ch = 0; ch < nPhys; ++ch) {
|
||||||
allSpectra.push_back(analyzer_.channelSpectrum(ch));
|
allSpectraScratch_.push_back(analyzer_.channelSpectrum(ch));
|
||||||
const auto& c = channelColors_[ch % kMaxChannels];
|
const auto& c = channelColors_[ch % kMaxChannels];
|
||||||
uint8_t r = static_cast<uint8_t>(c.x * 255);
|
uint8_t r = static_cast<uint8_t>(c.x * 255);
|
||||||
uint8_t g = static_cast<uint8_t>(c.y * 255);
|
uint8_t g = static_cast<uint8_t>(c.y * 255);
|
||||||
uint8_t b = static_cast<uint8_t>(c.z * 255);
|
uint8_t b = static_cast<uint8_t>(c.z * 255);
|
||||||
styles.push_back({IM_COL32(r, g, b, 220), IM_COL32(r, g, b, 35)});
|
stylesScratch_.push_back({IM_COL32(r, g, b, 220), IM_COL32(r, g, b, 35)});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Math channels.
|
// Math channels.
|
||||||
for (int mi = 0; mi < nMath; ++mi) {
|
for (int mi = 0; mi < nMath; ++mi) {
|
||||||
if (mi < static_cast<int>(mathChannels_.size()) && mathChannels_[mi].enabled) {
|
if (mi < static_cast<int>(mathChannels_.size()) && mathChannels_[mi].enabled) {
|
||||||
allSpectra.push_back(mathSpectra_[mi]);
|
allSpectraScratch_.push_back(mathSpectra_[mi]);
|
||||||
const auto& c = mathChannels_[mi].color;
|
const auto& c = mathChannels_[mi].color;
|
||||||
uint8_t r = static_cast<uint8_t>(c.x * 255);
|
uint8_t r = static_cast<uint8_t>(c.x * 255);
|
||||||
uint8_t g = static_cast<uint8_t>(c.y * 255);
|
uint8_t g = static_cast<uint8_t>(c.y * 255);
|
||||||
uint8_t b = static_cast<uint8_t>(c.z * 255);
|
uint8_t b = static_cast<uint8_t>(c.z * 255);
|
||||||
styles.push_back({IM_COL32(r, g, b, 220), IM_COL32(r, g, b, 35)});
|
stylesScratch_.push_back({IM_COL32(r, g, b, 220), IM_COL32(r, g, b, 35)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
specDisplay_.updatePeakHold(allSpectra);
|
specDisplay_.updatePeakHold(allSpectraScratch_);
|
||||||
specDisplay_.draw(allSpectra, styles, minDB_, maxDB_,
|
specDisplay_.draw(allSpectraScratch_, stylesScratch_, minDB_, maxDB_,
|
||||||
settings_.sampleRate, settings_.isIQ, freqScale_,
|
settings_.sampleRate, settings_.isIQ, freqScale_,
|
||||||
specPosX_, specPosY_, specSizeX_, specSizeY_,
|
specPosX_, specPosY_, specSizeX_, specSizeY_,
|
||||||
viewLo_, viewHi_);
|
viewLo_, viewHi_);
|
||||||
|
|||||||
@@ -170,6 +170,12 @@ private:
|
|||||||
// Panel geometry (stored for cursor interaction)
|
// Panel geometry (stored for cursor interaction)
|
||||||
float specPosX_ = 0, specPosY_ = 0, specSizeX_ = 0, specSizeY_ = 0;
|
float specPosX_ = 0, specPosY_ = 0, specSizeX_ = 0, specSizeY_ = 0;
|
||||||
float wfPosX_ = 0, wfPosY_ = 0, wfSizeX_ = 0, wfSizeY_ = 0;
|
float wfPosX_ = 0, wfPosY_ = 0, wfSizeX_ = 0, wfSizeY_ = 0;
|
||||||
|
|
||||||
|
// Pre-allocated scratch buffers (avoid per-frame heap allocations)
|
||||||
|
std::vector<std::vector<float>> wfSpectraScratch_;
|
||||||
|
std::vector<WaterfallChannelInfo> wfChInfoScratch_;
|
||||||
|
std::vector<std::vector<float>> allSpectraScratch_;
|
||||||
|
std::vector<ChannelStyle> stylesScratch_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
@@ -71,28 +71,35 @@ void WaterfallDisplay::pushLineMulti(
|
|||||||
float minDB, float maxDB) {
|
float minDB, float maxDB) {
|
||||||
if (width_ == 0 || height_ == 0) return;
|
if (width_ == 0 || height_ == 0) return;
|
||||||
|
|
||||||
int nCh = static_cast<int>(channelSpectra.size());
|
|
||||||
int row = currentRow_;
|
int row = currentRow_;
|
||||||
int rowOffset = row * width_ * 3;
|
int rowOffset = row * width_ * 3;
|
||||||
float range = maxDB - minDB;
|
float range = maxDB - minDB;
|
||||||
if (range < 1.0f) range = 1.0f;
|
if (range < 1.0f) range = 1.0f;
|
||||||
|
float invRange = 1.0f / range;
|
||||||
|
|
||||||
|
// Pre-filter enabled channels to avoid per-texel branching.
|
||||||
|
struct ActiveCh { const float* data; int bins; float r, g, b; };
|
||||||
|
activeChBuf_.clear();
|
||||||
|
int nCh = std::min(static_cast<int>(channelSpectra.size()),
|
||||||
|
static_cast<int>(channels.size()));
|
||||||
|
for (int ch = 0; ch < nCh; ++ch) {
|
||||||
|
if (!channels[ch].enabled || channelSpectra[ch].empty()) continue;
|
||||||
|
activeChBuf_.push_back({channelSpectra[ch].data(),
|
||||||
|
static_cast<int>(channelSpectra[ch].size()),
|
||||||
|
channels[ch].r, channels[ch].g, channels[ch].b});
|
||||||
|
}
|
||||||
|
|
||||||
// One texel per bin — direct 1:1 mapping.
|
// One texel per bin — direct 1:1 mapping.
|
||||||
for (int x = 0; x < width_; ++x) {
|
for (int x = 0; x < width_; ++x) {
|
||||||
float accR = 0.0f, accG = 0.0f, accB = 0.0f;
|
float accR = 0.0f, accG = 0.0f, accB = 0.0f;
|
||||||
|
|
||||||
for (int ch = 0; ch < nCh; ++ch) {
|
for (const auto& ac : activeChBuf_) {
|
||||||
if (ch >= static_cast<int>(channels.size()) || !channels[ch].enabled)
|
float dB = (x < ac.bins) ? ac.data[x] : -200.0f;
|
||||||
continue;
|
float intensity = std::clamp((dB - minDB) * invRange, 0.0f, 1.0f);
|
||||||
if (channelSpectra[ch].empty()) continue;
|
|
||||||
|
|
||||||
int bins = static_cast<int>(channelSpectra[ch].size());
|
accR += ac.r * intensity;
|
||||||
float dB = (x < bins) ? channelSpectra[ch][x] : -200.0f;
|
accG += ac.g * intensity;
|
||||||
float intensity = std::clamp((dB - minDB) / range, 0.0f, 1.0f);
|
accB += ac.b * intensity;
|
||||||
|
|
||||||
accR += channels[ch].r * intensity;
|
|
||||||
accG += channels[ch].g * intensity;
|
|
||||||
accB += channels[ch].b * intensity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pixelBuf_[rowOffset + x * 3 + 0] =
|
pixelBuf_[rowOffset + x * 3 + 0] =
|
||||||
@@ -112,7 +119,7 @@ void WaterfallDisplay::uploadRow(int row) {
|
|||||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, row, width_, 1,
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, row, width_, 1,
|
||||||
GL_RGB, GL_UNSIGNED_BYTE,
|
GL_RGB, GL_UNSIGNED_BYTE,
|
||||||
pixelBuf_.data() + row * width_ * 3);
|
pixelBuf_.data() + row * width_ * 3);
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
// Note: no unbind — ImGui will bind its own textures before drawing.
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
@@ -49,6 +49,10 @@ private:
|
|||||||
|
|
||||||
ColorMap colorMap_;
|
ColorMap colorMap_;
|
||||||
std::vector<uint8_t> pixelBuf_;
|
std::vector<uint8_t> pixelBuf_;
|
||||||
|
|
||||||
|
// Scratch buffer for pushLineMulti (pre-filtered enabled channels).
|
||||||
|
struct ActiveCh { const float* data; int bins; float r, g, b; };
|
||||||
|
std::vector<ActiveCh> activeChBuf_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace baudline
|
} // namespace baudline
|
||||||
|
|||||||
Reference in New Issue
Block a user