-
Notifications
You must be signed in to change notification settings - Fork 5
/
WaterfallData.h
236 lines (192 loc) · 7.06 KB
/
WaterfallData.h
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
#ifndef WATERFALLDATA_H
#define WATERFALLDATA_H
#include <qwt_matrix_raster_data.h>
#include <ctime>
template <class T>
class WaterfallData : public QwtMatrixRasterData
{
static_assert(std::is_arithmetic<T>::value, "WaterfallData's data must be numeric !");
public:
WaterfallData(double dXMin, double dXMax, // X bounds
const size_t historyExtent, // will define Y width
const size_t layerPoints) :
m_data(new T[historyExtent * layerPoints]),
m_offset(0),
m_layerPoints(layerPoints),
m_maxHistoryLength(historyExtent),
m_currentHistoryLength(0),
m_layersTimestamps(new time_t[historyExtent])
{
if (m_layerPoints == 0 || m_maxHistoryLength == 0)
{
throw "Bad usage of WaterfallData !"; // better: call abort();
}
// initialize data with zeroes or the minimal value of T type
clear();
// sanitize
if (dXMin > dXMax)
{
std::swap(dXMin, dXMax);
}
m_xMin = dXMin;
m_xMax = dXMax;
setInterval(Qt::XAxis,
QwtInterval(dXMin, dXMax, QwtInterval::ExcludeMaximum));
setInterval(Qt::YAxis,
QwtInterval(m_offset, m_maxHistoryLength + m_offset, QwtInterval::ExcludeMaximum));
}
~WaterfallData() override
{
delete [] m_data;
delete [] m_layersTimestamps;
}
// overriden methods
double value(double x, double y) const override
{
const QwtInterval xInterval = interval(Qt::XAxis);
const QwtInterval yInterval = interval(Qt::YAxis);
// + valid
if (!(xInterval.contains(x) && yInterval.contains(y)))
{
return qQNaN();
}
// spacing !
double dx = xInterval.width() / m_layerPoints;
double dy = yInterval.width() / m_maxHistoryLength;
int row = int((y - yInterval.minValue()) / dy);
int col = int((x - xInterval.minValue()) / dx);
if (row >= m_maxHistoryLength)
{
row = m_maxHistoryLength - 1;
}
if (col >= m_layerPoints)
{
col = m_layerPoints - 1;
}
return double(m_data[row * m_layerPoints + col]);
}
/* pixelHint() returns the geometry of a pixel, that can be used
to calculate the resolution and alignment of the plot item, that is
representing the data.
- NearestNeighbour\n
pixelHint() returns the surrounding pixel of the top left value
in the matrix.
- BilinearInterpolation\n
Returns an empty rectangle recommending
to render in target device ( f.e. screen ) resolution.
*/
QRectF pixelHint(const QRectF& area) const override
{
Q_UNUSED(area)
QRectF rect;
if (resampleMode() == NearestNeighbour)
{
const QwtInterval intervalX = interval(Qt::XAxis);
const QwtInterval intervalY = interval(Qt::YAxis);
if (intervalX.isValid() && intervalY.isValid())
{
// spacing in X and Y
const double dx = intervalX.width() / m_layerPoints;
const double dy = intervalY.width() / m_maxHistoryLength;
rect = QRectF(intervalX.minValue(), intervalY.minValue(),
dx, dy);
}
}
return rect;
}
bool addData(const T* const fftData, const size_t length, const time_t timestamp)
{
if (length != m_layerPoints)
{
return false;
}
// another solution is to use move_backward and use m_currentHistoryLength
// the only benefit is to move only the filled layers
// and not all the waterfall layers - 1 when this last is not completely filled !
std::move(m_data + m_layerPoints,
m_data + m_layerPoints + (m_maxHistoryLength - 1) * m_layerPoints,
m_data);
std::copy(fftData, fftData + length, &m_data[m_layerPoints * (m_maxHistoryLength - 1)]);
// do the same for the array of timestamps !
std::move(m_layersTimestamps + 1,
m_layersTimestamps + 1 + (m_maxHistoryLength - 1),
m_layersTimestamps);
m_layersTimestamps[m_maxHistoryLength - 1] = timestamp;
if (m_currentHistoryLength < m_maxHistoryLength)
{
++m_currentHistoryLength;
}
++m_offset;
setInterval(Qt::YAxis,
QwtInterval(m_offset, m_maxHistoryLength + m_offset, QwtInterval::ExcludeMaximum));
return true;
}
void clear()
{
std::fill(m_data, m_data + m_layerPoints * m_maxHistoryLength, 0.);
m_currentHistoryLength = 0;
std::fill(m_layersTimestamps, m_layersTimestamps + m_maxHistoryLength, 0);
m_offset = 0;
setInterval(Qt::YAxis,
QwtInterval(0, m_maxHistoryLength, QwtInterval::ExcludeMaximum));
}
inline size_t getLayerPoints() const { return m_layerPoints; }
inline size_t getMaxHistoryLength() const { return m_maxHistoryLength; }
inline size_t getHistoryLength() const { return m_currentHistoryLength; }
// representation/view data range (may not be equal to the stored data range)
void setRange(double dLower, double dUpper)
{
if (dLower > dUpper)
{
std::swap(dLower, dUpper);
}
setInterval(Qt::ZAxis, QwtInterval(dLower, dUpper));
}
void getRange(double& rangeMin, double& rangeMax) const
{
const QwtInterval& range = interval(Qt::ZAxis);
rangeMin = range.minValue();
rangeMax = range.maxValue();
}
// stored data range !
void getDataRange(double& rangeMin, double& rangeMax) const
{
if (m_currentHistoryLength > 0)
{
auto resultPair = std::minmax_element(
m_data + (m_maxHistoryLength - m_currentHistoryLength) * m_layerPoints,
m_data + m_layerPoints * m_maxHistoryLength);
rangeMin = double(*resultPair.first);
rangeMax = double(*resultPair.second);
}
else
{
rangeMin = rangeMax = 0;
}
}
size_t getCurrentHistoryLength() const { return m_currentHistoryLength; }
time_t getLayerDate(const double y) const
{
const size_t index = y;
if (index < m_maxHistoryLength)
{
return m_layersTimestamps[index];
}
return 0;
}
const T* getData() const { return m_data; }
const time_t* getTimes() const { return m_layersTimestamps; }
double getXMin() const { return m_xMin; }
double getXMax() const { return m_xMax; }
double getOffset() const { return m_offset; }
protected:
T* const m_data;
double m_offset;
const size_t m_layerPoints; // fft points
const size_t m_maxHistoryLength; // max number of layers (Y width)
size_t m_currentHistoryLength; // filled layers count
time_t* const m_layersTimestamps;
double m_xMin;
double m_xMax;
};
#endif // WATERFALLDATA_H