1 #ifndef FREQUENCY_DISPLAY_PLOT_C
2 #define FREQUENCY_DISPLAY_PLOT_C
4 #include <FrequencyDisplayPlot.h>
6 #include <qwt_scale_draw.h>
8 class FreqPrecisionClass
11 FreqPrecisionClass(const int freqPrecision)
13 _frequencyPrecision = freqPrecision;
16 virtual ~FreqPrecisionClass()
20 virtual unsigned int GetFrequencyPrecision() const
22 return _frequencyPrecision;
25 virtual void SetFrequencyPrecision(const unsigned int newPrecision)
27 _frequencyPrecision = newPrecision;
30 unsigned int _frequencyPrecision;
36 class FreqDisplayScaleDraw: public QwtScaleDraw, public FreqPrecisionClass
39 FreqDisplayScaleDraw(const unsigned int precision)
40 : QwtScaleDraw(), FreqPrecisionClass(precision)
44 virtual ~FreqDisplayScaleDraw()
48 virtual QwtText label(double value) const
50 return QString("%1").arg(value, 0, 'f', GetFrequencyPrecision());
59 class FreqDisplayZoomer: public QwtPlotZoomer, public FreqPrecisionClass
62 FreqDisplayZoomer(QwtPlotCanvas* canvas, const unsigned int freqPrecision)
63 : QwtPlotZoomer(canvas),FreqPrecisionClass(freqPrecision)
65 setTrackerMode(QwtPicker::AlwaysOn);
68 virtual ~FreqDisplayZoomer(){
72 virtual void updateTrackerText(){
77 virtual QwtText trackerText( const QwtDoublePoint& p ) const
79 QwtText t(QString("%1 %2, %3 dB").arg(p.x(), 0, 'f', GetFrequencyPrecision()).arg( (GetFrequencyPrecision() == 0) ? "Hz" : "kHz").arg(p.y(), 0, 'f', 2));
85 FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
89 _stopFrequency = 4000;
91 timespec_reset(&_lastReplot);
93 resize(parent->width(), parent->height());
95 _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
97 _useCenterFrequencyFlag = false;
100 _dataPoints = new double[_numPoints];
101 _minFFTPoints = new double[_numPoints];
102 _maxFFTPoints = new double[_numPoints];
103 _xAxisPoints = new double[_numPoints];
105 // Disable polygon clipping
106 QwtPainter::setDeviceClipping(false);
108 // We don't need the cache here
109 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
110 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
113 palette.setColor(canvas()->backgroundRole(), QColor("white"));
114 canvas()->setPalette(palette);
116 setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(0));
117 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
118 setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
120 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
121 setAxisScale(QwtPlot::yLeft, -210, 5);
122 setAxisTitle(QwtPlot::yLeft, "Power (dB)");
124 // Automatically deleted when parent is deleted
125 _fft_plot_curve = new QwtPlotCurve("Power Spectrum");
126 _fft_plot_curve->attach(this);
127 _fft_plot_curve->setPen(QPen(Qt::blue));
128 _fft_plot_curve->setRawData(_xAxisPoints, _dataPoints, _numPoints);
130 _min_fft_plot_curve = new QwtPlotCurve("Minimum Power");
131 _min_fft_plot_curve->attach(this);
132 _min_fft_plot_curve->setPen(QPen(Qt::magenta));
133 _min_fft_plot_curve->setRawData(_xAxisPoints, _minFFTPoints, _numPoints);
134 _min_fft_plot_curve->setVisible(false);
136 _max_fft_plot_curve = new QwtPlotCurve("Maximum Power");
137 _max_fft_plot_curve->attach(this);
138 _max_fft_plot_curve->setPen(QPen(Qt::darkYellow));
139 _max_fft_plot_curve->setRawData(_xAxisPoints, _maxFFTPoints, _numPoints);
140 _max_fft_plot_curve->setVisible(false);
142 _lower_intensity_marker = new QwtPlotMarker();
143 _lower_intensity_marker->setLineStyle(QwtPlotMarker::HLine);
144 _lower_intensity_marker->setLinePen(QPen(Qt::cyan));
145 _lower_intensity_marker->attach(this);
147 _upper_intensity_marker = new QwtPlotMarker();
148 _upper_intensity_marker->setLineStyle(QwtPlotMarker::HLine);
149 _upper_intensity_marker->setLinePen(QPen(Qt::green));
150 _upper_intensity_marker->attach(this);
152 memset(_dataPoints, 0x0, _numPoints*sizeof(double));
153 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
155 for(int64_t number = 0; number < _numPoints; number++){
156 _minFFTPoints[number] = 200.0;
157 _maxFFTPoints[number] = -280.0;
163 // set up peak marker
166 _markerPeakAmplitude = new QwtPlotMarker();
167 _markerPeakAmplitude->setLinePen(QPen(Qt::yellow));
168 symbol.setStyle(QwtSymbol::Diamond);
170 symbol.setPen(QPen(Qt::yellow));
171 symbol.setBrush(QBrush(Qt::yellow));
172 _markerPeakAmplitude->setSymbol(symbol);
173 _markerPeakAmplitude->attach(this);
175 _markerNoiseFloorAmplitude = new QwtPlotMarker();
176 _markerNoiseFloorAmplitude->setLineStyle(QwtPlotMarker::HLine);
177 _markerNoiseFloorAmplitude->setLinePen(QPen(Qt::darkRed, 0, Qt::DotLine));
178 _markerNoiseFloorAmplitude->attach(this);
181 _peakAmplitude = -HUGE_VAL;
183 _noiseFloorAmplitude = -HUGE_VAL;
187 _zoomer = new FreqDisplayZoomer(canvas(), 0);
188 #if QT_VERSION < 0x040000
189 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
190 Qt::RightButton, Qt::ControlModifier);
192 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
193 Qt::RightButton, Qt::ControlModifier);
195 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
198 _panner = new QwtPlotPanner(canvas());
199 _panner->setAxisEnabled(QwtPlot::yRight, false);
200 _panner->setMouseButton(Qt::MidButton);
202 // Avoid jumping when labels with more/less digits
203 // appear/disappear when scrolling vertically
205 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
206 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
207 sd->setMinimumExtent( fm.width("100.00") );
209 const QColor c(Qt::darkRed);
210 _zoomer->setRubberBandPen(c);
211 _zoomer->setTrackerPen(c);
214 FrequencyDisplayPlot::~FrequencyDisplayPlot()
216 delete[] _dataPoints;
217 delete[] _maxFFTPoints;
218 delete[] _minFFTPoints;
219 delete[] _xAxisPoints;
221 // _fft_plot_curves deleted when parent deleted
222 // _zoomer and _panner deleted when parent deleted
226 FrequencyDisplayPlot::SetFrequencyRange(const double constStartFreq,
227 const double constStopFreq,
228 const double constCenterFreq,
229 const bool useCenterFrequencyFlag,
230 const double units, const std::string &strunits)
232 double startFreq = constStartFreq / units;
233 double stopFreq = constStopFreq / units;
234 double centerFreq = constCenterFreq / units;
236 _useCenterFrequencyFlag = useCenterFrequencyFlag;
238 if(_useCenterFrequencyFlag){
239 startFreq = (startFreq + centerFreq);
240 stopFreq = (stopFreq + centerFreq);
243 _startFrequency = startFreq;
244 _stopFrequency = stopFreq;
247 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
248 setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(2));
249 setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
250 ((FreqDisplayZoomer*)_zoomer)->SetFrequencyPrecision(2);
252 // Load up the new base zoom settings
253 QwtDoubleRect newSize = _zoomer->zoomBase();
254 newSize.setLeft(_startFrequency);
255 newSize.setWidth(_stopFrequency-_startFrequency);
256 _zoomer->setZoomBase(newSize);
258 // Zooms back to the base and clears any other zoom levels
264 FrequencyDisplayPlot::GetStartFrequency() const
266 return _startFrequency;
270 FrequencyDisplayPlot::GetStopFrequency() const
272 return _stopFrequency;
276 FrequencyDisplayPlot::replot()
278 const timespec startTime = get_highres_clock();
280 _markerNoiseFloorAmplitude->setYValue(_noiseFloorAmplitude);
282 // Make sure to take into account the start frequency
283 if(_useCenterFrequencyFlag){
284 _markerPeakAmplitude->setXValue((_peakFrequency/1000.0) + _startFrequency);
287 _markerPeakAmplitude->setXValue(_peakFrequency + _startFrequency);
289 _markerPeakAmplitude->setYValue(_peakAmplitude);
293 double differenceTime = (diff_timespec(get_highres_clock(), startTime));
295 differenceTime *= 99.0;
296 // Require at least a 10% duty cycle
297 if(differenceTime > (1.0/10.0)){
298 _displayIntervalTime = differenceTime;
303 FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDataPoints,
304 const double noiseFloorAmplitude, const double peakFrequency,
305 const double peakAmplitude)
307 if(numDataPoints > 0){
309 if(numDataPoints != _numPoints){
310 _numPoints = numDataPoints;
312 delete[] _dataPoints;
313 delete[] _minFFTPoints;
314 delete[] _maxFFTPoints;
315 delete[] _xAxisPoints;
316 _dataPoints = new double[_numPoints];
317 _xAxisPoints = new double[_numPoints];
318 _minFFTPoints = new double[_numPoints];
319 _maxFFTPoints = new double[_numPoints];
321 _fft_plot_curve->setRawData(_xAxisPoints, _dataPoints, _numPoints);
322 _min_fft_plot_curve->setRawData(_xAxisPoints, _minFFTPoints, _numPoints);
323 _max_fft_plot_curve->setRawData(_xAxisPoints, _maxFFTPoints, _numPoints);
329 memcpy(_dataPoints, dataPoints, numDataPoints*sizeof(double));
330 for(int64_t point = 0; point < numDataPoints; point++){
331 if(dataPoints[point] < _minFFTPoints[point]){
332 _minFFTPoints[point] = dataPoints[point];
334 if(dataPoints[point] > _maxFFTPoints[point]){
335 _maxFFTPoints[point] = dataPoints[point];
339 _noiseFloorAmplitude = noiseFloorAmplitude;
340 _peakFrequency = peakFrequency;
341 _peakAmplitude = peakAmplitude;
345 // Allow at least a 50% duty cycle
346 if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
347 // Only replot the screen if it is visible
351 _lastReplot = get_highres_clock();
356 FrequencyDisplayPlot::ClearMaxData()
358 for(int64_t number = 0; number < _numPoints; number++){
359 _maxFFTPoints[number] = -280.0;
364 FrequencyDisplayPlot::ClearMinData()
366 for(int64_t number = 0; number < _numPoints; number++){
367 _minFFTPoints[number] = 200.0;
372 FrequencyDisplayPlot::SetMaxFFTVisible(const bool visibleFlag)
374 _max_fft_plot_curve->setVisible(visibleFlag);
378 FrequencyDisplayPlot::SetMinFFTVisible(const bool visibleFlag)
380 _min_fft_plot_curve->setVisible(visibleFlag);
384 FrequencyDisplayPlot::_resetXAxisPoints()
386 double fft_bin_size = (_stopFrequency-_startFrequency) / static_cast<double>(_numPoints);
387 double freqValue = _startFrequency;
388 for(int64_t loc = 0; loc < _numPoints; loc++){
389 _xAxisPoints[loc] = freqValue;
390 freqValue += fft_bin_size;
395 FrequencyDisplayPlot::SetLowerIntensityLevel(const double lowerIntensityLevel)
397 _lower_intensity_marker->setYValue( lowerIntensityLevel );
401 FrequencyDisplayPlot::SetUpperIntensityLevel(const double upperIntensityLevel)
403 _upper_intensity_marker->setYValue( upperIntensityLevel );
407 #endif /* FREQUENCY_DISPLAY_PLOT_C */