static int Sound_into_MelFilter_frame (Sound me, MelFilter thee, long frame) { autoMatrix pv = Sound_to_spectralpower (me); double z1 = pv -> x1; double dz = pv -> dx; long nf = pv -> nx; double df = thy dy; for (long i = 1; i <= thy ny; i++) { double p = 0; double fc_mel = thy y1 + (i - 1) * df; double fc_hz = MELTOHZ (fc_mel); double fl_hz = MELTOHZ (fc_mel - df); double fh_hz = MELTOHZ (fc_mel + df); double *pow = pv -> z[1]; for (long j = 1; j <= nf; j++) { // Filter with a triangular filter the power (=amplitude-squared) double f = z1 + (j - 1) * dz; double a = NUMtriangularfilter_amplitude (fl_hz, fc_hz, fh_hz, f); p += a * pow[j] ; } thy z[i][frame] = p; } return 1; }
void MelFilter_drawFilterFunctions (MelFilter me, Graphics g, int toFreqScale, int fromFilter, int toFilter, double zmin, double zmax, int dbScale, double ymin, double ymax, int garnish) { if (! checkLimits (me, FilterBank_MEL, toFreqScale, & fromFilter, & toFilter, & zmin, & zmax, dbScale, & ymin, & ymax)) { return; } long n = 1000; autoNUMvector<double> a (1, n); Graphics_setInner (g); Graphics_setWindow (g, zmin, zmax, ymin, ymax); for (long j = fromFilter; j <= toFilter; j++) { double df = (zmax - zmin) / (n - 1); double fc_mel = my y1 + (j - 1) * my dy; double fc_hz = MELTOHZ (fc_mel); double fl_hz = MELTOHZ (fc_mel - my dy); double fh_hz = MELTOHZ (fc_mel + my dy); long ibegin, iend; for (long i = 1; i <= n; i++) { // Filterfunction: triangular on a linear frequency scale AND a linear amplitude scale. double f = zmin + (i - 1) * df; double z = scaleFrequency (f, toFreqScale, FilterBank_HERTZ); if (z == NUMundefined) { a[i] = NUMundefined; } else { a[i] = NUMtriangularfilter_amplitude (fl_hz, fc_hz, fh_hz, z); if (dbScale) { a[i] = to_dB (a[i], 10, ymin); } } } setDrawingLimits (a.peek(), n, ymin, ymax, &ibegin, &iend); if (ibegin <= iend) { double fmin = zmin + (ibegin - 1) * df; double fmax = zmax - (n - iend) * df; Graphics_function (g, a.peek(), ibegin, iend, fmin, fmax); } } Graphics_unsetInner (g); if (garnish) { double distance = dbScale ? 10 : 1; char32 const *ytext = dbScale ? U"Amplitude (dB)" : U"Amplitude"; Graphics_drawInnerBox (g); Graphics_marksBottom (g, 2, 1, 1, 0); Graphics_marksLeftEvery (g, 1, distance, 1, 1, 0); Graphics_textLeft (g, 1, ytext); Graphics_textBottom (g, 1, GetFreqScaleText (toFreqScale)); } }
static double scaleFrequency (double f, int scale_from, int scale_to) { double fhz = NUMundefined; if (scale_from == scale_to) { return f; } if (scale_from == FilterBank_HERTZ) { fhz = f; } else if (scale_from == FilterBank_BARK) { fhz = BARKTOHZ (f); } else if (scale_from == FilterBank_MEL) { fhz = MELTOHZ (f); } if (scale_to == FilterBank_HERTZ || fhz == NUMundefined) { return fhz; } if (scale_to == FilterBank_BARK) { f = HZTOBARK (fhz); } else if (scale_to == FilterBank_MEL) { f = HZTOMEL (fhz); } else { return NUMundefined; } return f; }