Now adding y-axis controls for frequency domain plot.
[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   set_yaxis(-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::set_yaxis(double min, double max)
227 {
228   setAxisScale(QwtPlot::yLeft, min, max);
229 }
230
231 void
232 FrequencyDisplayPlot::SetFrequencyRange(const double constStartFreq,
233                                         const double constStopFreq,
234                                         const double constCenterFreq,
235                                         const bool useCenterFrequencyFlag,
236                                         const double units, const std::string &strunits)
237 {
238   double startFreq = constStartFreq / units;
239   double stopFreq = constStopFreq / units;
240   double centerFreq = constCenterFreq / units;
241
242   _useCenterFrequencyFlag = useCenterFrequencyFlag;
243
244   if(_useCenterFrequencyFlag){
245     startFreq = (startFreq + centerFreq);
246     stopFreq = (stopFreq + centerFreq);
247   }
248
249   _startFrequency = startFreq;
250   _stopFrequency = stopFreq;
251   _resetXAxisPoints();
252   
253   setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
254   setAxisScaleDraw(QwtPlot::xBottom, new FreqDisplayScaleDraw(2));
255   setAxisTitle(QwtPlot::xBottom, QString("Frequency (%1)").arg(strunits.c_str()));
256   ((FreqDisplayZoomer*)_zoomer)->SetFrequencyPrecision(2);
257
258   // Load up the new base zoom settings
259   QwtDoubleRect newSize = _zoomer->zoomBase();
260   newSize.setLeft(_startFrequency);
261   newSize.setWidth(_stopFrequency-_startFrequency);
262   _zoomer->setZoomBase(newSize);
263   
264   // Zooms back to the base and clears any other zoom levels
265   _zoomer->zoom(0);
266 }
267
268
269 double
270 FrequencyDisplayPlot::GetStartFrequency() const
271 {
272   return _startFrequency;
273 }
274
275 double
276 FrequencyDisplayPlot::GetStopFrequency() const
277 {
278   return _stopFrequency;
279 }
280
281 void
282 FrequencyDisplayPlot::replot()
283 {
284   const timespec startTime = get_highres_clock();
285
286   _markerNoiseFloorAmplitude->setYValue(_noiseFloorAmplitude);
287   
288   // Make sure to take into account the start frequency
289   if(_useCenterFrequencyFlag){
290     _markerPeakAmplitude->setXValue((_peakFrequency/1000.0) + _startFrequency);
291   }
292   else{
293     _markerPeakAmplitude->setXValue(_peakFrequency + _startFrequency);
294   }
295   _markerPeakAmplitude->setYValue(_peakAmplitude);
296   
297   QwtPlot::replot();
298
299   double differenceTime = (diff_timespec(get_highres_clock(), startTime));
300
301   differenceTime *= 99.0;
302   // Require at least a 10% duty cycle
303   if(differenceTime > (1.0/10.0)){
304     _displayIntervalTime = differenceTime;
305   }
306 }
307
308 void
309 FrequencyDisplayPlot::PlotNewData(const double* dataPoints, const int64_t numDataPoints,
310                                   const double noiseFloorAmplitude, const double peakFrequency,
311                                   const double peakAmplitude)
312 {
313   if(numDataPoints > 0){
314
315     if(numDataPoints != _numPoints){
316       _numPoints = numDataPoints;
317
318       delete[] _dataPoints;
319       delete[] _minFFTPoints;
320       delete[] _maxFFTPoints;
321       delete[] _xAxisPoints;
322       _dataPoints = new double[_numPoints];
323       _xAxisPoints = new double[_numPoints];
324       _minFFTPoints = new double[_numPoints];
325       _maxFFTPoints = new double[_numPoints];
326       
327       _fft_plot_curve->setRawData(_xAxisPoints, _dataPoints, _numPoints);
328       _min_fft_plot_curve->setRawData(_xAxisPoints, _minFFTPoints, _numPoints);
329       _max_fft_plot_curve->setRawData(_xAxisPoints, _maxFFTPoints, _numPoints);
330
331       _resetXAxisPoints();
332       ClearMaxData();
333       ClearMinData();
334     }
335     memcpy(_dataPoints, dataPoints, numDataPoints*sizeof(double));
336     for(int64_t point = 0; point < numDataPoints; point++){
337       if(dataPoints[point] < _minFFTPoints[point]){
338         _minFFTPoints[point] = dataPoints[point];
339       }
340       if(dataPoints[point] > _maxFFTPoints[point]){
341         _maxFFTPoints[point] = dataPoints[point];
342       }
343     }
344
345     _noiseFloorAmplitude = noiseFloorAmplitude;
346     _peakFrequency = peakFrequency;
347     _peakAmplitude = peakAmplitude;
348
349   }
350
351   // Allow at least a 50% duty cycle
352   if(diff_timespec(get_highres_clock(), _lastReplot) > _displayIntervalTime){
353     // Only replot the screen if it is visible
354     if(isVisible()){
355       replot();
356     }
357     _lastReplot = get_highres_clock();
358   }
359 }
360
361 void
362 FrequencyDisplayPlot::ClearMaxData()
363 {
364   for(int64_t number = 0; number < _numPoints; number++){
365     _maxFFTPoints[number] = -280.0;
366   }
367 }
368
369 void
370 FrequencyDisplayPlot::ClearMinData()
371 {
372   for(int64_t number = 0; number < _numPoints; number++){
373     _minFFTPoints[number] = 200.0;
374   }
375 }
376
377 void
378 FrequencyDisplayPlot::SetMaxFFTVisible(const bool visibleFlag)
379 {
380   _max_fft_plot_curve->setVisible(visibleFlag);
381 }
382
383 void
384 FrequencyDisplayPlot::SetMinFFTVisible(const bool visibleFlag)
385 {
386   _min_fft_plot_curve->setVisible(visibleFlag);
387 }
388
389 void
390 FrequencyDisplayPlot::_resetXAxisPoints()
391 {
392   double fft_bin_size = (_stopFrequency-_startFrequency) / static_cast<double>(_numPoints);
393   double freqValue = _startFrequency;
394   for(int64_t loc = 0; loc < _numPoints; loc++){
395     _xAxisPoints[loc] = freqValue;
396     freqValue += fft_bin_size;
397   }
398 }
399
400 void
401 FrequencyDisplayPlot::SetLowerIntensityLevel(const double lowerIntensityLevel)
402 {
403   _lower_intensity_marker->setYValue( lowerIntensityLevel );
404 }
405
406 void
407 FrequencyDisplayPlot::SetUpperIntensityLevel(const double upperIntensityLevel)
408 {
409   _upper_intensity_marker->setYValue( upperIntensityLevel );
410 }
411
412
413 #endif /* FREQUENCY_DISPLAY_PLOT_C */