-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEEGStack.cpp
More file actions
208 lines (187 loc) · 5.37 KB
/
EEGStack.cpp
File metadata and controls
208 lines (187 loc) · 5.37 KB
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
// $Id: EEGStack.cpp,v 1.6 2008/04/09 04:57:26 samn Exp $
// EEGStack.cpp: implementation of the CEEGStack class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "wclust.h"
#include "EEGStack.h"
#include "Hist.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEEGStack::CEEGStack()
{
NUM_CHANNELS = 0;
NUM_SAMPLES = 0;
FREQUENCY = 0;
}
CEEGStack::~CEEGStack()
{
SetEmpty();
}
//////////////////////////////////////////////////////////////////////
void CEEGStack::AddEEG(CEEG *toStore)
{
eegStack.push_back(toStore);
}
//////////////////////////////////////////////////////////////////////
void CEEGStack::SetParams(char numChannels, short numSamples)
{
NUM_CHANNELS = numChannels;
NUM_SAMPLES = numSamples;
}
//////////////////////////////////////////////////////////////////////
void CEEGStack::SetEmpty()
{
MY_EEG_STACK::iterator index;
CEEG *eeg;
for ( index = eegStack.begin(); index != eegStack.end(); index++)
{
eeg = (CEEG*) *index;
eeg->SetEmpty();
delete eeg;
}
NUM_CHANNELS = 0;
NUM_SAMPLES = 0;
eegStack.clear();
}
//////////////////////////////////////////////////////////////////////
char CEEGStack::LoadEEG(CFile *file)
{
const int iBufferSz = NUM_CHANNELS * NUM_SAMPLES;
int TS;
vector<short> buffer(iBufferSz);
file->Read(&TS,4);
file->Read(&buffer[0], iBufferSz * sizeof(short) );
CEEG* newEEG = new CEEG();
newEEG->SetTimeStamp(TS);
newEEG->SetMyPalette(m_PalStack);
newEEG->SetEEG(buffer);
AddEEG(newEEG);
return 0;
}
//////////////////////////////////////////////////////////////////////
void CEEGStack::DrawEEG( CDC *pDC, CRect drawWin, int startTS, int timeWindow, unsigned int params, vector<double>& vGainList)
{
timeWindow *= 10;
int minT,maxT;
int indexEEG = eegStack.size() / 2 ; // start at middle of stack
int increment = indexEEG;
minT = 0; maxT = eegStack.size() - 1;
char finishedFind = 0;
while (increment > 0) // find index into eegStack that corresponds to startTS timestamp
{ // this is done with binary search
if ( startTS > ((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() )
{
minT = indexEEG;
increment = (maxT - minT)/2;
indexEEG += increment;
}
else{
if ( startTS < ((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() )
{
maxT = indexEEG;
increment = (maxT - minT)/2;
indexEEG -= increment;
}
else{
// ==
finishedFind = 1;
increment = 0;
}
}
}
if ( ! finishedFind )
{
indexEEG = minT;
if ( startTS > ((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() )
{
indexEEG = maxT;
}
else indexEEG = minT;
}
// Draw
int x1,x2;
char fine = 0;
float ratioTS = (float)drawWin.Width() / (float)timeWindow;
int indexFirst = indexEEG;
if (indexEEG >= eegStack.size()) //??
{
indexEEG = eegStack.size();
fine = 1;
}
// if ( ((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() >= startTS )
{
unsigned char toDraw = 0;
unsigned char bite = 4;
for ( int i = 0; i < NUM_CHANNELS; i++ )
{
if ( params & ( bite << i ) )
toDraw++;
}
int ySize1EEG = drawWin.Height() / toDraw;
if ( indexEEG > 0 )
{
indexEEG--;
}
double dGainSum = 0.0;
vector<int> vHeights(NUM_CHANNELS);
for(i=0;i<NUM_CHANNELS;i++)
{ if( !(params & ( bite << i ))) // if channel shouldn't be drawn
continue;
dGainSum += vGainList[i];
}
for(i=0;i<NUM_CHANNELS;i++)
vHeights[i]=(vGainList[i]/dGainSum)*drawWin.Height();
while ( ! fine && ((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() < startTS + timeWindow)
{ // draw from startTS until startTS+timeWindow units
{
x1 = drawWin.left + ratioTS * (((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() - startTS);
x2 = drawWin.left + ratioTS * (((CEEG*) *(eegStack.begin() + indexEEG))->GetTimeStamp() - startTS + NUM_SAMPLES*10000/FREQUENCY); //350 ~ 4000
int yWin = 0, yPos = 0;
CRect eegWin;
char iCurCh = 0;
for ( iCurCh = 0; iCurCh < NUM_CHANNELS; iCurCh++ ) //draw portion of each eeg channel
{ if ( params & ( bite << iCurCh ) )
{
eegWin.left = x1;
eegWin.right = x2;
eegWin.top = drawWin.top + yPos;
eegWin.bottom = drawWin.top + yPos + vHeights[iCurCh];
((CEEG*) *(eegStack.begin() + indexEEG))->DrawEEG(pDC,eegWin,drawWin,iCurCh,NUM_SAMPLES,vGainList[iCurCh]);
yWin++;
yPos += vHeights[iCurCh];
}
}
}
indexEEG++;
if (indexEEG >= eegStack.size())
{
indexEEG = 0;//--;
fine = 1;
}
}
}
// char a[200];
// sprintf(a,"%d %d " ,startTS, startTS + timeWindow);
// pDC->TextOut(10,30,a);
}
//////////////////////////////////////////////////////////////////////
int CEEGStack::GetFirstTimeStamp()
{
if ( *eegStack.begin() != NULL )
return ((CEEG*) *eegStack.begin())->GetTimeStamp();
return 0;
}
//////////////////////////////////////////////////////////////////////
int CEEGStack::GetLastTimeStamp()
{
if ( *(eegStack.end()-1) != NULL )
return ((CEEG*) *(eegStack.end()-1))->GetTimeStamp();
return 0;
}