1 | /* |
---|
2 | Copyright (C) 2003 Paul Brossier |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | |
---|
21 | /* Requires lsmp_t to be long or double. float will NOT give reliable |
---|
22 | * results */ |
---|
23 | |
---|
24 | #include "aubio_priv.h" |
---|
25 | #include "fvec.h" |
---|
26 | #include "mathutils.h" |
---|
27 | #include "temporal/filter.h" |
---|
28 | #include "temporal/filter_priv.h" |
---|
29 | |
---|
30 | /* bug: mono only */ |
---|
31 | void aubio_filter_do(aubio_filter_t * f, fvec_t * in) { |
---|
32 | uint_t i,j,l, order = f->order; |
---|
33 | lsmp_t *x = f->x; |
---|
34 | lsmp_t *y = f->y; |
---|
35 | lsmp_t *a = f->a; |
---|
36 | lsmp_t *b = f->b; |
---|
37 | i=0;//for (i=0;i<in->channels;i++) { |
---|
38 | for (j = 0; j < in->length; j++) { |
---|
39 | /* new input */ |
---|
40 | //AUBIO_DBG("befor %f\t", in->data[i][j]); |
---|
41 | x[0] = in->data[i][j]; |
---|
42 | y[0] = b[0] * x[0]; |
---|
43 | for (l=1;l<order; l++) { |
---|
44 | y[0] += b[l] * x[l]; |
---|
45 | y[0] -= a[l] * y[l]; |
---|
46 | } /* + 1e-37; for denormal ? */ |
---|
47 | /* new output */ |
---|
48 | in->data[i][j] = y[0]; |
---|
49 | //AUBIO_DBG("after %f\n", in->data[i][j]); |
---|
50 | /* store states for next sample */ |
---|
51 | for (l=order-1; l>0; l--){ |
---|
52 | x[l] = x[l-1]; |
---|
53 | y[l] = y[l-1]; |
---|
54 | } |
---|
55 | } |
---|
56 | /* store states for next buffer */ |
---|
57 | f->x = x; |
---|
58 | f->y = y; |
---|
59 | //} |
---|
60 | } |
---|
61 | |
---|
62 | void aubio_filter_do_outplace(aubio_filter_t * f, fvec_t * in, fvec_t * out) { |
---|
63 | uint_t i,j,l, order = f->order; |
---|
64 | lsmp_t *x = f->x; |
---|
65 | lsmp_t *y = f->y; |
---|
66 | lsmp_t *a = f->a; |
---|
67 | lsmp_t *b = f->b; |
---|
68 | |
---|
69 | i=0; // works in mono only !!! |
---|
70 | //for (i=0;i<in->channels;i++) { |
---|
71 | for (j = 0; j < in->length; j++) { |
---|
72 | /* new input */ |
---|
73 | x[0] = in->data[i][j]; |
---|
74 | y[0] = b[0] * x[0]; |
---|
75 | for (l=1;l<order; l++) { |
---|
76 | y[0] += b[l] * x[l]; |
---|
77 | y[0] -= a[l] * y[l]; |
---|
78 | } |
---|
79 | // + 1e-37; |
---|
80 | /* new output */ |
---|
81 | out->data[i][j] = y[0]; |
---|
82 | /* store for next sample */ |
---|
83 | for (l=order-1; l>0; l--){ |
---|
84 | x[l] = x[l-1]; |
---|
85 | y[l] = y[l-1]; |
---|
86 | } |
---|
87 | } |
---|
88 | /* store for next run */ |
---|
89 | f->x = x; |
---|
90 | f->y = y; |
---|
91 | //} |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * |
---|
96 | * despite mirroring, end effects destroy both phse and amplitude. the longer |
---|
97 | * the buffer, the less affected they are. |
---|
98 | * |
---|
99 | * replacing with zeros clicks. |
---|
100 | * |
---|
101 | * seems broken for order > 4 (see biquad_do_filtfilt for audible one) |
---|
102 | */ |
---|
103 | void aubio_filter_do_filtfilt(aubio_filter_t * f, fvec_t * in, fvec_t * tmp) { |
---|
104 | uint_t j,i=0; |
---|
105 | uint_t length = in->length; |
---|
106 | //uint_t order = f->order; |
---|
107 | //lsmp_t mir; |
---|
108 | /* mirroring */ |
---|
109 | //mir = 2*in->data[i][0]; |
---|
110 | //for (j=1;j<order;j++) |
---|
111 | //f->x[j] = 0.;//mir - in->data[i][order-j]; |
---|
112 | /* apply filtering */ |
---|
113 | aubio_filter_do(f,in); |
---|
114 | /* invert */ |
---|
115 | for (j = 0; j < length; j++) |
---|
116 | tmp->data[i][length-j-1] = in->data[i][j]; |
---|
117 | /* mirror inverted */ |
---|
118 | //mir = 2*tmp->data[i][0]; |
---|
119 | //for (j=1;j<order;j++) |
---|
120 | //f->x[j] = 0.;//mir - tmp->data[i][order-j]; |
---|
121 | /* apply filtering on inverted */ |
---|
122 | aubio_filter_do(f,tmp); |
---|
123 | /* invert back */ |
---|
124 | for (j = 0; j < length; j++) |
---|
125 | in->data[i][j] = tmp->data[i][length-j-1]; |
---|
126 | } |
---|
127 | |
---|
128 | aubio_filter_t * new_aubio_filter(uint_t samplerate UNUSED, uint_t order) { |
---|
129 | aubio_filter_t * f = AUBIO_NEW(aubio_filter_t); |
---|
130 | lsmp_t * x = f->x; |
---|
131 | lsmp_t * y = f->y; |
---|
132 | lsmp_t * a = f->a; |
---|
133 | lsmp_t * b = f->b; |
---|
134 | uint_t l; |
---|
135 | f->order = order; |
---|
136 | a = AUBIO_ARRAY(lsmp_t,f->order); |
---|
137 | b = AUBIO_ARRAY(lsmp_t,f->order); |
---|
138 | x = AUBIO_ARRAY(lsmp_t,f->order); |
---|
139 | y = AUBIO_ARRAY(lsmp_t,f->order); |
---|
140 | /* initial states to zeros */ |
---|
141 | for (l=0; l<f->order; l++){ |
---|
142 | x[l] = 0.; |
---|
143 | y[l] = 0.; |
---|
144 | } |
---|
145 | f->x = x; |
---|
146 | f->y = y; |
---|
147 | f->a = a; |
---|
148 | f->b = b; |
---|
149 | return f; |
---|
150 | } |
---|
151 | |
---|
152 | void del_aubio_filter(aubio_filter_t * f) { |
---|
153 | AUBIO_FREE(f->a); |
---|
154 | AUBIO_FREE(f->b); |
---|
155 | AUBIO_FREE(f->x); |
---|
156 | AUBIO_FREE(f->y); |
---|
157 | AUBIO_FREE(f); |
---|
158 | return; |
---|
159 | } |
---|