Sound_to_Harmonicity.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Sound_to_Harmonicity.cpp
  2. *
  3. * Copyright (C) 1992-2011,2015,2016,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 "Sound_to_Pitch.h"
  19. #include "Sound_to_Harmonicity.h"
  20. autoHarmonicity Sound_to_Harmonicity_ac (Sound me, double dt, double minimumPitch,
  21. double silenceThreshold, double periodsPerWindow)
  22. {
  23. try {
  24. autoPitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 1,
  25. silenceThreshold, 0.0, 0.0, 0.0, 0.0, 0.5 / my dx);
  26. autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
  27. pitch -> dx, pitch -> x1);
  28. for (integer i = 1; i <= thy nx; i ++) {
  29. if (pitch -> frame [i]. candidate [1]. frequency == 0.0) {
  30. thy z [1] [i] = -200.0;
  31. } else {
  32. double r = pitch -> frame [i]. candidate [1]. strength;
  33. thy z [1] [i] = ( r <= 1e-15 ? -150.0 : r > 1.0 - 1e-15 ? 150.0 : 10.0 * log10 (r / (1.0 - r)) );
  34. }
  35. }
  36. return thee;
  37. } catch (MelderError) {
  38. Melder_throw (me, U": harmonicity analysis (ac) not performed.");
  39. }
  40. }
  41. autoHarmonicity Sound_to_Harmonicity_cc (Sound me, double dt, double minimumPitch,
  42. double silenceThreshold, double periodsPerWindow)
  43. {
  44. try {
  45. autoPitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 3,
  46. silenceThreshold, 0.0, 0.0, 0.0, 0.0, 0.5 / my dx);
  47. autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
  48. pitch -> dx, pitch -> x1);
  49. for (integer i = 1; i <= thy nx; i ++) {
  50. if (pitch -> frame [i]. candidate [1]. frequency == 0.0) {
  51. thy z [1] [i] = -200.0;
  52. } else {
  53. double r = pitch -> frame [i]. candidate [1]. strength;
  54. thy z [1] [i] = ( r <= 1e-15 ? -150.0 : r > 1.0 - 1e-15 ? 150.0 : 10.0 * log10 (r / (1.0 - r)) );
  55. }
  56. }
  57. return thee;
  58. } catch (MelderError) {
  59. Melder_throw (me, U": harmonicity analysis (cc) not performed.");
  60. }
  61. }
  62. /* End of file Sound_to_Harmonicity.cpp */