SPINET_to_Pitch.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* SPINET_to_Pitch.cpp
  2. *
  3. * Copyright (C) 1993-2017 David Weenink
  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. See the GNU
  13. * 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. /*
  19. djmw 19970408
  20. djmw 20020813 GPL header
  21. djmw 20021106 Latest modification
  22. */
  23. #include "SPINET_to_Pitch.h"
  24. #include "Pitch_extensions.h"
  25. #include "NUM2.h"
  26. /*
  27. from Erb-scale to log2-scale
  28. */
  29. autoPitch SPINET_to_Pitch (SPINET me, double harmonicFallOffSlope, double ceiling, integer maxnCandidates) {
  30. try {
  31. integer nPointsPerOctave = 48;
  32. double fmin = NUMerbToHertz (SampledXY_indexToY (me, 1L));
  33. double fmax = NUMerbToHertz (SampledXY_indexToY (me, my ny));
  34. double fminl2 = NUMlog2 (fmin), fmaxl2 = NUMlog2 (fmax);
  35. double points = (fmaxl2 - fminl2) * nPointsPerOctave;
  36. double dfl2 = (fmaxl2 - fminl2) / (points - 1);
  37. integer nFrequencyPoints = Melder_ifloor (points);
  38. integer maxHarmonic = Melder_ifloor (fmax / fmin);
  39. double maxStrength = 0.0, unvoicedCriterium = 0.45, maxPower = 0.0;
  40. Melder_require (nFrequencyPoints > 1, U"Frequency range too small.");
  41. Melder_require (fmin < ceiling, U"The centre frequency of the lowest filter should be smaller than the ceiling.");
  42. autoPitch thee = Pitch_create (my xmin, my xmax, my nx, my dx, my x1, ceiling, maxnCandidates);
  43. autoNUMvector<double> power (1, my nx);
  44. autoNUMvector<double> pitch (1, nFrequencyPoints);
  45. autoNUMvector<double> sumspec (1, nFrequencyPoints);
  46. autoNUMvector<double> y (1, my ny);
  47. autoNUMvector<double> yv2 (1, my ny);
  48. autoNUMvector<double> fl2 (1, my ny);
  49. // From ERB's to log (f)
  50. for (integer i = 1; i <= my ny; i ++) {
  51. double f = NUMerbToHertz (my y1 + (i - 1) * my dy);
  52. fl2 [i] = NUMlog2 (f);
  53. }
  54. // Determine global maximum power in frame
  55. for (integer j = 1; j <= my nx; j ++) {
  56. double p = 0.0;
  57. for (integer i = 1; i <= my ny; i ++) {
  58. p += my s [i] [j];
  59. }
  60. if (p > maxPower) {
  61. maxPower = p;
  62. }
  63. power [j] = p;
  64. }
  65. Melder_require (maxPower != 0.0, U"The sound should not have all amplitudes equal to zero.");
  66. for (integer j = 1; j <= my nx; j ++) {
  67. Pitch_Frame pitchFrame = &thy frame [j];
  68. pitchFrame -> intensity = power [j] / maxPower;
  69. for (integer i = 1; i <= my ny; i ++) {
  70. y [i] = my s [i] [j];
  71. }
  72. NUMcubicSplineInterpolation_getSecondDerivatives (fl2.peek(), y.peek(), my ny, 1e30, 1e30, yv2.peek());
  73. for (integer k = 1; k <= nFrequencyPoints; k ++) {
  74. double f = fminl2 + (k - 1) * dfl2;
  75. pitch [k] = NUMcubicSplineInterpolation (fl2.peek(), y.peek(), yv2.peek(), my ny, f);
  76. sumspec [k] = 0.0;
  77. }
  78. // Formula (8): weighted harmonic summation.
  79. for (integer m = 1; m <= maxHarmonic; m ++) {
  80. double hm = 1 - harmonicFallOffSlope * NUMlog2 (m);
  81. integer kb = 1 + Melder_ifloor (nPointsPerOctave * NUMlog2 (m));
  82. for (integer k = kb; k <= nFrequencyPoints; k ++) {
  83. if (pitch [k] > 0.0) {
  84. sumspec [k - kb + 1] += pitch [k] * hm;
  85. }
  86. }
  87. }
  88. // into Pitch object
  89. Pitch_Frame_init (pitchFrame, maxnCandidates);
  90. pitchFrame -> nCandidates = 0; /* !!!!! */
  91. Pitch_Frame_addPitch (pitchFrame, 0, 0, maxnCandidates); /* unvoiced */
  92. for (integer k = 2; k <= nFrequencyPoints - 1; k ++) {
  93. double y1 = sumspec [k - 1], y2 = sumspec [k], y3 = sumspec [k + 1];
  94. if (y2 > y1 && y2 >= y3) {
  95. double denum = y1 - 2.0 * y2 + y3, tmp = y3 - 4.0 * y2;
  96. double x = dfl2 * (y1 - y3) / (2 * denum);
  97. double f = pow (2.0, fminl2 + (k - 1) * dfl2 + x);
  98. double strength = (2.0 * y1 * (4.0 * y2 + y3) - y1 * y1 - tmp * tmp) / (8.0 * denum);
  99. if (strength > maxStrength) {
  100. maxStrength = strength;
  101. }
  102. Pitch_Frame_addPitch (pitchFrame, f, strength, maxnCandidates);
  103. }
  104. }
  105. }
  106. // Scale the pitch strengths
  107. for (integer j = 1; j <= my nx; j ++) {
  108. double f0, localStrength;
  109. Pitch_Frame_getPitch (&thy frame [j], &f0, &localStrength);
  110. Pitch_Frame_resizeStrengths (&thy frame [j], localStrength / maxStrength, unvoicedCriterium);
  111. }
  112. return thee;
  113. } catch (MelderError) {
  114. Melder_throw (me, U": no Pitch created.");
  115. }
  116. }
  117. /* End of file SPINET_to_Pitch.cpp */