| 1 | /* SPINET_to_Pitch.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 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 | /* |
| 20 | djmw 19970408 |
| 21 | djmw 20020813 GPL header |
| 22 | */ |
| 23 | |
| 24 | #include "SPINET_to_Pitch.h"cxx . . $ `<file>/dwtools/SPINET_to_Pitch.h`/ |
| 25 | #include "Pitch_extensions.h"cxx . . $ `<file>/dwtools/Pitch_extensions.h`/ |
| 26 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 27 | |
| 28 | /* |
| 29 | from Erb-scale to log2-scale |
| 30 | */ |
| 31 | |
| 32 | autoPitchcxx . . $ autoPitch# SPINET_to_Pitchcxx . . $ SPINET_to_Pitch(1a5f73525cbe491a). (SPINETcxx . . $ SPINET# me, double harmonicFallOffSlope, double ceiling, integercxx . . $ integer# maxnCandidates) { |
| 33 | try { |
| 34 | constexpr integercxx . . $ integer# nPointsPerOctave = 48; |
| 35 | const double fmin = NUMerbToHertzcxx . . $ NUMerbToHertz(447c67c6676f75c3). (SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, 1L)); |
| 36 | const double fmax = NUMerbToHertzcxx . . $ NUMerbToHertz(447c67c6676f75c3). (SampledXY_indexToYcxx . . $ SampledXY_indexToY(3ae72866b16d2115). (me, mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.)); |
| 37 | const double fminl2 = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (fmin), fmaxl2 = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (fmax); |
| 38 | const double points = (fmaxl2 - fminl2) * nPointsPerOctave; |
| 39 | const double dfl2 = (fmaxl2 - fminl2) / (points - 1); |
| 40 | const integercxx . . $ integer# numberOfFrequencyPoints = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (points); |
| 41 | const integercxx . . $ integer# maxHarmonic = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (fmax / fmin); |
| 42 | const double unvoicedCriterium = 0.45; |
| 43 | |
| 44 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfFrequencyPoints > 1, |
| 45 | U"Frequency range too small."); |
| 46 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fmin < ceiling, |
| 47 | U"The centre frequency of the lowest filter should be smaller than the ceiling."); |
| 48 | |
| 49 | autoPitchcxx . . $ autoPitch# thee = Pitch_createcxx . . $ Pitch_create(15f7ea67007465c5). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1., ceiling, maxnCandidates); |
| 50 | autoVECcxx . . $ autoVEC# power = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); |
| 51 | autoVECcxx . . $ autoVEC# pitch = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfFrequencyPoints); |
| 52 | autoVECcxx . . $ autoVEC# sumspec = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfFrequencyPoints); |
| 53 | autoVECcxx . . $ autoVEC# y = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 54 | autoVECcxx . . $ autoVEC# yv2 = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 55 | autoVECcxx . . $ autoVEC# fl2 = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 56 | /* |
| 57 | From ERB's to log (f) |
| 58 | */ |
| 59 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; i ++) { |
| 60 | const double f = NUMerbToHertzcxx . . $ NUMerbToHertz(447c67c6676f75c3). (mycxx . . $ `melder/melder.h:44:9`! y1cxx . . $ structSampledXY#y1. + (i - 1) * mycxx . . $ `melder/melder.h:44:9`! dycxx . . $ structSampledXY#dy.); |
| 61 | fl2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (f); |
| 62 | } |
| 63 | /* |
| 64 | Determine global maximum power in frame |
| 65 | */ |
| 66 | double maxStrength = 0.0, maxPower = 0.0; |
| 67 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++) { |
| 68 | const double p = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (mycxx . . $ `melder/melder.h:44:9`! scxx . . $ structSPINET#s..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (j)); |
| 69 | if (p > maxPower) |
| 70 | maxPower = p; |
| 71 | power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = p; |
| 72 | } |
| 73 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (maxPower != 0.0, |
| 74 | U"The sound should not have all amplitudes equal to zero."); |
| 75 | |
| 76 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++) { |
| 77 | const Pitch_Framecxx . . $ Pitch_Frame# pitchFrame = & thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]; |
| 78 | |
| 79 | pitchFrame -> intensitycxx . . $ structPitch_Frame#intensity. = power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] / maxPower; |
| 80 | y.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! scxx . . $ structSPINET#s..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (j); |
| 81 | |
| 82 | NUMcubicSplineInterpolation_getSecondDerivativescxx . . $ NUMcubicSplineInterpolation_getSecondDerivatives(1ccc247dbce018f8). (yv2.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fl2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ycxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1e30, 1e30); |
| 83 | for (integercxx . . $ integer# k = 1; k <= numberOfFrequencyPoints; k ++) { |
| 84 | const double f = fminl2 + (k - 1) * dfl2; |
| 85 | pitch [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = NUMcubicSplineInterpolationcxx . . $ NUMcubicSplineInterpolation(b0781aae48543720). (fl2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ycxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), yv2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), f); |
| 86 | sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] = 0.0; |
| 87 | } |
| 88 | /* |
| 89 | Formula (8): weighted harmonic summation. |
| 90 | */ |
| 91 | for (integercxx . . $ integer# m = 1; m <= maxHarmonic; m ++) { |
| 92 | const double hm = 1 - harmonicFallOffSlope * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (m); |
| 93 | const integercxx . . $ integer# kb = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (nPointsPerOctave * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (m)); // TODO: what is kb? |
| 94 | for (integercxx . . $ integer# k = kb; k <= numberOfFrequencyPoints; k ++) |
| 95 | if (pitch [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] > 0.0) |
| 96 | sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k - kb + 1] += pitch [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] * hm; |
| 97 | } |
| 98 | /* |
| 99 | Into Pitch object |
| 100 | */ |
| 101 | Pitch_Frame_initcxx . . $ Pitch_Frame_init(dff4ec8b4c81c245). (pitchFrame, maxnCandidates); |
| 102 | pitchFrame -> candidatescxx . . $ structPitch_Frame#candidates.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (pitchFrame -> nCandidatescxx . . $ structPitch_Frame#nCandidates. = 0); // !!!!! |
| 103 | Pitch_Frame_addPitchcxx . . $ Pitch_Frame_addPitch(203fb7178073f670). (pitchFrame, 0, 0, maxnCandidates); // unvoiced |
| 104 | |
| 105 | for (integercxx . . $ integer# k = 2; k <= numberOfFrequencyPoints - 1; k ++) { |
| 106 | const double y1 = sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k - 1], y2 = sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k], y3 = sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k + 1]; |
| 107 | if (y2 > y1 && y2 >= y3) { |
| 108 | const double denum = y1 - 2.0 * y2 + y3, tmp = y3 - 4.0 * y2; |
| 109 | const double x = dfl2 * (y1 - y3) / (2 * denum); |
| 110 | const double f = powcxx . . $ pow(b71928d39fde4295). (2.0, fminl2 + (k - 1) * dfl2 + x); |
| 111 | const double strength = (2.0 * y1 * (4.0 * y2 + y3) - y1 * y1 - tmp * tmp) / (8.0 * denum); |
| 112 | if (strength > maxStrength) |
| 113 | maxStrength = strength; |
| 114 | Pitch_Frame_addPitchcxx . . $ Pitch_Frame_addPitch(203fb7178073f670). (pitchFrame, f, strength, maxnCandidates); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Scale the pitch strengths |
| 120 | |
| 121 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++) { |
| 122 | double f0, localStrength; |
| 123 | Pitch_Frame_getPitchcxx . . $ Pitch_Frame_getPitch(804b7ce859a03a77). (& thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j], & f0, & localStrength); |
| 124 | Pitch_Frame_resizeStrengthscxx . . $ Pitch_Frame_resizeStrengths(cb539ae45523e044). (& thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j], localStrength / maxStrength, unvoicedCriterium); |
| 125 | } |
| 126 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 127 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 128 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Pitch created."); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* End of file SPINET_to_Pitch.cpp */ |