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(){
76 void SetUnitType(const std::string &type)
82 virtual QwtText trackerText( const QwtDoublePoint& p ) const
84 QwtText t(QString("%1 %2, %3 dB").
85 arg(p.x(), 0, 'f', GetFrequencyPrecision()).
86 arg(_unitType.c_str()).arg(p.y(), 0, 'f', 2));
91 std::string _unitType;
94 FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
98 _stopFrequency = 4000;
100 timespec_reset(&_lastReplot);
102 resize(parent->width(), parent->height());
104 _useCenterFrequencyFlag = false;
107 _dataPoints = new double[_numPoints];
108 _minFFTPoints = new double[_numPoints];
109 _maxFFTPoints = new double[_numPoints];
110 _xAxisPoints = new double[_numPoints];
112 // Disable polygon clipping
113 QwtPainter::setDeviceClipping(false);
115 // We don't need the cache here
116 canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
117 canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
120 palette.setColor(canvas()->backgroundRole(), QColor("white"));
121 canvas()->setPalette(palette);
123 setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
124 setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(0));
128 setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
129 setAxisScale(QwtPlot::yLeft, _minYAxis, _maxYAxis);
130 setAxisTitle(QwtPlot::yLeft, "Power (dB)");
132 // Automatically deleted when parent is deleted
133 _fft_plot_curve = new QwtPlotCurve("Power Spectrum");
134 _fft_plot_curve->attach(this);
135 _fft_plot_curve->setPen(QPen(Qt::blue));
136 _fft_plot_curve->setRawData(_xAxisPoints, _dataPoints, _numPoints);
138 _min_fft_plot_curve = new QwtPlotCurve("Minimum Power");
139 _min_fft_plot_curve->attach(this);
140 _min_fft_plot_curve->setPen(QPen(Qt::magenta));
141 _min_fft_plot_curve->setRawData(_xAxisPoints, _minFFTPoints, _numPoints);
142 _min_fft_plot_curve->setVisible(false);
144 _max_fft_plot_curve = new QwtPlotCurve("Maximum Power");
145 _max_fft_plot_curve->attach(this);
146 _max_fft_plot_curve->setPen(QPen(Qt::darkYellow));
147 _max_fft_plot_curve->setRawData(_xAxisPoints, _maxFFTPoints, _numPoints);
148 _max_fft_plot_curve->setVisible(false);
150 _lower_intensity_marker = new QwtPlotMarker();
151 _lower_intensity_marker->setLineStyle(QwtPlotMarker::HLine);
152 _lower_intensity_marker->setLinePen(QPen(Qt::cyan));
153 _lower_intensity_marker->attach(this);
155 _upper_intensity_marker = new QwtPlotMarker();
156 _upper_intensity_marker->setLineStyle(QwtPlotMarker::HLine);
157 _upper_intensity_marker->setLinePen(QPen(Qt::green, 0, Qt::DotLine));
158 _upper_intensity_marker->attach(this);
160 memset(_dataPoints, 0x0, _numPoints*sizeof(double));
161 memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
163 for(int64_t number = 0; number < _numPoints; number++){
164 _minFFTPoints[number] = 200.0;
165 _maxFFTPoints[number] = -280.0;
168 // set up peak marker
171 _markerPeakAmplitude = new QwtPlotMarker();
172 _markerPeakAmplitude->setLinePen(QPen(Qt::yellow));
173 symbol.setStyle(QwtSymbol::Diamond);
175 symbol.setPen(QPen(Qt::yellow));
176 symbol.setBrush(QBrush(Qt::yellow));
177 _markerPeakAmplitude->setSymbol(symbol);
178 _markerPeakAmplitude->attach(this);
180 _markerNoiseFloorAmplitude = new QwtPlotMarker();
181 _markerNoiseFloorAmplitude->setLineStyle(QwtPlotMarker::HLine);
182 _markerNoiseFloorAmplitude->setLinePen(QPen(Qt::darkRed, 0, Qt::DotLine));
183 _markerNoiseFloorAmplitude->attach(this);
186 _peakAmplitude = -HUGE_VAL;
188 _noiseFloorAmplitude = -HUGE_VAL;
192 _zoomer = new FreqDisplayZoomer(canvas(), 0);
193 #if QT_VERSION < 0x040000
194 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
195 Qt::RightButton, Qt::ControlModifier);
197 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
198 Qt::RightButton, Qt::ControlModifier);
200 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
203 _panner = new QwtPlotPanner(canvas());
204 _panner->setAxisEnabled(QwtPlot::yRight, false);
205 _panner->setMouseButton(Qt::MidButton);
207 // Avoid jumping when labels with more/less digits
208 // appear/disappear when scrolling vertically
210 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
211 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
212 sd->setMinimumExtent( fm.width("100.00") );
214 const QColor c(Qt::darkRed);
215 _zoomer->setRubberBandPen(c);
216 _zoomer->setTrackerPen(c);
218 // Do this after the zoomer has been built
222 FrequencyDisplayPlot::~FrequencyDisplayPlot()
224 delete[] _dataPoints;
225 delete[] _maxFFTPoints;
226 delete[] _minFFTPoints;
227 delete[] _xAxisPoints;
229 // _fft_plot_curves deleted when parent deleted
230 // _zoomer and _panner deleted when parent deleted
234 FrequencyDisplayPlot::set_yaxis(double min, double max)
236 // Get the new max/min values for the plot
240 // Set the axis max/min to the new values
241 setAxisScale(QwtPlot::yLeft, _minYAxis, _maxYAxis);
243 // Reset the base zoom level to the new axis scale set here
244 _zoomer->setZoomBase();
248 FrequencyDisplayPlot::SetFrequencyRange(const double constStartFreq,
249 const double constStopFreq,
250 const double constCenterFreq,
251 const bool useCenterFrequencyFlag,
252 const double units, const std::string &strunits)
254 double startFreq = constStartFreq / units;
255 double stopFreq = constStopFreq / units;
256 double centerFreq = constCenterFreq / units;
258 _useCenterFrequencyFlag = useCenterFrequencyFlag;
260 if(_useCenterFrequencyFlag){
261 startFreq = (startFreq + centerFreq);
262 stopFreq = (stopFreq + centerFreq);
266 if((startFreq != _startFrequency) || (stopFreq != _stopFrequency))
269 if(stopFreq > startFreq) {
270 _startFrequency = startFreq;
271 _stopFrequency = stopFreq;
273 if((axisScaleDraw(QwtPlot::xBottom) != NULL) && (_zoomer != NULL)){
274 double display_units = ceil(log10(units)/2.0);
275 setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(display_units));
276 setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
281 ((FreqDisplayZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
282 ((FreqDisplayZoomer*)_zoomer)->SetUnitType(strunits);
289 FrequencyDisplayPlot::GetStartFrequency() const
291 return _startFrequency;
295 FrequencyDisplayPlot::GetStopFrequency() const
297 return _stopFrequency;
301 FrequencyDisplayPlot::replot()
303 _markerNoiseFloorAmplitude->setYValue(_noiseFloorAmplitude);
305 // Make sure to take into account the start frequency
306 if(_useCenterFrequencyFlag){
307 _markerPeakAmplitude->setXValue((_peakFrequency/1000.0) + _startFrequency);
310 _markerPeakAmplitude->setXValue(_peakFrequency + _startFrequency);
312 _markerPeakAmplitude->setYValue(_peakAmplitude);
318 FrequencyDisplayPlot::resizeSlot( QSize *s )
320 resize(s->width(), s->height());
324 FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDataPoints,
325 const double noiseFloorAmplitude, const double peakFrequency,
326 const double peakAmplitude, const double timeInterval)
328 // Only update plot if there is data and if the time interval has elapsed
329 if((numDataPoints > 0) &&
330 (diff_timespec(get_highres_clock(), _lastReplot) > timeInterval)) {
332 if(numDataPoints != _numPoints) {
333 _numPoints = numDataPoints;
335 delete[] _dataPoints;
336 delete[] _minFFTPoints;
337 delete[] _maxFFTPoints;
338 delete[] _xAxisPoints;
339 _dataPoints = new double[_numPoints];
340 _xAxisPoints = new double[_numPoints];
341 _minFFTPoints = new double[_numPoints];
342 _maxFFTPoints = new double[_numPoints];
344 _fft_plot_curve->setRawData(_xAxisPoints, _dataPoints, _numPoints);
345 _min_fft_plot_curve->setRawData(_xAxisPoints, _minFFTPoints, _numPoints);
346 _max_fft_plot_curve->setRawData(_xAxisPoints, _maxFFTPoints, _numPoints);
353 memcpy(_dataPoints, dataPoints, numDataPoints*sizeof(double));
354 for(int64_t point = 0; point < numDataPoints; point++){
355 if(dataPoints[point] < _minFFTPoints[point]){
356 _minFFTPoints[point] = dataPoints[point];
358 if(dataPoints[point] > _maxFFTPoints[point]){
359 _maxFFTPoints[point] = dataPoints[point];
363 _noiseFloorAmplitude = noiseFloorAmplitude;
364 _peakFrequency = peakFrequency;
365 _peakAmplitude = peakAmplitude;
367 SetUpperIntensityLevel(_peakAmplitude);
371 _lastReplot = get_highres_clock();
376 FrequencyDisplayPlot::ClearMaxData()
378 for(int64_t number = 0; number < _numPoints; number++){
379 _maxFFTPoints[number] = _minYAxis;
384 FrequencyDisplayPlot::ClearMinData()
386 for(int64_t number = 0; number < _numPoints; number++){
387 _minFFTPoints[number] = _maxYAxis;
392 FrequencyDisplayPlot::SetMaxFFTVisible(const bool visibleFlag)
394 _max_fft_plot_curve->setVisible(visibleFlag);
398 FrequencyDisplayPlot::SetMinFFTVisible(const bool visibleFlag)
400 _min_fft_plot_curve->setVisible(visibleFlag);
404 FrequencyDisplayPlot::_resetXAxisPoints()
406 double fft_bin_size = (_stopFrequency-_startFrequency) / static_cast<double>(_numPoints);
407 double freqValue = _startFrequency;
408 for(int64_t loc = 0; loc < _numPoints; loc++){
409 _xAxisPoints[loc] = freqValue;
410 freqValue += fft_bin_size;
413 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
415 // Set up zoomer base for maximum unzoom x-axis
416 // and reset to maximum unzoom level
417 QwtDoubleRect zbase = _zoomer->zoomBase();
418 zbase.setLeft(_startFrequency);
419 zbase.setRight(_stopFrequency);
420 _zoomer->zoom(zbase);
421 _zoomer->setZoomBase(zbase);
426 FrequencyDisplayPlot::SetLowerIntensityLevel(const double lowerIntensityLevel)
428 _lower_intensity_marker->setYValue( lowerIntensityLevel );
432 FrequencyDisplayPlot::SetUpperIntensityLevel(const double upperIntensityLevel)
434 _upper_intensity_marker->setYValue( upperIntensityLevel );
438 #endif /* FREQUENCY_DISPLAY_PLOT_C */