forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob_deviation.cpp
157 lines (128 loc) · 3.47 KB
/
ob_deviation.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
/* -----------------------------------------------------------------------------
BrainBay - Version 1.7, GPL 2003-2010
MODULE: OB_DEVIATION.CPP
Author: Chris Veigl
This Object outputs the Standard Deviation and Mean of n
Samples captured from it's input-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_deviation.h"
LRESULT CALLBACK DeviationDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static bool init;
DEVIATIONOBJ * st;
st = (DEVIATIONOBJ *) actobject;
if ((st==NULL)||(st->type!=OB_DEVIATION)) 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_DEVIATIONINTERVALBAR),SB_CTL,&lpsi, TRUE);
init = true;
SetScrollPos(GetDlgItem(hDlg,IDC_DEVIATIONINTERVALBAR), SB_CTL,st->interval, TRUE);
SetDlgItemInt(hDlg, IDC_DEVIATIONINTERVAL, st->interval, FALSE);
init = false;
break;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
return TRUE;
break;
case WM_HSCROLL:
{
int nNewPos;
if (!init && (nNewPos = get_scrollpos(wParam,lParam)) >= 0)
{
if (lParam == (long) GetDlgItem(hDlg,IDC_DEVIATIONINTERVALBAR))
{
SetDlgItemInt(hDlg, IDC_DEVIATIONINTERVAL, nNewPos, TRUE);
st->change_interval(nNewPos);
}
}
break;
}
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
return(TRUE);
}
return FALSE;
}
DEVIATIONOBJ::DEVIATIONOBJ(int num) : BASE_CL()
{
outports = 2;
inports = 1;
width=75;
strcpy(in_ports[0].in_name,"in");
strcpy(out_ports[0].out_name,"dev");
strcpy(out_ports[1].out_name,"mean");
meanaccu = 0.0; devaccu=0.0;
for (int i = 0; i < NUMSAMPLES; i++)
{
samples[i] = 0.0;
}
interval = 256;
writepos = 0;
added = 0;
}
void DEVIATIONOBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_DEVIATIONBOX, ghWndStatusbox, (DLGPROC)DeviationDlgHandler));
}
void DEVIATIONOBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("interval",P_INT,&interval);
}
void DEVIATIONOBJ::save(HANDLE hFile)
{
save_object_basics(hFile,this);
save_property(hFile,"interval",P_INT,&interval);
}
void DEVIATIONOBJ::incoming_data(int port, float value)
{
if (value!=INVALID_VALUE)
{
samples[writepos] = value;
meanaccu += value;
added++;
if (added > interval)
{
int oldest = writepos - interval;
if (oldest < 0)
oldest += NUMSAMPLES;
meanaccu -= samples[oldest];
devaccu -= squares[oldest];
added = interval;
}
mean=meanaccu / added;
squares[writepos]=(value-mean)*(value-mean);
devaccu += squares[writepos];
deviation = (float) sqrt ((double) (devaccu / added));
writepos++;
if (writepos >= NUMSAMPLES)
writepos = 0;
}
}
void DEVIATIONOBJ::work(void)
{
pass_values(0, deviation);
pass_values(1, mean);
}
void DEVIATIONOBJ::change_interval(int newinterval)
{
interval = newinterval;
added = 0;
meanaccu = 0;
devaccu=0;
}
DEVIATIONOBJ::~DEVIATIONOBJ() {}