| 1 | /* Formant_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012-2021 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 | #include "DataModeler.h" |
| 20 | #include "Formant_extensions.h" |
| 21 | #include "LPC_and_Formant.h" |
| 22 | #include "NUM2.h" |
| 23 | |
| 24 | #include "enums_getText.h" |
| 25 | #include "Formant_extensions_enums.h" |
| 26 | #include "enums_getValue.h" |
| 27 | #include "Formant_extensions_enums.h" |
| 28 | |
| 29 | void Formant_formula (Formant me, double tmin, double tmax, integer formantmin, integer formantmax, Interpreter interpreter, conststring32 expression) { |
| 30 | try { |
| 31 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 32 | const integer numberOfPossibleFormants = my maxnFormants; |
| 33 | if (formantmax >= formantmin) { |
| 34 | formantmin = 1; |
| 35 | formantmax = numberOfPossibleFormants; |
| 36 | } |
| 37 | Melder_clipLeft (1_integer, & formantmin); |
| 38 | Melder_clipRight (& formantmax, numberOfPossibleFormants); |
| 39 | |
| 40 | autoMatrix fb = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 2 * numberOfPossibleFormants, 2 * numberOfPossibleFormants, 1.0, 1.0); |
| 41 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 42 | const Formant_Frame frame = & my frames [iframe]; |
| 43 | const integer numberOfFormants = std::min (frame -> numberOfFormants, numberOfPossibleFormants); |
| 44 | for (integer iformant = 1; iformant <= numberOfFormants; iformant++) |
| 45 | if (iformant <= frame -> numberOfFormants) { |
| 46 | fb -> z [2 * iformant - 1] [iframe] = frame -> formant [iformant]. frequency; |
| 47 | fb -> z [2 * iformant ] [iframe] = frame -> formant [iformant]. bandwidth; |
| 48 | } |
| 49 | } |
| 50 | /* |
| 51 | Apply the formaula |
| 52 | */ |
| 53 | const double ymin = 2.0 * formantmin - 1.0, ymax = 2.0 * formantmax; |
| 54 | Matrix_formula_part (fb.get(), tmin, tmax, ymin, ymax, expression, interpreter, nullptr); |
| 55 | /* |
| 56 | Put results back in Formant |
| 57 | */ |
| 58 | integer ixmin, ixmax, iymin, iymax; |
| 59 | (void) Matrix_getWindowSamplesX (fb.get(), tmin, tmax, & ixmin, & ixmax); |
| 60 | (void) Matrix_getWindowSamplesY (fb.get(), ymin, ymax, & iymin, & iymax); |
| 61 | |
| 62 | for (integer iframe = ixmin; iframe <= ixmax; iframe ++) { |
| 63 | /* |
| 64 | If some of the formant frequencies are set to zero => remove the formant |
| 65 | */ |
| 66 | const Formant_Frame frame = & my frames [iframe]; |
| 67 | const integer numberOfFormants = std::min (frame -> numberOfFormants, formantmax); |
| 68 | integer iformantto = ( formantmin > 1 ? formantmin - 1 : 0 ); |
| 69 | for (integer iformant = formantmin; iformant <= numberOfFormants; iformant++) { |
| 70 | const double frequency = fb -> z [2 * iformant - 1] [iframe]; |
| 71 | const double bandWidth = fb -> z [2 * iformant ] [iframe]; |
| 72 | if (frequency > 0 && bandWidth > 0) { |
| 73 | iformantto ++; |
| 74 | frame -> formant [iformantto]. frequency = frequency; |
| 75 | frame -> formant [iformantto]. bandwidth = bandWidth; |
| 76 | } else |
| 77 | frame -> formant [iformant]. frequency = frame -> formant [iformant]. bandwidth = 0.0; |
| 78 | } |
| 79 | /* |
| 80 | Shift the (higher) formants down if necessary. |
| 81 | */ |
| 82 | for (integer iformant = formantmax + 1; iformant <= frame -> numberOfFormants; iformant ++) { |
| 83 | const double frequency = fb -> z [2 * iformant - 1] [iframe]; |
| 84 | const double bandWidth = fb -> z [2 * iformant ] [iframe]; |
| 85 | if (frequency > 0 && bandWidth > 0) { |
| 86 | iformantto ++; |
| 87 | frame -> formant [iformantto]. frequency = frequency; |
| 88 | frame -> formant [iformantto]. bandwidth = bandWidth; |
| 89 | } else |
| 90 | frame -> formant [iformant]. frequency = frame -> formant [iformant]. bandwidth = 0.0; |
| 91 | } |
| 92 | frame -> numberOfFormants = iformantto; |
| 93 | } |
| 94 | } catch (MelderError) { |
| 95 | Melder_throw (me, U": not filtered."); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | HTK parameter files have (almost) no signature to recognize them except their *.fb file extension. |
| 101 | We can only read them and hope for the best. |
| 102 | */ |
| 103 | autoFormant Formant_readFromHTKParameterFile (MelderFile file) { |
| 104 | try { |
| 105 | autofile f = Melder_fopen (file, "rb"); |
| 106 | const integer numberOfFrames = bingetinteger32BE (f); |
| 107 | const integer samplePeriodTimes100ns = bingetinteger32BE (f); |
| 108 | /* |
| 109 | For the VTR HTK parameter formant files of Deng et al., this |
| 110 | number is 10000. According to the HTK file specification this is in units |
| 111 | of 100 ns which result in 10000 /(1000000000 /100) = 1 / 1000 = 1 ms. |
| 112 | What does this number mean? Are they a factor 10 off if they used a sampling |
| 113 | frequency of 10 kHz? |
| 114 | */ |
| 115 | const integer frameSize = bingetinteger16BE (f); |
| 116 | Melder_require (frameSize % 8 == 0, |
| 117 | U"The vector size in bytes needs to be divisible by 8."); |
| 118 | const integer htkType = bingetinteger16BE (f); |
| 119 | Melder_require (htkType == 9, // user defined type |
| 120 | U"The HTK type ID (", htkType, U") of the file needs to be 9."); |
| 121 | const integer numberOfFormants = frameSize / 8; // r32, formants + bandwidths |
| 122 | const double tmin = 0.0, timeStep = 0.01; |
| 123 | const double tmax = numberOfFrames * timeStep; // the best guess we can make |
| 124 | autoFormant me = Formant_create (tmin, tmax, numberOfFrames, timeStep, timeStep, numberOfFormants); |
| 125 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 126 | const Formant_Frame ffi = & my frames [iframe]; |
| 127 | ffi -> formant = newvectorzero <structFormant_Formant> (numberOfFormants); |
| 128 | ffi -> numberOfFormants = numberOfFormants; |
| 129 | for (integer inum = 1; inum <= numberOfFormants; inum ++) |
| 130 | ffi -> formant [inum].frequency = bingetr32 (f) * 1000.0; |
| 131 | for (integer inum = 1; inum <= numberOfFormants; inum ++) |
| 132 | ffi -> formant [inum].bandwidth = bingetr32 (f) * 1000.0; |
| 133 | } |
| 134 | f.close (file); |
| 135 | return me; |
| 136 | } catch (MelderError) { |
| 137 | Melder_throw (U"Formant not read from HTK parameter file ", MelderFile_messageName (file), U"."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | autoVEC Formant_listFormantSlope (Formant me, integer iformant, double tmin, double tmax, kSlopeCurve curveType) { |
| 142 | integer itmin, itmax; |
| 143 | Function_bidirectionalAutowindow (me, & tmin, & tmax); |
| 144 | Melder_require (Function_intersectRangeWithDomain (me, & tmin, & tmax), |
| 145 | U"The requested time range should be within the domain of the Formant."); |
| 146 | integer numberOfParameters = 3, numberOfSlopeParameters = 7; |
| 147 | kDataModelerFunction modelerFunctionType; |
| 148 | if (curveType == kSlopeCurve::PARABOLIC) |
| 149 | modelerFunctionType = kDataModelerFunction::POLYNOME; |
| 150 | else if (curveType == kSlopeCurve::EXPONENTIAL_PLUS_CONSTANT) |
| 151 | modelerFunctionType = kDataModelerFunction::EXPONENTIAL_PLUS_CONSTANT; |
| 152 | else if (curveType == kSlopeCurve::SIGMOID_PLUS_CONSTANT) { |
| 153 | modelerFunctionType = kDataModelerFunction::SIGMOID_PLUS_CONSTANT; |
| 154 | numberOfParameters = 4; |
| 155 | numberOfSlopeParameters = 8; |
| 156 | } |
| 157 | autoVEC fitResults = raw_VEC (numberOfSlopeParameters); |
| 158 | fitResults.get() <<= undefined; |
| 159 | const integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax); |
| 160 | if (numberOfFrames < numberOfParameters) |
| 161 | return fitResults; |
| 162 | autoDataModeler dm = DataModeler_create (tmin, tmax, numberOfFrames, numberOfParameters, modelerFunctionType); |
| 163 | integer numberOfDataPoints = 0; |
| 164 | for (integer iframe = itmin; iframe <= itmax; iframe ++) { |
| 165 | const Formant_Frame frame = & my frames [iframe]; |
| 166 | const integer numberOfFormants = frame -> numberOfFormants; |
| 167 | const double frequency = frame -> formant [iformant]. frequency; |
| 168 | const double bandwidth = frame -> formant [iformant]. bandwidth; |
| 169 | if (iformant > numberOfFormants || ! isdefined (frequency) || ! isdefined (bandwidth)) |
| 170 | continue; |
| 171 | dm -> data [++ numberOfDataPoints].x = Sampled_indexToX (me, iframe); |
| 172 | dm -> data [numberOfDataPoints].y = frequency; |
| 173 | dm -> data [numberOfDataPoints] .sigmaY = bandwidth; |
| 174 | dm -> data [numberOfDataPoints] .status = kDataModelerData::VALID; |
| 175 | } |
| 176 | Melder_require (numberOfDataPoints >= numberOfParameters, |
| 177 | U"Not enough valid data points."); |
| 178 | if (numberOfDataPoints != numberOfFrames) { |
| 179 | dm -> data.resize (numberOfDataPoints); |
| 180 | dm -> numberOfDataPoints = numberOfDataPoints; |
| 181 | } |
| 182 | DataModeler_fit (dm.get()); |
| 183 | autoVEC parameters = DataModeler_listParameterValues (dm.get()); |
| 184 | const double flocus = DataModeler_getModelValueAtX (dm.get(), tmin); |
| 185 | const double ftarget = DataModeler_getModelValueAtX (dm.get(), tmax); |
| 186 | const double rSquared = DataModeler_getCoefficientOfDetermination (dm.get(), nullptr, nullptr); |
| 187 | const double averageSlope = (ftarget - flocus) / (tmax - tmin); |
| 188 | fitResults [1] = averageSlope; |
| 189 | fitResults [2] = rSquared; |
| 190 | fitResults [3] = flocus; |
| 191 | fitResults [4] = ftarget; |
| 192 | fitResults.part (5, 5 + numberOfParameters - 1) <<= parameters.get(); |
| 193 | return fitResults; |
| 194 | } |
| 195 | |
| 196 | autoIntensityTier Formant_Spectrogram_to_IntensityTier (Formant me, Spectrogram thee, integer iformant) { |
| 197 | try { |
| 198 | Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 199 | U"The start and end times of the Formant and the Spectrogram should be equal."); |
| 200 | Melder_require (iformant > 0 && iformant <= my maxnFormants, |
| 201 | U"Formant number should be in the range [1, ", my maxnFormants, U"]."); |
| 202 | autoIntensityTier him = IntensityTier_create (my xmin, my xmax); |
| 203 | double previousValue_dB = -80000.0; // can never occur |
| 204 | double previousTime = my xmin; |
| 205 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 206 | const Formant_Frame frame = & my frames [iframe]; |
| 207 | const integer numberOfFormants = frame -> numberOfFormants; |
| 208 | const double time = Sampled_indexToX (me, iframe); |
| 209 | double value = 0.0; |
| 210 | if (iformant <= numberOfFormants) { |
| 211 | const double f = frame -> formant [iformant]. frequency; |
| 212 | value = Matrix_getValueAtXY (thee, time, f); |
| 213 | value = isdefined (value) ? value : 0.0; |
| 214 | } |
| 215 | const double value_dB = 10.0 * log10 ((value + 1e-30) / 4.0e-10); /* dB / Hz */ |
| 216 | if (value_dB != previousValue_dB) { |
| 217 | if (iframe > 1 && previousTime < time - 1.5 * my dx) // mark the end of the same interval |
| 218 | RealTier_addPoint (him.get(), time - my dx, previousValue_dB); |
| 219 | RealTier_addPoint (him.get(), time, value_dB); |
| 220 | previousTime = time; |
| 221 | } |
| 222 | previousValue_dB = value_dB; |
| 223 | } |
| 224 | return him; |
| 225 | } catch (MelderError) { |
| 226 | Melder_throw (U"IntensityTier not created from ", me, U" and ", thee, U"."); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | autoFormant Formant_extractPart (Formant me, double tmin, double tmax) { |
| 231 | try { |
| 232 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 233 | Melder_require (tmin < my xmax && tmax > my xmin, |
| 234 | U"Your start and end time should be between ", my xmin, U" and ", my xmax, U"."); |
| 235 | integer ifmin, ifmax; |
| 236 | const integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax); |
| 237 | const double t1 = Sampled_indexToX (me, ifmin); |
| 238 | autoFormant thee = Formant_create (tmin, tmax, numberOfFrames, my dx, t1, my maxnFormants); |
| 239 | for (integer iframe = ifmin; iframe <= ifmax; iframe ++) { |
| 240 | const Formant_Frame myFrame = & my frames [iframe]; |
| 241 | const Formant_Frame thyFrame = & thy frames [iframe - ifmin + 1]; |
| 242 | myFrame -> copy (thyFrame); |
| 243 | } |
| 244 | return thee; |
| 245 | } catch (MelderError) { |
| 246 | Melder_throw (U"Formant part could not be extracted."); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* End of file Formant_extensions.cpp */ |