-
Notifications
You must be signed in to change notification settings - Fork 5
/
mycallback.cpp
executable file
·219 lines (165 loc) · 5.38 KB
/
mycallback.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "mycallback.h"
MyCallback::MyCallback(GLWidget *glwidget_, QObject *parent) :
Sbs2Callback(parent), glwidget(glwidget_)
{
verticesData = new DTU::DtuArray2D<double>(64,1028);
colorData = new DTU::DtuArray2D<double>(1028,4); //rgba values
(*colorData) = 0;
QObject::connect(sbs2DataHandler,SIGNAL(sourceReconstructionSpectrogramReady()),this,SLOT(sourceReconstructionPowerReady()));
//QObject::connect(sbs2DataHandler,SIGNAL(sourceReconstructionReady()),this,SLOT(sourceReconstructionReady()));
//QObject::connect(glwidget,SIGNAL(turnSourceReconstructionPowerOn(int,int,int,int, QString)),this,SLOT(turnSourceReconstructionPowerOn(int,int,int,int, QString)));
QObject::connect(glwidget,SIGNAL(turnSourceReconstructionPowerOn(int,int,int,int,QString)),this,SLOT(turnOnSourceReconstructionLoreta(int,int,int,int,QString)));
QObject::connect(glwidget,SIGNAL(changeBand(QString)),this,SLOT(changeBand(QString)));
QObject::connect(this,SIGNAL(deviceFoundSignal(QMap<QString,QVariant>)),glwidget,SLOT(deviceFound(QMap<QString,QVariant>)));
qsrand(QDateTime::currentMSecsSinceEpoch());
updateColorMap(3);
/* This is optional. Remove to work on all brain or add additional regions. */
//dtuEmotivRegion = new DtuEmotivRegion();
//dtuEmotivRegion->addRegion("Frontal_Lobe");
meanWindowLength = 32;
maxValues = new QVector<double>();
minValues = new QVector<double>();
changeBand("alpha");
collectedSamples = 0;
QVector<double> lambdas = QVector<double>(5);
lambdas[0]=0.01;
lambdas[1]=0.1;
lambdas[2]=1;
lambdas[3]=10;
lambdas[4]=100;
//turnOnSourceReconstructioSparse(8, lambdas);
}
void MyCallback::changeBand(QString name)
{
minValues->clear();
maxValues->clear();
if (name.compare("delta"))
{
lowFreq = 1;
highFreq = 4;
}
if (name.compare("theta"))
{
lowFreq = 4;
highFreq = 8;
}
if (name.compare("alpha"))
{
lowFreq = 8;
highFreq = 12;
}
if (name.compare("lowBeta"))
{
lowFreq = 12;
highFreq = 16;
}
if (name.compare("beta"))
{
lowFreq = 16;
highFreq = 20;
}
}
void MyCallback::getData(Sbs2Packet *packet)
{
thisPacket = packet;
currentPacketCounter = packet->counter;
currentPacket += 1;
glwidget->updateGyroX(packet->gyroX);
glwidget->updateGyroY(packet->gyroY);
sbs2DataHandler->setThisPacket(thisPacket);
//sbs2DataHandler->doSourceReconstruction();
sbs2DataHandler->doSourceReconstructionSpectrogram();
}
void MyCallback::sourceReconstructionPowerReady()
{
createColorMatrix(sbs2DataHandler->getSourceReconstructionSpectrogramValues());
updateModel();
}
void MyCallback::createColorMatrix2(DTU::DtuArray2D<double> *verticesData_)
{
for (int vertex = 0; vertex<verticesData_->dim2(); ++vertex)
{
double v = 0.0;
if ((*verticesData_)[0][vertex] > 0) v = 1.0;
(*colorData)[vertex][0] = 1.0-v;
(*colorData)[vertex][1] = 1.0-v;
(*colorData)[vertex][2] = 1.0;
(*colorData)[vertex][3] = 1.0;
}
}
/**
This function sees 64x1028 input matrix and should produce 1028x3 color matrix ready to pass to visualization.
*/
void MyCallback::createColorMatrix(DTU::DtuArray2D<double> *verticesData_)
{
double meanMax = 0;
double meanMin = 0;
for (int t=0; t<minValues->size(); ++t)
{
meanMin += minValues->at(t);
}
if (minValues->size())
meanMin /= (double)minValues->size();
for (int t=0; t<maxValues->size(); ++t)
{
meanMax += maxValues->at(t);
}
if (maxValues->size())
meanMax /= (double)maxValues->size();
double currentMax = -999999999;
double currentMin = 9999999999;
double scaling = meanMax - meanMin;
for (int vertex = 0; vertex<verticesData_->dim2(); ++vertex)
{
double this_vertex_power = 0.0;
for (int freq = lowFreq; freq < highFreq; ++freq)
{
this_vertex_power += (*verticesData_)[freq][vertex];
}
this_vertex_power = 20 * qLn(this_vertex_power + 1)/qLn(10);
if (this_vertex_power > currentMax) currentMax = this_vertex_power;
if (this_vertex_power < currentMin) currentMin = this_vertex_power;
double v = 0.0;
v += (this_vertex_power - meanMin)/scaling * 1.0;
if (v < 0.5) v = 0;
if (v > 1.0) v = 1.0;
(*colorData)[vertex][0] = 1.0 - v;
(*colorData)[vertex][1] = 1.0 - v;
(*colorData)[vertex][2] = 1.0;
(*colorData)[vertex][3] = 1.0;
}
minValues->append(currentMin);
if (minValues->size() == meanWindowLength) minValues->erase(minValues->begin());
maxValues->append(currentMax);
if (maxValues->size() == meanWindowLength) maxValues->erase(maxValues->begin());
}
void MyCallback::updateModel()
{
for (int vertex = 0; vertex < colorData->dim1(); ++vertex)
{
glwidget->updateColorForVertex(vertex,(*colorData)[vertex][0],(*colorData)[vertex][1],(*colorData)[vertex][2], (*colorData)[vertex][3]);
}
visualized = 1;
}
void MyCallback::updateColorMap(int colorMap)
{
qDebug() << "updating color map "<<colorMap;
QString filename(":/colortable");
filename.append(QString::number(colorMap));
QFile file2(filename);
if (!file2.open(QIODevice::ReadOnly | QIODevice::Text))
qDebug() <<"file problem";
int i=0;
cmap.clear();
while (!file2.atEnd())
{
QByteArray line = file2.readLine();
QString str = line.data();
QStringList list1 = str.split(",");
for (int j = 0; j < list1.size(); j++)
{
cmap.push_back(list1.at(j).toDouble());
}
++i;
}
}