From 4b3d088407c1bf78432d51ea753e081d6b7fa5eb Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 5 Aug 2019 01:56:29 +0200 Subject: [PATCH] GLSpectrum settings refactoring (1) --- plugins/channelrx/demodbfm/bfmdemodgui.cpp | 2 +- plugins/channelrx/udpsink/udpsinkgui.cpp | 2 +- plugins/channeltx/udpsource/udpsourcegui.cpp | 2 +- sdrbase/CMakeLists.txt | 2 + sdrbase/dsp/glspectrumsettings.cpp | 175 +++++++++++++++++++ sdrbase/dsp/glspectrumsettings.h | 67 +++++++ sdrgui/dsp/spectrumvis.cpp | 2 +- sdrgui/dsp/spectrumvis.h | 9 +- sdrgui/gui/glspectrumgui.cpp | 12 +- 9 files changed, 258 insertions(+), 15 deletions(-) create mode 100644 sdrbase/dsp/glspectrumsettings.cpp create mode 100644 sdrbase/dsp/glspectrumsettings.h diff --git a/plugins/channelrx/demodbfm/bfmdemodgui.cpp b/plugins/channelrx/demodbfm/bfmdemodgui.cpp index b4eac0328..c6b92e8c1 100644 --- a/plugins/channelrx/demodbfm/bfmdemodgui.cpp +++ b/plugins/channelrx/demodbfm/bfmdemodgui.cpp @@ -386,7 +386,7 @@ BFMDemodGUI::BFMDemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, Baseban 64, // FFT size 10, // overlapping % 0, // number of averaging samples - 0, // no averaging + SpectrumVis::AvgModeNone, // no averaging FFTWindow::BlackmanHarris, false); // logarithmic scale connect(&MainWindow::getInstance()->getMasterTimer(), SIGNAL(timeout()), this, SLOT(tick())); diff --git a/plugins/channelrx/udpsink/udpsinkgui.cpp b/plugins/channelrx/udpsink/udpsinkgui.cpp index c784ebe39..4fdbd823e 100644 --- a/plugins/channelrx/udpsink/udpsinkgui.cpp +++ b/plugins/channelrx/udpsink/udpsinkgui.cpp @@ -190,7 +190,7 @@ UDPSinkGUI::UDPSinkGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, BasebandS 64, // FFT size 10, // overlapping % 0, // number of averaging samples - 0, // no averaging + SpectrumVis::AvgModeNone, // no averaging FFTWindow::BlackmanHarris, false); // logarithmic scale diff --git a/plugins/channeltx/udpsource/udpsourcegui.cpp b/plugins/channeltx/udpsource/udpsourcegui.cpp index c57aeed74..f21228cf2 100644 --- a/plugins/channeltx/udpsource/udpsourcegui.cpp +++ b/plugins/channeltx/udpsource/udpsourcegui.cpp @@ -147,7 +147,7 @@ UDPSourceGUI::UDPSourceGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, Baseb 64, // FFT size 10, // overlapping % 0, // number of averaging samples - 0, // no averaging + SpectrumVis::AvgModeNone, // no averaging FFTWindow::BlackmanHarris, false); // logarithmic scale diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index a34d6085f..ff18b6ba2 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -85,6 +85,7 @@ set(sdrbase_SOURCES dsp/filerecord.cpp dsp/freqlockcomplex.cpp dsp/interpolator.cpp + dsp/glspectrumsettings.cpp dsp/hbfilterchainconverter.cpp dsp/hbfiltertraits.cpp dsp/lowpass.cpp @@ -200,6 +201,7 @@ set(sdrbase_HEADERS dsp/filerecord.h dsp/freqlockcomplex.h dsp/gfft.h + dsp/glspectrumsettings.h dsp/hbfilterchainconverter.h dsp/iirfilter.h dsp/interpolator.h diff --git a/sdrbase/dsp/glspectrumsettings.cpp b/sdrbase/dsp/glspectrumsettings.cpp new file mode 100644 index 000000000..3e9a3d629 --- /dev/null +++ b/sdrbase/dsp/glspectrumsettings.cpp @@ -0,0 +1,175 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 Edouard Griffiths, F4EXB. // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "fftwindow.h" +#include "util/simpleserializer.h" +#include "glspectrumsettings.h" + +void GLSpectrumSettings::resetToDefaults() +{ + m_fftSize = 1024; + m_fftOverlap = 0; + m_fftWindow = FFTWindow::Hamming; + m_refLevel = 0; + m_powerRange = 100; + m_decay = 1; + m_decayDivisor = 1; + m_histogramStroke = 30; + m_displayGridIntensity = 5, + m_displayWaterfall = true; + m_invertedWaterfall = false; + m_displayMaxHold = false; + m_displayHistogram = false; + m_displayGrid = false; + m_invert = true; + m_averagingMode = AvgModeNone; + m_averagingIndex = 0; + m_linear = false; +} + +QByteArray GLSpectrumSettings::serialize() const +{ + SimpleSerializer s(1); + s.writeS32(1, m_fftSize); + s.writeS32(2, m_fftOverlap); + s.writeS32(3, m_fftWindow); + s.writeReal(4, m_refLevel); + s.writeReal(5, m_powerRange); + s.writeBool(6, m_displayWaterfall); + s.writeBool(7, m_invertedWaterfall); + s.writeBool(8, m_displayMaxHold); + s.writeBool(9, m_displayHistogram); + s.writeS32(10, m_decay); + s.writeBool(11, m_displayGrid); + s.writeBool(12, m_invert); + s.writeS32(13, m_displayGridIntensity); + s.writeS32(14, m_decayDivisor); + s.writeS32(15, m_histogramStroke); + s.writeBool(16, m_displayCurrent); + s.writeS32(17, m_displayTraceIntensity); + s.writeReal(18, m_waterfallShare); + s.writeS32(19, (int) m_averagingMode); + s.writeS32(20, (qint32) getAveragingValue(m_averagingIndex, m_averagingMode)); + s.writeBool(21, m_linear); + return s.final(); +} + +bool GLSpectrumSettings::deserialize(const QByteArray& data) +{ + SimpleDeserializer d(data); + + if(!d.isValid()) { + resetToDefaults(); + return false; + } + + int tmp; + + if(d.getVersion() == 1) { + d.readS32(1, &m_fftSize, 1024); + d.readS32(2, &m_fftOverlap, 0); + d.readS32(3, &m_fftWindow, FFTWindow::Hamming); + d.readReal(4, &m_refLevel, 0); + d.readReal(5, &m_powerRange, 100); + d.readBool(6, &m_displayWaterfall, true); + d.readBool(7, &m_invertedWaterfall, false); + d.readBool(8, &m_displayMaxHold, false); + d.readBool(9, &m_displayHistogram, false); + d.readS32(10, &m_decay, 1); + d.readBool(11, &m_displayGrid, false); + d.readBool(12, &m_invert, true); + d.readS32(13, &m_displayGridIntensity, 5); + d.readS32(14, &m_decayDivisor, 1); + d.readS32(15, &m_histogramStroke, 30); + d.readBool(16, &m_displayCurrent, false); + d.readS32(17, &m_displayTraceIntensity, 50); + d.readReal(18, &m_waterfallShare, 0.66); + d.readS32(19, &tmp, 0); + m_averagingMode = tmp < 0 ? AvgModeNone : tmp > 3 ? AvgModeMax : (AveragingMode) tmp; + d.readS32(20, &tmp, 0); + m_averagingIndex = getAveragingIndex(tmp, m_averagingMode); + m_averagingNb = getAveragingValue(m_averagingIndex, m_averagingMode); + d.readBool(21, &m_linear, false); + + return true; + } else { + resetToDefaults(); + return false; + } +} + +int GLSpectrumSettings::getAveragingMaxScale(AveragingMode averagingMode) +{ + if (averagingMode == AvgModeMoving) { + return 2; + } else { + return 5; + } +} + +int GLSpectrumSettings::getAveragingValue(int averagingIndex, AveragingMode averagingMode) +{ + if (averagingIndex <= 0) { + return 1; + } + + int v = averagingIndex - 1; + int m = pow(10.0, v/3 > getAveragingMaxScale(averagingMode) ? getAveragingMaxScale(averagingMode) : v/3); + int x = 1; + + if (v % 3 == 0) { + x = 2; + } else if (v % 3 == 1) { + x = 5; + } else if (v % 3 == 2) { + x = 10; + } + + return x * m; +} + +int GLSpectrumSettings::getAveragingIndex(int averagingValue, AveragingMode averagingMode) +{ + if (averagingValue <= 1) { + return 0; + } + + int v = averagingValue; + int j = 0; + + for (int i = 0; i <= getAveragingMaxScale(averagingMode); i++) + { + if (v < 20) + { + if (v < 2) { + j = 0; + } else if (v < 5) { + j = 1; + } else if (v < 10) { + j = 2; + } else { + j = 3; + } + + return 3*i + j; + } + + v /= 10; + } + + return 3*getAveragingMaxScale(averagingMode) + 3; +} \ No newline at end of file diff --git a/sdrbase/dsp/glspectrumsettings.h b/sdrbase/dsp/glspectrumsettings.h new file mode 100644 index 000000000..a49094caa --- /dev/null +++ b/sdrbase/dsp/glspectrumsettings.h @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2019 Edouard Griffiths, F4EXB. // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include + +#include "export.h" +#include "dsp/dsptypes.h" + +class SDRBASE_API GLSpectrumSettings +{ +public: + enum AveragingMode + { + AvgModeNone, + AvgModeMoving, + AvgModeFixed, + AvgModeMax + }; + + int m_fftSize; + int m_fftOverlap; + int m_fftWindow; + Real m_refLevel; + Real m_powerRange; + int m_decay; + int m_decayDivisor; + int m_histogramStroke; + int m_displayGridIntensity; + int m_displayTraceIntensity; + bool m_displayWaterfall; + bool m_invertedWaterfall; + Real m_waterfallShare; + bool m_displayMaxHold; + bool m_displayCurrent; + bool m_displayHistogram; + bool m_displayGrid; + bool m_invert; + AveragingMode m_averagingMode; + int m_averagingIndex; + int m_averagingMaxScale; //!< Max power of 10 multiplier to 2,5,10 base ex: 2 -> 2,5,10,20,50,100,200,500,1000 + unsigned int m_averagingNb; + bool m_linear; //!< linear else logarithmic scale + + GLSpectrumSettings(); + void resetToDefaults(); + + QByteArray serialize() const; + bool deserialize(const QByteArray& data); + + static int getAveragingMaxScale(AveragingMode averagingMode); + static int getAveragingValue(int averagingIndex, AveragingMode averagingMode); + static int getAveragingIndex(int averagingValue, AveragingMode averagingMode); +}; diff --git a/sdrgui/dsp/spectrumvis.cpp b/sdrgui/dsp/spectrumvis.cpp index 9f52ce9b3..022145265 100644 --- a/sdrgui/dsp/spectrumvis.cpp +++ b/sdrgui/dsp/spectrumvis.cpp @@ -45,7 +45,7 @@ void SpectrumVis::configure(MessageQueue* msgQueue, int fftSize, int overlapPercent, unsigned int averagingNb, - int averagingMode, + AvgMode averagingMode, FFTWindow::Function window, bool linear) { diff --git a/sdrgui/dsp/spectrumvis.h b/sdrgui/dsp/spectrumvis.h index b01c6459d..73d3ca254 100644 --- a/sdrgui/dsp/spectrumvis.h +++ b/sdrgui/dsp/spectrumvis.h @@ -33,18 +33,17 @@ public: int fftSize, int overlapPercent, unsigned int averageNb, - int preProcessMode, + AvgMode avgMode, FFTWindow::Function window, bool linear) : Message(), m_fftSize(fftSize), m_overlapPercent(overlapPercent), m_averageNb(averageNb), + m_avgMode(avgMode), m_window(window), m_linear(linear) - { - m_avgMode = preProcessMode < 0 ? AvgModeNone : preProcessMode > 3 ? AvgModeMax : (SpectrumVis::AvgMode) preProcessMode; - } + {} int getFFTSize() const { return m_fftSize; } int getOverlapPercent() const { return m_overlapPercent; } @@ -69,7 +68,7 @@ public: int fftSize, int overlapPercent, unsigned int averagingNb, - int averagingMode, + AvgMode averagingMode, FFTWindow::Function window, bool m_linear); diff --git a/sdrgui/gui/glspectrumgui.cpp b/sdrgui/gui/glspectrumgui.cpp index 64bf9b78f..367ebc7d2 100644 --- a/sdrgui/gui/glspectrumgui.cpp +++ b/sdrgui/gui/glspectrumgui.cpp @@ -205,7 +205,7 @@ void GLSpectrumGUI::applySettings() m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); } @@ -221,7 +221,7 @@ void GLSpectrumGUI::on_fftWindow_currentIndexChanged(int index) m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); } @@ -235,7 +235,7 @@ void GLSpectrumGUI::on_fftSize_currentIndexChanged(int index) m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); } @@ -263,7 +263,7 @@ void GLSpectrumGUI::on_averagingMode_currentIndexChanged(int index) m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); } @@ -288,7 +288,7 @@ void GLSpectrumGUI::on_averaging_currentIndexChanged(int index) m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); } @@ -314,7 +314,7 @@ void GLSpectrumGUI::on_linscale_toggled(bool checked) m_fftSize, m_fftOverlap, m_averagingNb, - m_averagingMode, + (SpectrumVis::AvgMode) m_averagingMode, (FFTWindow::Function)m_fftWindow, m_linear); }