v2.0.0
Loading...
Searching...
No Matches
tfplot.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
19#include "tfplot.h"
20
21#include "helpers/colormap.h"
22
23//=============================================================================================================
24// QT INCLUDES
25//=============================================================================================================
26
27#include <QGridLayout>
28#include <QGraphicsView>
29#include <QGraphicsPixmapItem>
30#include <QDebug>
31
32//=============================================================================================================
33// EIGEN INCLUDES
34//=============================================================================================================
35
36//=============================================================================================================
37// USED NAMESPACES
38//=============================================================================================================
39
40using namespace DISPLIB;
41
42//=============================================================================================================
43// DEFINE MEMBER METHODS
44//=============================================================================================================
45
46TFplot::TFplot(Eigen::MatrixXd tf_matrix,
47 qreal sample_rate,
48 qreal lower_frq,
49 qreal upper_frq,
50 ColorMaps cmap = Jet)
51{
52 qreal max_frq = sample_rate / 2.0;
53 qreal frq_per_px = max_frq / tf_matrix.rows();
54
55 if (upper_frq > max_frq || upper_frq <= 0)
56 upper_frq = max_frq;
57 if (lower_frq < 0 || lower_frq >= max_frq)
58 lower_frq = 0;
59 if (upper_frq < lower_frq) {
60 qreal temp = upper_frq;
61 upper_frq = lower_frq;
62 lower_frq = temp;
63 }
64
65 qint32 lower_px = floor(lower_frq / frq_per_px);
66 qint32 upper_px = floor(upper_frq / frq_per_px);
67
68 Eigen::MatrixXd zoomed_tf_matrix = Eigen::MatrixXd::Zero(upper_px - lower_px, tf_matrix.cols());
69
70 qint32 pxls = 0;
71 for (qint32 it = lower_px; it < upper_px; it++) {
72 zoomed_tf_matrix.row(pxls) = tf_matrix.row(it);
73 pxls++;
74 }
75
76 //zoomed_tf_matrix = tf_matrix.block(tf_matrix.rows() - upper_px, 0, upper_px-lower_px, tf_matrix.cols());
77
78 calc_plot(zoomed_tf_matrix, sample_rate, cmap, lower_frq, upper_frq);
79}
80
81//=============================================================================================================
82
83TFplot::TFplot(Eigen::MatrixXd tf_matrix,
84 qreal sample_rate,
85 ColorMaps cmap = Jet)
86{
87 calc_plot(tf_matrix, sample_rate, cmap, 0, 0);
88}
89
90//=============================================================================================================
91
92void TFplot::calc_plot(Eigen::MatrixXd tf_matrix,
93 qreal sample_rate,
94 ColorMaps cmap,
95 qreal lower_frq = 0,
96 qreal upper_frq = 0)
97{
98 //normalisation of the tf-matrix
99 qreal norm1 = tf_matrix.maxCoeff();
100 qreal mnorm = tf_matrix.minCoeff();
101 if (std::fabs(mnorm) > norm1)
102 norm1 = mnorm;
103 tf_matrix /= norm1;
104
105 //setup image
106 QImage* image_to_tf_plot = new QImage(tf_matrix.cols(), tf_matrix.rows(), QImage::Format_RGB32);
107
108 //setup pixelcolors in image
109 QColor color;
110 for (qint32 y = 0; y < tf_matrix.rows(); y++) {
111 for (qint32 x = 0; x < tf_matrix.cols(); x++) {
112 switch (cmap) {
113 case Jet:
114 color.setRgb(ColorMap::valueToJet(std::fabs(tf_matrix(y, x))));
115 break;
116 case Hot:
117 color.setRgb(ColorMap::valueToHot(std::fabs(tf_matrix(y, x))));
118 break;
119 case HotNeg1:
120 color.setRgb(ColorMap::valueToHotNegative1(std::fabs(tf_matrix(y, x))));
121 break;
122 case HotNeg2:
123 color.setRgb(ColorMap::valueToHotNegative2(std::fabs(tf_matrix(y, x))));
124 break;
125 case Bone:
126 color.setRgb(ColorMap::valueToBone(std::fabs(tf_matrix(y, x))));
127 break;
128 case RedBlue:
129 color.setRgb(ColorMap::valueToRedBlue(std::fabs(tf_matrix(y, x))));
130 break;
131 }
132 image_to_tf_plot->setPixel(x, tf_matrix.rows() - 1 - y, color.rgb());
133 }
134 }
135
136 *image_to_tf_plot = image_to_tf_plot->scaled(tf_matrix.cols(), tf_matrix.cols() / 2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
137 *image_to_tf_plot = image_to_tf_plot->scaledToWidth(/*0.9 **/ 1026, Qt::SmoothTransformation);
138 //image to pixmap
139 QGraphicsPixmapItem* tf_pixmap = new QGraphicsPixmapItem(QPixmap::fromImage(*image_to_tf_plot));
140 //tf_pixmap->setScale(100);
141 QGraphicsScene* tf_scene = new QGraphicsScene();
142 tf_scene->addItem(tf_pixmap);
143
144 QImage* coeffs_image = new QImage(10, tf_matrix.rows(), QImage::Format_RGB32);
145 qreal norm = tf_matrix.maxCoeff();
146 for (qint32 it = 0; it < tf_matrix.rows(); it++) {
147 for (qint32 x = 0; x < 10; x++) {
148 switch (cmap) {
149 case Jet:
150 color.setRgb(ColorMap::valueToJet(it * norm / tf_matrix.rows()));
151 break;
152 case Hot:
153 color.setRgb(ColorMap::valueToHot(it * norm / tf_matrix.rows()));
154 break;
155 case HotNeg1:
156 color.setRgb(ColorMap::valueToHotNegative1(it * norm / tf_matrix.rows()));
157 break;
158 case HotNeg2:
159 color.setRgb(ColorMap::valueToHotNegative2(it * norm / tf_matrix.rows()));
160 break;
161 case Bone:
162 color.setRgb(ColorMap::valueToBone(it * norm / tf_matrix.rows()));
163 break;
164 case RedBlue:
165 color.setRgb(ColorMap::valueToRedBlue(it * norm / tf_matrix.rows()));
166 break;
167 }
168 coeffs_image->setPixel(x, tf_matrix.rows() - 1 - it, color.rgb());
169 }
170 }
171
172 *coeffs_image = coeffs_image->scaled(10, tf_matrix.cols() / 2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
173 *coeffs_image = coeffs_image->scaledToHeight(image_to_tf_plot->height(), Qt::SmoothTransformation);
174
175 QLayout* layout = new QGridLayout();
176 QGraphicsView* view = new QGraphicsView();
177 view->setObjectName("tf_view");
178 view->setScene(tf_scene);
179 QLinearGradient lgrad(tf_scene->sceneRect().topLeft(), tf_scene->sceneRect().bottomRight());
180 lgrad.setColorAt(0.0, Qt::white);
181 lgrad.setColorAt(1.0, Qt::lightGray);
182
183 tf_scene->setBackgroundBrush(lgrad); //Qt::white);
184
185 //setup x-axis
186 QGraphicsTextItem* x_axis_name = new QGraphicsTextItem("time [sec]", tf_pixmap);
187 x_axis_name->setFont(QFont("arial", 14));
188
189 QList<QGraphicsItem*> x_axis_values;
190 QList<QGraphicsItem*> x_axis_lines;
191
192 qreal scaleXText = (tf_matrix.cols() - 1) / sample_rate / 20.0; // divide signallength
193
194 for (qint32 j = 0; j < 21; j++) {
195 QGraphicsTextItem* text_item = new QGraphicsTextItem(QString::number(j * scaleXText, 'f', 2), tf_pixmap);
196 text_item->setFont(QFont("arial", 10));
197 x_axis_values.append(text_item); // scalevalue as string
198 QGraphicsLineItem* x_line_item = new QGraphicsLineItem(tf_pixmap);
199 x_line_item->setLine(0, -3, 0, 3);
200 x_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
201 x_axis_lines.append(x_line_item); // scalelines
202 }
203
204 x_axis_name->setPos(tf_pixmap->boundingRect().width() / 2 - x_axis_name->boundingRect().width() / 2,
205 tf_pixmap->boundingRect().height() + 0.8 * x_axis_values.at(0)->boundingRect().height());
206
207 qreal scale_x = qreal(tf_pixmap->boundingRect().width()) / qreal(x_axis_values.length() - 1);
208
209 for (qint32 i = 0; i < x_axis_values.length(); i++) {
210 x_axis_values.at(i)->setPos(qreal(i) * scale_x - x_axis_values.at(0)->boundingRect().width() / 2,
211 tf_pixmap->boundingRect().height());
212 x_axis_lines.at(i)->setPos(qreal(i) * scale_x,
213 tf_pixmap->boundingRect().height());
214
215 } //end x axis
216
217 //y-axis
218 QGraphicsTextItem* y_axis_name = new QGraphicsTextItem("frequency [Hz]", tf_pixmap);
219 y_axis_name->setFont(QFont("arial", 14));
220
221 QList<QGraphicsItem*> y_axis_values;
222 QList<QGraphicsItem*> y_axis_lines;
223
224 qreal scale_y_text = 0;
225
226 if (lower_frq == 0 && upper_frq == 0) {
227 scale_y_text = 0.5 * sample_rate / 10.0; // divide signallength
228 } else {
229 scale_y_text = (upper_frq - lower_frq) / 10.0;
230 }
231
232 for (qint32 j = 0; j < 11; j++) {
233 QGraphicsTextItem* text_item = new QGraphicsTextItem(QString::number(lower_frq + j * scale_y_text, //pow(10, j)/pow(10, 11) /*(j+1)/log(12)*/ * max_frequency,//scale_y_text,
234 'f', 0),
235 tf_pixmap);
236 text_item->setFont(QFont("arial", 10));
237 y_axis_values.append(text_item); // scalevalue as string
238 QGraphicsLineItem* y_line_item = new QGraphicsLineItem(tf_pixmap);
239 y_line_item->setLine(-3, 0, 3, 0);
240 y_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
241 y_axis_lines.append(y_line_item); // scalelines
242 }
243
244 y_axis_name->setPos(-x_axis_values.at(0)->boundingRect().width() - y_axis_name->boundingRect().height(),
245 tf_pixmap->boundingRect().height() / 2 + y_axis_name->boundingRect().height() / 2);
246 y_axis_name->setRotation(-90);
247
248 qreal scale_y = qreal(tf_pixmap->boundingRect().height()) / qreal(y_axis_values.length() - 1);
249
250 for (qint32 i = 0; i < y_axis_values.length(); i++) {
251 y_axis_values.at(i)->setPos(-y_axis_values.last()->boundingRect().width() - 0.5 * y_axis_lines.last()->boundingRect().width() - 1, tf_pixmap->boundingRect().height() - y_axis_values.last()->boundingRect().height() / 2 - qreal(i) * scale_y);
252 y_axis_lines.at(i)->setPos(0, qreal(i) * scale_y);
253 }
254 //end y axis
255
256 QGraphicsPixmapItem* coeffs_item = tf_scene->addPixmap(QPixmap::fromImage(*coeffs_image)); //addItem();
257 coeffs_item->setParentItem(tf_pixmap);
258 coeffs_item->setPos(tf_pixmap->boundingRect().width() + 5, 0);
259
260 QGraphicsSimpleTextItem* axis_name_item = new QGraphicsSimpleTextItem("coefficients", coeffs_item);
261 QGraphicsSimpleTextItem* axis_zero_item = new QGraphicsSimpleTextItem("0", coeffs_item);
262 QGraphicsSimpleTextItem* axis_one_item = new QGraphicsSimpleTextItem("1", coeffs_item);
263 axis_name_item->setFont(QFont("arial", 14));
264 axis_zero_item->setFont(QFont("arial", 10));
265 axis_one_item->setFont(QFont("arial", 10));
266
267 axis_name_item->setPos(coeffs_item->boundingRect().width() + 1,
268 coeffs_item->boundingRect().height() / 2 + axis_name_item->boundingRect().height() / 2);
269 axis_name_item->setRotation(-90);
270 axis_zero_item->setPos(1 + coeffs_item->boundingRect().width(),
271 coeffs_item->boundingRect().height() - axis_zero_item->boundingRect().height());
272 axis_one_item->setPos(1 + coeffs_item->boundingRect().width(), 0);
273 //end coeffs picture
274
275 view->fitInView(layout->contentsRect(), Qt::KeepAspectRatio);
276 layout->addWidget(view);
277 this->setLayout(layout);
278}
279
280//=============================================================================================================
281
282void TFplot::resizeEvent(QResizeEvent* event)
283{
284 Q_UNUSED(event);
285
286 QWidget* widget = this->layout()->itemAt(0)->widget();
287 if (widget != nullptr) {
288 QGraphicsView* view = (QGraphicsView*)widget;
289 view->fitInView(view->sceneRect(), Qt::KeepAspectRatio);
290 }
291}
Static scalar-to-colour lookup helpers (Jet, Hot, Bone, Viridis, Cool, RedBlue, MNE) used by every pl...
Time-frequency spectrogram widget rendering an Eigen matrix as a colour image.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
ColorMaps
Definition tfplot.h:62
@ Jet
Definition tfplot.h:66
@ RedBlue
Definition tfplot.h:68
@ HotNeg1
Definition tfplot.h:64
@ Hot
Definition tfplot.h:63
@ Bone
Definition tfplot.h:67
@ HotNeg2
Definition tfplot.h:65
static QRgb valueToBone(double v)
Definition colormap.h:755
static QRgb valueToJet(double v)
Definition colormap.h:723
static QRgb valueToRedBlue(double v)
Definition colormap.h:763
static QRgb valueToHotNegative2(double v)
Definition colormap.h:747
static QRgb valueToHot(double v)
Definition colormap.h:731
static QRgb valueToHotNegative1(double v)
Definition colormap.h:739
void calc_plot(Eigen::MatrixXd tf_matrix, qreal sample_rate, ColorMaps cmap, qreal lower_frq, qreal upper_frq)
Definition tfplot.cpp:92
virtual void resizeEvent(QResizeEvent *event)
Definition tfplot.cpp:282
TFplot(Eigen::MatrixXd tf_matrix, qreal sample_rate, qreal lower_frq, qreal upper_frq, ColorMaps cmap)
Definition tfplot.cpp:46