| 1 | /* Cepstrumc.c |
| 2 | * |
| 3 | * Copyright (C) 1994-2020 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 20020812 GPL header |
| 21 | djmw 20061218 Changed info to Melder_writeLine<x> format. |
| 22 | djmw 20071017 oo_CAN_WRITE_AS_ENCODING.h |
| 23 | djmw 20080122 Version 1: float -> double |
| 24 | djmw 20110304 Thing_new |
| 25 | */ |
| 26 | |
| 27 | #include "Cepstrumc.h"cxx . . $ `<file>/LPC/Cepstrumc.h`/ |
| 28 | #include "DTW.h"cxx . . $ `<file>/dwtools/DTW.h`/ |
| 29 | |
| 30 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 31 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 32 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 33 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 34 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 35 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 36 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 37 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 38 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 39 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 40 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 41 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 42 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 43 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 44 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 45 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 46 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 47 | #include "Cepstrumc_def.h"cxx . . $ `<file>/LPC/Cepstrumc_def.h`/ |
| 48 | |
| 49 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Cepstrumc, Sampled, 1); |
| 50 | |
| 51 | void structCepstrumc :: v1_info () { |
| 52 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 53 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" Start time: ", xmincxx . . $ structFunction#xmin.); |
| 54 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" End time: ", xmaxcxx . . $ structFunction#xmax.); |
| 55 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" Number of frames: ", nxcxx . . $ structSampled#nx.); |
| 56 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" Time step: ", dxcxx . . $ structSampled#dx.); |
| 57 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" First frame at: ", x1cxx . . $ structSampled#x1.); |
| 58 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U" Number of coefficients: ", maxnCoefficients); |
| 59 | } |
| 60 | |
| 61 | void Cepstrumc_Frame_initcxx . . $ Cepstrumc_Frame_init(b4d5f83352c0fd7d). (Cepstrumc_Frame me, integercxx . . $ integer# nCoefficients) { |
| 62 | mycxx . . $ `melder/melder.h:44:9`! c =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (nCoefficients); |
| 63 | mycxx . . $ `melder/melder.h:44:9`! nCoefficients = mycxx . . $ `melder/melder.h:44:9`! c.sizecxx . . $ autovector#size.; // maintain invariant |
| 64 | } |
| 65 | |
| 66 | void Cepstrumc_initcxx . . $ Cepstrumc_init(391c24fe3acf54a9). (Cepstrumc me, double tmin, double tmax, integercxx . . $ integer# nt, double dt, double t1, integercxx . . $ integer# nCoefficients, double samplingFrequency) { |
| 67 | mycxx . . $ `melder/melder.h:44:9`! samplingFrequency = samplingFrequency; |
| 68 | mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients = nCoefficients; |
| 69 | Sampled_initcxx . . $ Sampled_init(5b6f70c633f31208). (me, tmin, tmax, nt, dt, t1); |
| 70 | mycxx . . $ `melder/melder.h:44:9`! frame =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <structCepstrumc_Frame> (nt); |
| 71 | } |
| 72 | |
| 73 | autoCepstrumc Cepstrumc_createcxx . . $ Cepstrumc_create(3ada163742ca27ec). (double tmin, double tmax, integercxx . . $ integer# nt, double dt, double t1, integercxx . . $ integer# nCoefficients, double samplingFrequency) { |
| 74 | try { |
| 75 | autoCepstrumc me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Cepstrumc); |
| 76 | Cepstrumc_initcxx . . $ Cepstrumc_init(391c24fe3acf54a9). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, nt, dt, t1, nCoefficients, samplingFrequency); |
| 77 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 78 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 79 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cepstrum not created."); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void regressioncxx . . $ regression(6764478d5bab528d). (VECcxx . . $ VEC# const& r, Cepstrumc me, integercxx . . $ integer# frameNumber, integercxx . . $ integer# numberOfRegressionFrames) { |
| 84 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (r.size == mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients + 1); |
| 85 | rcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0; |
| 86 | |
| 87 | if (frameNumber <= numberOfRegressionFrames / 2 || frameNumber > mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - numberOfRegressionFrames / 2) |
| 88 | return; |
| 89 | integercxx . . $ integer# nc = INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!; |
| 90 | longdoublecxx . . $ longdouble# sumsq = 0.0; |
| 91 | for (integercxx . . $ integer# j = -numberOfRegressionFrames / 2; j <= numberOfRegressionFrames / 2; j ++) { |
| 92 | const Cepstrumc_Frame f = & mycxx . . $ `melder/melder.h:44:9`! frame [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).frameNumber + j]; |
| 93 | if (f -> nCoefficients < nc) |
| 94 | nc = f -> nCoefficients; |
| 95 | sumsq += j * j; |
| 96 | } |
| 97 | for (integercxx . . $ integer# i = 0; i <= nc; i ++) { |
| 98 | for (integercxx . . $ integer# j = -numberOfRegressionFrames / 2; j <= numberOfRegressionFrames / 2; j ++) { |
| 99 | const Cepstrumc_Frame f = & mycxx . . $ `melder/melder.h:44:9`! frame [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).frameNumber + j]; |
| 100 | r [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i + 1] += f -> c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * j / (double (sumsq) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | autoDTWcxx . . $ autoDTW# Cepstrumc_to_DTWcxx . . $ Cepstrumc_to_DTW(2430c5c1d0416da4). (Cepstrumc me, Cepstrumc thee, double wc, double wle, double wr, double wer, double dtr, int matchStart, int matchEnd, int constraint) { |
| 106 | try { |
| 107 | integercxx . . $ integer# numberOfRegressionFrames = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (dtr / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 108 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients == thycxx . . $ `melder/melder.h:45:9`! maxnCoefficients, |
| 109 | U"Cepstrumc orders should be equal."); |
| 110 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (! (wr != 0.0 && numberOfRegressionFrames < 2), |
| 111 | U"Time window for regression coefficients too small."); |
| 112 | if (numberOfRegressionFrames % 2 == 0) |
| 113 | numberOfRegressionFrames += 1; |
| 114 | if (wr != 0.0) |
| 115 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Number of frames used for regression coefficients ", numberOfRegressionFrames); |
| 116 | autoDTWcxx . . $ autoDTW# him = DTW_createcxx . . $ DTW_create(266b4c6b39481820). (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., thycxx . . $ `melder/melder.h:45:9`! xmincxx . . $ structFunction#xmin., thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax., thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx., thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx., thycxx . . $ `melder/melder.h:45:9`! x1cxx . . $ structSampled#x1.); |
| 117 | autoVECcxx . . $ autoVEC# ri = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients + 1); |
| 118 | autoVECcxx . . $ autoVEC# rj = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients + 1); |
| 119 | /* |
| 120 | Calculate distance matrix. |
| 121 | */ |
| 122 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U""); |
| 123 | for (integercxx . . $ integer# iframe = 1; iframe <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) { |
| 124 | const Cepstrumc_Frame fi = & mycxx . . $ `melder/melder.h:44:9`! frame [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 125 | regressioncxx . . $ regression(6764478d5bab528d). (ri.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), me, iframe, numberOfRegressionFrames); |
| 126 | for (integercxx . . $ integer# jframe = 1; jframe <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; jframe ++) { |
| 127 | const Cepstrumc_Frame fj = & thycxx . . $ `melder/melder.h:45:9`! frame [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).jframe]; |
| 128 | longdoublecxx . . $ longdouble# dist = 0.0, distr = 0.0; |
| 129 | /* |
| 130 | Cepstral distance. |
| 131 | */ |
| 132 | if (wc != 0.0) { |
| 133 | for (integercxx . . $ integer# k = 1; k <= fj -> nCoefficients; k ++) { |
| 134 | const double dci = fi -> c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] - fj -> c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]; |
| 135 | dist += dci * dci; |
| 136 | } |
| 137 | dist *= wc; |
| 138 | } |
| 139 | /* |
| 140 | Log energy distance. |
| 141 | */ |
| 142 | const double dc0 = fi -> c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).0] - fj -> c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).0]; |
| 143 | dist += wle * dc0 * dc0; |
| 144 | /* |
| 145 | Regression distance. |
| 146 | */ |
| 147 | if (wr != 0.0) { |
| 148 | regressioncxx . . $ regression(6764478d5bab528d). (rj.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), thee, jframe, numberOfRegressionFrames); |
| 149 | for (integercxx . . $ integer# k = 1; k <= fj -> nCoefficients; k ++) { |
| 150 | const double drci = ri [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k + 1] - rj [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k + 1]; |
| 151 | distr += drci * drci; |
| 152 | } |
| 153 | dist += wr * distr; |
| 154 | } |
| 155 | /* |
| 156 | Regression on c [0]: log(energy) |
| 157 | */ |
| 158 | if (wer != 0.0) { |
| 159 | if (wr == 0.0) |
| 160 | regressioncxx . . $ regression(6764478d5bab528d). (rj.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), thee, jframe, numberOfRegressionFrames); |
| 161 | const double drc0 = ri [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] - rj [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 162 | dist += wer * drc0 * drc0; |
| 163 | } |
| 164 | dist /= wc + wle + wr + wer; |
| 165 | hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iframe] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).jframe] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). ((double) dist); // prototype along y-direction |
| 166 | } |
| 167 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ((double) iframe / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., U"Calculate distances: frame ", iframe, U" from ", mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., U"."); |
| 168 | } |
| 169 | DTW_findPathcxx . . $ DTW_findPath(7c4c07b8524d5375). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), matchStart, matchEnd, constraint); |
| 170 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 171 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 172 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"DTW not created."); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | autoMatrixcxx . . $ autoMatrix# Cepstrumc_to_Matrixcxx . . $ Cepstrumc_to_Matrix(aaf97273cc18fe63). (Cepstrumc me) { |
| 177 | try { |
| 178 | autoMatrixcxx . . $ autoMatrix# thee = Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (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., 0, mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients, mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients + 1, 1, 0); |
| 179 | |
| 180 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 181 | const Cepstrumc_Frame him = & mycxx . . $ `melder/melder.h:44:9`! frame [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 182 | for (integercxx . . $ integer# j = 1; j <= hiscxx . . $ `melder/melder.h:47:9`! nCoefficients + 1; j ++) |
| 183 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = hiscxx . . $ `melder/melder.h:47:9`! c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - 1]; |
| 184 | } |
| 185 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 186 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 187 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Matrix created."); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* End of file Cepstrumc.cpp */ |