source: src/filterbank.c @ 71d3bf0

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 71d3bf0 was 71d3bf0, checked in by Amaury Hazan <mahmoudax@gmail.com>, 17 years ago

small adds

  • Property mode set to 100644
File size: 3.6 KB
Line 
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 "filterbank.h"
24
25// Initialization
26
27int 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){
28
29    int n, i, k, *fft_peak, M, next_peak; 
30    smpl_t norm, mel_freq_max, mel_freq_min, norm_fact, height, inc, val, 
31        freq_bw_mel, *mel_peak, *height_norm, *lin_peak;
32
33    mel_peak = height_norm = lin_peak = NULL;
34    fft_peak = NULL;
35    norm = 1; 
36
37    mel_freq_max = 1127 * log(1 + freq_max / 700);
38    mel_freq_min = 1127 * log(1 + freq_min / 700);
39    freq_bw_mel = (mel_freq_max - mel_freq_min) / freq_bands;
40
41    mel_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t)); 
42    /* +2 for zeros at start and end */
43    lin_peak = (smpl_t *)malloc((freq_bands + 2) * sizeof(smpl_t));
44    fft_peak = (int *)malloc((freq_bands + 2) * sizeof(int));
45    height_norm = (smpl_t *)malloc(freq_bands * sizeof(smpl_t));
46
47    if(mel_peak == NULL || height_norm == NULL || 
48                    lin_peak == NULL || fft_peak == NULL)
49                    return XTRACT_MALLOC_FAILED;
50   
51    M = N >> 1;
52
53    mel_peak[0] = mel_freq_min;
54    lin_peak[0] = 700 * (exp(mel_peak[0] / 1127) - 1);
55    fft_peak[0] = lin_peak[0] / nyquist * M;
56
57
58    for (n = 1; n <= freq_bands; n++){ 
59    /*roll out peak locations - mel, linear and linear on fft window scale */
60        mel_peak[n] = mel_peak[n - 1] + freq_bw_mel;
61        lin_peak[n] = 700 * (exp(mel_peak[n] / 1127) -1);
62        fft_peak[n] = lin_peak[n] / nyquist * M;
63    }
64
65    for (n = 0; n < freq_bands; n++){
66        /*roll out normalised gain of each peak*/
67        if (style == XTRACT_EQUAL_GAIN){
68            height = 1; 
69            norm_fact = norm;
70        }
71        else{
72            height = 2 / (lin_peak[n + 2] - lin_peak[n]);
73            norm_fact = norm / (2 / (lin_peak[2] - lin_peak[0]));
74        }
75        height_norm[n] = height * norm_fact;
76    }
77
78    i = 0;
79   
80    for(n = 0; n < freq_bands; n++){
81 
82  /*calculate the rise increment*/
83        if(n > 0)
84            inc = height_norm[n] / (fft_peak[n] - fft_peak[n - 1]);
85        else
86            inc = height_norm[n] / fft_peak[n];
87        val = 0; 
88 
89  /*zero the start of the array*/
90  for(k = 0; k < i; k++)
91     fft_tables[n][k] = 0.f;
92 
93  /*fill in the rise */
94        for(; i <= fft_peak[n]; i++){ 
95            fft_tables[n][i] = val;
96            val += inc;
97        }
98 
99        /*calculate the fall increment */
100        inc = height_norm[n] / (fft_peak[n + 1] - fft_peak[n]);
101 
102        val = 0;
103  next_peak = fft_peak[n + 1];
104 
105  /*reverse fill the 'fall' */
106        for(i = next_peak; i > fft_peak[n]; i--){ 
107            fft_tables[n][i] = val;
108            val += inc;
109        }
110
111  /*zero the rest of the array*/
112  for(k = next_peak + 1; k < N; k++)
113      fft_tables[n][k] = 0.f;
114    }
115
116    free(mel_peak);
117    free(lin_peak);
118    free(height_norm);
119    free(fft_peak);
120
121    return XTRACT_SUCCESS;
122
123}
Note: See TracBrowser for help on using the repository browser.