[96fb8ad] | 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 "sample.h" |
---|
| 26 | #include "mathutils.h" |
---|
| 27 | #include "filter.h" |
---|
| 28 | |
---|
| 29 | struct _aubio_filter_t { |
---|
| 30 | uint_t order; |
---|
| 31 | lsmp_t * a; |
---|
| 32 | lsmp_t * b; |
---|
| 33 | lsmp_t * y; |
---|
| 34 | lsmp_t * x; |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | /* bug: mono only */ |
---|
| 38 | void aubio_filter_do(aubio_filter_t * f, fvec_t * in) { |
---|
| 39 | uint_t i,j,l, order = f->order; |
---|
| 40 | lsmp_t *x = f->x; |
---|
| 41 | lsmp_t *y = f->y; |
---|
| 42 | lsmp_t *a = f->a; |
---|
| 43 | lsmp_t *b = f->b; |
---|
| 44 | i=0;//for (i=0;i<in->channels;i++) { |
---|
| 45 | for (j = 0; j < in->length; j++) { |
---|
| 46 | /* new input */ |
---|
| 47 | //AUBIO_DBG("befor %f\t", in->data[i][j]); |
---|
| 48 | x[0] = in->data[i][j]; |
---|
| 49 | y[0] = b[0] * x[0]; |
---|
| 50 | for (l=1;l<order; l++) { |
---|
| 51 | y[0] += b[l] * x[l]; |
---|
| 52 | y[0] -= a[l] * y[l]; |
---|
| 53 | } /* + 1e-37; for denormal ? */ |
---|
| 54 | /* new output */ |
---|
| 55 | in->data[i][j] = y[0]; |
---|
| 56 | //AUBIO_DBG("after %f\n", in->data[i][j]); |
---|
| 57 | /* store states for next sample */ |
---|
| 58 | for (l=order-1; l>0; l--){ |
---|
| 59 | x[l] = x[l-1]; |
---|
| 60 | y[l] = y[l-1]; |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | /* store states for next buffer */ |
---|
| 64 | f->x = x; |
---|
| 65 | f->y = y; |
---|
| 66 | //} |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void aubio_filter_do_outplace(aubio_filter_t * f, fvec_t * in, fvec_t * out) { |
---|
| 70 | uint_t i,j,l, order = f->order; |
---|
| 71 | lsmp_t *x = f->x; |
---|
| 72 | lsmp_t *y = f->y; |
---|
| 73 | lsmp_t *a = f->a; |
---|
| 74 | lsmp_t *b = f->b; |
---|
| 75 | |
---|
| 76 | i=0; // works in mono only !!! |
---|
| 77 | //for (i=0;i<in->channels;i++) { |
---|
| 78 | for (j = 0; j < in->length; j++) { |
---|
| 79 | /* new input */ |
---|
| 80 | x[0] = in->data[i][j]; |
---|
| 81 | y[0] = b[0] * x[0]; |
---|
| 82 | for (l=1;l<order; l++) { |
---|
| 83 | y[0] += b[l] * x[l]; |
---|
| 84 | y[0] -= a[l] * y[l]; |
---|
| 85 | } |
---|
| 86 | // + 1e-37; |
---|
| 87 | /* new output */ |
---|
| 88 | out->data[i][j] = y[0]; |
---|
| 89 | /* store for next sample */ |
---|
| 90 | for (l=order-1; l>0; l--){ |
---|
| 91 | x[l] = x[l-1]; |
---|
| 92 | y[l] = y[l-1]; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | /* store for next run */ |
---|
| 96 | f->x = x; |
---|
| 97 | f->y = y; |
---|
| 98 | //} |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | /* |
---|
| 102 | * |
---|
| 103 | * despite mirroring, end effects destroy both phse and amplitude. the longer |
---|
| 104 | * the buffer, the less affected they are. |
---|
| 105 | * |
---|
| 106 | * replacing with zeros clicks. |
---|
| 107 | * |
---|
| 108 | * seems broken for order > 4 (see biquad_do_filtfilt for audible one) |
---|
| 109 | */ |
---|
| 110 | void aubio_filter_do_filtfilt(aubio_filter_t * f, fvec_t * in, fvec_t * tmp) { |
---|
| 111 | uint_t j,i=0; |
---|
| 112 | uint_t length = in->length; |
---|
| 113 | //uint_t order = f->order; |
---|
| 114 | //lsmp_t mir; |
---|
| 115 | /* mirroring */ |
---|
| 116 | //mir = 2*in->data[i][0]; |
---|
| 117 | //for (j=1;j<order;j++) |
---|
| 118 | //f->x[j] = 0.;//mir - in->data[i][order-j]; |
---|
| 119 | /* apply filtering */ |
---|
| 120 | aubio_filter_do(f,in); |
---|
| 121 | /* invert */ |
---|
| 122 | for (j = 0; j < length; j++) |
---|
| 123 | tmp->data[i][length-j-1] = in->data[i][j]; |
---|
| 124 | /* mirror inverted */ |
---|
| 125 | //mir = 2*tmp->data[i][0]; |
---|
| 126 | //for (j=1;j<order;j++) |
---|
| 127 | //f->x[j] = 0.;//mir - tmp->data[i][order-j]; |
---|
| 128 | /* apply filtering on inverted */ |
---|
| 129 | aubio_filter_do(f,tmp); |
---|
| 130 | /* invert back */ |
---|
| 131 | for (j = 0; j < length; j++) |
---|
| 132 | in->data[i][j] = tmp->data[i][length-j-1]; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | aubio_filter_t * new_aubio_adsgn_filter(uint_t samplerate) { |
---|
| 137 | aubio_filter_t * f = new_aubio_filter(samplerate, 7); |
---|
| 138 | lsmp_t * a = f->a; |
---|
| 139 | lsmp_t * b = f->b; |
---|
| 140 | /* uint_t l; */ |
---|
| 141 | /* for now, 44100, adsgn */ |
---|
| 142 | a[0] = 1.00000000000000000000000000000000000000000000000000000; |
---|
| 143 | a[1] = -4.01957618111583236952810693765059113502502441406250000; |
---|
| 144 | a[2] = 6.18940644292069386267485242569819092750549316406250000; |
---|
| 145 | a[3] = -4.45319890354411640487342083360999822616577148437500000; |
---|
| 146 | a[4] = 1.42084294962187751565352300531230866909027099609375000; |
---|
| 147 | a[5] = -0.14182547383030480458998567883099894970655441284179688; |
---|
| 148 | a[6] = 0.00435117723349511334451911181986361043527722358703613; |
---|
| 149 | b[0] = 0.25574112520425740235907596797915175557136535644531250; |
---|
| 150 | b[1] = -0.51148225040851391653973223583307117223739624023437500; |
---|
| 151 | b[2] = -0.25574112520426162120656954357400536537170410156250000; |
---|
| 152 | b[3] = 1.02296450081703405032840237254276871681213378906250000; |
---|
| 153 | b[4] = -0.25574112520426051098354491841746494174003601074218750; |
---|
| 154 | b[5] = -0.51148225040851369449512731080176308751106262207031250; |
---|
| 155 | b[6] = 0.25574112520425729133677350546349771320819854736328125; |
---|
| 156 | /* DBG: filter coeffs at creation time */ |
---|
| 157 | /* |
---|
| 158 | for (l=0; l<f->order; l++){ |
---|
| 159 | AUBIO_DBG("a[%d]=\t%1.16f\tb[%d]=\t%1.16f\n",l,a[l],l,b[l]); |
---|
| 160 | } |
---|
| 161 | */ |
---|
| 162 | f->a = a; |
---|
| 163 | f->b = b; |
---|
| 164 | return f; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | aubio_filter_t * new_aubio_cdsgn_filter(uint_t samplerate) { |
---|
| 168 | aubio_filter_t * f = new_aubio_filter(samplerate, 5); |
---|
| 169 | lsmp_t * a = f->a; |
---|
| 170 | lsmp_t * b = f->b; |
---|
| 171 | /* uint_t l; */ |
---|
| 172 | /* for now, 44100, cdsgn */ |
---|
| 173 | a[0] = 1.000000000000000000000000000000000000000000000000000000000000; |
---|
| 174 | a[1] = -2.134674963687040794013682898366823792457580566406250000000000; |
---|
| 175 | a[2] = 1.279333533236063358273781886964570730924606323242187500000000; |
---|
| 176 | a[3] = -0.149559846089396208945743182994192466139793395996093750000000; |
---|
| 177 | a[4] = 0.004908700174624848651394604104325480875559151172637939453125; |
---|
| 178 | b[0] = 0.217008561949218803377448239189106971025466918945312500000000; |
---|
| 179 | b[1] = -0.000000000000000222044604925031308084726333618164062500000000; |
---|
| 180 | b[2] = -0.434017123898438272888711253472138196229934692382812500000000; |
---|
| 181 | b[3] = 0.000000000000000402455846426619245903566479682922363281250000; |
---|
| 182 | b[4] = 0.217008561949218969910901932962588034570217132568359375000000; |
---|
| 183 | /* DBG: filter coeffs at creation time */ |
---|
| 184 | /* |
---|
| 185 | for (l=0; l<f->order; l++){ |
---|
| 186 | AUBIO_DBG("a[%d]=\t%1.16f\tb[%d]=\t%1.16f\n",l,a[l],l,b[l]); |
---|
| 187 | } |
---|
| 188 | */ |
---|
| 189 | f->a = a; |
---|
| 190 | f->b = b; |
---|
| 191 | return f; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | aubio_filter_t * new_aubio_filter(uint_t samplerate, uint_t order) { |
---|
| 195 | aubio_filter_t * f = AUBIO_NEW(aubio_filter_t); |
---|
| 196 | lsmp_t * x = f->x; |
---|
| 197 | lsmp_t * y = f->y; |
---|
| 198 | lsmp_t * a = f->a; |
---|
| 199 | lsmp_t * b = f->b; |
---|
| 200 | uint_t l; |
---|
| 201 | f->order = order; |
---|
| 202 | a = AUBIO_ARRAY(lsmp_t,f->order); |
---|
| 203 | b = AUBIO_ARRAY(lsmp_t,f->order); |
---|
| 204 | x = AUBIO_ARRAY(lsmp_t,f->order); |
---|
| 205 | y = AUBIO_ARRAY(lsmp_t,f->order); |
---|
| 206 | /* initial states to zeros */ |
---|
| 207 | for (l=0; l<f->order; l++){ |
---|
| 208 | x[l] = 0.; |
---|
| 209 | y[l] = 0.; |
---|
| 210 | } |
---|
| 211 | f->x = x; |
---|
| 212 | f->y = y; |
---|
| 213 | f->a = a; |
---|
| 214 | f->b = b; |
---|
| 215 | return f; |
---|
| 216 | } |
---|
| 217 | |
---|
[bb20f87] | 218 | void del_aubio_filter(aubio_filter_t * f) { |
---|
| 219 | AUBIO_FREE(f->a); |
---|
| 220 | AUBIO_FREE(f->b); |
---|
| 221 | AUBIO_FREE(f->x); |
---|
| 222 | AUBIO_FREE(f->y); |
---|
| 223 | AUBIO_FREE(f); |
---|
| 224 | return; |
---|
| 225 | } |
---|