Excitation_to_Formant.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Excitation_to_Formant.cpp
  2. *
  3. * Copyright (C) 1992-2011,2015,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Excitation_to_Formant.h"
  19. autoFormant Excitation_to_Formant (Excitation me, integer maximumNumberOfFormants) {
  20. try {
  21. integer numberOfFrequencies = my nx, nform = 0;
  22. double *p = my z [1];
  23. autoFormant thee = Formant_create (0.0, 1.0, 1, 1.0, 0.5, maximumNumberOfFormants);
  24. thy d_frames [1]. formant = NUMvector <structFormant_Formant> (1, maximumNumberOfFormants);
  25. for (integer i = 2; i < numberOfFrequencies; i ++)
  26. if (p [i] > p [i - 1] && p [i] >= p [i + 1]) {
  27. double firstDerivative = p [i+1] - p [i-1], secondDerivative = 2 * p [i] - p [i-1] - p [i+1];
  28. integer j;
  29. Formant_Formant formant = & thy d_frames [1]. formant [++ nform];
  30. formant -> frequency = Excitation_barkToHertz (
  31. my x1 + my dx * (i - 1 + 0.5 * firstDerivative / secondDerivative));
  32. double min3phon = p [i] + 0.125 * firstDerivative * firstDerivative / secondDerivative - 3.0;
  33. /* Search left. */
  34. j = i - 1; while (p [j] > min3phon && j > 1) j --;
  35. double left = Excitation_barkToHertz (
  36. p [j] > min3phon ? my xmin : my x1 + my dx * (j - 1 + (min3phon - p [j]) / (p [j + 1] - p [j])));
  37. /* Search right. */
  38. j = i + 1; while (p [j] > min3phon && j < numberOfFrequencies) j ++;
  39. double right = Excitation_barkToHertz (
  40. p [j] > min3phon ? my xmax : my x1 + my dx * (j - 1 - (min3phon - p [j]) / (p [j - 1] - p [j])));
  41. formant -> bandwidth = right - left;
  42. if (nform == thy maxnFormants) break;
  43. }
  44. thy d_frames [1]. nFormants = nform;
  45. return thee;
  46. } catch (MelderError) {
  47. Melder_throw (me, U": not converted to Formant.");
  48. }
  49. }
  50. /* End of file Excitation_to_Formant.cpp */