46 #include <QGridLayout>
47 #include <QGraphicsView>
48 #include <QGraphicsPixmapItem>
59 using namespace DISPLIB;
71 qreal max_frq = sample_rate/2.0;
72 qreal frq_per_px = max_frq/tf_matrix.rows();
74 if(upper_frq > max_frq || upper_frq <= 0) upper_frq = max_frq;
75 if(lower_frq < 0 || lower_frq >= max_frq) lower_frq = 0;
76 if(upper_frq < lower_frq) {
77 qreal temp = upper_frq;
78 upper_frq = lower_frq;
82 qint32 lower_px = floor(lower_frq / frq_per_px);
83 qint32 upper_px = floor(upper_frq / frq_per_px);
85 Eigen::MatrixXd zoomed_tf_matrix = Eigen::MatrixXd::Zero(upper_px-lower_px, tf_matrix.cols());
90 for(qint32 it = lower_px; it < upper_px; it++) {
91 zoomed_tf_matrix.row(pxls) = tf_matrix.row(it);
97 calc_plot(zoomed_tf_matrix, sample_rate, cmap, lower_frq, upper_frq);
104 ColorMaps cmap = Jet)
106 calc_plot(tf_matrix, sample_rate, cmap, 0, 0);
118 qreal norm1 = tf_matrix.maxCoeff();
119 qreal mnorm = tf_matrix.minCoeff();
120 if(std::fabs(mnorm) > norm1) norm1 = mnorm;
124 QImage * image_to_tf_plot =
new QImage(tf_matrix.cols(), tf_matrix.rows(), QImage::Format_RGB32);
128 for ( qint32 y = 0; y < tf_matrix.rows(); y++ ) {
129 for ( qint32 x = 0; x < tf_matrix.cols(); x++ ) {
150 image_to_tf_plot->setPixel(x, tf_matrix.rows() - 1 - y, color.rgb());
154 *image_to_tf_plot = image_to_tf_plot->scaled(tf_matrix.cols(), tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
155 *image_to_tf_plot = image_to_tf_plot->scaledToWidth( 1026, Qt::SmoothTransformation);
157 QGraphicsPixmapItem *tf_pixmap =
new QGraphicsPixmapItem(QPixmap::fromImage(*image_to_tf_plot));
159 QGraphicsScene *tf_scene =
new QGraphicsScene();
160 tf_scene->addItem(tf_pixmap);
162 QImage * coeffs_image =
new QImage(10, tf_matrix.rows(), QImage::Format_RGB32);
163 qreal norm = tf_matrix.maxCoeff();
164 for(qint32 it = 0; it < tf_matrix.rows(); it++) {
165 for ( qint32 x = 0; x < 10; x++ ) {
186 coeffs_image->setPixel(x, tf_matrix.rows() - 1 - it, color.rgb());
190 *coeffs_image = coeffs_image->scaled(10, tf_matrix.cols()/2, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
191 *coeffs_image = coeffs_image->scaledToHeight(image_to_tf_plot->height(), Qt::SmoothTransformation);
193 QLayout * layout =
new QGridLayout();
194 QGraphicsView * view =
new QGraphicsView();
195 view->setObjectName(
"tf_view");
196 view->setScene(tf_scene);
197 QLinearGradient lgrad(tf_scene->sceneRect().topLeft(), tf_scene->sceneRect().bottomRight());
198 lgrad.setColorAt(0.0, Qt::white);
199 lgrad.setColorAt(1.0, Qt::lightGray);
201 tf_scene->setBackgroundBrush(lgrad);
204 QGraphicsTextItem *x_axis_name =
new QGraphicsTextItem(
"time [sec]", tf_pixmap);
205 x_axis_name->setFont(QFont(
"arial", 14));
207 QList<QGraphicsItem *> x_axis_values;
208 QList<QGraphicsItem *> x_axis_lines;
210 qreal scaleXText = (tf_matrix.cols() - 1) / sample_rate / 20.0;
212 for(qint32 j = 0; j < 21; j++) {
213 QGraphicsTextItem *text_item =
new QGraphicsTextItem(QString::number(j * scaleXText,
'f', 2), tf_pixmap);
214 text_item->setFont(QFont(
"arial", 10));
215 x_axis_values.append(text_item);
216 QGraphicsLineItem *x_line_item =
new QGraphicsLineItem(tf_pixmap);
217 x_line_item->setLine(0,-3,0,3);
218 x_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
219 x_axis_lines.append(x_line_item);
222 x_axis_name->setPos(tf_pixmap->boundingRect().width()/2 - x_axis_name->boundingRect().width()/2,
223 tf_pixmap->boundingRect().height() + 0.8 * x_axis_values.at(0)->boundingRect().height());
225 qreal scale_x = qreal(tf_pixmap->boundingRect().width()) / qreal(x_axis_values.length()-1);
227 for(qint32 i = 0; i < x_axis_values.length(); i++) {
228 x_axis_values.at(i)->setPos(qreal(i)*scale_x - x_axis_values.at(0)->boundingRect().width()/2,
229 tf_pixmap->boundingRect().height());
230 x_axis_lines.at(i)->setPos(qreal(i)*scale_x,
231 tf_pixmap->boundingRect().height());
236 QGraphicsTextItem *y_axis_name =
new QGraphicsTextItem(
"frequency [Hz]", tf_pixmap);
237 y_axis_name->setFont(QFont(
"arial", 14));
239 QList<QGraphicsItem *> y_axis_values;
240 QList<QGraphicsItem *> y_axis_lines;
242 qreal scale_y_text = 0;
244 if(lower_frq == 0 && upper_frq == 0) {
245 scale_y_text = 0.5* sample_rate / 10.0;
247 scale_y_text = (upper_frq - lower_frq) / 10.0;
250 for(qint32 j = 0; j < 11; j++) {
251 QGraphicsTextItem *text_item =
new QGraphicsTextItem(QString::number(lower_frq + j*scale_y_text,
253 text_item->setFont(QFont(
"arial", 10));
254 y_axis_values.append(text_item);
255 QGraphicsLineItem *y_line_item =
new QGraphicsLineItem(tf_pixmap);
256 y_line_item->setLine(-3,0,3,0);
257 y_line_item->setPen(QPen(Qt::darkGray, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin));
258 y_axis_lines.append(y_line_item);
261 y_axis_name->setPos(- x_axis_values.at(0)->boundingRect().width() - y_axis_name->boundingRect().height(),
262 tf_pixmap->boundingRect().height()/2 + y_axis_name->boundingRect().height()/2);
263 y_axis_name->setRotation(-90);
265 qreal scale_y = qreal(tf_pixmap->boundingRect().height()) / qreal(y_axis_values.length()-1);
267 for(qint32 i = 0; i < y_axis_values.length(); i++) {
268 y_axis_values.at(i)->setPos( -y_axis_values.last()->boundingRect().width()
269 -0.5*y_axis_lines.last()->boundingRect().width()
271 ,tf_pixmap->boundingRect().height()
272 - y_axis_values.last()->boundingRect().height()/2
274 y_axis_lines.at(i)->setPos(0, qreal(i)*scale_y);
278 QGraphicsPixmapItem * coeffs_item = tf_scene->addPixmap(QPixmap::fromImage(*coeffs_image));
279 coeffs_item->setParentItem(tf_pixmap);
280 coeffs_item->setPos(tf_pixmap->boundingRect().width() +5, 0);
282 QGraphicsSimpleTextItem *axis_name_item =
new QGraphicsSimpleTextItem(
"coefficients", coeffs_item);
283 QGraphicsSimpleTextItem *axis_zero_item =
new QGraphicsSimpleTextItem(
"0", coeffs_item);
284 QGraphicsSimpleTextItem *axis_one_item =
new QGraphicsSimpleTextItem(
"1", coeffs_item);
285 axis_name_item->setFont(QFont(
"arial", 14));
286 axis_zero_item->setFont(QFont(
"arial", 10));
287 axis_one_item->setFont(QFont(
"arial", 10));
289 axis_name_item->setPos(coeffs_item->boundingRect().width() + 1,
290 coeffs_item->boundingRect().height()/2 + axis_name_item->boundingRect().height()/2);
291 axis_name_item->setRotation(-90);
292 axis_zero_item->setPos( 1 + coeffs_item->boundingRect().width(),
293 coeffs_item->boundingRect().height()- axis_zero_item->boundingRect().height());
294 axis_one_item->setPos( 1 + coeffs_item->boundingRect().width(), 0);
297 view->fitInView(layout->contentsRect(),Qt::KeepAspectRatio);
298 layout->addWidget(view);
299 this->setLayout(layout);
304 void TFplot::resizeEvent(QResizeEvent *event)
308 QWidget *widget = this->layout()->itemAt(0)-> widget();
309 if (widget != NULL ) {
310 QGraphicsView* view = (QGraphicsView*)widget;
311 view->fitInView(view->sceneRect(),Qt::KeepAspectRatio);