-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vertex.cpp
5186 lines (4517 loc) · 135 KB
/
Vertex.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: Vertex.cpp,v 1.26 2009/01/23 00:55:53 samn Exp $
// Vertex.cpp: implementation of the CVertex class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "wclust.h"
#include "Vertex.h"
#include "SyncStack.h"
#include "SelectTT.h"
#include "uff.h"
#include "pca.h"
#include "FileUtils.h"
#include "MyVers.h"
#include "simple_haar.h"
#include "Log.h"
#include "pca2.hpp"
#include "WCMath.h"
#include "ScopedTimer.h"
#include "Hist.h"
#include "StringUtils.h"
#include <algorithm>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fstream>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
void CParamDim::SetParam(int index, float param)
{
switch (index)
{
case 1:
m_Par1 = param;
break;
case 2:
m_Par2 = param;
break;
case 3:
m_Par3 = param;
break;
case 4:
m_Par4 = param;
break;
}
}
/////////////////////////////////////////////////////////////////////////////
int CVertex::FREQ = 32000;
int CVertex::SAMPLES = 64;
int CVertex::RESOLUTION = 16;
int CVertex::CHANNELS = 4;
vector<float> CVertex::VX;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CVertex::ExportValues(FILE *file,int* pOverRideCluster)
{
if(pOverRideCluster)
fprintf(file,"%d\t%d\t", GetTimeStamp(), pOverRideCluster[0] );
else
fprintf(file,"%d\t%d\t", GetTimeStamp(), GetNoise() ? 0 : GetClust() );
//starts at m_Vertex.begin() + 1 because the first value is the # of clusters
//that this CVertex belongs to (see IsInCluster)
VERTEX::iterator index;
for (index = m_Vertex.begin() + 1; index != m_Vertex.end(); index++)
{
fprintf(file,"%f\t",*index);
}
fprintf(file,"\n");
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::AddPnt(float toStore)
{
m_Vertex.push_back(toStore);
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::ClearWaveformData()
{
m_d2YValues.clear();
m_YValues.clear();
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::AddClust(int toStore)
{
if(false)
{
m_MyClusts.push_back(toStore);
SetValue(0,GetValue(0)+1);
}
else
{
int HowMany = GetValue(0);
vector<int> newMyClusts(HowMany+1);
for (int i=0; i<HowMany; i++)
newMyClusts[i] = m_MyClusts[i];
newMyClusts[HowMany] = toStore;
m_MyClusts = newMyClusts;
SetValue(0,HowMany+1);
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::SelectCluster(int modeCl,vector<bool>& vNoise)
{
int NumbCl = GetValue(0);
if (modeCl == 0) // assignment random
{
if ( NumbCl == 2 )
{
m_MyClusts[1] &= 0xFFFF;
}
else if ( NumbCl > 2 )
{
int rnd = (NumbCl-1) * rand()/SHRT_MAX;
if (rnd == NumbCl)
rnd -=1;
int newCl = GetClust(rnd+1) & 0xFFFF;
EmptyClust();
AddClust(0);
AddClust(newCl);
SetNoise(vNoise[newCl]);
}
}
else if (modeCl == 1) // exclude
{
if (NumbCl == 2)
{
m_MyClusts[1] &= 0xFFFF;
}
else if (NumbCl > 2)
{
EmptyClust();
AddClust(0);
SetNoise(0);
}
}
else if (modeCl == 2) // weighted random
{
if (NumbCl == 2)
{
m_MyClusts[1] &= 0xFFFF;
}
else if ( NumbCl > 2 )
{
int weight = 0;
for (int i=1; i<NumbCl; i++)
{
weight += (GetClust(i) & 0xFFFF0000) / 0xFFFF;
}
int rnd = (weight+1) * rand()/SHRT_MAX;
int state=0;
weight=0;
for (i=1; i<NumbCl && state==0; i++)
{
weight += (GetClust(i) & 0xFFFF0000) / 0xFFFF;
if (weight >= rnd)
state=i;
}
int newCl = GetClust(state) & 0xFFFF;
EmptyClust();
AddClust(0);
AddClust(newCl);
SetNoise(vNoise[newCl]);
}
}
}
void CVertex::SetNoise(char TurnOn)
{
if (TurnOn)
{
m_Flag |= 0x80;
m_KmeansClust = m_InfoClust = m_APClust = m_KKClust = m_FLClust = -1;
}
else
{
m_Flag &= 0xFFFFFF7F;
if( m_KmeansClust < 0 ) m_KmeansClust = 0;
if( m_InfoClust < 0 ) m_InfoClust = 0;
if( m_APClust < 0 ) m_APClust = 0;
if( m_KKClust < 0 ) m_KKClust = 0;
if( m_FLClust < 0 ) m_FLClust = 0;
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::SetYValues(vector<short>& vBuffer, vector<float>& x, vector<float>& u, int channels, int samples)
{
int iSz = vBuffer.size();
//ensure proper size for buffers
m_YValues.resize(iSz);
m_d2YValues.resize(iSz);
// Convert to real value
for (int m_l=0;m_l<iSz;m_l++)
m_YValues[m_l] = (float) (10 * vBuffer[m_l])/SHRT_MAX;
//do interpolation
for (int i=0; i<channels; i++)
Spline(&x[0],&m_YValues[samples*i],samples,&m_d2YValues[samples*i],&u[0]);
m_Flag |= 0x0002;
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::SetYValues(vector<float>& mNew, vector<float>& x,vector<float>& u, int channels, int samples)
{
m_YValues.resize(mNew.size());
copy(mNew.begin(),mNew.end(),m_YValues.begin());
m_d2YValues.resize(channels*samples);
for (int i=0; i<channels; i++)
Spline(&x[0],&m_YValues[samples*i],samples,&m_d2YValues[samples*i],&u[0]);
m_Flag |= 0x0002;
}
/////////////////////////////////////////////////////////////////////////////
float CVertex::GetYValues(int Index)
{
if ( Index>=0 && Index<m_YValues.size() && m_Flag&0x0002 )
return m_YValues[Index];
else
return 0;
}
/////////////////////////////////////////////////////////////////////////////
int CVertex::IsInCluster(int num)
{
int i=0;
while ( i < *m_Vertex.begin() )
{
if ( m_MyClusts[i] == num )
return 1;
i++;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
int CVertex::GetClust()
{
// int Index;
// Index = *m_Vertex.begin();
// Index--;
// return *(m_MyClusts + Index);
return GetClust(GetValue(0)-1);
};
void CVertex::DoDraw(CDC* pDC,CRect DrawWin, CFloatRect* whichValues,CPoint whichAxes,int PntSize,unsigned long color)
{
float fx = m_Vertex[whichAxes.x];
float fy = m_Vertex[whichAxes.y];
if (whichValues->IsInRect(fx,fy))
{
int x=DrawWin.left+(fx-whichValues->GetLeft())*(DrawWin.right-DrawWin.left)/whichValues->GetSizeX();
int y=DrawWin.bottom+(fy-whichValues->GetBottom())*(DrawWin.top-DrawWin.bottom)/whichValues->GetSizeY();
#ifdef _DEBUG
if(whichValues->GetSizeX()==0.0 || whichValues->GetSizeY()==0.0)
{
int moo=0;
}
#endif
if (PntSize==1)
{
pDC->SetPixelV(x,y,color);
}
else if (PntSize==2)
{
pDC->SetPixelV(x,y,color);
pDC->SetPixelV(x+1,y+1,color);
pDC->SetPixelV(x+1,y,color);
pDC->SetPixelV(x,y+1,color);
}
else if (PntSize==3)
{
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
CBrush newBrush(color);
CBrush *pOldBrush=pDC->SelectObject(&newBrush);
pDC->Rectangle(x-1,y-1,x+2,y+2);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
else if (PntSize==4)
{
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
CBrush newBrush(color);
CBrush *pOldBrush=pDC->SelectObject(&newBrush);
pDC->Ellipse(x-2,y-2,x+2,y+2);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
else if (PntSize==5)
{
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
CBrush newBrush(color);
CBrush *pOldBrush=pDC->SelectObject(&newBrush);
pDC->Ellipse(x-2,y-2,x+3,y+3);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::Draw(CDC *pDC,CRect DrawWin,CFloatRect *whichValues,CPoint whichAxes,unsigned char *Clust,int PntSize)
{
//if(GetNoise()) MessageBox(0,"draw noise","draw noise",0);
if ( m_Flag & 0x0001 )
{
if(GetNoise() && !(Clust[255] & 1)) return;
if(GetNoise() && *m_Vertex.begin()==0)
{ //not hiding given cluster
if( (*(Clust+255)) & 1)
{
unsigned long color=RGB(204,204,204);
DoDraw(pDC,DrawWin,whichValues,whichAxes,PntSize,color);
}
}
else
{ //not hiding given cluster
int Index = *m_Vertex.begin();
Index--;
if ( (*(Clust+(int) m_MyClusts[Index])) & 1 )
{
unsigned long color=m_PalStack->GetSColor( m_MyClusts[Index] );
if(GetNoise()) color = RGB(204,204,204);
DoDraw(pDC,DrawWin,whichValues,whichAxes,PntSize,color);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::DrawOrig(CDC *pDC,CRect DrawWin,CFloatRect *whichValues,CPoint whichAxes,unsigned char *Clust,int PntSize)
{
unsigned long color;
int ShowIt=0;
#ifdef _DEBUG
if(m_OrigClust < 0)
int moo = 9;
#endif
if ( m_Flag & 0x0001 )
{
if(m_OrigClust >= 0 && m_OrigClust < 255 && Clust[m_OrigClust] & 1)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) (m_OrigClust) );
if ((int)m_OrigClust == 255)
MessageBeep(0);
}
}
if (m_OrigClust < 0 || GetNoise())
color = RGB(204,204,204);
if ( ShowIt )
{
DoDraw(pDC,DrawWin,whichValues,whichAxes,PntSize,color);
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::DrawAutoC(CDC* pDC,CRect DrawWin,CFloatRect* whichValues,CPoint whichAxes,unsigned char* Clust,int PntSize,char whichAuto)
{
if ( m_Flag & 0x0001 )
{
if(GetNoise())
{
if( (*(Clust+255)) & 1)
{
unsigned long color = RGB(204,204,204);
DoDraw(pDC,DrawWin,whichValues,whichAxes,PntSize,color);
}
}
else
{
char c;
if(whichAuto == CLUST_KM)
c = m_KmeansClust;
else if(whichAuto == CLUST_INFO)
c = m_InfoClust;
else if(whichAuto == CLUST_AP)
c = m_APClust;
else if(whichAuto == CLUST_KK)
c = m_KKClust;
else
c = m_FLClust;
if ( (*(Clust+(int) c)) & 1 )
{
unsigned long color=m_PalStack->GetSColor( (int) c );
DoDraw(pDC,DrawWin,whichValues,whichAxes,PntSize,color);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::CalcAvgSpike(float *xa,int Freq,int Samples,CRect DrawWin,int whichChanel,int whichClust,float *Average,int *Numb,int whichDraw)
{
int CountIt=0;
if(whichDraw!=CLUST_USER && whichDraw!=CLUST_ORIG && GetNoise())
{
if(whichClust>=0) return;
else CountIt=1;
}
int Index = 0;
if(!CountIt) switch(whichDraw)
{
case CLUST_USER:
Index = *(m_Vertex.begin())-1;
if ( m_MyClusts[Index]==whichClust ) CountIt=1;
break;
case CLUST_ORIG:
if(m_OrigClust==whichClust) CountIt=1;
break;
case CLUST_KM:
if(whichClust==0 || m_KmeansClust==whichClust) CountIt=1;
break;
case CLUST_INFO:
if(whichClust==0 || m_InfoClust==whichClust) CountIt=1;
break;
case CLUST_AP:
if(whichClust==0 || m_APClust==whichClust) CountIt=1;
break;
case CLUST_KK:
if(whichClust==0 || m_KKClust==whichClust) CountIt=1;
break;
case CLUST_FL:
if(whichClust==0 || m_FLClust==whichClust) CountIt=1;
break;
}
if ( CountIt )
{
* Numb += 1;
float fy;
float t=0;
float tStep=(float) (Samples-1)/DrawWin.Width();
for (int i=0; i<DrawWin.Width(); i++)
{
fy = Splint(xa, t/(Freq) ,(int)floor(t),(int)floor(t)+1,whichChanel,Samples);
*(Average+i) += fy;
t += tStep;
}
}
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::DrawTimeSpikes(CDC *pDC, float *xa, int freq, int samples, CRect spikeWin, CRect drawWin, int whichChanel, int whichSpDraw)
{
unsigned long color = 0;
int Index = *m_Vertex.begin();
if(GetNoise())
{
color = RGB(204,204,204);
}
else switch(whichSpDraw)
{
case CLUST_ORIG:
case CLUST_USER:
color = ( whichSpDraw == 0 ) ? m_PalStack->GetSColor( (int) m_MyClusts[Index-1] ) : m_PalStack->GetSColor( GetOrigClust() );
break;
case CLUST_AP:
color = m_PalStack->GetSColor( m_APClust );
break;
case CLUST_KM:
color = m_PalStack->GetSColor( m_KmeansClust );
break;
case CLUST_INFO:
color = m_PalStack->GetSColor( m_InfoClust );
break;
case CLUST_KK:
color = m_PalStack->GetSColor( m_KKClust );
break;
case CLUST_FL:
color = m_PalStack->GetSColor( m_FLClust );
break;
}
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
int y, yPrev;
float fy;
float t=0;
float tStep=(float) (samples - 1) / spikeWin.Width();
fy = Splint(xa, t/(freq), (int)floor(t), (int)floor(t)+1, whichChanel, samples);
yPrev = spikeWin.bottom - fy*spikeWin.Height()/20 - spikeWin.Height()/2;
if ( spikeWin.right > drawWin.right )
{
spikeWin.right = drawWin.right;
}
// if ( spikeWin.left < drawWin.left )
// {
// t = tStep * ( drawWin.left - spikeWin.left );
// spikeWin.left = drawWin.left;
// }
for (int i = spikeWin.left + 1; i <= spikeWin.right; i++)
{
fy = Splint(xa, t/(freq), (int)floor(t), (int)floor(t)+1, whichChanel, samples);
y = spikeWin.bottom - fy*spikeWin.Height()/20 - spikeWin.Height()/2;
pDC->MoveTo(i-1,yPrev);
pDC->LineTo(i,y);
yPrev=y;
t += tStep;
}
pDC->SelectObject(pOldPen);
};
/////////////////////////////////////////////////////////////////////////////
void CVertex::DrawSpikes(CDC *pDC, float *xa, int Freq, int Samples, CRect DrawWin,int whichChanel,int whichClust,int whichDrawMode)
{
int ShowIt=0;
int Index = *m_Vertex.begin();
unsigned long color;
if(GetNoise() && whichDrawMode!=CLUST_USER && whichDrawMode!=CLUST_ORIG)
{
if(whichClust>=0) return;
else{ ShowIt=1; color = RGB(204,204,204); }
}
if(!ShowIt) switch(whichDrawMode)
{
case CLUST_USER:
if ( true || (m_Flag & 0x0004) )
{
Index--;
if ( Index>=0 && m_MyClusts[Index]==whichClust )
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) m_MyClusts[Index] );
}
if(GetNoise())
color = RGB(204,204,204);
}
break;
case CLUST_ORIG:
if(whichClust == m_OrigClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
if(GetNoise()) color = RGB(204,204,204);
}
break;
case CLUST_KM:
if(whichClust==m_KmeansClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
}
break;
case CLUST_INFO:
if(whichClust==m_InfoClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
}
break;
case CLUST_AP:
if(whichClust==m_APClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
}
break;
case CLUST_KK:
if(whichClust==m_KKClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
}
break;
case CLUST_FL:
if(whichClust==m_FLClust)
{
ShowIt=1;
color=m_PalStack->GetSColor( (int) whichClust );
}
break;
}
if ( ShowIt )
{
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
int y, yPrev;
float fy;
float t=0;
float tStep=(float) (Samples - 1)/DrawWin.Width();
fy = Splint(xa, t/(Freq) ,(int)floor(t),(int)floor(t)+1,whichChanel,Samples);
yPrev = DrawWin.bottom - fy*DrawWin.Height()/20 - DrawWin.Height()/2;
for (int i=DrawWin.left+1;i<DrawWin.right;i++)
{
fy = Splint(xa, t/(Freq), (int)floor(t), (int)floor(t)+1, whichChanel, Samples);
y = DrawWin.bottom - fy*DrawWin.Height()/20 - DrawWin.Height()/2;
pDC->MoveTo(i-1,yPrev);
pDC->LineTo(i,y);
yPrev=y;
t += tStep;
}
pDC->SelectObject(pOldPen);
}
}
/////////////////////////////////////////////////////////////////////////////
int CVertex::DrawSpikes(CDC *pDC, float *xa, int Freq, int Samples, CRect DrawWin, int whichChanel)
{
if(GetNoise()) return 0;
if (m_Flag & 0x0004)
{
unsigned long color;
int Index = *m_Vertex.begin();
if(Index - 1 < 0) return 0;
color=m_PalStack->GetSColor( m_MyClusts[Index-1] );
CPen newPen(PS_SOLID,1,color);
CPen *pOldPen=pDC->SelectObject(&newPen);
int y, yPrev;
float fy;
float t=0;
float tStep=(float) (Samples - 1)/DrawWin.Width();
fy = Splint(xa, t/(Freq), (int)floor(t), (int)floor(t)+1, whichChanel, Samples);
yPrev = DrawWin.bottom - fy*DrawWin.Height()/20 - DrawWin.Height()/2;
for (int i=DrawWin.left+1;i<=DrawWin.right;i++)
{
fy = Splint(xa, t/(Freq), (int)floor(t), (int)floor(t)+1, whichChanel, Samples);
y = DrawWin.bottom - fy*DrawWin.Height()/20 - DrawWin.Height()/2;
pDC->MoveTo(i-1,yPrev);
pDC->LineTo(i,y);
yPrev=y;
t += tStep;
}
pDC->SelectObject(pOldPen);
return 1;
}
else return 0;
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::ClearHighDim(int Dim)
{
while (m_Vertex.size() > Dim+1)
m_Vertex.pop_back();
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::RemHighDim(int num)
{
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::CalcParam(CParamDim *param,float *xa,int Freq,int Samples, sSpike *spike)
{
if ( param->GetType() == 2 )
{ // Y-value in time = par1, chanel = par2
float t = param->m_Par1 * Freq;
float fy = Splint(xa, param->m_Par1, (int)floor(t), (int)floor(t)+1, (int) param->m_Par2, Samples);
AddPnt(fy);
}
else if ( param->GetType() == 3 )
{ // dY-value in time = par1, chanel = par2
float t = param->m_Par1 * Freq;
float fy = Splope(xa, param->m_Par1, (int)floor(t), (int)floor(t)+1, (int) param->m_Par2, Samples)/1000;
AddPnt(fy);
}
else if ( param->GetType() == 4 )
{ // Energy-value in t1 = par1, t2 = par2, chanel = par2
int t1 = (int) floor (param->m_Par1*Freq);
int t2 = (int) ceil (param->m_Par2*Freq);
if (t2 > (Samples-1))
t2 = Samples-1;
int shift = Samples * param->m_Par3, iVals = 0;
float fy = 0.0f;
for (int i=t1; i<t2; i++, iVals++)
{
fy += m_YValues[shift+i] * m_YValues[shift+i];
}
fy = sqrt(fy);
if(iVals) fy /= iVals;
AddPnt(fy);
}
else if ( param->GetType() == 5 )
{ // WCP external parameter: par1 = index in names, par2 = channel, par3 = index in spike->paramFile
float fy = (float) *( spike->fileParams.begin() + (int) param->GetParam( 3 ) );
AddPnt(fy);
}
else if ( param->GetType() == 6 )
{
int isz = param->m_Par2 , i = 0;
int iS = 0, iSamps = CeilPowOf2(m_YValues.size());
vector<double> vals(iSamps);
copy(m_YValues.begin(),m_YValues.end(),vals.begin());
int iDimsAdded = 0;
simple_haar haar;
haar.wavelet_calc(vals);
int iA,iC;
for(iA=0;iA<haar.coef.size() && iDimsAdded < isz;iA++)
{ for(iC=0;iC<haar.coef[iA].size() && iDimsAdded < isz;iC++)
{ iDimsAdded++;
AddPnt(haar.coef[iA][iC]);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
bool CVertex::CheckNoise(int iChannels,int iSamples,double dLowSat,double dHighSat,double dFracSat)
{
// NOISE DISABLED
// if ( timeMax > 100 )
// SetNoise(1);
// else
// if ( timeMin > 100 )
// SetNoise(1);
// else
// SetNoise(0);
// SetNoise(0);
bool bIsNoise = false;
static int iSM = 0;
int iC = 0 , iSaturate = 0;
for(;iC<iChannels;iC++) //do saturation check
{
int iSat = 0;
float* y = &m_YValues[iSamples*iC];
int i=0;
for(;i<iSamples;i++)
{ //max val has abs val of 10, allow slightly looser measure
if(y[i] > dHighSat || y[i] < dLowSat)
{
iSat++; iSaturate++;
}
}
if(iSaturate>iSM)
iSM=iSaturate;
//if 33% of values too high or too low
//means spike is probably noise
if(static_cast<double>(iSat)/iSamples >= dFracSat)
{
bIsNoise=true;
break;
}
}
if(bIsNoise)
{
SetNoise(1);
*m_Vertex.begin()=0;
return true;
}
return false;
}
prob_t CVertex::GetEnergy(int iSamples,int iChannel)
{
prob_t dEnergy = 0.0;
float* y = &m_YValues[iSamples*iChannel];
int i;
for(i=0;i<iSamples;i++)
dEnergy += y[i]*y[i];
dEnergy = sqrt(dEnergy);
if(iSamples)
dEnergy /= iSamples;
return dEnergy;
}
/////////////////////////////////////////////////////////////////////////////
void CVertex::Swap(int channels, int samples)
{
for (int j = 0; j< channels * samples; j++)
{
m_YValues[j] = - m_YValues[j];
m_d2YValues[j] = - m_d2YValues[j];
}
float Stack1Value;
//bad - hard coded 8
//caused crash when reduced # of params from 17 to 12
//couldn't find bug for hours :( :( :(
//don't want to hard code another number
//what if # of dimensions is even??
for (char i = 0; i<8; i++)
{
Stack1Value = *(m_Vertex.begin() + i + 1);
*(m_Vertex.begin() + i + 1) = *(m_Vertex.begin() + i + 8 + 1);
*(m_Vertex.begin() + i + 8 + 1) = Stack1Value;
}
char toSwap = timeMax; timeMax = timeMin; timeMin = toSwap;
}
/////////////////////////////////////////////////////////////////////////////
// Written by Andre Fenton
void CVertex::Spline(float *x,float *y,int n,float *d2y,float *u)
{
register int i;
register float p, sig;
d2y[0] = u[0] = 0.0;
for (i = 1; i < n - 1; i++) {
sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
p = sig * d2y[i - 1] + 2.0;
d2y[i] = (sig - 1.0) / p;
u[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]);
u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
}
d2y[n - 1] = 0.0;
for (i = n - 1; i--; )
d2y[i] = d2y[i] * d2y[i + 1] + u[i];
return;
}
void CVertex::Spline(float *x,int n,float *u)
{
register int i;
register float p, sig;
float *y,*d2y;
y = &m_YValues[0];
d2y = &m_d2YValues[0];
d2y[0] = u[0] = 0.0;
for (i = 1; i < n - 1; i++) {
sig = (x[i] - x[i - 1]) / (x[i + 1] - x[i - 1]);
p = sig * d2y[i - 1] + 2.0;
d2y[i] = (sig - 1.0) / p;
u[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]) - (y[i] - y[i - 1]) / (x[i] - x[i - 1]);
u[i] = (6.0 * u[i] / (x[i + 1] - x[i - 1]) - sig * u[i - 1]) / p;
}
d2y[n - 1] = 0.0;
for (i = n - 1; i--; )
d2y[i] = d2y[i] * d2y[i + 1] + u[i];
return;
}
/////////////////////////////////////////////////////////////////////////////
// Written by Andre Fenton
float CVertex::Splope(float *xa,float *ya,float *d2y,float x,int lo_pt,int hi_pt)
{
register float a, b, c, d, e, f, g, h;
a = xa[lo_pt];
b = xa[hi_pt];
c = ya[lo_pt];
d = ya[hi_pt];
e = d2y[lo_pt];
f = d2y[hi_pt];
g = b - a;
h = (x * (e * b - f * a) + d - c) / g;
h += (x * x * (f - e) + f * a * a - e * b * b) / (2.0 * g);
h += g * (e - f) / 6.0;
return(h);
}
float CVertex::Splope(float *xa,float x,int lo_pt,int hi_pt)
{
float *ya,*d2y;
ya = &m_YValues[0];
d2y = &m_d2YValues[0];
register float a, b, c, d, e, f, g, h;
a = xa[lo_pt];
b = xa[hi_pt];
c = ya[lo_pt];
d = ya[hi_pt];
e = d2y[lo_pt];
f = d2y[hi_pt];
g = b - a;
h = (x * (e * b - f * a) + d - c) / g;
h += (x * x * (f - e) + f * a * a - e * b * b) / (2.0 * g);
h += g * (e - f) / 6.0;
return(h);
}
float CVertex::Splope(float *xa,float x,int lo_pt,int hi_pt,int channel,int Samples)
{
float *ya,*d2y;
ya = &m_YValues[channel*Samples];
d2y = &m_d2YValues[channel*Samples];
register float a, b, c, d, e, f, g, h;
a = xa[lo_pt];
b = xa[hi_pt];
c = ya[lo_pt];
d = ya[hi_pt];
e = d2y[lo_pt];
f = d2y[hi_pt];
g = b - a;
h = (x * (e * b - f * a) + d - c) / g;
h += (x * x * (f - e) + f * a * a - e * b * b) / (2.0 * g);
h += g * (e - f) / 6.0;
return(h);
}
/////////////////////////////////////////////////////////////////////////////
// Written by Andre Fenton
float CVertex::Splint(float *xa,float *ya,float *d2y,float x,int lo_pt,int hi_pt)
{
register float h, b, a;
float y;
h = xa[hi_pt] - xa[lo_pt];
a = (xa[hi_pt] - x) / h;
b = (x - xa[lo_pt]) / h;
y = a * ya[lo_pt] + b * ya[hi_pt] + ((a * a * a - a) * d2y[lo_pt] + (b * b * b - b) * d2y[hi_pt]) * (h * h) / 6.0;
return(y);
}
float CVertex::Splint(float *xa,float x,int lo_pt,int hi_pt)
{
float *ya,*d2y;
ya = &m_YValues[0];
d2y = &m_d2YValues[0];
register float h, b, a;
float y;
h = xa[hi_pt] - xa[lo_pt];
a = (xa[hi_pt] - x) / h;
b = (x - xa[lo_pt]) / h;