1 | /* |
---|
2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | /* Requires lsmp_t to be long or double. float will NOT give reliable |
---|
23 | * results */ |
---|
24 | |
---|
25 | #include "aubio_priv.h" |
---|
26 | #include "fvec.h" |
---|
27 | #include "lvec.h" |
---|
28 | #include "mathutils.h" |
---|
29 | #include "temporal/filter.h" |
---|
30 | |
---|
31 | struct _aubio_filter_t |
---|
32 | { |
---|
33 | uint_t order; |
---|
34 | uint_t samplerate; |
---|
35 | lvec_t *a; |
---|
36 | lvec_t *b; |
---|
37 | lvec_t *y; |
---|
38 | lvec_t *x; |
---|
39 | }; |
---|
40 | |
---|
41 | void |
---|
42 | aubio_filter_do_outplace (aubio_filter_t * f, fvec_t * in, fvec_t * out) |
---|
43 | { |
---|
44 | fvec_copy (in, out); |
---|
45 | aubio_filter_do (f, out); |
---|
46 | } |
---|
47 | |
---|
48 | void |
---|
49 | aubio_filter_do (aubio_filter_t * f, fvec_t * in) |
---|
50 | { |
---|
51 | uint_t i, j, l, order = f->order; |
---|
52 | lsmp_t *x; |
---|
53 | lsmp_t *y; |
---|
54 | lsmp_t *a = f->a->data[0]; |
---|
55 | lsmp_t *b = f->b->data[0]; |
---|
56 | |
---|
57 | for (i = 0; i < in->channels; i++) { |
---|
58 | x = f->x->data[i]; |
---|
59 | y = f->y->data[i]; |
---|
60 | for (j = 0; j < in->length; j++) { |
---|
61 | /* new input */ |
---|
62 | x[0] = KILL_DENORMAL (in->data[i][j]); |
---|
63 | y[0] = b[0] * x[0]; |
---|
64 | for (l = 1; l < order; l++) { |
---|
65 | y[0] += b[l] * x[l]; |
---|
66 | y[0] -= a[l] * y[l]; |
---|
67 | } |
---|
68 | /* new output */ |
---|
69 | in->data[i][j] = y[0]; |
---|
70 | /* store for next sample */ |
---|
71 | for (l = order - 1; l > 0; l--) { |
---|
72 | x[l] = x[l - 1]; |
---|
73 | y[l] = y[l - 1]; |
---|
74 | } |
---|
75 | } |
---|
76 | /* store for next run */ |
---|
77 | f->x->data[i] = x; |
---|
78 | f->y->data[i] = y; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /* The rough way: reset memory of filter between each run to avoid end effects. */ |
---|
83 | void |
---|
84 | aubio_filter_do_filtfilt (aubio_filter_t * f, fvec_t * in, fvec_t * tmp) |
---|
85 | { |
---|
86 | uint_t j, i = 0; |
---|
87 | uint_t length = in->length; |
---|
88 | /* apply filtering */ |
---|
89 | aubio_filter_do (f, in); |
---|
90 | aubio_filter_do_reset (f); |
---|
91 | /* mirror */ |
---|
92 | for (j = 0; j < length; j++) |
---|
93 | tmp->data[i][length - j - 1] = in->data[i][j]; |
---|
94 | /* apply filtering on mirrored */ |
---|
95 | aubio_filter_do (f, tmp); |
---|
96 | aubio_filter_do_reset (f); |
---|
97 | /* invert back */ |
---|
98 | for (j = 0; j < length; j++) |
---|
99 | in->data[i][j] = tmp->data[i][length - j - 1]; |
---|
100 | } |
---|
101 | |
---|
102 | lvec_t * |
---|
103 | aubio_filter_get_feedback (aubio_filter_t * f) |
---|
104 | { |
---|
105 | return f->a; |
---|
106 | } |
---|
107 | |
---|
108 | lvec_t * |
---|
109 | aubio_filter_get_feedforward (aubio_filter_t * f) |
---|
110 | { |
---|
111 | return f->b; |
---|
112 | } |
---|
113 | |
---|
114 | uint_t |
---|
115 | aubio_filter_get_order (aubio_filter_t * f) |
---|
116 | { |
---|
117 | return f->order; |
---|
118 | } |
---|
119 | |
---|
120 | uint_t |
---|
121 | aubio_filter_get_samplerate (aubio_filter_t * f) |
---|
122 | { |
---|
123 | return f->samplerate; |
---|
124 | } |
---|
125 | |
---|
126 | uint_t |
---|
127 | aubio_filter_set_samplerate (aubio_filter_t * f, uint_t samplerate) |
---|
128 | { |
---|
129 | f->samplerate = samplerate; |
---|
130 | return AUBIO_OK; |
---|
131 | } |
---|
132 | |
---|
133 | void |
---|
134 | aubio_filter_do_reset (aubio_filter_t * f) |
---|
135 | { |
---|
136 | lvec_zeros (f->x); |
---|
137 | lvec_zeros (f->y); |
---|
138 | } |
---|
139 | |
---|
140 | aubio_filter_t * |
---|
141 | new_aubio_filter (uint_t order, uint_t channels) |
---|
142 | { |
---|
143 | aubio_filter_t *f = AUBIO_NEW (aubio_filter_t); |
---|
144 | f->x = new_lvec (order, channels); |
---|
145 | f->y = new_lvec (order, channels); |
---|
146 | f->a = new_lvec (order, 1); |
---|
147 | f->b = new_lvec (order, 1); |
---|
148 | /* by default, samplerate is not set */ |
---|
149 | f->samplerate = 0; |
---|
150 | f->order = order; |
---|
151 | /* set default to identity */ |
---|
152 | f->a->data[0][1] = 1.; |
---|
153 | return f; |
---|
154 | } |
---|
155 | |
---|
156 | void |
---|
157 | del_aubio_filter (aubio_filter_t * f) |
---|
158 | { |
---|
159 | del_lvec (f->a); |
---|
160 | del_lvec (f->b); |
---|
161 | del_lvec (f->x); |
---|
162 | del_lvec (f->y); |
---|
163 | AUBIO_FREE (f); |
---|
164 | return; |
---|
165 | } |
---|