Skip to content

Commit b25f8bf

Browse files
committed
add a way to get the mean of buffer
1 parent 8498097 commit b25f8bf

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

chart.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#include "chart.h"
66
#include <QDebug>
77
#include <QRandomGenerator>
8+
#include <iostream>
9+
10+
using namespace std;
811

912
Chart::Chart(QObject *parent) : QObject(parent) {
1013
dataUpdater.setInterval(0);
1114
dataUpdater.setSingleShot(true);
1215
QObject::connect(&dataUpdater, &QTimer::timeout, this, &Chart::updateAllSeries);
16+
17+
matrix = new MatrixXd(250,24);
1318
}
1419

1520
void Chart::startUpdating(const QList<QLineSeries *> &seriesList, const QVector<qreal> &values, qreal windowWidth,
@@ -20,7 +25,14 @@ void Chart::startUpdating(const QList<QLineSeries *> &seriesList, const QVector<
2025
return;
2126
}
2227

28+
auto valuesVector = std::vector<qreal>(values.begin(), values.end());
29+
RowVectorXd vector = Map<RowVectorXd, Unaligned>(valuesVector.data(), valuesVector.size());
30+
// cout << vector << endl;
31+
2332
c_seriesList = seriesList;
33+
matrix->row(valueCounter) = vector;
34+
// cout << matrix->row(0) << endl;
35+
// cout << "hello" << endl;
2436

2537
// qreal xAdjustment = 20.0 / (10 * values.length());
2638
// qreal yMultiplier = 3.0 / qreal(values.length());
@@ -36,7 +48,7 @@ void Chart::startUpdating(const QList<QLineSeries *> &seriesList, const QVector<
3648
// }
3749
// qInfo() << m_data;
3850

39-
tempData.append(values);
51+
// tempData.append(values);
4052

4153
// for (int i = 0; i < values.length(); ++i) {
4254
//// seriesList[i]->clear();
@@ -67,9 +79,35 @@ void Chart::updateAllSeries() {
6779
qInfo() << "updates called";
6880
qInfo() << tempData.length();
6981

70-
tempData.clear();
82+
// tempData.clear();
83+
removeZeroRows(*matrix);
84+
cout << matrix->colwise().mean() << endl;
85+
// exit(0);
7186

7287
// for (int i = 0; i < c_seriesList.length(); ++i) {
7388
// c_seriesList[i]->replace(m_data);
7489
// }
90+
delete matrix;
91+
matrix = nullptr;
92+
matrix = new MatrixXd(250, 24);
93+
}
94+
95+
void Chart::removeZeroRows(Eigen::MatrixXd& mat)
96+
{
97+
Matrix<bool, Dynamic, 1> empty = (mat.array() == 0).rowwise().all();
98+
99+
size_t last = mat.rows() - 1;
100+
for (size_t i = 0; i < last + 1;)
101+
{
102+
if (empty(i))
103+
{
104+
mat.row(i).swap(mat.row(last));
105+
empty.segment<1>(i).swap(empty.segment<1>(last));
106+
--last;
107+
}
108+
else {
109+
++i;
110+
}
111+
}
112+
mat.conservativeResize(last + 1, mat.cols());
75113
}

chart.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,49 @@
1010
#include <QtCharts/QLineSeries>
1111
#include <qelapsedtimer.h>
1212
#include <qtimer.h>
13+
#include <Eigen/Dense>
1314

1415
QT_CHARTS_USE_NAMESPACE
16+
using namespace Eigen;
1517

1618
class Chart : public QObject {
1719

1820
Q_OBJECT
1921
public:
2022
explicit Chart(QObject *parent = 0);
23+
24+
/**
25+
* This method is called by the Bluetooth stream API
26+
*
27+
* @param seriesList List of the \see Qt::QtCharts::QLineSeries
28+
* @param values A \see QVector of the streamed values
29+
* @param windowWidth Width of the UI window
30+
* @param frequency Frequency of the streaming. This will change depending on the OS and the hardware
31+
*/
2132
void startUpdating(const QList<QLineSeries *> &seriesList, const QVector<qreal> &values, qreal windowWidth, qreal frequency);
2233

2334
private slots:
35+
/**
36+
* Updates the mean values to the chart
37+
*/
2438
void updateAllSeries();
2539

2640
private:
2741
QList<QLineSeries *> c_seriesList;
2842
QVector<QPointF> m_data;
29-
qreal valueCounter = 0;
43+
qint32 valueCounter = 0;
3044
QElapsedTimer timer;
3145
QTimer dataUpdater;
3246
QVector<QVector<qreal>> tempData;
3347
qreal pointCounter = 0;
48+
MatrixXd *matrix = nullptr;
3449

50+
private:
51+
/**
52+
* Removes an row with 0s in it.
53+
* @param mat new MatrixXd
54+
*/
55+
void removeZeroRows(Eigen::MatrixXd& mat);
3556

3657
};
3758

0 commit comments

Comments
 (0)