Debian Codegraph @

dwtools/MFCC.cpp

raw symbols SCIP
1 /* MFCC.cpp
2 *
3 * Mel Frequency Cepstral Coefficients class.
4 *
5 * Copyright (C) 1993-2017, 2026 David Weenink
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * djmw 20020813 GPL header
23 * djmw 20110304 Thing_new
24 */
25
26 #include "MFCC.h"
27 #include "Spectrogram_extensions.h"
28 #include "NUM2.h"
29
30
31 Thing_implement (MFCC, CC, 1);
32
33 void structMFCC :: v1_info () {
34 structCC :: v1_info ();
35 MelderInfo_writeLine (U"Minimum frequency: ", fmin, U" mel");
36 MelderInfo_writeLine (U"Maximum frequency: ", fmax, U" mel");
37 }
38
39 autoMFCC MFCC_create (double tmin, double tmax, integer nt, double dt, double t1, integer maximumNumberOfCoefficients, double fmin_mel, double fmax_mel) {
40 try {
41 autoMFCC me = Thing_new (MFCC);
42 CC_init (me.get(), tmin, tmax, nt, dt, t1, maximumNumberOfCoefficients, fmin_mel, fmax_mel);
43 return me;
44 } catch (MelderError) {
45 Melder_throw (U"MFCC not created.");
46 }
47 }
48
49 void MFCC_lifter (MFCC me, integer lifter) {
50 try {
51 Melder_assert (lifter > 0);
52 autoVEC c = zero_VEC (my maximumNumberOfCoefficients);
53 for (integer icoef = 1; icoef <= my maximumNumberOfCoefficients; icoef ++)
54 c [icoef] = (1 + lifter / 2 * sin (NUMpi * icoef / lifter)); // BUG ?
55 for (integer iframe = 1; iframe <= my nx; iframe ++) {
56 CC_Frame cf = & my frame [iframe];
57 for (integer icoef = 1; icoef <= cf -> numberOfCoefficients; icoef ++)
58 cf -> c [icoef] *= c [icoef];
59 }
60 } catch (MelderError) {
61 Melder_throw (me, U": not lifted.");
62 }
63 }
64
65 autoTableOfReal MFCC_to_TableOfReal (MFCC me, bool includeC0) {
66 try {
67 integer numberOfColumns = my maximumNumberOfCoefficients + ( includeC0 ? 1 : 0 );
68 autoTableOfReal thee = TableOfReal_create (my nx, numberOfColumns);
69 for (integer i = 1; i <= numberOfColumns; i ++)
70 TableOfReal_setColumnLabel (thee.get(), i, Melder_cat (U"c", ( includeC0 ? i - 1 : i )));
71 const integer offset = ( includeC0 ? 1 : 0 );
72 for (integer iframe = 1; iframe <= my nx; iframe ++) {
73 const CC_Frame cf = & my frame [iframe];
74 for (integer j = 1; j <= cf -> numberOfCoefficients; j ++)
75 thy data [iframe] [j + offset] = cf -> c [j];
76 if (includeC0)
77 thy data [iframe] [1] = cf -> c0;
78 }
79 return thee;
80 } catch (MelderError) {
81 Melder_throw (me, U": not converted to TabelOfReal.");
82 }
83 }
84
85 // as_Sound not to_Sound
86 autoSound MFCC_to_Sound (MFCC me) {
87 try {
88 /*
89 All frames of an MFCC have the same size, which can differ from maximumNumberOfCoefficients.
90 */
91 const CC_Frame cf1 = & my frame [1];
92 const integer numberOfCoefficients = cf1 -> c.size;
93 autoSound thee = Sound_create (numberOfCoefficients, my xmin, my xmax, my nx, my dx, my x1);
94 for (integer iframe = 1; iframe <= my nx; iframe ++) {
95 const CC_Frame cf = & my frame [iframe];
96 if (cf -> c.size != numberOfCoefficients)
97 Melder_crash (U"MFCC_to_Sound: ", cf -> c.size, U" ", numberOfCoefficients);
98 thy z.column (iframe) <<= cf -> c.all();
99 }
100 return thee;
101 } catch (MelderError) {
102 Melder_throw (me, U": not represented as Sound.");
103 }
104 }
105
106 static void checkMFCCpair (MFCC me, MFCC thee) {
107 Melder_require (my dx == thy dx,
108 U"The samplings of the two MFCC's should be equal.");
109 Melder_require (my maximumNumberOfCoefficients == thy maximumNumberOfCoefficients,
110 U"The maximum number of coefficients in the two MFCC's should be equal.");
111 const CC_Frame my_cf1 = & my frame [1];
112 const integer my_numberOfCoefficients = my_cf1 -> c.size;
113 const CC_Frame thy_cf1 = & thy frame [1];
114 const integer thy_numberOfCoefficients = thy_cf1 -> c.size;
115 Melder_require (my_numberOfCoefficients == thy_numberOfCoefficients,
116 U"The number of coefficients in the two MFCC's should be equal.");
117 }
118
119 autoSound MFCCs_crossCorrelate (MFCC me, MFCC thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) {
120 try {
121 checkMFCCpair (me, thee);
122 autoSound target = MFCC_to_Sound (me);
123 autoSound source = MFCC_to_Sound (thee);
124 autoSound cc = Sounds_crossCorrelate (target.get(), source.get(), scaling, signalOutsideTimeDomain);
125 return cc;
126 } catch (MelderError) {
127 Melder_throw (U"No cross-correlation between ", me, U" and ", thee, U" calculated.");
128 }
129 }
130
131 autoSound MFCCs_convolve (MFCC me, MFCC thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) {
132 try {
133 checkMFCCpair (me, thee);
134 autoSound target = MFCC_to_Sound (me);
135 autoSound source = MFCC_to_Sound (thee);
136 autoSound cc = Sounds_convolve (target.get(), source.get(), scaling, signalOutsideTimeDomain);
137 return cc;
138 } catch (MelderError) {
139 Melder_throw (me, U" and ", thee, U" not convolved.");
140 }
141 }
142
143 static double CC_Frames_distance (CC_Frame me, CC_Frame thee, bool includeEnergy) {
144 double dist = 0;
145 if (includeEnergy) {
146 const double d0 = my c0 - thy c0;
147 dist += d0 * d0;
148 }
149 for (integer i = 1; i <= my numberOfCoefficients; i ++) {
150 const double di = my c [i] - thy c [i];
151 dist += di * di;
152 }
153 return sqrt (dist);
154 }
155
156 /*
157 1: cepstral difference function (d)
158 2: spectral stability (dstab)
159 3: spectral center of gravity (gs)
160 4: stable internal duration
161 */
162 autoMatrix MFCC_to_Matrix_features (MFCC me, double windowLength, bool includeEnergy) {
163 try {
164 const integer nw = Melder_ifloor (windowLength / my dx / 2.0);
165 autoMelSpectrogram him = MFCC_to_MelSpectrogram (me, 0, 0, 1);
166 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 4.0, 4, 1.0, 1.0);
167 thy z [1] [1] = thy z [1] [my nx] = 0; // first & last frame
168 for (integer iframe = 2; iframe <= my nx - 1; iframe ++) {
169 const CC_Frame cfi = & my frame [iframe];
170 // 1. cepstral difference
171 integer nwi = ( iframe > nw ? nw : iframe - 1 );
172 if (nwi > my nx - iframe)
173 nwi = my nx - iframe;
174 double numer = 0.0;
175 for (integer j = 1; j <= nwi; j ++)
176 numer += (double) j * (double) j;
177 numer *= 2.0;
178 double dsq = 0.0;
179 if (includeEnergy) {
180 longdouble sumj = 0.0;
181 for (integer j = 1; j <= nwi; j ++) {
182 const CC_Frame cfp = & my frame [iframe + j];
183 const CC_Frame cfm = & my frame [iframe - j];
184 sumj += j * (cfp -> c0 - cfm -> c0);
185 }
186 sumj /= numer;
187 dsq += sumj * sumj;
188 }
189 for (integer i = 1; i <= my maximumNumberOfCoefficients; i ++) {
190 longdouble sumj = 0.0;
191 for (integer j = 1; j <= nwi; j ++) {
192 const CC_Frame cfp = & my frame [iframe + j];
193 const CC_Frame cfm = & my frame [iframe - j];
194 sumj += j * (cfp -> c [j] - cfm -> c [j]);
195 }
196 sumj /= numer;
197 dsq += sumj * sumj;
198 }
199 thy z [1] [iframe] = dsq;
200
201 // 2: spectral stability (dstab)
202 const CC_Frame cfp = & my frame [iframe + 1];
203 const CC_Frame cfm = & my frame [iframe - 1];
204 const double dim1 = CC_Frames_distance (cfi, cfm, includeEnergy);
205 const double dip1 = CC_Frames_distance (cfi, cfp, includeEnergy);
206 thy z [2] [iframe] = (dim1 + dip1) / 2.0;
207
208 // 3: spectral centere of gravity (gs)
209 longdouble msm = 0.0, sm = 0.0;
210 for (integer j = 1; j <= his ny; j ++) {
211 sm += his z [j] [iframe];
212 msm += j * his z [j] [iframe];
213 }
214 const double gs = ( sm == 0.0 ? 0.0 : (double) (msm / sm) );
215 thy z [3] [iframe] = gs;
216
217 // 4: stable internal duration
218 }
219 return thee;
220 } catch (MelderError) {
221 Melder_throw (me, U": no features calculated.");
222 }
223 }
224
225 /* End of file MFCC.cpp */