| 1 | /* FormantTier.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2007,2011,2012,2014-2020 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 2006/07/21 made Sound_FormantTier_filter_inplace () accurate for higher numbers of formants |
| 22 | * pb 2007/01/27 made compatible with stereo sounds |
| 23 | * pb 2007/03/17 domain quantity |
| 24 | * pb 2007/10/01 can write as encoding |
| 25 | * pb 2011/03/01 moved Formant filtering to FormantGrid (reimplemented) |
| 26 | * pb 2011/05/26 C++ |
| 27 | */ |
| 28 | |
| 29 | #include "FormantTier.h"cxx . . $ `<file>/fon/FormantTier.h`/ |
| 30 | #include "AnyTier.h"cxx . . $ `<file>/fon/AnyTier.h`/ |
| 31 | |
| 32 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 33 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 34 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 35 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 36 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 37 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 38 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 39 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 40 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 41 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 42 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 43 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 44 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 45 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 46 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 47 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 48 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 49 | #include "FormantTier_def.h"cxx . . $ `<file>/fon/FormantTier_def.h`/ |
| 50 | |
| 51 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (FormantPoint, Daata, 0); |
| 52 | |
| 53 | autoFormantPointcxx . . $ autoFormantPoint# FormantPoint_createcxx . . $ FormantPoint_create(5e09653330a9b86a). (double time, integercxx . . $ integer# numberOfFormants) { |
| 54 | try { |
| 55 | autoFormantPointcxx . . $ autoFormantPoint# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (FormantPoint); |
| 56 | mycxx . . $ `melder/melder.h:44:9`! numbercxx . . $ structSimpleDouble#number. = time; |
| 57 | mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. = numberOfFormants; |
| 58 | mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormantPoint#formant. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfFormants); |
| 59 | mycxx . . $ `melder/melder.h:44:9`! bandwidthcxx . . $ structFormantPoint#bandwidth. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfFormants); |
| 60 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 61 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 62 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Formant point not created."); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (FormantTier, AnyTier, 0); |
| 67 | |
| 68 | autoFormantTiercxx . . $ autoFormantTier# FormantTier_createcxx . . $ FormantTier_create(b73b3e0655b20178). (double tmin, double tmax) { |
| 69 | try { |
| 70 | autoFormantTiercxx . . $ autoFormantTier# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (FormantTier); |
| 71 | mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = tmin; |
| 72 | mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = tmax; |
| 73 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 74 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 75 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"FormantTier not created."); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | double FormantTier_getValueAtTimecxx . . $ FormantTier_getValueAtTime(d2df9a2b10de51bb). (FormantTiercxx . . $ FormantTier# me, integercxx . . $ integer# iformant, double t) { |
| 80 | integercxx . . $ integer# n = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.; |
| 81 | if (n == 0 || iformant < 1) |
| 82 | return undefinedcxx . . $ undefined.; |
| 83 | FormantPointcxx . . $ FormantPoint# pointRight = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]; |
| 84 | if (t <= pointRight -> numbercxx . . $ structSimpleDouble#number.) { |
| 85 | if (iformant > pointRight -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.) |
| 86 | return undefinedcxx . . $ undefined.; |
| 87 | return pointRight -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; // constant extrapolation |
| 88 | } |
| 89 | FormantPointcxx . . $ FormantPoint# pointLeft = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).n]; |
| 90 | if (t >= pointLeft -> numbercxx . . $ structSimpleDouble#number.) { |
| 91 | if (iformant > pointLeft -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.) |
| 92 | return undefinedcxx . . $ undefined.; |
| 93 | return pointLeft -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; // constant extrapolation |
| 94 | } |
| 95 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 2); |
| 96 | integercxx . . $ integer# ileft = AnyTier_timeToLowIndexcxx . . $ AnyTier_timeToLowIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structFormantTier#asAnyTier(2fecc017ee6dfbab).(), t), iright = ileft + 1; |
| 97 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (ileft >= 1 && iright <= n); |
| 98 | pointLeft = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ileft]; |
| 99 | pointRight = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).iright]; |
| 100 | const double tleft = pointLeft -> numbercxx . . $ structSimpleDouble#number.; |
| 101 | const double fleft = ( iformant > pointLeft -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. ? undefinedcxx . . $ undefined. : pointLeft -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant] ); |
| 102 | const double tright = pointRight -> numbercxx . . $ structSimpleDouble#number.; |
| 103 | const double fright = ( iformant > pointRight -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. ? undefinedcxx . . $ undefined. : pointRight -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant] ); |
| 104 | return isundefcxx . . $ isundef(f8197d536af80c96). (fleft) ? ( isundefcxx . . $ isundef(f8197d536af80c96). (fright) ? undefinedcxx . . $ undefined. : fright ) |
| 105 | : isundefcxx . . $ isundef(f8197d536af80c96). (fright) ? fleft |
| 106 | : t == tright ? fright // be very accurate |
| 107 | : tleft == tright ? 0.5 * (fleft + fright) // unusual, but possible; no preference |
| 108 | : fleft + (t - tleft) * (fright - fleft) / (tright - tleft); // linear interpolation |
| 109 | } |
| 110 | |
| 111 | double FormantTier_getBandwidthAtTimecxx . . $ FormantTier_getBandwidthAtTime(d2df9a2b10de51bb). (FormantTiercxx . . $ FormantTier# me, integercxx . . $ integer# iformant, double t) { |
| 112 | const integercxx . . $ integer# n = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.; |
| 113 | if (n == 0 || iformant < 1) |
| 114 | return undefinedcxx . . $ undefined.; |
| 115 | FormantPointcxx . . $ FormantPoint# pointRight = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1]; |
| 116 | if (t <= pointRight -> numbercxx . . $ structSimpleDouble#number.) { |
| 117 | if (iformant > pointRight -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.) |
| 118 | return undefinedcxx . . $ undefined.; |
| 119 | return pointRight -> bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; // constant extrapolation |
| 120 | } |
| 121 | FormantPointcxx . . $ FormantPoint# pointLeft = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).n]; |
| 122 | if (t >= pointLeft -> numbercxx . . $ structSimpleDouble#number.) { |
| 123 | if (iformant > pointLeft -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.) |
| 124 | return undefinedcxx . . $ undefined.; |
| 125 | return pointLeft -> bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; // constant extrapolation |
| 126 | } |
| 127 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 2); |
| 128 | const integercxx . . $ integer# ileft = AnyTier_timeToLowIndexcxx . . $ AnyTier_timeToLowIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structFormantTier#asAnyTier(2fecc017ee6dfbab).(), t), iright = ileft + 1; |
| 129 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (ileft >= 1 && iright <= n); |
| 130 | pointLeft = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ileft]; |
| 131 | pointRight = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).iright]; |
| 132 | const double tleft = pointLeft -> numbercxx . . $ structSimpleDouble#number.; |
| 133 | const double fleft = iformant > pointLeft -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. ? undefinedcxx . . $ undefined. : pointLeft -> bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; |
| 134 | const double tright = pointRight -> numbercxx . . $ structSimpleDouble#number.; |
| 135 | const double fright = iformant > pointRight -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. ? undefinedcxx . . $ undefined. : pointRight -> bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; |
| 136 | return isundefcxx . . $ isundef(f8197d536af80c96). (fleft) ? ( isundefcxx . . $ isundef(f8197d536af80c96). (fright) ? undefinedcxx . . $ undefined. : fright ) |
| 137 | : isundefcxx . . $ isundef(f8197d536af80c96). (fright) ? fleft |
| 138 | : t == tright ? fright // be very accurate |
| 139 | : tleft == tright ? 0.5 * (fleft + fright) // unusual, but possible; no preference |
| 140 | : fleft + (t - tleft) * (fright - fleft) / (tright - tleft); // linear interpolation |
| 141 | } |
| 142 | |
| 143 | void FormantTier_specklecxx . . $ FormantTier_speckle(c3f00363db535ed2). (FormantTiercxx . . $ FormantTier# me, Graphicscxx . . $ Graphics# g, double tmin, double tmax, double fmax, bool garnish) { |
| 144 | Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & tmin, & tmax); |
| 145 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, tmin, tmax, 0.0, fmax); |
| 146 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 147 | const integercxx . . $ integer# imin = AnyTier_timeToHighIndexcxx . . $ AnyTier_timeToHighIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structFormantTier#asAnyTier(2fecc017ee6dfbab).(), tmin); |
| 148 | const integercxx . . $ integer# imax = AnyTier_timeToLowIndexcxx . . $ AnyTier_timeToLowIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structFormantTier#asAnyTier(2fecc017ee6dfbab).(), tmax); |
| 149 | if (imin > 0) for (integercxx . . $ integer# i = imin; i <= imax; i ++) { |
| 150 | FormantPointcxx . . $ FormantPoint# point = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 151 | const double t = point -> numbercxx . . $ structSimpleDouble#number.; |
| 152 | for (integercxx . . $ integer# j = 1; j <= point -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.; j ++) { |
| 153 | const double f = point -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]; |
| 154 | if (f <= fmax) |
| 155 | Graphics_specklecxx . . $ Graphics_speckle(c84ba9158791aef6). (g, t, f); |
| 156 | } |
| 157 | } |
| 158 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 159 | if (garnish) { |
| 160 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 161 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Time (s)"); |
| 162 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 163 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 164 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Frequency (Hz)"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | autoFormantTiercxx . . $ autoFormantTier# Formant_downto_FormantTiercxx . . $ Formant_downto_FormantTier(1efa5204d1003a05). (Formantcxx . . $ Formant# me) { |
| 169 | try { |
| 170 | autoFormantTiercxx . . $ autoFormantTier# thee = FormantTier_createcxx . . $ FormantTier_create(b73b3e0655b20178). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 171 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 172 | const Formant_Framecxx . . $ Formant_Frame# frame = & mycxx . . $ `melder/melder.h:44:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 173 | /* mutable move */ autoFormantPointcxx . . $ autoFormantPoint# point = FormantPoint_createcxx . . $ FormantPoint_create(5e09653330a9b86a). (Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, i), frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.); |
| 174 | for (integercxx . . $ integer# j = 1; j <= frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; j ++) { |
| 175 | Formant_Formantcxx . . $ Formant_Formant# pair = & frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]; |
| 176 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pair -> frequencycxx . . $ structFormant_Formant#frequency.; |
| 177 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = pair -> bandwidthcxx . . $ structFormant_Formant#bandwidth.; |
| 178 | } |
| 179 | thycxx . . $ `melder/melder.h:45:9`! pointscxx . . $ structFormantTier#points.. addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (pointcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 180 | } |
| 181 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 182 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 183 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to FormantTier."); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | autoFormantTiercxx . . $ autoFormantTier# Formant_PointProcess_to_FormantTiercxx . . $ Formant_PointProcess_to_FormantTier(99f868c104dfe4d3). (Formantcxx . . $ Formant# me, PointProcesscxx . . $ PointProcess# pp) { |
| 188 | try { |
| 189 | const integercxx . . $ integer# maximumNumberOfFormants = Formant_getMaxNumFormantscxx . . $ Formant_getMaxNumFormants(6a0b146f5923ed8d). (me); |
| 190 | const autoFormantTiercxx . . $ autoFormantTier# temp = Formant_downto_FormantTiercxx . . $ Formant_downto_FormantTier(1efa5204d1003a05). (me); |
| 191 | /* mutable return */ autoFormantTiercxx . . $ autoFormantTier# thee = FormantTier_createcxx . . $ FormantTier_create(b73b3e0655b20178). (pp -> xmincxx . . $ structFunction#xmin., pp -> xmaxcxx . . $ structFunction#xmax.); |
| 192 | for (integercxx . . $ integer# ipoint = 1; ipoint <= pp -> ntcxx . . $ structPointProcess#nt.; ipoint ++) { |
| 193 | const double time = pp -> tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ipoint]; |
| 194 | /* mutable move */ autoFormantPointcxx . . $ autoFormantPoint# point = FormantPoint_createcxx . . $ FormantPoint_create(5e09653330a9b86a). (time, maximumNumberOfFormants); |
| 195 | /* mutable loop */ integercxx . . $ integer# iformant = 1; |
| 196 | for (; iformant <= maximumNumberOfFormants; iformant ++) { |
| 197 | const double frequency = FormantTier_getValueAtTimecxx . . $ FormantTier_getValueAtTime(d2df9a2b10de51bb). (temp.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iformant, time); |
| 198 | if (isundefcxx . . $ isundef(f8197d536af80c96). (frequency)) |
| 199 | break; |
| 200 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant] = frequency; |
| 201 | const double bandwidth = FormantTier_getBandwidthAtTimecxx . . $ FormantTier_getBandwidthAtTime(d2df9a2b10de51bb). (temp.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iformant, time); |
| 202 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (isdefined (bandwidth)); |
| 203 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant] = bandwidth; |
| 204 | } |
| 205 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. = iformant - 1; |
| 206 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). formantcxx . . $ structFormantPoint#formant.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.); // maintain invariant |
| 207 | point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). bandwidthcxx . . $ structFormantPoint#bandwidth.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (point ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.); // maintain invariant |
| 208 | thycxx . . $ `melder/melder.h:45:9`! pointscxx . . $ structFormantTier#points.. addItem_movecxx . . $ CollectionOf#addItem_move(bcc0cff787be24fe). (pointcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 209 | } |
| 210 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 211 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 212 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", pp, U": not converted to FormantTier."); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | integercxx . . $ integer# FormantTier_getMinNumFormantscxx . . $ FormantTier_getMinNumFormants(5af45cf6f9dd7399). (FormantTiercxx . . $ FormantTier# me) { |
| 217 | integercxx . . $ integer# minNumFormants = INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!; |
| 218 | for (integercxx . . $ integer# ipoint = 1; ipoint <= mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 219 | const FormantPointcxx . . $ FormantPoint# point = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint]; |
| 220 | if (point -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. < minNumFormants) |
| 221 | minNumFormants = point -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.; |
| 222 | } |
| 223 | return minNumFormants; |
| 224 | } |
| 225 | |
| 226 | integercxx . . $ integer# FormantTier_getMaxNumFormantscxx . . $ FormantTier_getMaxNumFormants(5af45cf6f9dd7399). (FormantTiercxx . . $ FormantTier# me) { |
| 227 | integercxx . . $ integer# maxNumFormants = 0; |
| 228 | for (integercxx . . $ integer# ipoint = 1; ipoint <= mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 229 | const FormantPointcxx . . $ FormantPoint# point = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint]; |
| 230 | if (point -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants. > maxNumFormants) |
| 231 | maxNumFormants = point -> numberOfFormantscxx . . $ structFormantPoint#numberOfFormants.; |
| 232 | } |
| 233 | return maxNumFormants; |
| 234 | } |
| 235 | |
| 236 | autoTableOfRealcxx . . $ autoTableOfReal# FormantTier_downto_TableOfRealcxx . . $ FormantTier_downto_TableOfReal(2d8e02cf9156e14c). (FormantTiercxx . . $ FormantTier# me, bool includeFormants, bool includeBandwidths) { |
| 237 | try { |
| 238 | const integercxx . . $ integer# maximumNumberOfFormants = FormantTier_getMaxNumFormantscxx . . $ FormantTier_getMaxNumFormants(5af45cf6f9dd7399). (me); |
| 239 | /* mutable return */ autoTableOfRealcxx . . $ autoTableOfReal# thee = TableOfReal_createcxx . . $ TableOfReal_create(e573ed3514e79d3). (mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size., 1 + |
| 240 | ( includeFormants ? maximumNumberOfFormants : 0 ) + |
| 241 | ( includeBandwidths ? maximumNumberOfFormants : 0 )); |
| 242 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1, U"Time"); |
| 243 | for (integercxx . . $ integer# icol = 1, iformant = 1; iformant <= maximumNumberOfFormants; iformant ++) { |
| 244 | char32cxx . . $ char32# label [4]; |
| 245 | if (includeFormants) { |
| 246 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (label,4, U"F", iformant); |
| 247 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ++ icol, label); |
| 248 | } |
| 249 | if (includeBandwidths) { |
| 250 | Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (label,4, U"B", iformant); |
| 251 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ++ icol, label); |
| 252 | } |
| 253 | } |
| 254 | for (integercxx . . $ integer# ipoint = 1; ipoint <= mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 255 | const FormantPointcxx . . $ FormantPoint# point = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structFormantTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint]; |
| 256 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ipoint] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = point -> numbercxx . . $ structSimpleDouble#number.; |
| 257 | for (integercxx . . $ integer# icol = 1, iformant = 1; iformant <= maximumNumberOfFormants; iformant ++) { |
| 258 | if (includeFormants) |
| 259 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ipoint] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).++ icol] = point -> formantcxx . . $ structFormantPoint#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; |
| 260 | if (includeBandwidths) |
| 261 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ipoint] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).++ icol] = point -> bandwidthcxx . . $ structFormantPoint#bandwidth. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]; |
| 262 | } |
| 263 | } |
| 264 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 265 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 266 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to TableOfReal."); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void Sound_FormantTier_filter_inplacecxx . . $ Sound_FormantTier_filter_inplace(ce3ec5621281cb79). (Soundcxx . . $ Sound# me, FormantTiercxx . . $ FormantTier# formantTier) { |
| 271 | const double dt = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 272 | const integercxx . . $ integer# maximumNumberOfFormants = FormantTier_getMaxNumFormantscxx . . $ FormantTier_getMaxNumFormants(5af45cf6f9dd7399). (formantTier); |
| 273 | if (formantTier -> pointscxx . . $ structFormantTier#points..sizecxx . . $ CollectionOf#size.) for (integercxx . . $ integer# iformant = 1; iformant <= maximumNumberOfFormants; iformant ++) { |
| 274 | for (integercxx . . $ integer# isamp = 1; isamp <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; isamp ++) { |
| 275 | const double t = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + (isamp - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 276 | /* |
| 277 | Compute LP coefficients. |
| 278 | */ |
| 279 | const double formant = FormantTier_getValueAtTimecxx . . $ FormantTier_getValueAtTime(d2df9a2b10de51bb). (formantTier, iformant, t); |
| 280 | const double bandwidth = FormantTier_getBandwidthAtTimecxx . . $ FormantTier_getBandwidthAtTime(d2df9a2b10de51bb). (formantTier, iformant, t); |
| 281 | if (isdefinedcxx . . $ isdefined(f8197d536af80c96). (formant) && isdefinedcxx . . $ isdefined(f8197d536af80c96). (bandwidth)) { |
| 282 | const double cosomdt = coscxx . . $ cos(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * formant * dt); |
| 283 | const double r = expcxx . . $ exp(fee4f1d22bc8e617). (- NUMpicxx . . $ NUMpi. * bandwidth * dt); |
| 284 | /* Formants at 0 Hz or the Nyquist are single poles; others are double poles. */ |
| 285 | if (fabscxx . . $ fabs(fee4f1d22bc8e617). (cosomdt) > 0.999999) { // allow for round-off errors |
| 286 | /* single pole: D(z) = 1 - r z^-1 */ |
| 287 | for (integercxx . . $ integer# channel = 1; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) |
| 288 | if (isamp > 1) |
| 289 | mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] += r * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp - 1]; |
| 290 | } else { |
| 291 | /* double pole: D(z) = 1 + p z^-1 + q z^-2 */ |
| 292 | const double p = - 2.0 * r * cosomdt; |
| 293 | const double q = r * r; |
| 294 | for (integercxx . . $ integer# channel = 1; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) { |
| 295 | if (isamp > 1) |
| 296 | mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] -= p * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp - 1]; |
| 297 | if (isamp > 2) |
| 298 | mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] -= q * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp - 2]; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | autoSoundcxx . . $ autoSound# Sound_FormantTier_filtercxx . . $ Sound_FormantTier_filter(2b29efac89156950). (Soundcxx . . $ Sound# me, FormantTiercxx . . $ FormantTier# formantTier) { |
| 307 | try { |
| 308 | autoSoundcxx . . $ autoSound# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 309 | Sound_FormantTier_filter_inplacecxx . . $ Sound_FormantTier_filter_inplace(ce3ec5621281cb79). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), formantTier); |
| 310 | Vector_scalecxx . . $ Vector_scale(54f31cdb89d1acde). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.99); |
| 311 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 312 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 313 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not filtered with ", formantTier, U"."); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | autoSoundcxx . . $ autoSound# Sound_FormantTier_filter_noscalecxx . . $ Sound_FormantTier_filter_noscale(2b29efac89156950). (Soundcxx . . $ Sound# me, FormantTiercxx . . $ FormantTier# formantTier) { |
| 318 | try { |
| 319 | autoSoundcxx . . $ autoSound# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 320 | Sound_FormantTier_filter_inplacecxx . . $ Sound_FormantTier_filter_inplace(ce3ec5621281cb79). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), formantTier); |
| 321 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 322 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 323 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not filtered with ", formantTier, U"."); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* End of file FormantTier.cpp */ |