| 1 | /* Sound_to_Pitch2.c |
| 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 20020813 GPL header |
| 21 | djmw 20021106 Latest modification |
| 22 | djmw 20041124 Changed call to Sound_to_Spectrum. |
| 23 | djmw 20070103 Sound interface changes |
| 24 | */ |
| 25 | |
| 26 | #include "Sound_to_Pitch2.h"cxx . . $ `<file>/dwtools/Sound_to_Pitch2.h`/ |
| 27 | #include "Pitch_extensions.h"cxx . . $ `<file>/dwtools/Pitch_extensions.h`/ |
| 28 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 29 | #include "Sound_to_SPINET.h"cxx . . $ `<file>/dwtools/Sound_to_SPINET.h`/ |
| 30 | #include "SPINET_to_Pitch.h"cxx . . $ `<file>/dwtools/SPINET_to_Pitch.h`/ |
| 31 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 32 | |
| 33 | static double Sound_approximateLocalSampleMeancxx . . $ Sound_approximateLocalSampleMean(a262d7c6e27aaeaa). (Soundcxx . . $ Sound# me, double fromTime, double toTime) { |
| 34 | const integercxx . . $ integer# n1 = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, fromTime)); |
| 35 | const integercxx . . $ integer# n2 = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, toTime), mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); |
| 36 | return n1 <= n2 ? NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1].partcxx . . $ vector#part(ae267db9e2c04be7). (n1, n2)) : undefinedcxx . . $ undefined.; |
| 37 | } |
| 38 | |
| 39 | static void spec_enhance_SHScxx . . $ spec_enhance_SHS(e75f7985a5e05502). (VECcxx . . $ VEC# const & a) { |
| 40 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.size >= 2); |
| 41 | |
| 42 | autoINTVECcxx . . $ autoINTVEC# posmax = raw_INTVECcxx . . $ raw_INTVEC(dab5d3ef28d6a25). ((a.sizecxx . . $ vector#size. + 1) / 2); |
| 43 | integercxx . . $ integer# nmax = 0; |
| 44 | if (a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] > a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2]) |
| 45 | posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ nmax] = 1; |
| 46 | |
| 47 | for (integercxx . . $ integer# i = 2; i <= a.sizecxx . . $ vector#size. - 1; i ++) |
| 48 | if (a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] > a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] && a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] >= a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i + 1]) |
| 49 | posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ nmax] = i; |
| 50 | |
| 51 | if (a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vector#size.] > a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vector#size. - 1]) |
| 52 | posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ nmax] = a.sizecxx . . $ vector#size.; |
| 53 | |
| 54 | if (nmax == 1) { |
| 55 | acxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] - 3) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 56 | acxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] + 3, a.sizecxx . . $ vector#size.) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 57 | } else { |
| 58 | for (integercxx . . $ integer# i = 2; i <= nmax; i ++) |
| 59 | acxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1] + 3, posmax [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] - 3) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static void spec_smoooth_SHScxx . . $ spec_smoooth_SHS(e75f7985a5e05502). (VECcxx . . $ VEC# const& a) { |
| 64 | /* |
| 65 | Convolve in-place with the small symmetric moving-average |
| 66 | kernel { 0.25, 0.5, 0.25 }, aligned around its second element. |
| 67 | The basic equation for an output element a_new [i] is: |
| 68 | a_new [i] := 0.25 * (a [i - 1] + 2.0 * a [i] + a [i + 1]) |
| 69 | |
| 70 | The procedure is performed in place, i.e., the vector a_new [] |
| 71 | appears as the new version of the vector a [], so that care has |
| 72 | to be taken to timely save elements that will be overwritten. |
| 73 | At the edges we perform "same" convolution, meaning that |
| 74 | the output vector has the same number of elements as the |
| 75 | input vector (this is a natural situation in case of in-place |
| 76 | filtering). The elements just beyond the edges of the vector, |
| 77 | namely a [0] and a [a.size + 1], are assumed to be zero. |
| 78 | */ |
| 79 | double a_i_minus_1 = 0.0; // save a [i - 1], for i == 1 |
| 80 | for (integercxx . . $ integer# i = 1; i <= a.sizecxx . . $ vector#size. - 1; i ++) { |
| 81 | const double a_i = a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; // save a [i] |
| 82 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.25 * (a_i_minus_1 + 2.0 * a_i + a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i + 1]); |
| 83 | a_i_minus_1 = a_i; |
| 84 | } |
| 85 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vector#size.] = 0.25 * (a_i_minus_1 + 2.0 * a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).a.sizecxx . . $ vector#size.]); |
| 86 | } |
| 87 | |
| 88 | autoPitchcxx . . $ autoPitch# Sound_to_Pitch_shscxx . . $ Sound_to_Pitch_shs(f6f07cf4b72cd94f). (Soundcxx . . $ Sound# me, double timeStep, double pitchFloor, double maximumFrequency, |
| 89 | double pitchCeiling, integercxx . . $ integer# maxnSubharmonics, integercxx . . $ integer# maxnCandidates, double compressionFactor, integercxx . . $ integer# numberOfPointsPerOctave) |
| 90 | { |
| 91 | try { |
| 92 | const double newSamplingFrequency = 2.0 * maximumFrequency; |
| 93 | const double windowDuration = 2.0 / pitchFloor, halfWindow = 0.5 * windowDuration; |
| 94 | const double atans = numberOfPointsPerOctave * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (65.0 / 50.0) - 1.0; |
| 95 | /* |
| 96 | Number of speech samples in the downsampled signal in each frame: |
| 97 | 100 for windowDuration == 0.04 and newSamplingFrequency == 2500 |
| 98 | */ |
| 99 | const integercxx . . $ integer# numberOfSamples = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (windowDuration * newSamplingFrequency); |
| 100 | const double frameDuration = numberOfSamples / newSamplingFrequency; |
| 101 | |
| 102 | const integercxx . . $ integer# nfft = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (256_integercxx . . $ `operator""_integer`(85240eb457c3721a). /* the minimum number of points for the FFT */, Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (numberOfSamples)); |
| 103 | const integercxx . . $ integer# nfft2 = nfft / 2 + 1; |
| 104 | const double fftframeDuration = nfft / newSamplingFrequency; |
| 105 | const double df = newSamplingFrequency / nfft; |
| 106 | /* |
| 107 | The number of points on the octave scale. |
| 108 | */ |
| 109 | const double fminl2 = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (pitchFloor), fmaxl2 = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (maximumFrequency); |
| 110 | const integercxx . . $ integer# numberOfFrequencyPoints = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((fmaxl2 - fminl2) * numberOfPointsPerOctave); |
| 111 | const double dfl2 = (fmaxl2 - fminl2) / (numberOfFrequencyPoints - 1); |
| 112 | |
| 113 | autoSoundcxx . . $ autoSound# sound = Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (me, newSamplingFrequency, 50); |
| 114 | integercxx . . $ integer# numberOfFrames; |
| 115 | double firstTime; |
| 116 | Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), windowDuration, timeStep, & numberOfFrames, & firstTime); |
| 117 | autoSoundcxx . . $ autoSound# fftframe = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, fftframeDuration, newSamplingFrequency); |
| 118 | autoSoundcxx . . $ autoSound# analysisframe = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, frameDuration, newSamplingFrequency); |
| 119 | autoSoundcxx . . $ autoSound# hamming = Sound_createHammingcxx . . $ Sound_createHamming(c2815364717639a8). (frameDuration, newSamplingFrequency); |
| 120 | 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., numberOfFrames, timeStep, firstTime, pitchCeiling, maxnCandidates); |
| 121 | autoVECcxx . . $ autoVEC# cc = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfFrames); |
| 122 | autoVECcxx . . $ autoVEC# specAmp = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nfft2); |
| 123 | autoVECcxx . . $ autoVEC# fl2 = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nfft2); |
| 124 | autoVECcxx . . $ autoVEC# yv2 = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nfft2); |
| 125 | autoVECcxx . . $ autoVEC# arctg = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfFrequencyPoints); |
| 126 | autoVECcxx . . $ autoVEC# al2 = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfFrequencyPoints); |
| 127 | |
| 128 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (fftframe -> nx >= numberOfSamples); |
| 129 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (hamming -> nx == numberOfSamples); |
| 130 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (analysisframe -> nx == numberOfSamples); |
| 131 | /* |
| 132 | Compute the absolute value of the globally largest amplitude w.r.t. the global mean. |
| 133 | */ |
| 134 | const double globalMean = Sound_approximateLocalSampleMeancxx . . $ Sound_approximateLocalSampleMean(a262d7c6e27aaeaa). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), sound ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). xmincxx . . $ structFunction#xmin., sound ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). xmaxcxx . . $ structFunction#xmax.); |
| 135 | const double globalPeak = Sound_localPeakcxx . . $ Sound_localPeak(fb20d80074545761). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), sound ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). xmincxx . . $ structFunction#xmin., sound ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). xmaxcxx . . $ structFunction#xmax., globalMean); |
| 136 | /* |
| 137 | For the cubic spline interpolation we need the frequencies on an octave |
| 138 | scale, i.e., a log2 scale. All frequencies should be DIFFERENT, otherwise |
| 139 | the cubic spline interpolation will give corrupt results. |
| 140 | Because log2(f==0) is not defined, we use the heuristic: f [2] - f [1] == f [3] - f [2]. |
| 141 | */ |
| 142 | for (integercxx . . $ integer# i = 2; i <= nfft2; i ++) |
| 143 | fl2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). ((i - 1) * df); |
| 144 | fl2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 2.0 * fl2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] - fl2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3]; |
| 145 | /* |
| 146 | Calculate frequencies regularly spaced on a log2-scale and the frequency weighting function. |
| 147 | */ |
| 148 | for (integercxx . . $ integer# i = 1; i <= numberOfFrequencyPoints; i ++) |
| 149 | arctg [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 0.5 + atancxx . . $ atan(fee4f1d22bc8e617). (3.0 * (i - atans) / numberOfPointsPerOctave) / NUMpicxx . . $ NUMpi.; |
| 150 | /* |
| 151 | Perform the analysis on all frames. |
| 152 | */ |
| 153 | for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 154 | const Pitch_Framecxx . . $ Pitch_Frame# pitchFrame = & thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 155 | const double tmid = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe); // The center of this frame |
| 156 | /* |
| 157 | Copy a frame from the sound, apply a hamming window. Get local 'intensity' |
| 158 | */ |
| 159 | Sound_into_Soundcxx . . $ Sound_into_Sound(9b5e4c6d6787168f). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), analysisframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmid - halfWindow); |
| 160 | Sounds_multiplycxx . . $ Sounds_multiply(3740d3b02ef916dc). (analysisframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), hamming.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 161 | const double localMean = Sound_approximateLocalSampleMeancxx . . $ Sound_approximateLocalSampleMean(a262d7c6e27aaeaa). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmid - 3.0 * halfWindow, tmid + 3.0 * halfWindow); |
| 162 | const double localPeak = Sound_localPeakcxx . . $ Sound_localPeak(fb20d80074545761). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmid - halfWindow, tmid + halfWindow, localMean); |
| 163 | pitchFrame -> intensitycxx . . $ structPitch_Frame#intensity. = localPeak > globalPeak ? 1.0 : localPeak / globalPeak; |
| 164 | /* |
| 165 | Get the Fourier spectrum. |
| 166 | */ |
| 167 | fftframecxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1].partcxx . . $ vector#part(ae267db9e2c04be7). (1, analysisframe ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.) <<=cxx . . $ `operator<<=`(adea042513329390). analysisframecxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1]; // |
| 168 | autoSpectrumcxx . . $ autoSpectrum# spec = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (fftframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); |
| 169 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (spec -> nx == nfft2); |
| 170 | /* |
| 171 | From complex spectrum to amplitude spectrum. |
| 172 | */ |
| 173 | for (integercxx . . $ integer# j = 1; j <= nfft2; j ++) { |
| 174 | const double rs = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j], is = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 175 | specAmp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = hypotcxx . . $ hypot(b71928d39fde4295). (rs, is); |
| 176 | } |
| 177 | /* |
| 178 | Enhance the peaks in the spectrum. |
| 179 | */ |
| 180 | spec_enhance_SHScxx . . $ spec_enhance_SHS(e75f7985a5e05502). (specAmp.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 181 | /* |
| 182 | Smooth the enhanced spectrum. |
| 183 | */ |
| 184 | spec_smoooth_SHScxx . . $ spec_smoooth_SHS(e75f7985a5e05502). (specAmp.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 185 | /* |
| 186 | Go to a logarithmic scale and perform cubic spline interpolation to get |
| 187 | spectral values for the increased number of frequency points. |
| 188 | */ |
| 189 | NUMcubicSplineInterpolation_getSecondDerivativescxx . . $ NUMcubicSplineInterpolation_getSecondDerivatives(1ccc247dbce018f8). (yv2.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fl2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), specAmpcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1e30, 1e30); |
| 190 | for (integercxx . . $ integer# j = 1; j <= numberOfFrequencyPoints; j ++) { |
| 191 | const double f = fminl2 + (j - 1) * dfl2; |
| 192 | al2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = NUMcubicSplineInterpolationcxx . . $ NUMcubicSplineInterpolation(b0781aae48543720). (fl2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), specAmpcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), yv2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), f); |
| 193 | } |
| 194 | /* |
| 195 | Multiply by frequency selectivity of the auditory system. |
| 196 | */ |
| 197 | for (integercxx . . $ integer# j = 1; j <= numberOfFrequencyPoints; j ++) |
| 198 | al2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = ( al2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] > 0.0 ? al2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] * arctg [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] : 0.0 ); |
| 199 | /* |
| 200 | The subharmonic summation. Shift spectra in octaves and sum. |
| 201 | */ |
| 202 | Pitch_Frame_initcxx . . $ Pitch_Frame_init(dff4ec8b4c81c245). (pitchFrame, maxnCandidates); |
| 203 | autoVECcxx . . $ autoVEC# sumspec = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfFrequencyPoints); |
| 204 | pitchFrame -> candidatescxx . . $ structPitch_Frame#candidates.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (pitchFrame -> nCandidatescxx . . $ structPitch_Frame#nCandidates. = 0); // !!!!! |
| 205 | |
| 206 | double hm = 1.0; |
| 207 | for (integercxx . . $ integer# m = 1; m <= maxnSubharmonics + 1; m ++) { |
| 208 | const integercxx . . $ integer# kb = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (numberOfPointsPerOctave * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (m)); |
| 209 | for (integercxx . . $ integer# k = kb; k <= numberOfFrequencyPoints; k ++) |
| 210 | sumspec [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k - kb + 1] += al2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] * hm; |
| 211 | hm *= compressionFactor; |
| 212 | } |
| 213 | /* |
| 214 | First register the voiceless candidate (always present). |
| 215 | */ |
| 216 | Pitch_Frame_addPitchcxx . . $ Pitch_Frame_addPitch(203fb7178073f670). (pitchFrame, 0.0, 0.0, maxnCandidates); |
| 217 | /* |
| 218 | Get the best local estimates for the pitch as the maxima of the |
| 219 | subharmonic sum spectrum by parabolic interpolation on three points: |
| 220 | The formula for a parabola with a maximum is: |
| 221 | y(x) = a - b (x - c)^2 with a, b, c >= 0 |
| 222 | The three points are (-x, y1), (0, y2) and (x, y3). |
| 223 | The solution for a (the maximum) and c (the position) is: |
| 224 | a = (2 y1 (4 y2 + y3) - y1^2 - (y3 - 4 y2)^2)/( 8 (y1 - 2 y2 + y3) |
| 225 | c = dx (y1 - y3) / (2 (y1 - 2 y2 + y3)) |
| 226 | (b = (2 y2 - y1 - y3) / (2 dx^2) ) |
| 227 | */ |
| 228 | for (integercxx . . $ integer# k = 2; k <= numberOfFrequencyPoints - 1; k ++) { |
| 229 | 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]; |
| 230 | if (y2 > y1 && y2 >= y3) { |
| 231 | const double denum = y1 - 2.0 * y2 + y3, tmp = y3 - 4.0 * y2; |
| 232 | const double x = dfl2 * (y1 - y3) / (2.0 * denum); |
| 233 | const double f = powcxx . . $ pow(b71928d39fde4295). (2.0, fminl2 + (k - 1) * dfl2 + x); |
| 234 | const double strength = (2.0 * y1 * (4.0 * y2 + y3) - y1 * y1 - tmp * tmp) / (8.0 * denum); |
| 235 | Pitch_Frame_addPitchcxx . . $ Pitch_Frame_addPitch(203fb7178073f670). (pitchFrame, f, strength, maxnCandidates); |
| 236 | } |
| 237 | } |
| 238 | /* |
| 239 | Check whether f0 corresponds to an actual periodicity T = 1 / f0: |
| 240 | correlate two signal periods of duration T, one starting at the |
| 241 | middle of the interval and one starting T seconds before. |
| 242 | If there is periodicity the correlation coefficient should be high. |
| 243 | |
| 244 | However, some sounds do not show any regularity, or very low |
| 245 | frequency and regularity, and nevertheless have a definite |
| 246 | pitch, e.g. Shepard sounds. |
| 247 | */ |
| 248 | double pitch_strength, f0; |
| 249 | Pitch_Frame_getPitchcxx . . $ Pitch_Frame_getPitch(804b7ce859a03a77). (pitchFrame, & f0, & pitch_strength); |
| 250 | if (f0 > 0.0) |
| 251 | cc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe] = Sound_correlatePartscxx . . $ Sound_correlateParts(fb20d80074545761). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmid - 1.0 / f0, tmid, 1.0 / f0); |
| 252 | } |
| 253 | /* |
| 254 | Base V/UV decision on correlation coefficients. |
| 255 | Resize the pitch strengths w.r.t. the cc. |
| 256 | */ |
| 257 | constexpr double vuvCriterion = 0.52; |
| 258 | for (integercxx . . $ integer# i = 1; i <= numberOfFrames; i ++) |
| 259 | Pitch_Frame_resizeStrengthscxx . . $ Pitch_Frame_resizeStrengths(cb539ae45523e044). (& thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], cc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], vuvCriterion); |
| 260 | |
| 261 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 262 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 263 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Pitch (shs) created."); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | autoPitchcxx . . $ autoPitch# Sound_to_Pitch_SPINETcxx . . $ Sound_to_Pitch_SPINET(60f2015242d2a3e1). (Soundcxx . . $ Sound# me, double timeStep, double windowDuration, double minimumFrequencyHz, double maximumFrequencyHz, integercxx . . $ integer# nFilters, double ceiling, int maxnCandidates) { |
| 268 | try { |
| 269 | autoSPINETcxx . . $ autoSPINET# him = Sound_to_SPINETcxx . . $ Sound_to_SPINET(ab6a30a57ddbf21). (me, timeStep, windowDuration, minimumFrequencyHz, maximumFrequencyHz, nFilters, 0.4, 0.6); |
| 270 | autoPitchcxx . . $ autoPitch# thee = SPINET_to_Pitchcxx . . $ SPINET_to_Pitch(1a5f73525cbe491a). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.15, ceiling, maxnCandidates); |
| 271 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 272 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 273 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Pitch (SPINET) created."); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* End of file Sound_to_Pitch2.cpp */ |