1 #ifndef WATERFALL_DISPLAY_PLOT_C
2 #define WATERFALL_DISPLAY_PLOT_C
4 #include <WaterfallDisplayPlot.h>
6 #include <qwt_color_map.h>
7 #include <qwt_scale_widget.h>
8 #include <qwt_scale_draw.h>
9 #include <qwt_plot_zoomer.h>
10 #include <qwt_plot_panner.h>
11 #include <qwt_plot_layout.h>
13 #include <qapplication.h>
15 class FreqOffsetAndPrecisionClass
18 FreqOffsetAndPrecisionClass(const int freqPrecision)
20 _frequencyPrecision = freqPrecision;
24 virtual ~FreqOffsetAndPrecisionClass()
28 virtual unsigned int GetFrequencyPrecision() const
30 return _frequencyPrecision;
33 virtual void SetFrequencyPrecision(const unsigned int newPrecision)
35 _frequencyPrecision = newPrecision;
38 virtual double GetCenterFrequency() const
40 return _centerFrequency;
43 virtual void SetCenterFrequency(const double newFreq)
45 _centerFrequency = newFreq;
49 unsigned int _frequencyPrecision;
50 double _centerFrequency;
56 class WaterfallFreqDisplayScaleDraw: public QwtScaleDraw, public FreqOffsetAndPrecisionClass{
58 WaterfallFreqDisplayScaleDraw(const unsigned int precision)
59 : QwtScaleDraw(), FreqOffsetAndPrecisionClass(precision)
63 virtual ~WaterfallFreqDisplayScaleDraw()
67 QwtText label(double value) const
69 return QString("%1").arg(value, 0, 'f', GetFrequencyPrecision());
72 virtual void initiateUpdate()
88 timespec_reset(&_zeroTime);
89 _secondsPerLine = 1.0;
92 virtual ~TimeScaleData()
96 virtual timespec GetZeroTime() const
101 virtual void SetZeroTime(const timespec newTime)
106 virtual void SetSecondsPerLine(const double newTime)
108 _secondsPerLine = newTime;
111 virtual double GetSecondsPerLine() const
113 return _secondsPerLine;
119 double _secondsPerLine;
125 class QwtTimeScaleDraw: public QwtScaleDraw, public TimeScaleData
128 QwtTimeScaleDraw():QwtScaleDraw(),TimeScaleData()
132 virtual ~QwtTimeScaleDraw()
136 virtual QwtText label(double value) const
138 QwtText returnLabel("");
140 timespec lineTime = timespec_add(GetZeroTime(), (-value) * GetSecondsPerLine());
142 gmtime_r(&lineTime.tv_sec, &timeTm);
143 returnLabel = (QString("").sprintf("%04d/%02d/%02d\n%02d:%02d:%02d.%03ld",
144 timeTm.tm_year+1900, timeTm.tm_mon+1,
145 timeTm.tm_mday, timeTm.tm_hour, timeTm.tm_min,
146 timeTm.tm_sec, lineTime.tv_nsec/1000000));
150 virtual void initiateUpdate()
152 // Do this in one call rather than when zeroTime and secondsPerLine
153 // updates is to prevent the display from being updated too often...
163 class WaterfallZoomer: public QwtPlotZoomer, public TimeScaleData, public FreqOffsetAndPrecisionClass
166 WaterfallZoomer(QwtPlotCanvas* canvas, const unsigned int freqPrecision)
167 : QwtPlotZoomer(canvas), TimeScaleData(),
168 FreqOffsetAndPrecisionClass(freqPrecision)
170 setTrackerMode(QwtPicker::AlwaysOn);
173 virtual ~WaterfallZoomer()
177 virtual void updateTrackerText()
182 void SetUnitType(const std::string &type)
188 virtual QwtText trackerText( const QwtDoublePoint& p ) const
192 timespec lineTime = timespec_add(GetZeroTime(), (-p.y()) * GetSecondsPerLine());
194 gmtime_r(&lineTime.tv_sec, &timeTm);
195 yLabel = (QString("").sprintf("%04d/%02d/%02d %02d:%02d:%02d.%03ld",
196 timeTm.tm_year+1900, timeTm.tm_mon+1,
197 timeTm.tm_mday, timeTm.tm_hour, timeTm.tm_min,
198 timeTm.tm_sec, lineTime.tv_nsec/1000000));
200 QwtText t(QString("%1 %2, %3").arg(p.x(), 0, 'f',
201 GetFrequencyPrecision()).arg(_unitType.c_str()).arg(yLabel));
207 std::string _unitType;
211 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
212 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_WHITE_HOT;
213 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_BLACK_HOT;
214 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_INCANDESCENT;
215 const int WaterfallDisplayPlot::INTENSITY_COLOR_MAP_TYPE_USER_DEFINED;
217 WaterfallDisplayPlot::WaterfallDisplayPlot(QWidget* parent)
222 _stopFrequency = 4000;
224 resize(parent->width(), parent->height());
227 _waterfallData = new WaterfallData(_startFrequency, _stopFrequency, _numPoints, 200);
230 palette.setColor(canvas()->backgroundRole(), QColor("white"));
231 canvas()->setPalette(palette);
233 setAxisTitle(QwtPlot::xBottom, "Frequency (Hz)");
234 setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(0));
236 setAxisTitle(QwtPlot::yLeft, "Time");
237 setAxisScaleDraw(QwtPlot::yLeft, new QwtTimeScaleDraw());
239 timespec_reset(&_lastReplot);
241 d_spectrogram = new PlotWaterfall(_waterfallData, "Waterfall Display");
243 _intensityColorMapType = INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR;
245 QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
246 colorMap.addColorStop(0.25, Qt::cyan);
247 colorMap.addColorStop(0.5, Qt::yellow);
248 colorMap.addColorStop(0.75, Qt::red);
250 d_spectrogram->setColorMap(colorMap);
252 d_spectrogram->attach(this);
254 // LeftButton for the zooming
255 // MidButton for the panning
256 // RightButton: zoom out by 1
257 // Ctrl+RighButton: zoom out to full size
259 _zoomer = new WaterfallZoomer(canvas(), 0);
260 #if QT_VERSION < 0x040000
261 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
262 Qt::RightButton, Qt::ControlModifier);
264 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
265 Qt::RightButton, Qt::ControlModifier);
267 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
270 _panner = new QwtPlotPanner(canvas());
271 _panner->setAxisEnabled(QwtPlot::yRight, false);
272 _panner->setMouseButton(Qt::MidButton);
274 // Avoid jumping when labels with more/less digits
275 // appear/disappear when scrolling vertically
277 const QFontMetrics fm(axisWidget(QwtPlot::yLeft)->font());
278 QwtScaleDraw *sd = axisScaleDraw(QwtPlot::yLeft);
279 sd->setMinimumExtent( fm.width("100.00") );
281 const QColor c(Qt::white);
282 _zoomer->setRubberBandPen(c);
283 _zoomer->setTrackerPen(c);
285 _UpdateIntensityRangeDisplay();
288 WaterfallDisplayPlot::~WaterfallDisplayPlot()
290 delete _waterfallData;
294 WaterfallDisplayPlot::Reset()
296 _waterfallData->ResizeData(_startFrequency, _stopFrequency, _numPoints);
297 _waterfallData->Reset();
299 // Load up the new base zoom settings
300 QwtDoubleRect newSize = _zoomer->zoomBase();
301 newSize.setLeft(_startFrequency);
302 newSize.setWidth(_stopFrequency-_startFrequency);
303 _zoomer->zoom(newSize);
304 _zoomer->setZoomBase(newSize);
309 WaterfallDisplayPlot::SetFrequencyRange(const double constStartFreq,
310 const double constStopFreq,
311 const double constCenterFreq,
312 const bool useCenterFrequencyFlag,
313 const double units, const std::string &strunits)
315 double startFreq = constStartFreq / units;
316 double stopFreq = constStopFreq / units;
317 double centerFreq = constCenterFreq / units;
319 _useCenterFrequencyFlag = useCenterFrequencyFlag;
321 if(_useCenterFrequencyFlag){
322 startFreq = (startFreq + centerFreq);
323 stopFreq = (stopFreq + centerFreq);
327 if((startFreq != _startFrequency) || (stopFreq != _stopFrequency))
330 if(stopFreq > startFreq) {
331 _startFrequency = startFreq;
332 _stopFrequency = stopFreq;
335 if((axisScaleDraw(QwtPlot::xBottom) != NULL) && (_zoomer != NULL)){
336 double display_units = ceil(log10(units)/2.0);
337 setAxisScale(QwtPlot::xBottom, _startFrequency, _stopFrequency);
338 setAxisScaleDraw(QwtPlot::xBottom, new WaterfallFreqDisplayScaleDraw(display_units));
344 ((WaterfallZoomer*)_zoomer)->SetFrequencyPrecision(display_units);
345 ((WaterfallZoomer*)_zoomer)->SetUnitType(strunits);
347 // Load up the new base zoom settings
348 _zoomer->setZoomBase();
350 // Zooms back to the base and clears any other zoom levels
358 WaterfallDisplayPlot::GetStartFrequency() const
360 return _startFrequency;
364 WaterfallDisplayPlot::GetStopFrequency() const
366 return _stopFrequency;
370 WaterfallDisplayPlot::PlotNewData(const double* dataPoints,
371 const int64_t numDataPoints,
372 const double timePerFFT,
373 const timespec timestamp,
374 const int droppedFrames)
376 if(numDataPoints > 0){
377 if(numDataPoints != _numPoints){
378 _numPoints = numDataPoints;
382 d_spectrogram->invalidateCache();
383 d_spectrogram->itemChanged();
389 _lastReplot = get_highres_clock();
392 if(diff_timespec(get_highres_clock(), _lastReplot) > timePerFFT) {
393 //FIXME: We may want to average the data between these updates to smooth display
394 _waterfallData->addFFTData(dataPoints, numDataPoints, droppedFrames);
395 _waterfallData->IncrementNumLinesToUpdate();
397 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
398 timeScale->SetSecondsPerLine(timePerFFT);
399 timeScale->SetZeroTime(timestamp);
401 ((WaterfallZoomer*)_zoomer)->SetSecondsPerLine(timePerFFT);
402 ((WaterfallZoomer*)_zoomer)->SetZeroTime(timestamp);
404 d_spectrogram->invalidateCache();
405 d_spectrogram->itemChanged();
409 _lastReplot = get_highres_clock();
415 WaterfallDisplayPlot::SetIntensityRange(const double minIntensity,
416 const double maxIntensity)
418 _waterfallData->setRange(QwtDoubleInterval(minIntensity, maxIntensity));
420 emit UpdatedLowerIntensityLevel(minIntensity);
421 emit UpdatedUpperIntensityLevel(maxIntensity);
423 _UpdateIntensityRangeDisplay();
427 WaterfallDisplayPlot::replot()
429 QwtTimeScaleDraw* timeScale = (QwtTimeScaleDraw*)axisScaleDraw(QwtPlot::yLeft);
430 timeScale->initiateUpdate();
432 WaterfallFreqDisplayScaleDraw* freqScale = (WaterfallFreqDisplayScaleDraw*)axisScaleDraw(QwtPlot::xBottom);
433 freqScale->initiateUpdate();
435 // Update the time axis display
436 if(axisWidget(QwtPlot::yLeft) != NULL){
437 axisWidget(QwtPlot::yLeft)->update();
440 // Update the Frequency Offset Display
441 if(axisWidget(QwtPlot::xBottom) != NULL){
442 axisWidget(QwtPlot::xBottom)->update();
446 ((WaterfallZoomer*)_zoomer)->updateTrackerText();
453 WaterfallDisplayPlot::resizeSlot( QSize *s )
455 resize(s->width(), s->height());
459 WaterfallDisplayPlot::GetIntensityColorMapType() const
461 return _intensityColorMapType;
465 WaterfallDisplayPlot::SetIntensityColorMapType(const int newType,
466 const QColor lowColor,
467 const QColor highColor)
469 if((_intensityColorMapType != newType) ||
470 ((newType == INTENSITY_COLOR_MAP_TYPE_USER_DEFINED) &&
471 (lowColor.isValid() && highColor.isValid()))){
473 case INTENSITY_COLOR_MAP_TYPE_MULTI_COLOR:{
474 _intensityColorMapType = newType;
475 QwtLinearColorMap colorMap(Qt::darkCyan, Qt::white);
476 colorMap.addColorStop(0.25, Qt::cyan);
477 colorMap.addColorStop(0.5, Qt::yellow);
478 colorMap.addColorStop(0.75, Qt::red);
479 d_spectrogram->setColorMap(colorMap);
482 case INTENSITY_COLOR_MAP_TYPE_WHITE_HOT:{
483 _intensityColorMapType = newType;
484 QwtLinearColorMap colorMap(Qt::black, Qt::white);
485 d_spectrogram->setColorMap(colorMap);
488 case INTENSITY_COLOR_MAP_TYPE_BLACK_HOT:{
489 _intensityColorMapType = newType;
490 QwtLinearColorMap colorMap(Qt::white, Qt::black);
491 d_spectrogram->setColorMap(colorMap);
494 case INTENSITY_COLOR_MAP_TYPE_INCANDESCENT:{
495 _intensityColorMapType = newType;
496 QwtLinearColorMap colorMap(Qt::black, Qt::white);
497 colorMap.addColorStop(0.5, Qt::darkRed);
498 d_spectrogram->setColorMap(colorMap);
501 case INTENSITY_COLOR_MAP_TYPE_USER_DEFINED:{
502 _userDefinedLowIntensityColor = lowColor;
503 _userDefinedHighIntensityColor = highColor;
504 _intensityColorMapType = newType;
505 QwtLinearColorMap colorMap(_userDefinedLowIntensityColor, _userDefinedHighIntensityColor);
506 d_spectrogram->setColorMap(colorMap);
512 _UpdateIntensityRangeDisplay();
517 WaterfallDisplayPlot::GetUserDefinedLowIntensityColor() const
519 return _userDefinedLowIntensityColor;
523 WaterfallDisplayPlot::GetUserDefinedHighIntensityColor() const
525 return _userDefinedHighIntensityColor;
529 WaterfallDisplayPlot::_UpdateIntensityRangeDisplay()
531 QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
532 rightAxis->setTitle("Intensity (dB)");
533 rightAxis->setColorBarEnabled(true);
534 rightAxis->setColorMap(d_spectrogram->data()->range(),
535 d_spectrogram->colorMap());
537 setAxisScale(QwtPlot::yRight,
538 d_spectrogram->data()->range().minValue(),
539 d_spectrogram->data()->range().maxValue() );
540 enableAxis(QwtPlot::yRight);
542 plotLayout()->setAlignCanvasToScales(true);
544 // Tell the display to redraw everything
545 d_spectrogram->invalidateCache();
546 d_spectrogram->itemChanged();
551 // Update the last replot timer
552 _lastReplot = get_highres_clock();
555 #endif /* WATERFALL_DISPLAY_PLOT_C */