| 1 | #ifndef _Sound_and_LPC_h_ |
| 2 | #define _Sound_and_LPC_h_ |
| 3 | /* Sound_and_LPC.h |
| 4 | * |
| 5 | * Copyright (C) 1994-2025 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 3 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 19971103 |
| 23 | djmw 20020812 GPL header |
| 24 | */ |
| 25 | |
| 26 | #include "SoundFrameIntoSampledFrame.h" |
| 27 | #include "Sound.h" |
| 28 | #include "LPC.h" |
| 29 | #include "SVD.h" |
| 30 | |
| 31 | /* |
| 32 | 20240603: |
| 33 | The output of the Sound_to_LPC_<x> might be a little bit different from previous outputs because: |
| 34 | 1. The sound frame now always has an odd number of samples, irrespective of the sampling frequency or the window shape. |
| 35 | This means that also the window function will have an odd number of samples. Therefore the sample at the centre of the sound frame always has weight 1.0. |
| 36 | Previously the number of samples could be even or odd, depending on how rounding turned out. |
| 37 | 2. The Gaussian window function was slightly improved. |
| 38 | 3. The precision of the autocorrelation and covariance method have been improved a little by using some `longdouble` accumulators. |
| 39 | */ |
| 40 | |
| 41 | /*********************** Autocorrelation method *************************************************************/ |
| 42 | |
| 43 | /* |
| 44 | Precondition: |
| 45 | Sound and LPC have the same sampling. |
| 46 | */ |
| 47 | void Sound_into_LPC_auto (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth); |
| 48 | |
| 49 | autoLPC Sound_to_LPC_auto (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency); |
| 50 | |
| 51 | /*********************** Covariance method *************************************************************/ |
| 52 | |
| 53 | /* |
| 54 | Precondition: |
| 55 | Sound and LPC have the same sampling. |
| 56 | */ |
| 57 | void Sound_into_LPC_covar (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth); |
| 58 | |
| 59 | autoLPC Sound_to_LPC_covar (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency); |
| 60 | |
| 61 | /*********************** Burg method *******************************************************/ |
| 62 | |
| 63 | /* |
| 64 | Precondition: |
| 65 | Sound and LPC have the same sampling. |
| 66 | */ |
| 67 | void Sound_into_LPC_burg (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth); |
| 68 | |
| 69 | autoLPC Sound_to_LPC_burg (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency); |
| 70 | |
| 71 | void soundFrameIntoLPCFrame_burg (VEC soundFrame, LPC_Frame lpcFrame, VEC b1, VEC b2, |
| 72 | VEC aa, integer &info); |
| 73 | |
| 74 | /*********************** Marple method *****************************************************/ |
| 75 | |
| 76 | /* |
| 77 | Precondition: |
| 78 | Sound and LPC have the same sampling. |
| 79 | */ |
| 80 | void Sound_into_LPC_marple (constSound me, mutableLPC thee, double analysisWidth, double tol1, double tol2); |
| 81 | |
| 82 | autoLPC Sound_to_LPC_marple (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, |
| 83 | double preEmphasisFrequency, double tol1, double tol2); |
| 84 | |
| 85 | /*********************** PLP (Hermansky) method ******************************************************/ |
| 86 | |
| 87 | /*********************** Robust method (LPC & Sound) *******************************************************/ |
| 88 | |
| 89 | Thing_define (RobustLPCWorkspace, Thing) { |
| 90 | |
| 91 | constLPC inputLPC; |
| 92 | mutableLPC outputLPC; |
| 93 | VEC soundFrame; |
| 94 | integer order, currentPredictionOrder; |
| 95 | double k_stdev; |
| 96 | integer iter; |
| 97 | integer itermax; |
| 98 | integer huber_iterations = 5; // = 5; |
| 99 | bool wantLocation = true; // |
| 100 | bool wantScale = true; // |
| 101 | double location = 0.0; // not a parameter in initBasic! |
| 102 | double scale; |
| 103 | double tol1; |
| 104 | double tolSVD = 1e-10; // not a parameter in initBasic! |
| 105 | autoVEC error; // soundFrameSize |
| 106 | autoVEC sampleWeights; // soundFrameSize |
| 107 | autoVEC coefficients; // inputLPC -> maxnCoefficients |
| 108 | autoVEC covariancesw; // inputLPC -> maxnCoefficients |
| 109 | autoMAT covarmatrixw; // inputLPC -> maxnCoefficients, inputLPC -> maxnCoefficients |
| 110 | integer computedSVDworksize; |
| 111 | autoSVD svd; |
| 112 | autoVEC svdwork1; // computedSVDworksize |
| 113 | autoVEC svdwork2; // order |
| 114 | autoVEC filterMemory; // order |
| 115 | autoVEC huberwork; // soundFrameSize) |
| 116 | |
| 117 | void init (constLPC inputLPC, constSound inputSound, mutableLPC outputLPC, double effectiveAnalysisWidth, |
| 118 | kSound_windowShape windowShape, double k_stdev, integer itermax, double tol, bool wantlocation); |
| 119 | |
| 120 | void resize (); |
| 121 | void setSampleWeights (); |
| 122 | void setCovariances (); |
| 123 | void solvelpc (); |
| 124 | void inputFrameIntoOutputFrame (LPC_Frame inputLPCFrame, LPC_Frame outputLPCFrame, |
| 125 | VEC soundFrame, integer& info); |
| 126 | }; |
| 127 | |
| 128 | autoRobustLPCWorkspace RobustLPCWorkspace_create (constLPC inputLPC, constSound inputSound, |
| 129 | mutableLPC outputLPC, double effectiveAnalysisWidth, kSound_windowShape windowShape, |
| 130 | double k_stdev, integer itermax, double tol, bool wantlocation); |
| 131 | |
| 132 | /* |
| 133 | Precondition: |
| 134 | Sound and LPC have the same sampling. |
| 135 | */ |
| 136 | void LPC_and_Sound_into_LPC_robust (constLPC inputLPC, constSound inputSound, mutableLPC outputLPC, |
| 137 | double effectiveAnalysisWidth, double k_stdev, integer itermax, double tol, bool wantlocation); |
| 138 | |
| 139 | /* |
| 140 | Precondition: |
| 141 | Sound and LPC have the same sampling. |
| 142 | */ |
| 143 | autoLPC LPC_and_Sound_to_LPC_robust (constLPC inputLPC, constSound inputSound, double effectiveAnalysisWidth, |
| 144 | double preEmphasisFrequency, double k_stdev, integer itermax, double tol, bool wantlocation); |
| 145 | |
| 146 | void soundFrameAndLPCFrameIntoLPCFrame (VEC soundFrame, LPC_Frame inputLPCFrame, LPC_Frame outputLPCFrame, |
| 147 | RobustLPCWorkspace ws, integer& info); |
| 148 | |
| 149 | /*********************** Robust method (Sound) *************************************************************/ |
| 150 | |
| 151 | void Sound_into_LPC_robust (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth, |
| 152 | double k_stdev, integer itermax, double tol, bool wantlocation); |
| 153 | |
| 154 | autoLPC Sound_to_LPC_robust (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, |
| 155 | double preEmphasisFrequency, double k_stdev, integer itermax, double tol, bool wantlocation); |
| 156 | |
| 157 | /* |
| 158 | Precondition: |
| 159 | Sound has been resampled and pre-emphasized |
| 160 | */ |
| 161 | |
| 162 | /* |
| 163 | * Function: |
| 164 | * Calculate linear prediction coefficients according to following model: |
| 165 | * Minimize E(m) = Sum(n=n0;n=n1; (x [n] + Sum(k=1;k=m; a [k]*x [n-k]))) |
| 166 | * Method: |
| 167 | * The minimization is carried out by solving the equations: |
| 168 | * Sum(i=1;i=m; a [i]*c [i] [k]) = -c [0] [k] for k=1,2,...,m |
| 169 | * where c [i] [k] = Sum(n=n0;n=n1;x [n-i]*x [n-k]) |
| 170 | * 1. Covariance: |
| 171 | * n0=m; n1 = N-1; |
| 172 | * c [i] [k] is symmetric, positive semi-definite matrix |
| 173 | * Markel&Gray, LP of Speech, page 221; |
| 174 | * 2. Autocorrelation |
| 175 | * signal is zero outside the interval; |
| 176 | * n0=-infinity; n1=infinity |
| 177 | * c [i] [k] symmetric, positive definite Toeplitz matrix |
| 178 | * Markel&Gray, LP of Speech, page 219; |
| 179 | * Preconditions: |
| 180 | * predictionOrder > 0; |
| 181 | * preEmphasisFrequency >= 0; |
| 182 | * |
| 183 | * Burg method: see Numerical recipes Chapter 13. |
| 184 | * |
| 185 | * Marple method: see Marple, L. (1980), A new autoregressive spectrum analysis |
| 186 | * algorithm, IEEE Trans. on ASSP 28, 441-453. |
| 187 | * tol1 : stop iteration when E(m) / E(0) < tol1 |
| 188 | * tol2 : stop iteration when (E(m)-E(m-1)) / E(m-1) < tol2, |
| 189 | */ |
| 190 | |
| 191 | autoSound LPC_Sound_filter (constLPC me, constSound thee, bool useGain); |
| 192 | /* |
| 193 | E(z) = X(z)A(z), |
| 194 | A(z) = 1 + Sum (k=1, k=m, a(k)z^-k); |
| 195 | |
| 196 | filter: |
| 197 | given e & a, determine x; |
| 198 | x(n) = e(n) - Sum (k=1, m, a(k)x(n-k)) |
| 199 | useGain determines whether the LPC-gain is used in the synthesis. |
| 200 | */ |
| 201 | |
| 202 | void LPC_Sound_filterWithFilterAtTime_inplace (constLPC me, mutableSound thee, integer channel, double time); |
| 203 | |
| 204 | autoSound LPC_Sound_filterWithFilterAtTime (constLPC me, constSound thee, integer channel, double time); |
| 205 | |
| 206 | autoSound LPC_Sound_filterInverse (constLPC me, constSound thee); |
| 207 | /* |
| 208 | E(z) = X(z)A(z), |
| 209 | A(z) = 1 + Sum (k=1, k=m, a(k)z^-k); |
| 210 | |
| 211 | filter inverse: |
| 212 | given x & a, determine e; |
| 213 | e(n) = x(n) + Sum (k=1, m, a(k)x(n-k)) |
| 214 | */ |
| 215 | |
| 216 | autoSound LPC_Sound_filterInverseWithFilterAtTime (constLPC me, constSound thee, integer channel, double time); |
| 217 | |
| 218 | void LPC_Sound_filterInverseWithFilterAtTime_inplace (constLPC me, mutableSound thee, integer channel, double time); |
| 219 | |
| 220 | /* |
| 221 | For all LPC analysis |
| 222 | */ |
| 223 | void checkLPCAnalysisParameters_e (double sound_dx, integer sound_nx, double physicalAnalysisWidth, integer predictionOrder); |
| 224 | |
| 225 | inline void Sound_and_LPC_require_equalDomainsAndSamplingPeriods (constSound me, constLPC thee) { |
| 226 | Melder_require (my xmin == thy xmin && thy xmax == my xmax, |
| 227 | U"The domains of the Sound and the LPC should be equal."); |
| 228 | Melder_require (my dx == thy samplingPeriod, |
| 229 | U"The sampling periods of the Sound and the LPC should be equal."); |
| 230 | } |
| 231 | |
| 232 | #endif /* _Sound_and_LPC_h_ */ |