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) upper_frq = max_frq;
56 if(lower_frq < 0 || lower_frq >= max_frq) lower_frq = 0;
57 if(upper_frq < lower_frq) {
58 qreal temp = upper_frq;
59 upper_frq = lower_frq;
60 lower_frq = temp;
61 }
62
63 qint32 lower_px = floor(lower_frq / frq_per_px);
64 qint32 upper_px = floor(upper_frq / frq_per_px);
65
66 Eigen::MatrixXd zoomed_tf_matrix = Eigen::MatrixXd::Zero(upper_px-lower_px, tf_matrix.cols());
67 //How to print to console here
68 //printf(("fff "+QString::number(zoomed_tf_matrix(12,12))).toUtf8().data());// << "; " << zoomed_tf_matrix.rows(2) << "; ";
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) norm1 = mnorm;
102 tf_matrix /= norm1;
103
104 //setup image
105 QImage * image_to_tf_plot = new QImage(tf_matrix.cols(), tf_matrix.rows(), QImage::Format_RGB32);
106
107 //setup pixelcolors in image
108 QColor color;
109 for ( qint32 y = 0; y < tf_matrix.rows(); y++ ) {
110 for ( qint32 x = 0; x < tf_matrix.cols(); x++ ) {
111 switch (cmap) {
112 case Jet:
113 color.setRgb(ColorMap::valueToJet(std::fabs(tf_matrix(y, x))));
114 break;
115 case Hot:
116 color.setRgb(ColorMap::valueToHot(std::fabs(tf_matrix(y, x))));
117 break;
118 case HotNeg1:
119 color.setRgb(ColorMap::valueToHotNegative1(std::fabs(tf_matrix(y, x))));
120 break;
121 case HotNeg2:
122 color.setRgb(ColorMap::valueToHotNegative2(std::fabs(tf_matrix(y, x))));
123 break;
124 case Bone:
125 color.setRgb(ColorMap::valueToBone(std::fabs(tf_matrix(y, x))));
126 break;
127 case RedBlue:
128 color.setRgb(ColorMap::valueToRedBlue(std::fabs(tf_matrix(y, x))));
129 break;
130 }
131 image_to_tf_plot->setPixel(x, tf_matrix.rows() - 1 - y, color.rgb());
132 }
133 }
134
135 *image_to_tf_plot = image_to_tf_plot->scaled(tf_matrix.cols(), tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
136 *image_to_tf_plot = image_to_tf_plot->scaledToWidth(/*0.9 **/ 1026, Qt::SmoothTransformation);
137 //image to pixmap
138 QGraphicsPixmapItem *tf_pixmap = new QGraphicsPixmapItem(QPixmap::fromImage(*image_to_tf_plot));
139 //tf_pixmap->setScale(100);
140 QGraphicsScene *tf_scene = new QGraphicsScene();
141 tf_scene->addItem(tf_pixmap);
142
143 QImage * coeffs_image = new QImage(10, tf_matrix.rows(), QImage::Format_RGB32);
144 qreal norm = tf_matrix.maxCoeff();
145 for(qint32 it = 0; it < tf_matrix.rows(); it++) {
146 for ( qint32 x = 0; x < 10; x++ ) {
147 switch (cmap) {
148 case Jet:
149 color.setRgb(ColorMap::valueToJet(it*norm/tf_matrix.rows()));
150 break;
151 case Hot:
152 color.setRgb(ColorMap::valueToHot(it*norm/tf_matrix.rows()));
153 break;
154 case HotNeg1:
155 color.setRgb(ColorMap::valueToHotNegative1(it*norm/tf_matrix.rows()));
156 break;
157 case HotNeg2:
158 color.setRgb(ColorMap::valueToHotNegative2(it*norm/tf_matrix.rows()));
159 break;
160 case Bone:
161 color.setRgb(ColorMap::valueToBone(it*norm/tf_matrix.rows()));
162 break;
163 case RedBlue:
164 color.setRgb(ColorMap::valueToRedBlue(it*norm/tf_matrix.rows()));
165 break;
166 }
167 coeffs_image->setPixel(x, tf_matrix.rows() - 1 - it, color.rgb());
168 }
169 }
170
171 *coeffs_image = coeffs_image->scaled(10, tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
172 *coeffs_image = coeffs_image->scaledToHeight(image_to_tf_plot->height(), Qt::SmoothTransformation);
173
174 QLayout * layout = new QGridLayout();
175 QGraphicsView * view = new QGraphicsView();
176 view->setObjectName("tf_view");
177 view->setScene(tf_scene);
178 QLinearGradient lgrad(tf_scene->sceneRect().topLeft(), tf_scene->sceneRect().bottomRight());
179 lgrad.setColorAt(0.0, Qt::white);
180 lgrad.setColorAt(1.0, Qt::lightGray);
181
182 tf_scene->setBackgroundBrush(lgrad);//Qt::white);
183
184 //setup x-axis
185 QGraphicsTextItem *x_axis_name = new QGraphicsTextItem("time [sec]", tf_pixmap);
186 x_axis_name->setFont(QFont("arial", 14));
187
188 QList<QGraphicsItem *> x_axis_values;
189 QList<QGraphicsItem *> x_axis_lines;
190
191 qreal scaleXText = (tf_matrix.cols() - 1) / sample_rate / 20.0; // divide signallength
192
193 for(qint32 j = 0; j < 21; j++) {
194 QGraphicsTextItem *text_item = new QGraphicsTextItem(QString::number(j * scaleXText, 'f', 2), tf_pixmap);
195 text_item->setFont(QFont("arial", 10));
196 x_axis_values.append(text_item); // scalevalue as string
197 QGraphicsLineItem *x_line_item = new QGraphicsLineItem(tf_pixmap);
198 x_line_item->setLine(0,-3,0,3);
199 x_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
200 x_axis_lines.append(x_line_item); // scalelines
201 }
202
203 x_axis_name->setPos(tf_pixmap->boundingRect().width()/2 - x_axis_name->boundingRect().width()/2,
204 tf_pixmap->boundingRect().height() + 0.8 * x_axis_values.at(0)->boundingRect().height());
205
206 qreal scale_x = qreal(tf_pixmap->boundingRect().width()) / qreal(x_axis_values.length()-1);
207
208 for(qint32 i = 0; i < x_axis_values.length(); i++) {
209 x_axis_values.at(i)->setPos(qreal(i)*scale_x - x_axis_values.at(0)->boundingRect().width()/2,
210 tf_pixmap->boundingRect().height());
211 x_axis_lines.at(i)->setPos(qreal(i)*scale_x,
212 tf_pixmap->boundingRect().height());
213
214 }//end x axis
215
216 //y-axis
217 QGraphicsTextItem *y_axis_name = new QGraphicsTextItem("frequency [Hz]", tf_pixmap);
218 y_axis_name->setFont(QFont("arial", 14));
219
220 QList<QGraphicsItem *> y_axis_values;
221 QList<QGraphicsItem *> y_axis_lines;
222
223 qreal scale_y_text = 0;
224
225 if(lower_frq == 0 && upper_frq == 0) {
226 scale_y_text = 0.5* sample_rate / 10.0; // divide signallength
227 } else {
228 scale_y_text = (upper_frq - lower_frq) / 10.0;
229 }
230
231 for(qint32 j = 0; j < 11; j++) {
232 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,
233 'f', 0), tf_pixmap);
234 text_item->setFont(QFont("arial", 10));
235 y_axis_values.append(text_item); // scalevalue as string
236 QGraphicsLineItem *y_line_item = new QGraphicsLineItem(tf_pixmap);
237 y_line_item->setLine(-3,0,3,0);
238 y_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
239 y_axis_lines.append(y_line_item); // scalelines
240 }
241
242 y_axis_name->setPos(- x_axis_values.at(0)->boundingRect().width() - y_axis_name->boundingRect().height(),
243 tf_pixmap->boundingRect().height()/2 + y_axis_name->boundingRect().height()/2);
244 y_axis_name->setRotation(-90);
245
246 qreal scale_y = qreal(tf_pixmap->boundingRect().height()) / qreal(y_axis_values.length()-1);
247
248 for(qint32 i = 0; i < y_axis_values.length(); i++) {
249 y_axis_values.at(i)->setPos( -y_axis_values.last()->boundingRect().width()
250 -0.5*y_axis_lines.last()->boundingRect().width()
251 -1
252 ,tf_pixmap->boundingRect().height()
253 - y_axis_values.last()->boundingRect().height()/2
254 - qreal(i)*scale_y);
255 y_axis_lines.at(i)->setPos(0, qreal(i)*scale_y);
256 }
257 //end y axis
258
259 QGraphicsPixmapItem * coeffs_item = tf_scene->addPixmap(QPixmap::fromImage(*coeffs_image));//addItem();
260 coeffs_item->setParentItem(tf_pixmap);
261 coeffs_item->setPos(tf_pixmap->boundingRect().width() +5, 0);
262
263 QGraphicsSimpleTextItem *axis_name_item = new QGraphicsSimpleTextItem("coefficients", coeffs_item);
264 QGraphicsSimpleTextItem *axis_zero_item = new QGraphicsSimpleTextItem("0", coeffs_item);
265 QGraphicsSimpleTextItem *axis_one_item = new QGraphicsSimpleTextItem("1", coeffs_item);
266 axis_name_item->setFont(QFont("arial", 14));
267 axis_zero_item->setFont(QFont("arial", 10));
268 axis_one_item->setFont(QFont("arial", 10));
269
270 axis_name_item->setPos(coeffs_item->boundingRect().width() + 1,
271 coeffs_item->boundingRect().height()/2 + axis_name_item->boundingRect().height()/2);
272 axis_name_item->setRotation(-90);
273 axis_zero_item->setPos( 1 + coeffs_item->boundingRect().width(),
274 coeffs_item->boundingRect().height()- axis_zero_item->boundingRect().height());
275 axis_one_item->setPos( 1 + coeffs_item->boundingRect().width(), 0);
276 //end coeffs picture
277
278 view->fitInView(layout->contentsRect(),Qt::KeepAspectRatio);
279 layout->addWidget(view);
280 this->setLayout(layout);
281}
282
283//=============================================================================================================
284
285void TFplot::resizeEvent(QResizeEvent *event)
286{
287 Q_UNUSED(event);
288
289 QWidget *widget = this->layout()->itemAt(0)-> widget();
290 if (widget != nullptr ) {
291 QGraphicsView* view = (QGraphicsView*)widget;
292 view->fitInView(view->sceneRect(),Qt::KeepAspectRatio);
293 }
294}
295
Time-frequency spectrogram widget rendering an Eigen matrix as a colour image.
Static scalar-to-colour lookup helpers (Jet, Hot, Bone, Viridis, Cool, RedBlue, MNE) used by every pl...
2-D display widgets and visualisation helpers (charts, topography, colour maps).
ColorMaps
Definition tfplot.h:61
@ Jet
Definition tfplot.h:65
@ RedBlue
Definition tfplot.h:67
@ HotNeg1
Definition tfplot.h:63
@ Hot
Definition tfplot.h:62
@ Bone
Definition tfplot.h:66
@ HotNeg2
Definition tfplot.h:64
static QRgb valueToBone(double v)
Definition colormap.h:753
static QRgb valueToJet(double v)
Definition colormap.h:721
static QRgb valueToRedBlue(double v)
Definition colormap.h:761
static QRgb valueToHotNegative2(double v)
Definition colormap.h:745
static QRgb valueToHot(double v)
Definition colormap.h:729
static QRgb valueToHotNegative1(double v)
Definition colormap.h:737
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:285
TFplot(Eigen::MatrixXd tf_matrix, qreal sample_rate, qreal lower_frq, qreal upper_frq, ColorMaps cmap)
Definition tfplot.cpp:46