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 "lvec.h" |
---|
27 | #include "mathutils.h" |
---|
28 | #include "temporal/filter.h" |
---|
29 | |
---|
30 | struct _aubio_filter_t { |
---|
31 | uint_t order; |
---|
32 | uint_t samplerate; |
---|
33 | lvec_t * a; |
---|
34 | lvec_t * b; |
---|
35 | lvec_t * y; |
---|
36 | lvec_t * x; |
---|
37 | }; |
---|
38 | |
---|
39 | void aubio_filter_do_outplace(aubio_filter_t * f, fvec_t * in, fvec_t * out) { |
---|
40 | fvec_copy(in, out); |
---|
41 | aubio_filter_do (f, out); |
---|
42 | } |
---|
43 | |
---|
44 | void aubio_filter_do(aubio_filter_t * f, fvec_t * in) { |
---|
45 | uint_t i,j,l, order = f->order; |
---|
46 | lsmp_t *x; |
---|
47 | lsmp_t *y; |
---|
48 | lsmp_t *a = f->a->data[0]; |
---|
49 | lsmp_t *b = f->b->data[0]; |
---|
50 | |
---|
51 | for (i = 0; i < in->channels; i++) { |
---|
52 | x = f->x->data[i]; |
---|
53 | y = f->y->data[i]; |
---|
54 | for (j = 0; j < in->length; j++) { |
---|
55 | /* new input */ |
---|
56 | x[0] = KILL_DENORMAL(in->data[i][j]); |
---|
57 | y[0] = b[0] * x[0]; |
---|
58 | for (l=1;l<order; l++) { |
---|
59 | y[0] += b[l] * x[l]; |
---|
60 | y[0] -= a[l] * y[l]; |
---|
61 | } |
---|
62 | /* new output */ |
---|
63 | in->data[i][j] = y[0]; |
---|
64 | /* store for next sample */ |
---|
65 | for (l=order-1; l>0; l--){ |
---|
66 | x[l] = x[l-1]; |
---|
67 | y[l] = y[l-1]; |
---|
68 | } |
---|
69 | } |
---|
70 | /* store for next run */ |
---|
71 | f->x->data[i] = x; |
---|
72 | f->y->data[i] = y; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /* |
---|
77 | * |
---|
78 | * despite mirroring, end effects destroy both phse and amplitude. the longer |
---|
79 | * the buffer, the less affected they are. |
---|
80 | * |
---|
81 | * replacing with zeros clicks. |
---|
82 | * |
---|
83 | * seems broken for order > 4 (see biquad_do_filtfilt for audible one) |
---|
84 | */ |
---|
85 | void aubio_filter_do_filtfilt(aubio_filter_t * f, fvec_t * in, fvec_t * tmp) { |
---|
86 | uint_t j,i=0; |
---|
87 | uint_t length = in->length; |
---|
88 | //uint_t order = f->order; |
---|
89 | //lsmp_t mir; |
---|
90 | /* mirroring */ |
---|
91 | //mir = 2*in->data[i][0]; |
---|
92 | //for (j=1;j<order;j++) |
---|
93 | //f->x[j] = 0.;//mir - in->data[i][order-j]; |
---|
94 | /* apply filtering */ |
---|
95 | aubio_filter_do(f,in); |
---|
96 | /* invert */ |
---|
97 | for (j = 0; j < length; j++) |
---|
98 | tmp->data[i][length-j-1] = in->data[i][j]; |
---|
99 | /* mirror inverted */ |
---|
100 | //mir = 2*tmp->data[i][0]; |
---|
101 | //for (j=1;j<order;j++) |
---|
102 | //f->x[j] = 0.;//mir - tmp->data[i][order-j]; |
---|
103 | /* apply filtering on inverted */ |
---|
104 | aubio_filter_do(f,tmp); |
---|
105 | /* invert back */ |
---|
106 | for (j = 0; j < length; j++) |
---|
107 | in->data[i][j] = tmp->data[i][length-j-1]; |
---|
108 | } |
---|
109 | |
---|
110 | lvec_t * aubio_filter_get_feedback ( aubio_filter_t *f ) { |
---|
111 | return f->a; |
---|
112 | } |
---|
113 | |
---|
114 | lvec_t * aubio_filter_get_feedforward ( aubio_filter_t *f ) { |
---|
115 | return f->b; |
---|
116 | } |
---|
117 | |
---|
118 | uint_t aubio_filter_get_order ( aubio_filter_t *f ) { |
---|
119 | return f->order; |
---|
120 | } |
---|
121 | |
---|
122 | uint_t aubio_filter_get_samplerate ( aubio_filter_t *f ) { |
---|
123 | return f->samplerate; |
---|
124 | } |
---|
125 | |
---|
126 | aubio_filter_t * new_aubio_filter(uint_t samplerate, |
---|
127 | uint_t order, uint_t channels) { |
---|
128 | aubio_filter_t * f = AUBIO_NEW(aubio_filter_t); |
---|
129 | f->x = new_lvec(order, channels); |
---|
130 | f->y = new_lvec(order, channels); |
---|
131 | f->a = new_lvec(order, 1); |
---|
132 | f->b = new_lvec(order, 1); |
---|
133 | f->samplerate = samplerate; |
---|
134 | f->order = order; |
---|
135 | /* set default to identity */ |
---|
136 | f->a->data[0][1] = 1.; |
---|
137 | return f; |
---|
138 | } |
---|
139 | |
---|
140 | void del_aubio_filter(aubio_filter_t * f) { |
---|
141 | del_lvec(f->a); |
---|
142 | del_lvec(f->b); |
---|
143 | del_lvec(f->x); |
---|
144 | del_lvec(f->y); |
---|
145 | AUBIO_FREE(f); |
---|
146 | return; |
---|
147 | } |
---|