Debian Codegraph @

fon/Sound_to_Intensity.cpp

raw symbols SCIP
1 /* Sound_to_Intensity.cpp
2 *
3 * Copyright (C) 1992-2012,2014-2020,2023 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
19 /*
20 * pb 2002/07/16 GPL
21 * pb 2003/05/20 default time step is four times oversampling
22 * pb 2003/07/10 NUMbessel_i0_f
23 * pb 2003/11/19 Sound_to_Intensity veryAccurate
24 * pb 2003/12/15 removed bug introduced by previous change
25 * pb 2004/10/27 subtractMean
26 * pb 2006/12/31 compatible with stereo sounds
27 * pb 2007/01/27 for stereo sounds, add channel energies
28 * pb 2007/02/14 honoured precondition of Sampled_shortTermAnalysis (by checking whether minimumPitch is defined)
29 * pb 2008/01/19 double
30 * pb 2011/03/04 C++
31 * pb 2011/03/28 C++
32 */
33
34 #include "Sound_to_Intensity.h"cxx . . $ `<file>/fon/Sound_to_Intensity.h`/
35
36 static autoIntensitycxx . . $ autoIntensity# Sound_to_Intensity_cxx . . $ Sound_to_Intensity_(523838029c3b40bd). (Soundcxx . . $ Sound# me, double pitchFloor, double timeStep, bool subtractMeanPressure) {
37 try {
38 /*
39 Preconditions.
40 */
41 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (pitchFloor),
42 U"The pitch floor is undefined.");
43 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (timeStep),
44 U"The time step is undefined.");
45 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (timeStep >= 0.0,
46 U"The time step should be zero (= automatic) or positive, instead of ", timeStep, U" seconds.");
47 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! dx > 0.0,
48 U"The Sound's time step should be positive, instead of ", mycxx . . $ `melder/melder.h:44:9`! dx, U" seconds.");
49 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (pitchFloor > 0.0,
50 U"The pitch floor should be positive, instead of ", pitchFloor, U" Hz.");
51 /*
52 Defaults.
53 */
54 constexpr double minimumNumberOfPeriodsNeededForReliablePitchMeasurement = 3.2;
55 const double periodCeiling = 1.0 / pitchFloor;
56 const double logicalWindowDuration = minimumNumberOfPeriodsNeededForReliablePitchMeasurement * periodCeiling; // == 3.2 / pitchFloor
57 if (timeStep == 0.0) {
58 constexpr double defaultOversampling = 4.0;
59 timeStep = logicalWindowDuration / defaultOversampling; // == 0.8 / pitchFloor
60 }
61
62 const double physicalWindowDuration = 2.0 * logicalWindowDuration; // == 6.4 / pitchFloor
63 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (physicalWindowDuration > 0.0);
64 const double halfWindowDuration = 0.5 * physicalWindowDuration;
65 const integercxx . . $ integer# halfWindowSamples = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (halfWindowDuration / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.);
66 const integercxx . . $ integer# windowNumberOfSamples = 2 * halfWindowSamples + 1;
67 autoVECcxx . . $ autoVEC# amplitude = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (windowNumberOfSamples);
68 autoVECcxx . . $ autoVEC# window = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (windowNumberOfSamples);
69 const integercxx . . $ integer# windowCentreSampleNumber = halfWindowSamples + 1;
70
71 for (integercxx . . $ integer# i = 1; i <= windowNumberOfSamples; i ++) {
72 const double x = (i - windowCentreSampleNumber) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. / halfWindowDuration;
73 const double root = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (0.0, 1.0 - sqrcxx . . $ sqr(ada6a8422704cf8a). (x))); // clipping should be rare
74 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = NUMbessel_i0_fcxx . . $ NUMbessel_i0_f(447c67c6676f75c3). ((2.0 * NUMpicxx . . $ NUMpi. * NUMpicxx . . $ NUMpi. + 0.5) * root);
75 }
76
77 integercxx . . $ integer# numberOfFrames;
78 double thyFirstTime;
79 try {
80 Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, physicalWindowDuration, timeStep, & numberOfFrames, & thyFirstTime);
81 } catch (MelderErrorcxx . . $ MelderError#) {
82 const double physicalSoundDuration = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
83 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"The physical duration of the sound (the number of samples times the sampling period) in an intensity analysis "
84 "should be at least 6.4 divided by the pitch floor (", pitchFloor, U" Hz), "
85 U"i.e. at least ", physicalWindowDuration, U" s, instead of ", physicalSoundDuration, U" s.");
86 }
87 autoIntensitycxx . . $ autoIntensity# thee = Intensity_createcxx . . $ Intensity_create(83c615748dcd7df). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, timeStep, thyFirstTime);
88 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
89 const double midTime = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
90 const integercxx . . $ integer# soundCentreSampleNumber = Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, midTime); // time accuracy is half a sampling period
91
92 integercxx . . $ integer# leftSample = soundCentreSampleNumber - halfWindowSamples;
93 integercxx . . $ integer# rightSample = soundCentreSampleNumber + halfWindowSamples;
94 /*
95 Catch some edge cases, which are uncommon because Sampled_shortTermAnalysis() filtered out most problems.
96 */
97 Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & leftSample);
98 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& rightSample, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
99 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (rightSample >= leftSample,
100 U"Unexpected edge case: right sample (", rightSample, U") less than left sample (", leftSample, U").");
101
102 const integercxx . . $ integer# windowFromSoundOffset = windowCentreSampleNumber - soundCentreSampleNumber;
103 VECcxx . . $ VEC# amplitudePart = amplitude.partcxx . . $ autovector#part(2d8d48654ca64e70). (windowFromSoundOffset + leftSample, windowFromSoundOffset + rightSample);
104 constVECcxx . . $ constVEC# windowPart = windowcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (windowFromSoundOffset + leftSample, windowFromSoundOffset + rightSample);
105 longdoublecxx . . $ longdouble# sumxw = 0.0, sumw = 0.0;
106 for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++) {
107 amplitudePartcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ichan].partcxx . . $ vector#part(ae267db9e2c04be7). (leftSample, rightSample);
108 if (subtractMeanPressure)
109 centre_VEC_inoutcxx . . $ centre_VEC_inout(b915798f9bafc8a). (amplitudePartcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd).);
110 for (integercxx . . $ integer# isamp = 1; isamp <= amplitudePart.sizecxx . . $ vector#size.; isamp ++) {
111 sumxw += sqrcxx . . $ sqr(ada6a8422704cf8a). (amplitudePart [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]) * windowPart [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).isamp];
112 sumw += windowPart [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).isamp];
113 }
114 }
115 const double intensity_in_Pa2 = double (sumxw / sumw);
116 constexpr double hearingThreshold_in_Pa = 2.0e-5;
117 constexpr double hearingThreshold_in_Pa2 = sqrcxx . . $ sqr(ada6a8422704cf8a). (hearingThreshold_in_Pa);
118 const double intensity_re_hearingThreshold = intensity_in_Pa2 / hearingThreshold_in_Pa2;
119 const double intensity_in_dB_re_hearingThreshold = ( intensity_re_hearingThreshold < 1.0e-30 ? -300.0 :
120 10.0 * log10cxx . . $ log10(fee4f1d22bc8e617). (intensity_re_hearingThreshold) );
121 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = intensity_in_dB_re_hearingThreshold;
122 }
123 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
124 } catch (MelderErrorcxx . . $ MelderError#) {
125 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": intensity analysis not performed.");
126 }
127 }
128
129 autoIntensitycxx . . $ autoIntensity# Sound_to_Intensitycxx . . $ Sound_to_Intensity(523838029c3b40bd). (Soundcxx . . $ Sound# me, double pitchFloor, double timeStep, bool subtractMeanPressure) {
130 const bool veryAccurate = false;
131 if (veryAccurate) {
132 autoSoundcxx . . $ autoSound# up = Sound_upsamplecxx . . $ Sound_upsample(4e9053a6db58af83). (me); // because squaring doubles the frequency content, i.e. you get super-Nyquist components
133 return Sound_to_Intensity_cxx . . $ Sound_to_Intensity_(523838029c3b40bd). (up.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pitchFloor, timeStep, subtractMeanPressure);
134 } else {
135 return Sound_to_Intensity_cxx . . $ Sound_to_Intensity_(523838029c3b40bd). (me, pitchFloor, timeStep, subtractMeanPressure);
136 }
137 }
138
139 autoIntensityTiercxx . . $ autoIntensityTier# Sound_to_IntensityTiercxx . . $ Sound_to_IntensityTier(832dd94c110cf030). (Soundcxx . . $ Sound# me, double pitchFloor, double timeStep, bool subtractMean) {
140 try {
141 autoIntensitycxx . . $ autoIntensity# intensity = Sound_to_Intensitycxx . . $ Sound_to_Intensity(523838029c3b40bd). (me, pitchFloor, timeStep, subtractMean);
142 return Intensity_downto_IntensityTiercxx . . $ Intensity_downto_IntensityTier(2cd22a554f648163). (intensity.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
143 } catch (MelderErrorcxx . . $ MelderError#) {
144 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no IntensityTier created.");
145 }
146 }
147
148 /* End of file Sound_to_Intensity.cpp */