forked from zhitko/speech-recognition-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotter.cpp
144 lines (121 loc) · 3.28 KB
/
plotter.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
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
#include "plotter.h"
#include <QDebug>
#include <qwt_plot_canvas.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_layout.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
const int colorCount = 13;
const QColor colors[] = {
Qt::red, //A1
Qt::green, //A2
Qt::blue, //A3
Qt::black, //Spectrum Normalize
Qt::cyan, //Frequency parameters
Qt::magenta,
Qt::yellow,
Qt::darkRed,
Qt::darkGreen,
Qt::darkBlue,
Qt::darkCyan,
Qt::darkMagenta,
Qt::darkYellow
};
Plotter::Plotter( QWidget *parent ):
QwtPlot( parent )
{
// Assign a title
setTitle( "WAVE DATA" );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
canvas->setLineWidth( 1 );
canvas->setPalette( Qt::white );
setCanvas( canvas );
alignScales();
// Insert grid
d_grid = new QwtPlotGrid();
d_grid->attach( this );
// Axis
setAxisTitle( QwtPlot::xBottom, "Time" );
// setAxisScale( QwtPlot::xBottom, -d_interval, 0.0 );
setAxisTitle( QwtPlot::yLeft, "Amplitude" );
// setAxisScale( QwtPlot::yLeft, -1.0, 1.0 );
resize(600, 350);
}
//
// Set a plain canvas frame and align the scales to it
//
void Plotter::alignScales()
{
// The code below shows how to align the scales to
// the canvas frame, but is also a good example demonstrating
// why the spreaded API needs polishing.
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
{
QwtScaleWidget *scaleWidget = axisWidget( i );
if ( scaleWidget )
scaleWidget->setMargin( 0 );
QwtScaleDraw *scaleDraw = axisScaleDraw( i );
if ( scaleDraw )
scaleDraw->enableComponent( QwtAbstractScaleDraw::Backbone, false );
}
plotLayout()->setAlignCanvasToScales( true );
}
void Plotter::appendPoint(qreal y)
{
appendPoint(y, "default");
}
void Plotter::appendPoint(qreal y, QString name)
{
qreal lastX = 0;
QVector<QPointF> * points;
if(d_points.contains(name)){
points = d_points.value(name);
}else{
points = new QVector<QPointF>();
d_points.insert(name, points);
}
if(points->size() != 0)
lastX = points->last().rx();
lastX += 1.0;
addPoint(lastX, y, name);
}
void Plotter::addPoint(qreal x, qreal y)
{
addPoint(x, y, "default");
}
void Plotter::addPoint(qreal x, qreal y, QString name)
{
QwtPlotCurve * d_curve;
if(!d_curves.contains(name))
{
// Insert curve
d_curve = new QwtPlotCurve(name);
QColor color = colors[d_curves.count()];
d_curve->setPen( color, 2.0, Qt::SolidLine );
d_curve->attach( this );
d_curves.insert(name, d_curve);
}else{
d_curve = d_curves.value(name);
}
QVector<QPointF> * points;
if(d_points.contains(name)){
points = d_points.value(name);
}else{
points = new QVector<QPointF>();
d_points.insert(name, points);
}
points->append(QPointF(x,y));
d_curve->setSamples(*points);
}
void Plotter::applyPlot()
{
for(QString name: d_curves.keys())
{
QwtPlotCurve * d_curve = d_curves.value(name);
QVector<QPointF> * points = d_points.value(name);
d_curve->setSamples(*points);
}
Plotter::replot();
}