Merging qtdevel2 branch -r10565:10849. This adds a lot of fixes and capabilities...
[debian/gnuradio] / gr-qtgui / src / lib / FrequencyDisplayPlot.cc
1 #ifndef FREQUENCY_DISPLAY_PLOT_C
2 #define FREQUENCY_DISPLAY_PLOT_C
3
4 #include <FrequencyDisplayPlot.h>
5
6 #include <qwt_scale_draw.h>
7
8 class FreqPrecisionClass
9 {
10 public:
11   FreqPrecisionClass(const int freqPrecision)
12   {
13     _frequencyPrecision = freqPrecision;
14   }
15
16   virtual ~FreqPrecisionClass()
17   {
18   }
19
20   virtual unsigned int GetFrequencyPrecision() const
21   {
22     return _frequencyPrecision;
23   }
24
25   virtual void SetFrequencyPrecision(const unsigned int newPrecision)
26   {
27     _frequencyPrecision = newPrecision;
28   }
29 protected:
30   unsigned int _frequencyPrecision;
31
32 private:
33
34 };
35
36 class FreqDisplayScaleDraw: public QwtScaleDraw, public FreqPrecisionClass
37 {
38 public:
39   FreqDisplayScaleDraw(const unsigned int precision)
40     : QwtScaleDraw(), FreqPrecisionClass(precision)
41   {
42   }
43
44   virtual ~FreqDisplayScaleDraw() 
45   {
46   }
47
48   virtual QwtText label(double value) const
49   {
50     return QString("%1").arg(value, 0, 'f', GetFrequencyPrecision());
51   }
52
53 protected:
54
55 private:
56
57 };
58
59 class FreqDisplayZoomer: public QwtPlotZoomer, public FreqPrecisionClass
60 {
61 public:
62   FreqDisplayZoomer(QwtPlotCanvas* canvas, const unsigned int freqPrecision)
63     : QwtPlotZoomer(canvas),FreqPrecisionClass(freqPrecision)
64   {
65     setTrackerMode(QwtPicker::AlwaysOn);
66   }
67
68   virtual ~FreqDisplayZoomer(){
69
70   }
71   
72   virtual void updateTrackerText(){
73     updateDisplay();
74   }
75
76 protected:
77   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
78   {
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));
80
81     return t;
82   }
83 };
84
85 FrequencyDisplayPlot::FrequencyDisplayPlot(QWidget* parent)
86   : QwtPlot(parent)
87 {
88   _startFrequency = 0;
89   _stopFrequency = 4000;
90   
91   timespec_reset(&_lastReplot);
92
93   resize(parent->width(), parent->height());
94
95   _displayIntervalTime = (1.0/10.0); // 1/10 of a second between updates
96
97   _useCenterFrequencyFlag = false;
98
99   _numPoints = 1024;
100   _dataPoints = new double[_numPoints];
101   _minFFTPoints = new double[_numPoints];
102   _maxFFTPoints = new double[_numPoints];
103   _xAxisPoints = new double[_numPoints];
104
105   // Disable polygon clipping
106   QwtPainter::setDeviceClipping(false);
107   
108   // We don't need the cache here
109   canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
110   canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
111   
112   QPalette palette;
113   palette.setColor(canvas()->backgroundRole(), QColor("white"));
114   canvas()->setPalette(palette);  
115
116   setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(0));
117   setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
118   setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
119
120   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
121   setAxisScale(QwtPlot::yLeft, -210, 5);
122   setAxisTitle(QwtPlot::yLeft, "Power (dB)");
123
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);
129
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);
135
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);
141
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);
146
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);
151
152   memset(_dataPoints, 0x0, _numPoints*sizeof(double));
153   memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
154
155   for(int64_t number = 0; number < _numPoints; number++){
156     _minFFTPoints[number] = 200.0;
157     _maxFFTPoints[number] = -280.0;
158   }
159
160   _resetXAxisPoints();
161
162
163   // set up peak marker
164   QwtSymbol symbol;
165
166   _markerPeakAmplitude = new QwtPlotMarker();
167   _markerPeakAmplitude->setLinePen(QPen(Qt::yellow));
168   symbol.setStyle(QwtSymbol::Diamond);
169   symbol.setSize(8);
170   symbol.setPen(QPen(Qt::yellow));
171   symbol.setBrush(QBrush(Qt::yellow));
172   _markerPeakAmplitude->setSymbol(symbol);
173   _markerPeakAmplitude->attach(this);
174
175   _markerNoiseFloorAmplitude = new QwtPlotMarker();
176   _markerNoiseFloorAmplitude->setLineStyle(QwtPlotMarker::HLine);
177   _markerNoiseFloorAmplitude->setLinePen(QPen(Qt::darkRed, 0, Qt::DotLine));
178   _markerNoiseFloorAmplitude->attach(this);
179
180   _peakFrequency = 0;
181   _peakAmplitude = -HUGE_VAL;
182
183   _noiseFloorAmplitude = -HUGE_VAL;
184
185   replot();
186
187   _zoomer = new FreqDisplayZoomer(canvas(), 0);
188 #if QT_VERSION < 0x040000
189   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
190                           Qt::RightButton, Qt::ControlModifier);
191 #else
192   _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
193                           Qt::RightButton, Qt::ControlModifier);
194 #endif
195   _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
196                           Qt::RightButton);
197
198   _panner = new QwtPlotPanner(canvas());
199   _panner->setAxisEnabled(QwtPlot::yRight, false);
200   _panner->setMouseButton(Qt::MidButton);
201
202   // Avoid jumping when labels with more/less digits
203   // appear/disappear when scrolling vertically
204
205   const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
206   QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
207   sd->setMinimumExtent( fm.width("100.00") );
208
209   const QColor c(Qt::darkRed);
210   _zoomer->setRubberBandPen(c);
211   _zoomer->setTrackerPen(c);
212 }
213
214 FrequencyDisplayPlot::~FrequencyDisplayPlot()
215 {
216   delete[] _dataPoints;
217   delete[] _maxFFTPoints;
218   delete[] _minFFTPoints;
219   delete[] _xAxisPoints;
220
221   // _fft_plot_curves deleted when parent deleted
222   // _zoomer and _panner deleted when parent deleted
223 }
224
225 void
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)
231 {
232   double startFreq = constStartFreq / units;
233   double stopFreq = constStopFreq / units;
234   double centerFreq = constCenterFreq / units;
235
236   _useCenterFrequencyFlag = useCenterFrequencyFlag;
237
238   if(_useCenterFrequencyFlag){
239     startFreq = (startFreq + centerFreq);
240     stopFreq = (stopFreq + centerFreq);
241   }
242
243   _startFrequency = startFreq;
244   _stopFrequency = stopFreq;
245   _resetXAxisPoints();
246   
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);
251
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);
257   
258   // Zooms back to the base and clears any other zoom levels
259   _zoomer->zoom(0);
260 }
261
262
263 double
264 FrequencyDisplayPlot::GetStartFrequency() const
265 {
266   return _startFrequency;
267 }
268
269 double
270 FrequencyDisplayPlot::GetStopFrequency() const
271 {
272   return _stopFrequency;
273 }
274
275 void
276 FrequencyDisplayPlot::replot()
277 {
278   const timespec startTime = get_highres_clock();
279
280   _markerNoiseFloorAmplitude->setYValue(_noiseFloorAmplitude);
281   
282   // Make sure to take into account the start frequency
283   if(_useCenterFrequencyFlag){
284     _markerPeakAmplitude->setXValue((_peakFrequency/1000.0) + _startFrequency);
285   }
286   else{
287     _markerPeakAmplitude->setXValue(_peakFrequency + _startFrequency);
288   }
289   _markerPeakAmplitude->setYValue(_peakAmplitude);
290   
291   QwtPlot::replot();
292
293   double differenceTime = (diff_timespec(get_highres_clock(), startTime));
294
295   differenceTime *= 99.0;
296   // Require at least a 10% duty cycle
297   if(differenceTime > (1.0/10.0)){
298     _displayIntervalTime = differenceTime;
299   }
300 }
301
302 void
303 FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDataPoints,
304                                   const double noiseFloorAmplitude, const double peakFrequency,
305                                   const double peakAmplitude)
306 {
307   if(numDataPoints > 0){
308
309     if(numDataPoints != _numPoints){
310       _numPoints = numDataPoints;
311
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];
320       
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);
324
325       _resetXAxisPoints();
326       ClearMaxData();
327       ClearMinData();
328     }
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];
333       }
334       if(dataPoints[point] > _maxFFTPoints[point]){
335         _maxFFTPoints[point] = dataPoints[point];
336       }
337     }
338
339     _noiseFloorAmplitude = noiseFloorAmplitude;
340     _peakFrequency = peakFrequency;
341     _peakAmplitude = peakAmplitude;
342
343   }
344
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
348     if(isVisible()){
349       replot();
350     }
351     _lastReplot = get_highres_clock();
352   }
353 }
354
355 void
356 FrequencyDisplayPlot::ClearMaxData()
357 {
358   for(int64_t number = 0; number < _numPoints; number++){
359     _maxFFTPoints[number] = -280.0;
360   }
361 }
362
363 void
364 FrequencyDisplayPlot::ClearMinData()
365 {
366   for(int64_t number = 0; number < _numPoints; number++){
367     _minFFTPoints[number] = 200.0;
368   }
369 }
370
371 void
372 FrequencyDisplayPlot::SetMaxFFTVisible(const bool visibleFlag)
373 {
374   _max_fft_plot_curve->setVisible(visibleFlag);
375 }
376
377 void
378 FrequencyDisplayPlot::SetMinFFTVisible(const bool visibleFlag)
379 {
380   _min_fft_plot_curve->setVisible(visibleFlag);
381 }
382
383 void
384 FrequencyDisplayPlot::_resetXAxisPoints()
385 {
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;
391   }
392 }
393
394 void
395 FrequencyDisplayPlot::SetLowerIntensityLevel(const double lowerIntensityLevel)
396 {
397   _lower_intensity_marker->setYValue( lowerIntensityLevel );
398 }
399
400 void
401 FrequencyDisplayPlot::SetUpperIntensityLevel(const double upperIntensityLevel)
402 {
403   _upper_intensity_marker->setYValue( upperIntensityLevel );
404 }
405
406
407 #endif /* FREQUENCY_DISPLAY_PLOT_C */