| 1 | /* LPC_and_LFCC.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2019 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 20030205 Latest modifiation |
| 22 | */ |
| 23 | |
| 24 | #include "LPC_and_LFCC.h" |
| 25 | #include "NUM2.h" |
| 26 | |
| 27 | void LPC_Frame_into_CC_Frame (LPC_Frame me, CC_Frame thee) { |
| 28 | Melder_assert (my nCoefficients == my a.size); // check invariant |
| 29 | thy c.resize (my nCoefficients); |
| 30 | thy numberOfCoefficients = thy c.size; // maintain invariant |
| 31 | thy c0 = 0.5 * log (my gain); |
| 32 | |
| 33 | if (my nCoefficients < 1) |
| 34 | return; |
| 35 | |
| 36 | thy c [1] = - my a [1]; |
| 37 | for (integer n = 2; n <= std::min ((integer) my nCoefficients, thy numberOfCoefficients); n ++) { |
| 38 | longdouble s = 0.0; |
| 39 | for (integer k = 1; k < n; k ++) |
| 40 | s += my a [k] * thy c [n - k] * (n - k); |
| 41 | thy c [n] = - my a [n] - s / n; |
| 42 | } |
| 43 | for (integer n = my nCoefficients + 1; n <= thy numberOfCoefficients; n ++) { |
| 44 | longdouble s = 0.0; |
| 45 | for (integer k = 1; k <= my nCoefficients; k ++) |
| 46 | s += my a [k] * thy c [n - k] * (n - k); |
| 47 | thy c [n] = - s / n; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void CC_Frame_into_LPC_Frame (CC_Frame me, LPC_Frame thee) { |
| 52 | Melder_assert (my numberOfCoefficients == my c.size); // check invariant |
| 53 | thy a.resize (my numberOfCoefficients); |
| 54 | thy nCoefficients = thy a.size; // maintain invariant |
| 55 | if (my numberOfCoefficients < 1) |
| 56 | return; |
| 57 | thy gain = exp (2.0 * my c0); |
| 58 | thy a [1] = - my c [1]; |
| 59 | for (integer i = 2; i <= my numberOfCoefficients; i ++) { |
| 60 | longdouble ai = my c [i] * i; |
| 61 | for (integer j = 1; j < i; j ++) |
| 62 | ai += thy a [j] * my c [i - j] * (i - j); |
| 63 | thy a [i] = - double (ai / i); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | autoLFCC LPC_to_LFCC (LPC me, integer numberOfCoefficients) { |
| 68 | try { |
| 69 | if (numberOfCoefficients < 1) |
| 70 | numberOfCoefficients = my maxnCoefficients; |
| 71 | |
| 72 | autoLFCC thee = LFCC_create (my xmin, my xmax, my nx, my dx, my x1, numberOfCoefficients, 0, 0.5 / my samplingPeriod); |
| 73 | |
| 74 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 75 | CC_Frame_init (& thy frame [iframe], numberOfCoefficients); |
| 76 | LPC_Frame_into_CC_Frame (& my d_frames [iframe], & thy frame [iframe]); |
| 77 | } |
| 78 | return thee; |
| 79 | } catch (MelderError) { |
| 80 | Melder_throw (me, U": no LFCC created."); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | autoLPC LFCC_to_LPC (LFCC me, integer numberOfCoefficients) { |
| 85 | try { |
| 86 | if (numberOfCoefficients < 1) |
| 87 | numberOfCoefficients = my maximumNumberOfCoefficients; |
| 88 | |
| 89 | numberOfCoefficients = std::min (numberOfCoefficients, my maximumNumberOfCoefficients); |
| 90 | autoLPC thee = LPC_create (my xmin, my xmax, my nx, my dx, my x1, numberOfCoefficients, 0.5 / my fmax); |
| 91 | |
| 92 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 93 | LPC_Frame_init (& thy d_frames [iframe], numberOfCoefficients); |
| 94 | CC_Frame_into_LPC_Frame (& my frame [iframe], & thy d_frames [iframe]); |
| 95 | } |
| 96 | return thee; |
| 97 | } catch (MelderError) { |
| 98 | Melder_throw (me, U": no LPC created."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* End of file LPC_and_LFCC.cpp */ |