1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- c++ -*- */
/*
* Copyright 2008-2012 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef TIME_DOMAIN_DISPLAY_PLOT_C
#define TIME_DOMAIN_DISPLAY_PLOT_C
#include <qtgui/TimeDomainDisplayPlot.h>
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <QColor>
#include <cmath>
#include <iostream>
#include <volk/volk.h>
class TimePrecisionClass
{
public:
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);
}
virtual ~TimeDomainDisplayZoomer(){
}
virtual void updateTrackerText(){
updateDisplay();
}
void setUnitType(const std::string &type)
{
_unitType = type;
}
protected:
using QwtPlotZoomer::trackerText;
virtual QwtText trackerText( const QPoint& p ) const
{
QwtText t;
QwtDoublePoint dp = QwtPlotZoomer::invTransform(p);
if((dp.y() > 0.0001) && (dp.y() < 10000)) {
t.setText(QString("%1 %2, %3 V").
arg(dp.x(), 0, 'f', getTimePrecision()).
arg(_unitType.c_str()).
arg(dp.y(), 0, 'f', 4));
}
else {
t.setText(QString("%1 %2, %3 V").
arg(dp.x(), 0, 'f', getTimePrecision()).
arg(_unitType.c_str()).
arg(dp.y(), 0, 'e', 4));
}
return t;
}
private:
std::string _unitType;
};
/***********************************************************************
* Main Time domain plotter widget
**********************************************************************/
TimeDomainDisplayPlot::TimeDomainDisplayPlot(int nplots, QWidget* parent)
: DisplayPlot(nplots, parent)
{
_numPoints = 1024;
_xAxisPoints = new double[_numPoints];
memset(_xAxisPoints, 0x0, _numPoints*sizeof(double));
_zoomer = new TimeDomainDisplayZoomer(canvas(), 0);
#if QWT_VERSION < 0x060000
_zoomer->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection);
#endif
_zoomer->setMousePattern(QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlModifier);
_zoomer->setMousePattern(QwtEventPattern::MouseSelect3,
Qt::RightButton);
const QColor c(Qt::darkRed);
_zoomer->setRubberBandPen(c);
_zoomer->setTrackerPen(c);
d_semilogx = false;
d_semilogy = false;
setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
setXaxis(0, _numPoints);
setAxisTitle(QwtPlot::xBottom, "Time (sec)");
setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
setYaxis(-2.0, 2.0);
setAxisTitle(QwtPlot::yLeft, "Amplitude");
QList<QColor> colors;
colors << QColor(Qt::blue) << QColor(Qt::red) << QColor(Qt::green)
<< QColor(Qt::black) << QColor(Qt::cyan) << QColor(Qt::magenta)
<< QColor(Qt::yellow) << QColor(Qt::gray) << QColor(Qt::darkRed)
<< QColor(Qt::darkGreen) << QColor(Qt::darkBlue) << QColor(Qt::darkGray);
// Setup dataPoints and plot vectors
// Automatically deleted when parent is deleted
for(int i = 0; i < _nplots; i++) {
_dataPoints.push_back(new double[_numPoints]);
memset(_dataPoints[i], 0x0, _numPoints*sizeof(double));
_plot_curve.push_back(new QwtPlotCurve(QString("Data %1").arg(i)));
_plot_curve[i]->attach(this);
_plot_curve[i]->setPen(QPen(colors[i]));
_plot_curve[i]->setRenderHint(QwtPlotItem::RenderAntialiased);
QwtSymbol *symbol = new QwtSymbol(QwtSymbol::NoSymbol, QBrush(colors[i]),
QPen(colors[i]), QSize(7,7));
#if QWT_VERSION < 0x060000
_plot_curve[i]->setRawData(_xAxisPoints, _dataPoints[i], _numPoints);
_plot_curve[i]->setSymbol(*symbol);
#else
_plot_curve[i]->setRawSamples(_xAxisPoints, _dataPoints[i], _numPoints);
_plot_curve[i]->setSymbol(symbol);
#endif
}
_sampleRate = 1;
_resetXAxisPoints();
}
TimeDomainDisplayPlot::~TimeDomainDisplayPlot()
{
for(int i = 0; i < _nplots; i++)
delete [] _dataPoints[i];
delete[] _xAxisPoints;
// _zoomer and _panner deleted when parent deleted
}
void
TimeDomainDisplayPlot::replot()
{
QwtPlot::replot();
}
void
TimeDomainDisplayPlot::plotNewData(const std::vector<double*> dataPoints,
const int64_t numDataPoints,
const double timeInterval)
{
if(!_stop) {
if((numDataPoints > 0)) {
if(numDataPoints != _numPoints){
_numPoints = numDataPoints;
delete[] _xAxisPoints;
_xAxisPoints = new double[_numPoints];
for(int i = 0; i < _nplots; i++) {
delete[] _dataPoints[i];
_dataPoints[i] = new double[_numPoints];
#if QWT_VERSION < 0x060000
_plot_curve[i]->setRawData(_xAxisPoints, _dataPoints[i], _numPoints);
#else
_plot_curve[i]->setRawSamples(_xAxisPoints, _dataPoints[i], _numPoints);
#endif
}
_resetXAxisPoints();
}
for(int i = 0; i < _nplots; i++) {
if(d_semilogy) {
for(int n = 0; n < numDataPoints; n++)
_dataPoints[i][n] = fabs(dataPoints[i][n]);
}
else {
memcpy(_dataPoints[i], dataPoints[i], numDataPoints*sizeof(double));
}
}
if(_autoscale_state) {
double bottom=1e20, top=-1e20;
for(int n = 0; n < _nplots; n++) {
for(int64_t point = 0; point < numDataPoints; point++) {
if(_dataPoints[n][point] < bottom) {
bottom = _dataPoints[n][point];
}
if(_dataPoints[n][point] > top) {
top = _dataPoints[n][point];
}
}
}
_autoScale(bottom, top);
}
replot();
}
}
}
void
TimeDomainDisplayPlot::_resetXAxisPoints()
{
double delt = 1.0/_sampleRate;
for(long loc = 0; loc < _numPoints; loc++){
_xAxisPoints[loc] = loc*delt;
}
// Set up zoomer base for maximum unzoom x-axis
// and reset to maximum unzoom level
QwtDoubleRect zbase = _zoomer->zoomBase();
if(d_semilogx) {
setAxisScale(QwtPlot::xBottom, 1e-1, _numPoints*delt);
zbase.setLeft(1e-1);
}
else {
setAxisScale(QwtPlot::xBottom, 0, _numPoints*delt);
zbase.setLeft(0);
}
zbase.setRight(_numPoints*delt);
_zoomer->zoom(zbase);
_zoomer->setZoomBase(zbase);
_zoomer->zoom(0);
}
void
TimeDomainDisplayPlot::_autoScale(double bottom, double top)
{
// Auto scale the y-axis with a margin of 20% (10 dB for log scale)
double _bot = bottom - fabs(bottom)*0.20;
double _top = top + fabs(top)*0.20;
if(d_semilogy) {
if(bottom > 0) {
setYaxis(_bot-10, _top+10);
}
else {
setYaxis(1e-3, _top+10);
}
}
else {
setYaxis(_bot, _top);
}
}
void
TimeDomainDisplayPlot::setAutoScale(bool state)
{
_autoscale_state = state;
}
void
TimeDomainDisplayPlot::setSampleRate(double sr, double units,
const std::string &strunits)
{
double newsr = sr/units;
if(newsr != _sampleRate) {
_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);
}
}
void
TimeDomainDisplayPlot::stemPlot(bool en)
{
if(en) {
for(int i = 0; i < _nplots; i++) {
_plot_curve[i]->setStyle(QwtPlotCurve::Sticks);
setLineMarker(i, QwtSymbol::Ellipse);
}
}
else {
for(int i = 0; i < _nplots; i++) {
_plot_curve[i]->setStyle(QwtPlotCurve::Lines);
setLineMarker(i, QwtSymbol::NoSymbol);
}
}
}
void
TimeDomainDisplayPlot::setSemilogx(bool en)
{
d_semilogx = en;
if(!d_semilogx) {
setAxisScaleEngine(QwtPlot::xBottom, new QwtLinearScaleEngine);
}
else {
setAxisScaleEngine(QwtPlot::xBottom, new QwtLog10ScaleEngine);
}
_resetXAxisPoints();
}
void
TimeDomainDisplayPlot::setSemilogy(bool en)
{
if(d_semilogy != en) {
d_semilogy = en;
double max = axisScaleDiv(QwtPlot::yLeft)->upperBound();
if(!d_semilogy) {
setAxisScaleEngine(QwtPlot::yLeft, new QwtLinearScaleEngine);
setYaxis(-pow(10.0, max/10.0), pow(10.0, max/10.0));
}
else {
setAxisScaleEngine(QwtPlot::yLeft, new QwtLog10ScaleEngine);
setYaxis(1e-10, 10.0*log10(max));
}
}
}
#endif /* TIME_DOMAIN_DISPLAY_PLOT_C */
|