2
#include <qwt_scale_engine.h>
3
#include <qwt_symbol.h>
4
#include <qwt_plot_grid.h>
5
#include <qwt_plot_marker.h>
6
#include <qwt_plot_curve.h>
7
#include <qwt_legend.h>
10
#include "bode_plot.h"
12
static void logSpace(double *array, int size, double xmin, double xmax)
14
if ((xmin <= 0.0) || (xmax <= 0.0) || (size <= 0))
17
const int imax = size -1;
22
const double lxmin = log(xmin);
23
const double lxmax = log(xmax);
24
const double lstep = (lxmax - lxmin) / double(imax);
26
for (int i = 1; i < imax; i++)
27
array[i] = exp(lxmin + double(i) * lstep);
30
BodePlot::BodePlot(QWidget *parent):
35
setTitle("Frequency Response of a Second-Order System");
37
setCanvasBackground(QColor(Qt::darkBlue));
40
QwtLegend *legend = new QwtLegend;
41
legend->setFrameStyle(QFrame::Box|QFrame::Sunken);
42
insertLegend(legend, QwtPlot::BottomLegend);
45
QwtPlotGrid *grid = new QwtPlotGrid;
46
grid->enableXMin(true);
47
grid->setMajPen(QPen(Qt::white, 0, Qt::DotLine));
48
grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
52
enableAxis(QwtPlot::yRight);
53
setAxisTitle(QwtPlot::xBottom, "Normalized Frequency");
54
setAxisTitle(QwtPlot::yLeft, "Amplitude [dB]");
55
setAxisTitle(QwtPlot::yRight, "Phase [deg]");
57
setAxisMaxMajor(QwtPlot::xBottom, 6);
58
setAxisMaxMinor(QwtPlot::xBottom, 10);
59
setAxisScaleEngine(QwtPlot::xBottom, new QwtLog10ScaleEngine);
62
d_crv1 = new QwtPlotCurve("Amplitude");
63
#if QT_VERSION >= 0x040000
64
d_crv1->setRenderHint(QwtPlotItem::RenderAntialiased);
66
d_crv1->setPen(QPen(Qt::yellow));
67
d_crv1->setYAxis(QwtPlot::yLeft);
70
d_crv2 = new QwtPlotCurve("Phase");
71
#if QT_VERSION >= 0x040000
72
d_crv2->setRenderHint(QwtPlotItem::RenderAntialiased);
74
d_crv2->setPen(QPen(Qt::cyan));
75
d_crv2->setYAxis(QwtPlot::yRight);
79
d_mrk1 = new QwtPlotMarker();
80
d_mrk1->setValue(0.0, 0.0);
81
d_mrk1->setLineStyle(QwtPlotMarker::VLine);
82
d_mrk1->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
83
d_mrk1->setLinePen(QPen(Qt::green, 0, Qt::DashDotLine));
86
d_mrk2 = new QwtPlotMarker();
87
d_mrk2->setLineStyle(QwtPlotMarker::HLine);
88
d_mrk2->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
89
d_mrk2->setLinePen(QPen(QColor(200,150,0), 0, Qt::DashDotLine));
90
d_mrk2->setSymbol( QwtSymbol(QwtSymbol::Diamond,
91
QColor(Qt::yellow), QColor(Qt::green), QSize(7,7)));
99
void BodePlot::showData(double *frequency, double *amplitude,
100
double *phase, int count)
102
d_crv1->setData(frequency, amplitude, count);
103
d_crv2->setData(frequency, phase, count);
106
void BodePlot::showPeak(double freq, double amplitude)
109
label.sprintf("Peak: %.3g dB", amplitude);
112
text.setFont(QFont("Helvetica", 10, QFont::Bold));
113
text.setColor(QColor(200,150,0));
115
d_mrk2->setValue(freq, amplitude);
116
d_mrk2->setLabel(text);
119
void BodePlot::show3dB(double freq)
122
label.sprintf("-3 dB at f = %.3g", freq);
125
text.setFont(QFont("Helvetica", 10, QFont::Bold));
126
text.setColor(Qt::green);
128
d_mrk1->setValue(freq, 0.0);
129
d_mrk1->setLabel(text);
133
// re-calculate frequency response
135
void BodePlot::setDamp(double damping)
137
const bool doReplot = autoReplot();
138
setAutoReplot(false);
140
const int ArraySize = 200;
142
double frequency[ArraySize];
143
double amplitude[ArraySize];
144
double phase[ArraySize];
146
// build frequency vector with logarithmic division
147
logSpace(frequency, ArraySize, 0.01, 100);
151
double amax = -1000.0;
153
for (int i = 0; i < ArraySize; i++)
155
double f = frequency[i];
156
cplx g = cplx(1.0) / cplx(1.0 - f * f, 2.0 * damping * f);
157
amplitude[i] = 20.0 * log10(sqrt( g.real()*g.real() + g.imag()*g.imag()));
158
phase[i] = atan2(g.imag(), g.real()) * (180.0 / M_PI);
160
if ((i3 <= 1) && (amplitude[i] < -3.0))
162
if (amplitude[i] > amax)
170
double f3 = frequency[i3] -
171
(frequency[i3] - frequency[i3 - 1])
172
/ (amplitude[i3] - amplitude[i3 -1]) * (amplitude[i3] + 3);
174
showPeak(fmax, amax);
176
showData(frequency, amplitude, phase, ArraySize);
178
setAutoReplot(doReplot);