1 | /* |
---|
2 | Copyright (C) 2007 Amaury Hazan |
---|
3 | Ported to aubio from LibXtract |
---|
4 | http://libxtract.sourceforge.net/ |
---|
5 | |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation; either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with this program; if not, write to the Free Software |
---|
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
20 | |
---|
21 | */ |
---|
22 | |
---|
23 | #include "aubio_priv.h" |
---|
24 | #include "filterbank.h" |
---|
25 | |
---|
26 | |
---|
27 | // Struct Declaration |
---|
28 | |
---|
29 | /** \brief A structure to store a set of n_filters Mel filters */ |
---|
30 | typedef struct aubio_mel_filter_ { |
---|
31 | int n_filters; |
---|
32 | smpl_t **filters; |
---|
33 | }; |
---|
34 | |
---|
35 | // Initialization |
---|
36 | |
---|
37 | int aubio_mfcc_init(int N, smpl_t nyquist, int style, smpl_t freq_min, smpl_t freq_max, int freq_bands, smpl_t **fft_tables){ |
---|
38 | |
---|
39 | int n, i, k, *fft_peak, M, next_peak; |
---|
40 | smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val, |
---|
41 | freq_bw_mel, *mel_peak, *height_norm, *lin_peak; |
---|
42 | |
---|
43 | mel_peak = height_norm = lin_peak = NULL; |
---|
44 | fft_peak = NULL; |
---|
45 | norm = 1; |
---|
46 | |
---|
47 | mel_freq_max = 1127 * log(1 + freq_max / 700); |
---|
48 | mel_freq_min = 1127 * log(1 + freq_min / 700); |
---|
49 | freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands; |
---|
50 | |
---|
51 | mel_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); |
---|
52 | /* +2 for zeros at start and end */ |
---|
53 | lin_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); |
---|
54 | fft_peak = (int *)malloc((freq_bands + 2) * sizeof(int)); |
---|
55 | height_norm = (smpl_t *)malloc(freq_bands * sizeof(smpl_t)); |
---|
56 | |
---|
57 | if(mel_peak == NULL || height_norm == NULL || |
---|
58 | lin_peak == NULL || fft_peak == NULL) |
---|
59 | return XTRACT_MALLOC_FAILED; |
---|
60 | |
---|
61 | M = N >> 1; |
---|
62 | |
---|
63 | mel_peak[0] = mel_freq_min; |
---|
64 | lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1); |
---|
65 | fft_peak[0] = lin_peak[0] / nyquist * M; |
---|
66 | |
---|
67 | |
---|
68 | for (n = 1; n <= freq_bands; n++){ |
---|
69 | /*roll out peak locations - mel, linear and linear on fft window scale */ |
---|
70 | mel_peak[n] = mel_peak[n - 1] + freq_bw_mel; |
---|
71 | lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1); |
---|
72 | fft_peak[n] = lin_peak[n] / nyquist * M; |
---|
73 | } |
---|
74 | |
---|
75 | for (n = 0; n < freq_bands; n++){ |
---|
76 | /*roll out normalised gain of each peak*/ |
---|
77 | if (style == XTRACT_EQUAL_GAIN){ |
---|
78 | height = 1; |
---|
79 | norm_fact = norm; |
---|
80 | } |
---|
81 | else{ |
---|
82 | height = 2 / (lin_peak[n + 2] - lin_peak[n]); |
---|
83 | norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0])); |
---|
84 | } |
---|
85 | height_norm[n] = height * norm_fact; |
---|
86 | } |
---|
87 | |
---|
88 | i = 0; |
---|
89 | |
---|
90 | for(n = 0; n < freq_bands; n++){ |
---|
91 | |
---|
92 | /*calculate the rise increment*/ |
---|
93 | if(n > 0) |
---|
94 | inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]); |
---|
95 | else |
---|
96 | inc = height_norm[n] / fft_peak[n]; |
---|
97 | val = 0; |
---|
98 | |
---|
99 | /*zero the start of the array*/ |
---|
100 | for(k = 0; k < i; k++) |
---|
101 | fft_tables[n][k] = 0.f; |
---|
102 | |
---|
103 | /*fill in the rise */ |
---|
104 | for(; i <= fft_peak[n]; i++){ |
---|
105 | fft_tables[n][i] = val; |
---|
106 | val += inc; |
---|
107 | } |
---|
108 | |
---|
109 | /*calculate the fall increment */ |
---|
110 | inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]); |
---|
111 | |
---|
112 | val = 0; |
---|
113 | next_peak = fft_peak[n + 1]; |
---|
114 | |
---|
115 | /*reverse fill the 'fall' */ |
---|
116 | for(i = next_peak; i > fft_peak[n]; i--){ |
---|
117 | fft_tables[n][i] = val; |
---|
118 | val += inc; |
---|
119 | } |
---|
120 | |
---|
121 | /*zero the rest of the array*/ |
---|
122 | for(k = next_peak + 1; k < N; k++) |
---|
123 | fft_tables[n][k] = 0.f; |
---|
124 | } |
---|
125 | |
---|
126 | free(mel_peak); |
---|
127 | free(lin_peak); |
---|
128 | free(height_norm); |
---|
129 | free(fft_peak); |
---|
130 | |
---|
131 | return XTRACT_SUCCESS; |
---|
132 | |
---|
133 | } |
---|