From 13140ec28c9635874e3ffdab970d4c80ebe6cfcc Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Sun, 23 Aug 2015 17:51:20 -0400 Subject: [PATCH] SpectrumVisualProcessor thread fixes, spectrum label optimize --- src/panel/SpectrumPanel.cpp | 28 ++++++++++++++----------- src/process/SpectrumVisualProcessor.cpp | 27 +++++++++++++++++------- src/process/SpectrumVisualProcessor.h | 5 +++-- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/panel/SpectrumPanel.cpp b/src/panel/SpectrumPanel.cpp index c615490..2b6f65e 100644 --- a/src/panel/SpectrumPanel.cpp +++ b/src/panel/SpectrumPanel.cpp @@ -132,27 +132,31 @@ void SpectrumPanel::drawPanelContents() { long long leftFreq = (double) freq - ((double) bandwidth / 2.0); long long rightFreq = leftFreq + (double) bandwidth; - - long long firstMhz = (leftFreq / 1000000) * 1000000; - long double mhzStart = ((long double) (firstMhz - leftFreq) / (long double) (rightFreq - leftFreq)) * 2.0; + + long long hzStep = 1000000; long double mhzStep = (100000.0 / (long double) (rightFreq - leftFreq)) * 2.0; - double mhzVisualStep = 0.1f; - + double mhzVisualStep = 0.1; + + std::stringstream label; + label.precision(1); + if (mhzStep * 0.5 * viewWidth < 40) { mhzStep = (250000.0 / (long double) (rightFreq - leftFreq)) * 2.0; - mhzVisualStep = 0.25f; + mhzVisualStep = 0.25; + label.precision(2); } - if (mhzStep * 0.5 * viewWidth > 400) { + if (mhzStep * 0.5 * viewWidth > 350) { mhzStep = (10000.0 / (long double) (rightFreq - leftFreq)) * 2.0; - mhzVisualStep = 0.01f; + mhzVisualStep = 0.01; + label.precision(2); } - - long double currentMhz = trunc(floor(firstMhz / 1000000.0)); - std::stringstream label; - label.precision(2); + long long firstMhz = (leftFreq / hzStep) * hzStep; + long double mhzStart = ((long double) (firstMhz - leftFreq) / (long double) (rightFreq - leftFreq)) * 2.0; + long double currentMhz = trunc(floor(firstMhz / (long double)1000000.0)); + double hPos = 1.0 - (16.0 / viewHeight); double lMhzPos = 1.0 - (5.0 / viewHeight); diff --git a/src/process/SpectrumVisualProcessor.cpp b/src/process/SpectrumVisualProcessor.cpp index 9322462..8dc890d 100644 --- a/src/process/SpectrumVisualProcessor.cpp +++ b/src/process/SpectrumVisualProcessor.cpp @@ -14,7 +14,7 @@ SpectrumVisualProcessor::SpectrumVisualProcessor() : lastInputBandwidth(0), last fft_ceil_ma = fft_ceil_maa = 100.0; fft_floor_ma = fft_floor_maa = 0.0; - desiredInputSize = 0; + desiredInputSize.store(0); fft_average_rate = 0.65; } @@ -27,19 +27,25 @@ bool SpectrumVisualProcessor::isView() { } void SpectrumVisualProcessor::setView(bool bView) { + busy_run.lock(); is_view.store(bView); + busy_run.unlock(); } void SpectrumVisualProcessor::setFFTAverageRate(float fftAverageRate) { - this->fft_average_rate = fftAverageRate; + busy_run.lock(); + this->fft_average_rate.store(fftAverageRate); + busy_run.unlock(); } float SpectrumVisualProcessor::getFFTAverageRate() { - return this->fft_average_rate; + return this->fft_average_rate.load(); } void SpectrumVisualProcessor::setCenterFrequency(long long centerFreq_in) { + busy_run.lock(); centerFreq.store(centerFreq_in); + busy_run.unlock(); } long long SpectrumVisualProcessor::getCenterFrequency() { @@ -47,7 +53,9 @@ long long SpectrumVisualProcessor::getCenterFrequency() { } void SpectrumVisualProcessor::setBandwidth(long bandwidth_in) { + busy_run.lock(); bandwidth.store(bandwidth_in); + busy_run.unlock(); } long SpectrumVisualProcessor::getBandwidth() { @@ -55,12 +63,14 @@ long SpectrumVisualProcessor::getBandwidth() { } int SpectrumVisualProcessor::getDesiredInputSize() { - return desiredInputSize; + return desiredInputSize.load(); } void SpectrumVisualProcessor::setup(int fftSize_in) { + busy_run.lock(); + fftSize = fftSize_in; - desiredInputSize = fftSize; + desiredInputSize.store(fftSize); if (fftwInput) { free(fftwInput); @@ -82,7 +92,7 @@ void SpectrumVisualProcessor::setup(int fftSize_in) { fftwf_destroy_plan(fftw_plan); } fftw_plan = fftwf_plan_dft_1d(fftSize, fftwInput, fftwOutput, FFTW_FORWARD, FFTW_ESTIMATE); - + busy_run.unlock(); } void SpectrumVisualProcessor::process() { @@ -102,6 +112,7 @@ void SpectrumVisualProcessor::process() { } iqData->busy_rw.lock(); + busy_run.lock(); std::vector *data = &iqData->data; @@ -118,6 +129,7 @@ void SpectrumVisualProcessor::process() { if (!iqData->frequency || !iqData->sampleRate) { iqData->decRefCount(); iqData->busy_rw.unlock(); + busy_run.unlock(); return; } @@ -125,7 +137,7 @@ void SpectrumVisualProcessor::process() { int desired_input_size = fftSize / resamplerRatio; - this->desiredInputSize = desired_input_size; + this->desiredInputSize.store(desired_input_size); if (iqData->data.size() < desired_input_size) { // std::cout << "fft underflow, desired: " << desired_input_size << " actual:" << input->data.size() << std::endl; @@ -304,5 +316,6 @@ void SpectrumVisualProcessor::process() { iqData->decRefCount(); iqData->busy_rw.unlock(); + busy_run.unlock(); } diff --git a/src/process/SpectrumVisualProcessor.h b/src/process/SpectrumVisualProcessor.h index 8e1e694..149c170 100644 --- a/src/process/SpectrumVisualProcessor.h +++ b/src/process/SpectrumVisualProcessor.h @@ -53,7 +53,7 @@ private: double fft_ceil_ma, fft_ceil_maa; double fft_floor_ma, fft_floor_maa; - float fft_average_rate; + std::atomic fft_average_rate; std::vector fft_result; std::vector fft_result_ma; @@ -66,7 +66,8 @@ private: std::vector shiftBuffer; std::vector resampleBuffer; - int desiredInputSize; + std::atomic_int desiredInputSize; + std::mutex busy_run; };