forked from miek/inspectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceplot.cpp
74 lines (63 loc) · 2.58 KB
/
traceplot.cpp
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
/*
* Copyright (C) 2016, Mike Walters <[email protected]>
*
* This file is part of inspectrum.
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "samplesource.h"
#include "traceplot.h"
TracePlot::TracePlot(std::shared_ptr<AbstractSampleSource> source) : sampleSource(source) {
}
void TracePlot::paintMid(QPainter &painter, QRect &rect, range_t<off_t> sampleRange)
{
auto firstSample = sampleRange.minimum;
auto length = sampleRange.length();
// Is it a 2-channel (complex) trace?
if (auto src = dynamic_cast<SampleSource<std::complex<float>>*>(sampleSource.get())) {
auto samples = src->getSamples(firstSample, length);
if (samples == nullptr)
return;
painter.setPen(Qt::red);
plotTrace(painter, rect, reinterpret_cast<float*>(samples.get()), length, 2);
painter.setPen(Qt::blue);
plotTrace(painter, rect, reinterpret_cast<float*>(samples.get())+1, length, 2);
// Otherwise is it single channel?
} else if (auto src = dynamic_cast<SampleSource<float>*>(sampleSource.get())) {
auto samples = src->getSamples(firstSample, length);
if (samples == nullptr)
return;
painter.setPen(Qt::green);
plotTrace(painter, rect, samples.get(), length, 1);
} else {
throw std::runtime_error("TracePlot::paintMid: Unsupported source type");
}
}
void TracePlot::plotTrace(QPainter &painter, QRect &rect, float *samples, off_t count, int step = 1)
{
int xprev = 0;
int yprev = 0;
for (off_t i = 0; i < count; i++) {
float sample = samples[i*step];
int x = (float)i / count * rect.width();
int y = rect.height() - ((sample * rect.height()/2) + rect.height()/2);
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= rect.width()-1) x = rect.width()-2;
if (y >= rect.height()-1) y = rect.height()-2;
painter.drawLine(xprev + rect.x(), yprev + rect.y(), x + rect.x(), y + rect.y());
xprev = x;
yprev = y;
}
}