forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob_filter.cpp
384 lines (319 loc) · 10.8 KB
/
ob_filter.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
/* -----------------------------------------------------------------------------
BrainBay Version 1.7, GPL 2003-2010, contact: [email protected]
MODULE: OB_FILTER.CPP: contains assisting functions for the Filter-Object
Authors: Jim Peters, Chris Veigl
This module calls into the fid-lib filter library by Jim Peters to provide
filter functionalities. The Filter-Type and order can be selected.
A brief preview of the filter's frequency-response is shown in the
filter-toolbox-window. For a better display check out Jim Peters fiview-tool:
http://uazu.net/fiview
do_filt_design: Initialises a new filter using the init-String
(for exemple LpBe for a low-pass - bessel filter, see fiview-documentation)
and additional filter-parameters (like order, from-, to-freqeuncy)
update_filterdialog: enables/disables init-parameters according to the filter-type
FilterBoxDlgHandler: processes events for the filter-toolbox window.
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_filter.h"
struct FILTERTYPEStruct FILTERTYPE[FILTERTYPES]=
{{"BandStop Resonator","BsRe/",1},
{"LowPass Bessel","LpBe",1},
{"HighPass Bessel","HpBe",1},
{"BandPass Bessel","BpBe",2},
{"BandStop Bessel","BsBe",2},
{"LowPass Butterworth","LpBu",1},
{"HighPass Butterworth","HpBu",1},
{"BandPass Butterworth","BpBu",2},
{"BandStop Butterworth","BsBu",2}};
/* // I had problems with Chebyshev Filters (filter setup crashed) ..
"LowPass Chebyshev","LpCh"
"HighPass Chebyshev","HpCh"
"BandPass Chebyshev","BpCh"
"BandStop Chebyshev", "BsCh"
*/
int test_filterparams(int type,int p0,float p1,float p2)
{
if ((type<0)||(type>=FILTERTYPES)) return FALSE;
if (p0<1) return FALSE;
if ((p1<0.001f)||(p1>=1024.0f)) return FALSE;
if ((p2<0.001f)||(p2>=1024.0f)) return FALSE;
switch (type)
{
case 0: if (p0>=100) return FALSE; break;
case 1:
case 2:
case 3:
case 4: if (p0>=11) return FALSE; break;
case 5:
case 6: if (p0>=60) return FALSE; break;
case 7:
case 8: if (p0>=30) return FALSE; break;
}
return (TRUE);
}
FidFilter * do_filt_design(HWND hDlg, int ftype)
{
char sztemp[30];
char szorder[5];
int p0;
float p1,p2;
BOOL trans;
p0=GetDlgItemInt(hDlg, IDC_FILTERPAR0, &trans, 0); if (!trans) p0=0;
GetDlgItemText(hDlg,IDC_FILTERPAR1,sztemp,sizeof(sztemp));
sscanf(sztemp,"%f",&p1);
GetDlgItemText(hDlg,IDC_FILTERPAR2,sztemp,sizeof(sztemp));
sscanf(sztemp,"%f",&p2);
if (test_filterparams(ftype,p0,p1,p2))
{
strcpy(sztemp,FILTERTYPE[ftype].init);
GetDlgItemText(hDlg, IDC_FILTERPAR0, szorder, sizeof(szorder));
strcat(sztemp,szorder);
return (fid_design(sztemp, PACKETSPERSECOND, p1,p2,0,0));
}
return (NULL);
}
void update_filterdialog(HWND hDlg, int filternum)
{
if (filternum>=0)
{
switch (FILTERTYPE[filternum].param) {
case 1 : SetDlgItemText(hDlg, IDC_PAR0CAPT, "Filter Order");
SetDlgItemText(hDlg, IDC_PAR1CAPT, "Freq (Hz)");
SetDlgItemText(hDlg, IDC_PAR2CAPT, "");
EnableWindow(GetDlgItem(hDlg, IDC_FILTERPAR2), FALSE);
break;
case 2 :SetDlgItemText(hDlg, IDC_PAR0CAPT, "Filter Order");
SetDlgItemText(hDlg, IDC_PAR1CAPT, "from (Hz)");
SetDlgItemText(hDlg, IDC_PAR2CAPT, "to (Hz)");
EnableWindow(GetDlgItem(hDlg, IDC_FILTERPAR2), TRUE);
break;
}
}
}
/*-----------------------------------------------------------------------------
FUNCTION: CALLBACK FilterboxDlgHandler( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
PURPOSE: Handles Messages for Filteredit Dialog
PARAMETERS:
hDlg - Dialog window handle
-----------------------------------------------------------------------------*/
LRESULT CALLBACK FilterboxDlgHandler( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
int t; // ,z;
char newname[25],sztemp[30];
static int acttype;
static int dinit=FALSE;
static FidFilter * tempf=NULL,* newf=NULL;
FILTEROBJ * st;
st = (FILTEROBJ *) actobject;
if ((st==NULL)||(st->type!=OB_FILTER)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
dinit=TRUE;
SendMessage(GetDlgItem(hDlg, IDC_FILTERTYPECOMBO), CB_RESETCONTENT,0,0);
for (t = 0; t < FILTERTYPES; t++)
SendMessage( GetDlgItem(hDlg, IDC_FILTERTYPECOMBO), CB_ADDSTRING, 0, (LPARAM) (LPSTR) FILTERTYPE[t].tname) ;
SetDlgItemText(hDlg,IDC_FILTERTYPECOMBO, FILTERTYPE[st->filtertype].tname);
SetDlgItemText(hDlg,IDC_FILTERNEWNAME, st->name);
SetDlgItemInt(hDlg,IDC_FROMFREQ, st->dispfrom,0);
SetDlgItemInt(hDlg,IDC_TOFREQ, st->dispto,0);
SetDlgItemInt(hDlg,IDC_FILTERPAR0, st->par0,0);
sprintf(sztemp,"%.2f",st->par1);
SetDlgItemText(hDlg,IDC_FILTERPAR1, sztemp);
sprintf(sztemp,"%.2f",st->par2);
SetDlgItemText(hDlg,IDC_FILTERPAR2, sztemp);
acttype=st->filtertype;
dinit=FALSE;
newf=do_filt_design(hDlg,acttype);
if (newf) tempf=newf;
update_filterdialog(hDlg,st->filtertype);
return TRUE;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_FILTERTYPECOMBO:
acttype=SendMessage( GetDlgItem(hDlg, IDC_FILTERTYPECOMBO), CB_GETCURSEL, 0, 0 ) ;
update_filterdialog(hDlg,acttype);
case IDC_FILTERPAR0:
case IDC_FILTERPAR1:
case IDC_FILTERPAR2:
case IDC_FROMFREQ:
case IDC_TOFREQ:
if (!dinit)
{
newf=do_filt_design(hDlg,acttype);
if (newf) tempf=newf;
InvalidateRect(hDlg,NULL,TRUE);
}
break;
case IDC_FILTERSTORE:
if (newf)
{
GetDlgItemText(hDlg, IDC_FILTERNEWNAME,newname,sizeof(newname));
st->filtertype=acttype;
st->par0=GetDlgItemInt(hDlg,IDC_FILTERPAR0, NULL, 0);
GetDlgItemText(hDlg,IDC_FILTERPAR1,sztemp,sizeof(sztemp));
sscanf(sztemp,"%f",&st->par1);
GetDlgItemText(hDlg,IDC_FILTERPAR2,sztemp,sizeof(sztemp));
st->dispfrom=GetDlgItemInt(hDlg, IDC_FROMFREQ, NULL, 0);
st->dispto=GetDlgItemInt(hDlg, IDC_TOFREQ, NULL, 0);
sscanf(sztemp,"%f",&st->par2);
strcpy(st->name,newname);
st->filt=do_filt_design(hDlg, acttype);
st->run= fid_run_new(st->filt, &(st->funcp));
if (st->fbuf!=NULL)
{
fid_run_freebuf(st->fbuf);
st->fbuf=fid_run_newbuf(st->run);
}
}
break;
}
return(TRUE);
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
HPEN tpen;
HBRUSH tbrush;
int height;
int f1,f2;
float fstep,val,x;
hdc = BeginPaint (hDlg, &ps);
GetClientRect(hDlg, &rect);
tpen = CreatePen (PS_SOLID,1,0);
SelectObject (hdc, tpen);
tbrush = CreateSolidBrush(RGB(240,240,240));
SelectObject(hdc,tbrush);
rect.top+=80;
rect.bottom -= 18;
height= rect.bottom-rect.top;
Rectangle(hdc,rect.left,rect.top-1,rect.right,rect.bottom+20);
Rectangle(hdc,rect.left,rect.top-1,rect.right,rect.bottom);
Rectangle(hdc,rect.left,rect.bottom-(int)(height/1.3),rect.right,rect.bottom);
DeleteObject(tbrush);
DeleteObject(tpen);
tpen = CreatePen (PS_SOLID,1,RGB(0,100,0));
SelectObject (hdc, tpen);
f1=GetDlgItemInt(hDlg, IDC_FROMFREQ, NULL, 0);
f2=GetDlgItemInt(hDlg, IDC_TOFREQ, NULL, 0);
fstep=(float)(f2-f1)/(rect.right-rect.left);
MoveToEx(hdc,rect.left+1,rect.bottom-(int)(height*fid_response(tempf, (float)f1/256.0)/1.3),NULL);
for (t=rect.left; t<rect.right; t++)
{
MoveToEx(hdc,1+t,rect.bottom,NULL);
LineTo(hdc,1+t,rect.bottom-(int)(height*fid_response(tempf, (((float)f1+fstep*(t-rect.left))/PACKETSPERSECOND))/1.3));
}
SelectObject(hdc, DRAW.scaleFont);
wsprintf(sztemp,"1.0");
ExtTextOut( hdc, rect.left+2,rect.top+(int)(height*0.2308), 0, &rect,sztemp, strlen(sztemp), NULL ) ;
val=(f2-f1)/10.0f;
fstep=((rect.right-25)-rect.left)/10.0f;
for (t=0; t<=10; t++)
{
x=f1+val*t;
wsprintf(sztemp,"%d.%d",(int)x,(int)(x*10)%10);
ExtTextOut( hdc, rect.left+2+(int)(fstep*t),rect.bottom+2, 0, &rect,sztemp, strlen(sztemp), NULL ) ;
}
DeleteObject(tpen);
EndPaint(hDlg, &ps );
}
break;
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
return(TRUE);
}
return FALSE;
}
//
// Object Implementation
//
FILTEROBJ::FILTEROBJ(int num) : BASE_CL()
{
outports = 1;
inports = 1;
strcpy(in_ports[0].in_name,"in");
strcpy(out_ports[0].out_name,"out");
filtertype=3;
par0=8;
par1=8;
par2=12;
dispfrom=4;
dispto=15;
filt= fid_design("BpBe8", PACKETSPERSECOND, 8, 12, 0, 0);
run= fid_run_new(filt, &(funcp));
fbuf=fid_run_newbuf(run);
strcpy(name,"Alpha Bessel");
}
void FILTEROBJ::session_start(void)
{
if (fbuf!=NULL) fid_run_freebuf(fbuf);
fbuf=fid_run_newbuf(run);
}
void FILTEROBJ::session_reset(void)
{
if (fbuf!=NULL) fid_run_freebuf(fbuf);
fbuf=fid_run_newbuf(run);
}
void FILTEROBJ::session_pos(long pos)
{
if (fbuf!=NULL) fid_run_freebuf(fbuf);
fbuf=fid_run_newbuf(run);
}
void FILTEROBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_FILTERBOX, ghWndStatusbox, (DLGPROC)FilterboxDlgHandler));
}
void FILTEROBJ::load(HANDLE hFile)
{
char sztemp[30];
char szorder[5];
load_object_basics(this);
load_property("name",P_STRING,name);
load_property("type",P_INT,&filtertype);
load_property("order",P_INT,&par0);
load_property("display-from",P_INT,&dispfrom);
load_property("display-to",P_INT,&dispto);
load_property("par1",P_FLOAT,&par1);
load_property("par2",P_FLOAT,&par2);
if (fbuf!=NULL) fid_run_freebuf(fbuf);
if (filt!=NULL) fid_run_free(filt);
strcpy(sztemp,FILTERTYPE[filtertype].init);
wsprintf(szorder,"%d",par0);
strcat(sztemp,szorder);
filt=fid_design(sztemp, PACKETSPERSECOND, par1, par2, 0, 0);
run= fid_run_new(filt, &(funcp));
fbuf=fid_run_newbuf(run);
}
void FILTEROBJ::save(HANDLE hFile)
{
save_object_basics(hFile, this);
save_property(hFile,"name",P_STRING,name);
save_property(hFile,"type",P_INT,&filtertype);
save_property(hFile,"display-from",P_INT,&dispfrom);
save_property(hFile,"display-to",P_INT,&dispto);
save_property(hFile,"order",P_INT,&par0);
save_property(hFile,"par1",P_FLOAT,&par1);
save_property(hFile,"par2",P_FLOAT,&par2);
}
void FILTEROBJ::incoming_data(int port, float value) { input=value; }
void FILTEROBJ::work(void)
{ float x;
x=(float)(funcp(fbuf,(double)input));
// if ((filtertype==2)||(filtertype==3)||(filtertype==6)||(filtertype==7)) x+=512.0f;
pass_values(0,x);
}
FILTEROBJ::~FILTEROBJ()
{
if (fbuf!=NULL) fid_run_freebuf(fbuf);
if (filt!=NULL) fid_run_free(filt);
}