-
Notifications
You must be signed in to change notification settings - Fork 0
/
design.go
135 lines (110 loc) · 3.04 KB
/
design.go
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
/* gofidlib provides go bindings to Jim Peters' fidlib library.
Copyright (C) 2015 Kevin Schiesser
fidlib was originally designed as a backend for the 'Fiview'
application, and to provide performance filtering services to
EEG applications, such as those in the OpenEEG project:
http://uazu.net/fiview/
http://openeeg.sf.net/
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, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gofidlib
/*
#cgo CFLAGS: -DT_LINUX -O6 -DRF_CMDLIST
#cgo LDFLAGS: -lm
#include <stdio.h>
#include <stdlib.h>
#include "fidlib.h"
FidRun*
fid_run_new_(FidFilter* filt, FidFunc** funcp)
{
FidRun *run;
run = fid_run_new(filt, funcp);
return run;
}
FidFilter*
fid_cat_(int freeme, FidFilter* filt1, FidFilter* filt2)
{
FidFilter* filt;
filt = fid_cat(freeme, filt1, filt2, 0);
return filt;
}
*/
import "C"
import (
"unsafe"
"errors"
)
type FilterDesign struct {
fidFilter *C.FidFilter
fidRun unsafe.Pointer
funcp *C.FidFunc
}
func NewFilterDesign(spec string, rate float64) (*FilterDesign, error) {
var (
rate_ = C.double(rate)
spec_ = C.CString(spec)
filt_ = new(C.FidFilter)
funcp_ = new(C.FidFunc)
)
defer C.free(unsafe.Pointer(spec_))
err := C.fid_parse(rate_, &spec_, &filt_)
if err != nil {
return nil, errors.New(C.GoString(err))
}
run_ := C.fid_run_new_(filt_, &funcp_)
return &FilterDesign{
fidFilter: filt_,
fidRun: run_,
funcp: funcp_,
}, nil
}
func (fd *FilterDesign) Free() {
C.free(unsafe.Pointer(fd.fidFilter))
C.fid_run_free(fd.fidRun)
}
func FidCat(freeme int, filters []*FilterDesign) (*FilterDesign, error) {
var filt_ *C.FidFilter
cat := func(sumFilt *C.FidFilter, nextFilt *C.FidFilter) (*C.FidFilter, error) {
filt_, err := C.fid_cat_(C.int(freeme), sumFilt, nextFilt)
if err != nil {
return nil, err
}
return filt_, nil
}
numFilts := len(filters)
switch {
case numFilts < 2:
return nil, errors.New("Too few filters in argument slice. Must have atleast 2.")
case numFilts >= 2:
var err error
if filt_, err = cat(filters[0].fidFilter, filters[1].fidFilter); err != nil {
return nil, err
}
for i := 2; i < len(filters); i++ {
if filt_, err = cat(filt_, filters[i].fidFilter); err != nil {
return nil, err
}
}
if freeme == 1 {
for _, val := range filters {
C.fid_run_free(val.fidRun)
}
}
}
funcp_ := new(C.FidFunc)
run_ := C.fid_run_new_(filt_, &funcp_)
return &FilterDesign{
fidFilter: filt_,
fidRun: run_,
funcp: funcp_,
}, nil
}