1 | /* |
---|
2 | Copyright (C) 2007 Amaury Hazan <ahazan@iua.upf.edu> |
---|
3 | and Paul Brossier <piem@piem.org> |
---|
4 | |
---|
5 | This program is free software; you can redistribute it and/or modify |
---|
6 | it under the terms of the GNU General Public License as published by |
---|
7 | the Free Software Foundation; either version 2 of the License, or |
---|
8 | (at your option) any later version. |
---|
9 | |
---|
10 | This program is distributed in the hope that it will be useful, |
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | GNU General Public License for more details. |
---|
14 | |
---|
15 | You should have received a copy of the GNU General Public License |
---|
16 | along with this program; if not, write to the Free Software |
---|
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | /* part of this mfcc implementation were inspired from LibXtract |
---|
22 | http://libxtract.sourceforge.net/ |
---|
23 | */ |
---|
24 | |
---|
25 | #include "aubio_priv.h" |
---|
26 | #include "filterbank.h" |
---|
27 | |
---|
28 | /** \brief A structure to store a set of n_filters filters of lenghts win_s */ |
---|
29 | struct aubio_filterbank_t_ { |
---|
30 | uint_t win_s; |
---|
31 | uint_t n_filters; |
---|
32 | fvec_t *filters; |
---|
33 | }; |
---|
34 | |
---|
35 | aubio_filterbank_t * new_aubio_filterbank(uint_t n_filters, uint_t win_s){ |
---|
36 | /** allocating space for filterbank object */ |
---|
37 | aubio_filterbank_t * fb = AUBIO_NEW(aubio_filterbank_t); |
---|
38 | uint_t filter_cnt; |
---|
39 | fb->win_s=win_s; |
---|
40 | fb->n_filters=n_filters; |
---|
41 | |
---|
42 | /** allocating filter tables */ |
---|
43 | fb->filters=AUBIO_ARRAY(n_filters,fvec_t); |
---|
44 | for (filter_cnt=0; filter_cnt<n_filters; filter_cnt++) |
---|
45 | /* considering one-channel filters */ |
---|
46 | filters[filter_cnt]=new_fvec(win_s, 1); |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | void del_aubio_filterbank(aubio_filterbank_t * fb){ |
---|
51 | |
---|
52 | int filter_cnt; |
---|
53 | /** deleting filter tables first */ |
---|
54 | for (filter_cnt=0; filter_cnt<fb->n_filters; filter_cnt++) |
---|
55 | del_fvec(fb->filters[filter_cnt]); |
---|
56 | AUBIO_FREE(fb->filters); |
---|
57 | AUBIO_FREE(fb); |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | // Initialization |
---|
62 | |
---|
63 | void aubio_filterbank_mfcc_init(aubio_filterbank_t * fb, smpl_t nyquist, int style, smpl_t freq_min, smpl_t freq_max){ |
---|
64 | |
---|
65 | int n, i, k, *fft_peak, M, next_peak; |
---|
66 | smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val, |
---|
67 | freq_bw_mel, *mel_peak, *height_norm, *lin_peak; |
---|
68 | |
---|
69 | mel_peak = height_norm = lin_peak = NULL; |
---|
70 | fft_peak = NULL; |
---|
71 | norm = 1; |
---|
72 | |
---|
73 | mel_freq_max = 1127 * log(1 + freq_max / 700); |
---|
74 | mel_freq_min = 1127 * log(1 + freq_min / 700); |
---|
75 | freq_bw_mel = (mel_freq_max - mel_freq_min) / fb->n_filters; |
---|
76 | |
---|
77 | mel_peak = (smpl_t *)malloc((fb->n_filters + 2) * sizeof(smpl_t)); |
---|
78 | /* +2 for zeros at start and end */ |
---|
79 | lin_peak = (smpl_t *)malloc((fb->n_filters + 2) * sizeof(smpl_t)); |
---|
80 | fft_peak = (int *)malloc((fb->n_filters + 2) * sizeof(int)); |
---|
81 | height_norm = (smpl_t *)malloc(fb->n_filters * sizeof(smpl_t)); |
---|
82 | |
---|
83 | if(mel_peak == NULL || height_norm == NULL || |
---|
84 | lin_peak == NULL || fft_peak == NULL) |
---|
85 | return XTRACT_MALLOC_FAILED; |
---|
86 | |
---|
87 | M = fb->win_s >> 1; |
---|
88 | |
---|
89 | mel_peak[0] = mel_freq_min; |
---|
90 | lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1); |
---|
91 | fft_peak[0] = lin_peak[0] / nyquist * M; |
---|
92 | |
---|
93 | |
---|
94 | for (n = 1; n <= fb->n_filters; n++){ |
---|
95 | /*roll out peak locations - mel, linear and linear on fft window scale */ |
---|
96 | mel_peak[n] = mel_peak[n - 1] + freq_bw_mel; |
---|
97 | lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1); |
---|
98 | fft_peak[n] = lin_peak[n] / nyquist * M; |
---|
99 | } |
---|
100 | |
---|
101 | for (n = 0; n < fb->n_filters; n++){ |
---|
102 | /*roll out normalised gain of each peak*/ |
---|
103 | if (style == XTRACT_EQUAL_GAIN){ |
---|
104 | height = 1; |
---|
105 | norm_fact = norm; |
---|
106 | } |
---|
107 | else{ |
---|
108 | height = 2 / (lin_peak[n + 2] - lin_peak[n]); |
---|
109 | norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0])); |
---|
110 | } |
---|
111 | height_norm[n] = height * norm_fact; |
---|
112 | } |
---|
113 | |
---|
114 | i = 0; |
---|
115 | |
---|
116 | for(n = 0; n < fb->n_filters; n++){ |
---|
117 | |
---|
118 | /*calculate the rise increment*/ |
---|
119 | if(n > 0) |
---|
120 | inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]); |
---|
121 | else |
---|
122 | inc = height_norm[n] / fft_peak[n]; |
---|
123 | val = 0; |
---|
124 | |
---|
125 | /*zero the start of the array*/ |
---|
126 | for(k = 0; k < i; k++) |
---|
127 | //fft_tables[n][k] = 0.f; |
---|
128 | fb->filters[n]->data[0][k]=0.f; |
---|
129 | |
---|
130 | /*fill in the rise */ |
---|
131 | for(; i <= fft_peak[n]; i++){ |
---|
132 | // fft_tables[n][i] = val; |
---|
133 | fb->filters[n]->data[0][k]=val; |
---|
134 | val += inc; |
---|
135 | } |
---|
136 | |
---|
137 | /*calculate the fall increment */ |
---|
138 | inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]); |
---|
139 | |
---|
140 | val = 0; |
---|
141 | next_peak = fft_peak[n + 1]; |
---|
142 | |
---|
143 | /*reverse fill the 'fall' */ |
---|
144 | for(i = next_peak; i > fft_peak[n]; i--){ |
---|
145 | //fft_tables[n][i] = val; |
---|
146 | fb->filters[n]->data[0][k]=val; |
---|
147 | val += inc; |
---|
148 | } |
---|
149 | |
---|
150 | /*zero the rest of the array*/ |
---|
151 | for(k = next_peak + 1; k < fb->win_s; k++) |
---|
152 | //fft_tables[n][k] = 0.f; |
---|
153 | fb->filters[n]->data[0][k]=0.f; |
---|
154 | } |
---|
155 | |
---|
156 | free(mel_peak); |
---|
157 | free(lin_peak); |
---|
158 | free(height_norm); |
---|
159 | free(fft_peak); |
---|
160 | |
---|
161 | //return XTRACT_SUCCESS; |
---|
162 | |
---|
163 | } |
---|
164 | |
---|