Changeset 633400d for src/spectral/mfcc.c
- Timestamp:
- Dec 5, 2018, 10:34:39 PM (5 years ago)
- Branches:
- feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
- Children:
- 283a619a
- Parents:
- 5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/spectral/mfcc.c
r5b46bc3 r633400d 29 29 #include "spectral/filterbank.h" 30 30 #include "spectral/filterbank_mel.h" 31 #include "spectral/dct.h" 31 32 #include "spectral/mfcc.h" 32 33 … … 37 38 uint_t win_s; /** grain length */ 38 39 uint_t samplerate; /** sample rate (needed?) */ 39 uint_t n_filters; /** number of *filters */40 uint_t n_filters; /** number of filters */ 40 41 uint_t n_coefs; /** number of coefficients (<= n_filters/2 +1) */ 41 42 aubio_filterbank_t *fb; /** filter bank */ 42 43 fvec_t *in_dct; /** input buffer for dct * [fb->n_filters] */ 43 fmat_t *dct_coeffs; /** DCT transform n_filters * n_coeffs */ 44 aubio_dct_t *dct; /** dct object */ 45 fvec_t *output; /** dct output */ 46 smpl_t scale; 44 47 }; 45 48 … … 52 55 /* allocate space for mfcc object */ 53 56 aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t); 54 smpl_t scaling;55 57 56 uint_t i, j; 58 if ((sint_t)n_coefs <= 0) { 59 AUBIO_ERR("mfcc: n_coefs should be > 0, got %d\n", n_coefs); 60 goto failure; 61 } 62 if ((sint_t)samplerate <= 0) { 63 AUBIO_ERR("mfcc: samplerate should be > 0, got %d\n", samplerate); 64 goto failure; 65 } 57 66 58 67 mfcc->win_s = win_s; … … 63 72 /* filterbank allocation */ 64 73 mfcc->fb = new_aubio_filterbank (n_filters, mfcc->win_s); 65 aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); 74 75 if (!mfcc->fb) 76 goto failure; 77 78 if (n_filters == 40) 79 aubio_filterbank_set_mel_coeffs_slaney (mfcc->fb, samplerate); 80 else 81 aubio_filterbank_set_mel_coeffs(mfcc->fb, samplerate, 82 0, samplerate/2.); 66 83 67 84 /* allocating buffers */ 68 85 mfcc->in_dct = new_fvec (n_filters); 69 86 70 mfcc->dct_coeffs = new_fmat (n_coefs, n_filters); 87 mfcc->dct = new_aubio_dct (n_filters); 88 mfcc->output = new_fvec (n_filters); 71 89 72 /* compute DCT transform dct_coeffs[j][i] as 73 cos ( j * (i+.5) * PI / n_filters ) */ 74 scaling = 1. / SQRT (n_filters / 2.); 75 for (i = 0; i < n_filters; i++) { 76 for (j = 0; j < n_coefs; j++) { 77 mfcc->dct_coeffs->data[j][i] = 78 scaling * COS (j * (i + 0.5) * PI / n_filters); 79 } 80 mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.; 81 } 90 if (!mfcc->in_dct || !mfcc->dct || !mfcc->output) 91 goto failure; 92 93 mfcc->scale = 1.; 82 94 83 95 return mfcc; 96 97 failure: 98 del_aubio_mfcc(mfcc); 99 return NULL; 84 100 } 85 101 … … 87 103 del_aubio_mfcc (aubio_mfcc_t * mf) 88 104 { 89 90 /* delete filterbank */ 91 del_aubio_filterbank (mf->fb); 92 93 /* delete buffers */ 94 del_fvec (mf->in_dct); 95 del_fmat (mf->dct_coeffs); 96 97 /* delete mfcc object */ 105 if (mf->fb) 106 del_aubio_filterbank (mf->fb); 107 if (mf->in_dct) 108 del_fvec (mf->in_dct); 109 if (mf->dct) 110 del_aubio_dct (mf->dct); 111 if (mf->output) 112 del_fvec (mf->output); 98 113 AUBIO_FREE (mf); 99 114 } … … 103 118 aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out) 104 119 { 120 fvec_t tmp; 121 105 122 /* compute filterbank */ 106 123 aubio_filterbank_do (mf->fb, in, mf->in_dct); … … 109 126 fvec_log10 (mf->in_dct); 110 127 111 /* raise power */ 112 //fvec_pow (mf->in_dct, 3.); 128 if (mf->scale != 1) fvec_mul (mf->in_dct, mf->scale); 113 129 114 130 /* compute mfccs */ 115 fmat_vecmul(mf->dct_coeffs, mf->in_dct, out); 131 aubio_dct_do(mf->dct, mf->in_dct, mf->output); 132 // copy only first n_coeffs elements 133 // TODO assert mf->output->length == n_coeffs 134 tmp.data = mf->output->data; 135 tmp.length = out->length; 136 fvec_copy(&tmp, out); 116 137 117 138 return; 118 139 } 140 141 uint_t aubio_mfcc_set_power (aubio_mfcc_t *mf, smpl_t power) 142 { 143 return aubio_filterbank_set_power(mf->fb, power); 144 } 145 146 smpl_t aubio_mfcc_get_power (aubio_mfcc_t *mf) 147 { 148 return aubio_filterbank_get_power(mf->fb); 149 } 150 151 uint_t aubio_mfcc_set_scale (aubio_mfcc_t *mf, smpl_t scale) 152 { 153 mf->scale = scale; 154 return AUBIO_OK; 155 } 156 157 smpl_t aubio_mfcc_get_scale (aubio_mfcc_t *mf) 158 { 159 return mf->scale; 160 } 161 162 uint_t aubio_mfcc_set_mel_coeffs (aubio_mfcc_t *mf, smpl_t freq_min, 163 smpl_t freq_max) 164 { 165 return aubio_filterbank_set_mel_coeffs(mf->fb, mf->samplerate, 166 freq_min, freq_max); 167 } 168 169 uint_t aubio_mfcc_set_mel_coeffs_htk (aubio_mfcc_t *mf, smpl_t freq_min, 170 smpl_t freq_max) 171 { 172 return aubio_filterbank_set_mel_coeffs_htk(mf->fb, mf->samplerate, 173 freq_min, freq_max); 174 } 175 176 uint_t aubio_mfcc_set_mel_coeffs_slaney (aubio_mfcc_t *mf) 177 { 178 return aubio_filterbank_set_mel_coeffs_slaney (mf->fb, mf->samplerate); 179 }
Note: See TracChangeset
for help on using the changeset viewer.