return AUBIO_FAIL;
} else if (freqs->data[fn] > samplerate / 2) {
AUBIO_WRN("filterbank_mel: freqs should contain only "
- "values > samplerate / 2.\n");
+ "values < samplerate / 2.\n");
} else if (fn > 0 && freqs->data[fn] < freqs->data[fn-1]) {
AUBIO_ERR("filterbank_mel: freqs should be a list of frequencies "
"sorted from low to high, but freq[%d] < freq[%d-1]\n", fn, fn);
return AUBIO_FAIL;
+ } else if (fn > 0 && freqs->data[fn] == freqs->data[fn-1]) {
+ AUBIO_WRN("filterbank_mel: set_triangle_bands received a list "
+ "with twice the frequency %f\n", freqs->data[fn]);
}
}
}
/* compute triangle heights so that each triangle has unit area */
- for (fn = 0; fn < n_filters; fn++) {
- triangle_heights->data[fn] =
- 2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
+ if (aubio_filterbank_get_norm(fb)) {
+ for (fn = 0; fn < n_filters; fn++) {
+ triangle_heights->data[fn] =
+ 2. / (upper_freqs->data[fn] - lower_freqs->data[fn]);
+ }
+ } else {
+ fvec_ones (triangle_heights);
}
/* fill fft_freqs lookup table, which assigns the frequency in hz to each bin */
}
/* compute positive slope step size */
- riseInc =
- triangle_heights->data[fn] /
- (center_freqs->data[fn] - lower_freqs->data[fn]);
+ riseInc = triangle_heights->data[fn]
+ / (center_freqs->data[fn] - lower_freqs->data[fn]);
/* compute coefficients in positive slope */
for (; bin < win_s - 1; bin++) {
}
/* compute negative slope step size */
- downInc =
- triangle_heights->data[fn] /
- (upper_freqs->data[fn] - center_freqs->data[fn]);
+ downInc = triangle_heights->data[fn]
+ / (upper_freqs->data[fn] - center_freqs->data[fn]);
/* compute coefficents in negative slope */
for (; bin < win_s - 1; bin++) {
smpl_t lastlinearCF;
/* buffers to compute filter frequencies */
- fvec_t *freqs = new_fvec (n_filters + 2);
+ fvec_t *freqs;
+
+ if (samplerate <= 0) {
+ AUBIO_ERR("filterbank: set_mel_coeffs_slaney samplerate should be > 0\n");
+ return AUBIO_FAIL;
+ }
+
+ freqs = new_fvec (n_filters + 2);
/* first step: fill all the linear filter frequencies */
for (fn = 0; fn < linearFilters; fn++) {
return retval;
}
+
+static uint_t aubio_filterbank_check_freqs (aubio_filterbank_t *fb UNUSED,
+ smpl_t samplerate, smpl_t *freq_min, smpl_t *freq_max)
+{
+ if (samplerate <= 0) {
+ AUBIO_ERR("filterbank: set_mel_coeffs samplerate should be > 0\n");
+ return AUBIO_FAIL;
+ }
+ if (*freq_max < 0) {
+ AUBIO_ERR("filterbank: set_mel_coeffs freq_max should be > 0\n");
+ return AUBIO_FAIL;
+ } else if (*freq_max == 0) {
+ *freq_max = samplerate / 2.;
+ }
+ if (*freq_min < 0) {
+ AUBIO_ERR("filterbank: set_mel_coeffs freq_min should be > 0\n");
+ return AUBIO_FAIL;
+ }
+ return AUBIO_OK;
+}
+
+uint_t
+aubio_filterbank_set_mel_coeffs (aubio_filterbank_t * fb, smpl_t samplerate,
+ smpl_t freq_min, smpl_t freq_max)
+{
+ uint_t m, retval;
+ smpl_t start = freq_min, end = freq_max, step;
+ fvec_t *freqs;
+ fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
+ uint_t n_bands = coeffs->height;
+
+ if (aubio_filterbank_check_freqs(fb, samplerate, &start, &end)) {
+ return AUBIO_FAIL;
+ }
+
+ start = aubio_hztomel(start);
+ end = aubio_hztomel(end);
+
+ freqs = new_fvec(n_bands + 2);
+ step = (end - start) / (n_bands + 1);
+
+ for (m = 0; m < n_bands + 2; m++)
+ {
+ freqs->data[m] = MIN(aubio_meltohz(start + step * m), samplerate/2.);
+ }
+
+ retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
+
+ /* destroy vector used to store frequency limits */
+ del_fvec (freqs);
+ return retval;
+}
+
+uint_t
+aubio_filterbank_set_mel_coeffs_htk (aubio_filterbank_t * fb, smpl_t samplerate,
+ smpl_t freq_min, smpl_t freq_max)
+{
+ uint_t m, retval;
+ smpl_t start = freq_min, end = freq_max, step;
+ fvec_t *freqs;
+ fmat_t *coeffs = aubio_filterbank_get_coeffs(fb);
+ uint_t n_bands = coeffs->height;
+
+ if (aubio_filterbank_check_freqs(fb, samplerate, &start, &end)) {
+ return AUBIO_FAIL;
+ }
+
+ start = aubio_hztomel_htk(start);
+ end = aubio_hztomel_htk(end);
+
+ freqs = new_fvec (n_bands + 2);
+ step = (end - start) / (n_bands + 1);
+
+ for (m = 0; m < n_bands + 2; m++)
+ {
+ freqs->data[m] = MIN(aubio_meltohz_htk(start + step * m), samplerate/2.);
+ }
+
+ retval = aubio_filterbank_set_triangle_bands (fb, freqs, samplerate);
+
+ /* destroy vector used to store frequency limits */
+ del_fvec (freqs);
+ return retval;
+}