forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob_correlation.cpp
213 lines (178 loc) · 4.72 KB
/
ob_correlation.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
/* -----------------------------------------------------------------------------
BrainBay Version 1.7, GPL 2003-2010, contact: [email protected]
MODULE: OB_CORRELATION.CPP: functions for the Correlation-Object
Author: Jeremy Wilkerson
The Correlation between two input-streams is calculated and presented
at the output port.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
-----------------------------------------------------------------------------*/
#include "brainBay.h"
#include "ob_correlation.h"
#include <iostream>
#include <cmath>
using namespace std;
CORRELATIONOBJ::CORRELATIONOBJ(int num) : BASE_CL()
{
outports = 1;
inports = 2;
width=75;
strcpy(out_ports[0].out_name,"out");
out_ports[0].get_range=-1;
strcpy(out_ports[0].out_dim,"none");
strcpy(out_ports[0].out_desc,"Correlation");
out_ports[0].out_max=1.0f;
out_ports[0].out_min=-1.0f;
accum1 = 0.0;
accum2 = 0.0;
for (int i = 0; i < NUMSAMPLES; i++)
{
samples1[i] = 0.0;
samples2[i] = 0.0;
}
interval = 100;
writepos1 = 0;
writepos2 = 0;
added1 = 0;
added2 = 0;
}
void CORRELATIONOBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_CORRBOX, ghWndStatusbox, (DLGPROC)CorrDlgHandler));
}
void CORRELATIONOBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("interval",P_INT,&interval);
}
void CORRELATIONOBJ::save(HANDLE hFile)
{
save_object_basics(hFile, this);
save_property(hFile,"interval",P_INT,&interval);
}
void CORRELATIONOBJ::incoming_data(int port, float value)
{
float *accum, *samples;
int *writepos, *added;
switch (port)
{
case 0:
accum = &accum1;
samples = samples1;
writepos = &writepos1;
added = &added1;
break;
case 1:
accum = &accum2;
samples = samples2;
writepos = &writepos2;
added = &added2;
break;
default:
return;
}
samples[*writepos] = value;
(*accum) += value;
(*added)++;
if ((*added) > interval)
{
int oldest = (*writepos) - interval;
if (oldest < 0)
oldest += NUMSAMPLES;
(*accum) -= samples[oldest];
(*added) = interval;
}
(*writepos)++;
if ((*writepos) >= NUMSAMPLES)
(*writepos) = 0;
}
void CORRELATIONOBJ::work(void)
{
float diff1,diff2;
float mean1 = accum1 / added1;
float mean2 = accum2 / added2;
float C12 = 0, V1 = 0, V2 = 0;
int pos1 = writepos1;
int pos2 = writepos2;
int added = (added1 < added2)?added1:added2;
for (int i = 0; i < added; i++)
{
diff1 = samples1[pos1] - mean1;
diff2 = samples2[pos2] - mean2;
C12 += diff1 * diff2;
V1 += diff1 * diff1;
V2 += diff2 * diff2;
pos1--;
pos2--;
if (pos1 < 0)
pos1 = NUMSAMPLES - 1;
if (pos2 < 0)
pos2 = NUMSAMPLES - 1;
}
C12 /= interval;
V1 /= interval;
V2 /= interval;
float correlation = C12 / sqrt(V1 * V2);
pass_values(0, correlation);
}
void CORRELATIONOBJ::change_interval(int newinterval)
{
interval = newinterval;
added1 = 0;
added2 = 0;
accum1 = 0;
accum2 = 0;
}
CORRELATIONOBJ::~CORRELATIONOBJ() {}
LRESULT CALLBACK CorrDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool init;
CORRELATIONOBJ * st;
st = (CORRELATIONOBJ *) actobject;
if ((st==NULL)||(st->type!=OB_CORR)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
SCROLLINFO lpsi;
lpsi.cbSize=sizeof(SCROLLINFO);
lpsi.fMask=SIF_RANGE|SIF_POS;
lpsi.nMin=1; lpsi.nMax=NUMSAMPLES - 1;
SetScrollInfo(GetDlgItem(hDlg,IDC_CORRINTERVALBAR),SB_CTL,&lpsi, TRUE);
SetDlgItemText(hDlg, IDC_TAG, st->tag);
init = true;
SetScrollPos(GetDlgItem(hDlg,IDC_CORRINTERVALBAR), SB_CTL,st->interval, TRUE);
SetDlgItemInt(hDlg, IDC_CORRINTERVAL, st->interval, FALSE);
init = false;
break;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
// switch (LOWORD(wParam))
// {
// }
// return TRUE;
break;
case WM_HSCROLL:
{
int nNewPos;
if (!init && (nNewPos = get_scrollpos(wParam,lParam)) >= 0)
{
if (lParam == (long) GetDlgItem(hDlg,IDC_CORRINTERVALBAR))
{
SetDlgItemInt(hDlg, IDC_CORRINTERVAL, nNewPos, TRUE);
st->change_interval(nNewPos);
}
}
break;
}
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
return(TRUE);
}
return FALSE;
}