Maxing the x-axis of the time domain plot represent the actual time of the samples...
authorTom Rondeau <trondeau@vt.edu>
Sat, 1 May 2010 23:48:21 +0000 (19:48 -0400)
committerTom Rondeau <trondeau@vt.edu>
Sat, 1 May 2010 23:48:21 +0000 (19:48 -0400)
gr-qtgui/src/lib/TimeDomainDisplayPlot.cc
gr-qtgui/src/lib/TimeDomainDisplayPlot.h
gr-qtgui/src/lib/spectrumdisplayform.cc

index 708ac1661729b8ef279b00a5237117cb177bb4ac..e6f6581d230694233f2aa2d7fabbb48795b0e0a1 100644 (file)
@@ -7,10 +7,37 @@
 #include <qwt_legend.h>
 
 
-class TimeDomainDisplayZoomer: public QwtPlotZoomer
+class TimePrecisionClass
 {
 public:
-  TimeDomainDisplayZoomer(QwtPlotCanvas* canvas):QwtPlotZoomer(canvas)
+  TimePrecisionClass(const int timePrecision)
+  {
+    _timePrecision = timePrecision;
+  }
+
+  virtual ~TimePrecisionClass()
+  {
+  }
+
+  virtual unsigned int GetTimePrecision() const
+  {
+    return _timePrecision;
+  }
+
+  virtual void SetTimePrecision(const unsigned int newPrecision)
+  {
+    _timePrecision = newPrecision;
+  }
+protected:
+  unsigned int _timePrecision;
+};
+
+
+class TimeDomainDisplayZoomer: public QwtPlotZoomer, public TimePrecisionClass
+{
+public:
+  TimeDomainDisplayZoomer(QwtPlotCanvas* canvas, const unsigned int timePrecision)
+    : QwtPlotZoomer(canvas),TimePrecisionClass(timePrecision)
   {
     setTrackerMode(QwtPicker::AlwaysOn);
   }
@@ -23,13 +50,23 @@ public:
     updateDisplay();
   }
 
+  void SetUnitType(const std::string &type)
+  {
+    _unitType = type;
+  }
+
 protected:
   virtual QwtText trackerText( const QwtDoublePoint& p ) const 
   {
-    QwtText t(QString("Sample %1, %2 V").arg(p.x(), 0, 'f', 0).arg(p.y(), 0, 'f', 4));
+    QwtText t(QString("%1 %2, %3 V").arg(p.x(), 0, 'f', GetTimePrecision()).
+             arg(_unitType.c_str()).
+             arg(p.y(), 0, 'f', 4));
 
     return t;
   }
+
+private:
+  std::string _unitType;
 };
 
 TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
@@ -43,7 +80,7 @@ TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
   _imagDataPoints = new double[_numPoints];
   _xAxisPoints = new double[_numPoints];
 
-  _zoomer = new TimeDomainDisplayZoomer(canvas());
+  _zoomer = new TimeDomainDisplayZoomer(canvas(), 0);
 
   // Disable polygon clipping
   QwtPainter::setDeviceClipping(false);
@@ -58,7 +95,7 @@ TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
 
   setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
   set_xaxis(0, _numPoints);
-  setAxisTitle(QwtPlot::xBottom, "Sample Number");
+  setAxisTitle(QwtPlot::xBottom, "Time (sec)");
 
   setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
   set_yaxis(-2.0, 2.0);
@@ -80,6 +117,7 @@ TimeDomainDisplayPlot::TimeDomainDisplayPlot(QWidget* parent):QwtPlot(parent)
   memset(_imagDataPoints, 0x0, _numPoints*sizeof(double));
   memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
 
+  _sampleRate = 1;
   _resetXAxisPoints();
 
   replot();
@@ -187,19 +225,39 @@ void TimeDomainDisplayPlot::PlotNewData(const double* realDataPoints,
   }
 }
 
-void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag){
+void TimeDomainDisplayPlot::SetImaginaryDataVisible(const bool visibleFlag)
+{
   _imag_plot_curve->setVisible(visibleFlag);
 }
 
-void TimeDomainDisplayPlot::_resetXAxisPoints(){
+void TimeDomainDisplayPlot::_resetXAxisPoints()
+{
+  double delt = 1.0/_sampleRate;
   for(long loc = 0; loc < _numPoints; loc++){
-    _xAxisPoints[loc] = loc;
+    _xAxisPoints[loc] = loc*delt;
   }
-  setAxisScale(QwtPlot::xBottom, 0, _numPoints);
+  setAxisScale(QwtPlot::xBottom, 0, _numPoints*delt);
 }
 
-void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on){
+void TimeDomainDisplayPlot::LegendEntryChecked(QwtPlotItem* plotItem, bool on)
+{
   plotItem->setVisible(!on);
 }
 
+void
+TimeDomainDisplayPlot::SetSampleRate(double sr, double units,
+                                    const std::string &strunits)
+{
+  _sampleRate = sr/units;
+  _resetXAxisPoints();
+
+  // While we could change the displayed sigfigs based on the unit being
+  // displayed, I think it looks better by just setting it to 4 regardless.
+  //double display_units = ceil(log10(units)/2.0);
+  double display_units = 4;
+  setAxisTitle(QwtPlot::xBottom, QString("Time (%1)").arg(strunits.c_str()));
+  ((TimeDomainDisplayZoomer*)_zoomer)->SetTimePrecision(display_units);
+  ((TimeDomainDisplayZoomer*)_zoomer)->SetUnitType(strunits);
+}
+
 #endif /* TIME_DOMAIN_DISPLAY_PLOT_C */
index 88f97cb808694bfd72b9b43ee791dc4de72de2be..5525bbabe956bc2379c33426f0f28449056aff92 100644 (file)
@@ -33,6 +33,8 @@ public:
 
 public slots:
   void resizeSlot( QSize *s );
+  void SetSampleRate(double sr, double units, 
+                    const std::string &strunits);
 
 protected slots:
   void LegendEntryChecked(QwtPlotItem *plotItem, bool on);
@@ -52,6 +54,8 @@ private:
   double* _imagDataPoints;
   double* _xAxisPoints;
 
+  double _sampleRate;
+
   timespec _lastReplot;
 
   int64_t _numPoints;
index 0170f78623afa3d05ed9758ffdddef051a007bbe..9e7609a771a513117e99a0ca5222487f6a14b24d 100644 (file)
@@ -415,6 +415,7 @@ SpectrumDisplayForm::SetFrequencyRange(const double newCenterFrequency,
 
   if(fdiff > 0) {
     std::string strunits[4] = {"Hz", "kHz", "MHz", "GHz"};
+    std::string strtime[4] = {"sec", "ms", "us", "ns"};
     double units10 = floor(log10(fdiff));
     double units3  = std::max(floor(units10 / 3.0), 0.0);
     double units = pow(10, (units10-fmod(units10, 3.0)));
@@ -424,23 +425,25 @@ SpectrumDisplayForm::SetFrequencyRange(const double newCenterFrequency,
     _stopFrequency = newStopFrequency;
     _centerFrequency = newCenterFrequency;
 
-    _frequencyDisplayPlot->SetFrequencyRange(newStartFrequency,
-                                            newStopFrequency,
-                                            newCenterFrequency,
+    _frequencyDisplayPlot->SetFrequencyRange(_startFrequency,
+                                            _stopFrequency,
+                                            _centerFrequency,
                                             UseRFFrequenciesCheckBox->isChecked(),
                                             units, strunits[iunit]);
-    _waterfallDisplayPlot->SetFrequencyRange(newStartFrequency,
-                                            newStopFrequency,
-                                            newCenterFrequency,
+    _waterfallDisplayPlot->SetFrequencyRange(_startFrequency,
+                                            _stopFrequency,
+                                            _centerFrequency,
                                             UseRFFrequenciesCheckBox->isChecked(),
                                             units, strunits[iunit]);
     if((QGLFormat::hasOpenGL()) && (_useOpenGL)) {
-      _waterfall3DDisplayPlot->SetFrequencyRange(newStartFrequency,
-                                                newStopFrequency,
-                                                newCenterFrequency,
+      _waterfall3DDisplayPlot->SetFrequencyRange(_startFrequency,
+                                                _stopFrequency,
+                                                _centerFrequency,
                                                 UseRFFrequenciesCheckBox->isChecked(),
                                                 units, strunits[iunit]);
     }
+    _timeDomainDisplayPlot->SetSampleRate(_stopFrequency - _startFrequency,
+                                         units, strtime[iunit]);
   }
 }