| 1 | /* LPC_and_Cepstrumc.cpp |
| 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 20080122 float -> double |
| 22 | */ |
| 23 | |
| 24 | #include "LPC_and_Cepstrumc.h" |
| 25 | |
| 26 | void LPC_Frame_into_Cepstrumc_Frame (LPC_Frame me, Cepstrumc_Frame thee) { |
| 27 | Melder_assert (my nCoefficients == my a.size); // check invariant |
| 28 | thy c.resize (my nCoefficients); |
| 29 | thy nCoefficients = thy c.size; // maintain invariant |
| 30 | thy c0 = 0.5 * log (my gain); |
| 31 | if (my nCoefficients == 0) |
| 32 | return; |
| 33 | thy c [1] = - my a [1]; |
| 34 | for (integer i = 2; i <= my nCoefficients; i ++) { |
| 35 | thy c [i] = 0.0; |
| 36 | for (integer k = 1; k < i; k ++) |
| 37 | thy c [i] += my a [i - k] * thy c [k] * k; |
| 38 | thy c [i] = - my a [i] - thy c [i] / i; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void Cepstrumc_Frame_into_LPC_Frame (Cepstrumc_Frame me, LPC_Frame thee) { |
| 43 | Melder_assert (my nCoefficients == my c.size); // Check invariant |
| 44 | thy a.resize (my nCoefficients); |
| 45 | thy nCoefficients = thy a.size; // maintain invariant |
| 46 | thy gain = exp (2.0 * my c0); |
| 47 | if (thy nCoefficients == 0) |
| 48 | return; |
| 49 | thy a [1] = - my c [1]; |
| 50 | for (integer i = 2; i <= thy nCoefficients; i ++) |
| 51 | my c [i] *= i; |
| 52 | for (integer i = 2; i <= thy nCoefficients; i ++) { |
| 53 | thy a [i] = my c [i]; |
| 54 | for (integer j = 1 ; j < i; j ++) |
| 55 | thy a [i] += thy a [j] * my c [i - j]; |
| 56 | thy a [i] /= -i; |
| 57 | } |
| 58 | /* |
| 59 | Undo the modification of the c array |
| 60 | */ |
| 61 | for (integer i = 2; i <= thy nCoefficients; i ++) |
| 62 | my c [i] /= i; |
| 63 | } |
| 64 | |
| 65 | autoCepstrumc LPC_to_Cepstrumc (LPC me) { |
| 66 | try { |
| 67 | autoCepstrumc thee = Cepstrumc_create (my xmin, my xmax, my nx, my dx, my x1, my maxnCoefficients, 1.0 / my samplingPeriod); |
| 68 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 69 | Cepstrumc_Frame_init (& thy frame [iframe], my d_frames [iframe]. nCoefficients); |
| 70 | LPC_Frame_into_Cepstrumc_Frame (& my d_frames [iframe], & thy frame [iframe]); |
| 71 | } |
| 72 | return thee; |
| 73 | } catch (MelderError) { |
| 74 | Melder_throw (me, U": no Cepstrum created."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | autoLPC Cepstrumc_to_LPC (Cepstrumc me) { |
| 79 | try { |
| 80 | autoLPC thee = LPC_create (my xmin, my xmax, my nx, my dx, my x1, my maxnCoefficients, 1.0 / my samplingFrequency); |
| 81 | for (integer i = 1; i <= my nx; i ++) { |
| 82 | LPC_Frame_init (& thy d_frames [i], my frame [i].nCoefficients); |
| 83 | Cepstrumc_Frame_into_LPC_Frame (& my frame [i], & thy d_frames [i]); |
| 84 | } |
| 85 | return thee; |
| 86 | } catch (MelderError) { |
| 87 | Melder_throw (me, U":no LPC created."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* End of file LPC_and_Cepstrumc.cpp */ |