double structFormant :: v_getValueAtSample (long iframe, long which, int units) { Formant_Frame frame = & d_frames [iframe]; long iformant = which >> 1; if (iformant < 1 || iformant > frame -> nFormants) return NUMundefined; double frequency = frame -> formant [iformant]. frequency; if ((which & 1) == 0) { return units ? NUMhertzToBark (frequency) : frequency; } else { double bandwidth = frame -> formant [iformant]. bandwidth; if (units) { double fleft = frequency - 0.5 * bandwidth, fright = frequency + 0.5 * bandwidth; fleft = fleft <= 0 ? 0 : NUMhertzToBark (fleft); // prevent NUMundefined fright = NUMhertzToBark (fright); return fright - fleft; } return bandwidth; } return NUMundefined; }
double Formant_getStandardDeviation (Formant me, int iformant, double tmin, double tmax, int bark) { if (iformant < 1 || tmin == NUMundefined || tmax == NUMundefined) return NUMundefined; if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; } long itmin, itmax; if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax)) return NUMundefined; double mean = Formant_getMean (me, iformant, tmin, tmax, bark); double sum = 0.0; long n = 0; for (long iframe = itmin; iframe <= itmax; iframe ++) { Formant_Frame frame = & my d_frames [iframe]; if (iformant > frame -> nFormants) continue; double f = frame -> formant [iformant]. frequency; if (f == 0.0) continue; if (bark) f = NUMhertzToBark (f); n += 1; sum += (f - mean) * (f - mean); } if (n > 1) return sqrt (sum / (n - 1)); return NUMundefined; }
Sound Sound_deepenBandModulation (Sound me, double enhancement_dB, double flow, double fhigh, double slowModulation, double fastModulation, double bandSmoothing) { try { autoSound thee = Data_copy (me); double maximumFactor = pow (10, enhancement_dB / 20), alpha = sqrt (log (2.0)); double alphaslow = alpha / slowModulation, alphafast = alpha / fastModulation; for (long channel = 1; channel <= my ny; channel ++) { autoSound channelSound = Sound_extractChannel (me, channel); autoSpectrum orgspec = Sound_to_Spectrum (channelSound.peek(), true); /* * Keep the part of the sound that is outside the filter bank. */ autoSpectrum spec = Data_copy (orgspec.peek()); Spectrum_stopHannBand (spec.peek(), flow, fhigh, bandSmoothing); autoSound filtered = Spectrum_to_Sound (spec.peek()); long n = thy nx; double *amp = thy z [channel]; for (long i = 1; i <= n; i ++) amp [i] = filtered -> z [1] [i]; autoMelderProgress progress (U"Deepen band modulation..."); double fmin = flow; while (fmin < fhigh) { /* * Take a one-bark frequency band. */ double fmid_bark = NUMhertzToBark (fmin) + 0.5, ceiling; double fmax = NUMbarkToHertz (NUMhertzToBark (fmin) + 1); if (fmax > fhigh) fmax = fhigh; Melder_progress (fmin / fhigh, U"Band: ", Melder_fixed (fmin, 0), U" ... ", Melder_fixed (fmax, 0), U" Hz"); NUMmatrix_copyElements (orgspec -> z, spec -> z, 1, 2, 1, spec -> nx); Spectrum_passHannBand (spec.peek(), fmin, fmax, bandSmoothing); autoSound band = Spectrum_to_Sound (spec.peek()); /* * Compute a relative intensity contour. */ autoSound intensity = Data_copy (band.peek()); n = intensity -> nx; amp = intensity -> z [1]; for (long i = 1; i <= n; i ++) amp [i] = 10 * log10 (amp [i] * amp [i] + 1e-6); autoSpectrum intensityFilter = Sound_to_Spectrum (intensity.peek(), true); n = intensityFilter -> nx; for (long i = 1; i <= n; i ++) { double frequency = intensityFilter -> x1 + (i - 1) * intensityFilter -> dx; double slow = alphaslow * frequency, fast = alphafast * frequency; double factor = exp (- fast * fast) - exp (- slow * slow); intensityFilter -> z [1] [i] *= factor; intensityFilter -> z [2] [i] *= factor; } intensity.reset (Spectrum_to_Sound (intensityFilter.peek())); n = intensity -> nx; amp = intensity -> z [1]; for (long i = 1; i <= n; i ++) amp [i] = pow (10, amp [i] / 2); /* * Clip to maximum enhancement. */ ceiling = 1 + (maximumFactor - 1.0) * (0.5 - 0.5 * cos (NUMpi * fmid_bark / 13)); for (long i = 1; i <= n; i ++) amp [i] = 1 / (1 / amp [i] + 1 / ceiling); n = thy nx; amp = thy z [channel]; for (long i = 1; i <= n; i ++) amp [i] += band -> z [1] [i] * intensity -> z [1] [i]; fmin = fmax; } } Vector_scale (thee.peek(), 0.99); /* Truncate. */ thy xmin = my xmin; thy xmax = my xmax; thy nx = my nx; thy x1 = my x1; return thee.transfer(); } catch (MelderError) { Melder_throw (me, U": band modulation not deepened."); } }