| 1 | /* LPC.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2020, 2026 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 20030612 Removed LPC_Frame_free |
| 22 | djmw 20060510 LPC_drawPoles error cleared if something goes wrong. |
| 23 | djmw 20061212 Changed info to Melder_writeLine<x> format. |
| 24 | djmw 20071017 oo_CAN_WRITE_AS_ENCODING.h |
| 25 | djmw 20080122 float -> double |
| 26 | djmw 20081223 Corrected a bug in Matrix LPC_to_Matrix (last coefficient was not copied) |
| 27 | djmw 20110304 Thing_new |
| 28 | */ |
| 29 | |
| 30 | #include "LPC_and_Polynomial.h" |
| 31 | #include "NUM2.h" |
| 32 | #include "Roots.h" |
| 33 | |
| 34 | #include "oo_DESTROY.h" |
| 35 | #include "LPC_def.h" |
| 36 | #include "oo_COPY.h" |
| 37 | #include "LPC_def.h" |
| 38 | #include "oo_EQUAL.h" |
| 39 | #include "LPC_def.h" |
| 40 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 41 | #include "LPC_def.h" |
| 42 | #include "oo_WRITE_TEXT.h" |
| 43 | #include "LPC_def.h" |
| 44 | #include "oo_WRITE_BINARY.h" |
| 45 | #include "LPC_def.h" |
| 46 | #include "oo_READ_TEXT.h" |
| 47 | #include "LPC_def.h" |
| 48 | #include "oo_READ_BINARY.h" |
| 49 | #include "LPC_def.h" |
| 50 | #include "oo_DESCRIPTION.h" |
| 51 | #include "LPC_def.h" |
| 52 | |
| 53 | #include "enums_getText.h" |
| 54 | #undef _LPC_enums_h_ |
| 55 | #include "LPC_enums.h" |
| 56 | #include "enums_getValue.h" |
| 57 | #undef _LPC_enums_h_ |
| 58 | #include "LPC_enums.h" |
| 59 | |
| 60 | Thing_implement (LPC, Sampled, 1); |
| 61 | |
| 62 | void structLPC :: v1_info () { |
| 63 | structDaata :: v1_info (); |
| 64 | MelderInfo_writeLine (U"Time domain: ", xmin, U" to ", xmax, U" (s)."); |
| 65 | MelderInfo_writeLine (U"Prediction order: ", maxnCoefficients); |
| 66 | MelderInfo_writeLine (U"Number of frames: ", nx); |
| 67 | MelderInfo_writeLine (U"Time step: ", dx, U" (s)."); |
| 68 | MelderInfo_writeLine (U"First frame at: ", x1, U" (s)."); |
| 69 | } |
| 70 | |
| 71 | void LPC_Frame_init (LPC_Frame me, integer nCoefficients) { |
| 72 | if (nCoefficients != 0) |
| 73 | my a = zero_VEC (nCoefficients); |
| 74 | my nCoefficients = my a.size; // maintain invariant |
| 75 | } |
| 76 | |
| 77 | void LPC_init (LPC me, double tmin, double tmax, integer nt, double dt, double t1, integer predictionOrder, double samplingPeriod) { |
| 78 | my samplingPeriod = samplingPeriod; |
| 79 | my maxnCoefficients = predictionOrder; |
| 80 | Sampled_init (me, tmin, tmax, nt, dt, t1); |
| 81 | my d_frames = newvectorzero <structLPC_Frame> (nt); |
| 82 | } |
| 83 | |
| 84 | autoLPC LPC_create (double tmin, double tmax, integer nt, double dt, double t1, integer predictionOrder, double samplingPeriod) { |
| 85 | try { |
| 86 | autoLPC me = Thing_new (LPC); |
| 87 | LPC_init (me.get(), tmin, tmax, nt, dt, t1, predictionOrder, samplingPeriod); |
| 88 | return me; |
| 89 | } catch (MelderError) { |
| 90 | Melder_throw (U"LPC not created."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | autoLPC LPC_createCompletelyInitialized (double tmin, double tmax, integer nt, double dt, double t1, integer predictionOrder, double samplingPeriod) { |
| 95 | try { |
| 96 | autoLPC me = LPC_create (tmin, tmax, nt, dt, t1, predictionOrder, samplingPeriod); |
| 97 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 98 | LPC_Frame lpcFrame = & my d_frames [iframe]; |
| 99 | LPC_Frame_init (lpcFrame, my maxnCoefficients); |
| 100 | } |
| 101 | return me; |
| 102 | } catch (MelderError) { |
| 103 | Melder_throw (U"LPC (completely initialized) not created."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void LPC_drawGain (LPC me, Graphics g, double tmin, double tmax, double gmin, double gmax, bool garnish) { |
| 108 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 109 | integer itmin, itmax; |
| 110 | if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax)) |
| 111 | return; |
| 112 | |
| 113 | const integer numberOfSelected = itmax - itmin + 1; |
| 114 | autoVEC gain = raw_VEC (numberOfSelected); |
| 115 | |
| 116 | for (integer iframe = itmin; iframe <= itmax; iframe ++) |
| 117 | gain [iframe - itmin + 1] = my d_frames [iframe]. gain; |
| 118 | |
| 119 | if (gmax <= gmin) |
| 120 | NUMextrema_u (gain.get(), & gmin, & gmax); |
| 121 | if (isundef (gmin) || isundef (gmax)) |
| 122 | return; |
| 123 | if (gmax == gmin) { |
| 124 | gmin = 0.0; |
| 125 | gmax += 0.5; |
| 126 | } |
| 127 | |
| 128 | Graphics_setInner (g); |
| 129 | Graphics_setWindow (g, tmin, tmax, gmin, gmax); |
| 130 | for (integer iframe = itmin; iframe <= itmax; iframe ++) { |
| 131 | const double x = Sampled_indexToX (me, iframe); |
| 132 | Graphics_speckle (g, x, gain [iframe - itmin + 1]); |
| 133 | } |
| 134 | Graphics_unsetInner (g); |
| 135 | if (garnish) { |
| 136 | Graphics_drawInnerBox (g); |
| 137 | Graphics_textBottom (g, true, U"Time (seconds)"); |
| 138 | Graphics_textLeft (g, true, U"Gain"); |
| 139 | Graphics_marksBottom (g, 2, true, true, false); |
| 140 | Graphics_marksLeft (g, 2, true, true, false); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void LPC_drawPoles (LPC me, Graphics g, double time, bool garnish) { |
| 145 | autoPolynomial p = LPC_to_Polynomial_slice (me, time); |
| 146 | autoRoots r = Polynomial_to_Roots (p.get()); |
| 147 | Roots_draw (r.get(), g, -1.0, 1.0, -1.0, 1.0, U"+", 12.0, garnish); |
| 148 | } |
| 149 | |
| 150 | autoMatrix LPC_downto_Matrix_lpc (LPC me) { |
| 151 | try { |
| 152 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 0.5 + my maxnCoefficients, my maxnCoefficients, 1.0, 1.0); |
| 153 | for (integer j = 1; j <= my nx; j ++) { |
| 154 | const LPC_Frame lpcf = & my d_frames [j]; |
| 155 | thy z.column (j).part (1, lpcf -> nCoefficients) <<= lpcf -> a.get(); |
| 156 | } |
| 157 | return thee; |
| 158 | } catch (MelderError) { |
| 159 | Melder_throw (me, U": no Matrix with linear prediction coefficients created."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | autoMatrix LPC_downto_Matrix_rc (LPC me) { |
| 164 | try { |
| 165 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 0.5 + my maxnCoefficients, my maxnCoefficients, 1.0, 1.0); |
| 166 | autoVEC rc = zero_VEC (my maxnCoefficients); |
| 167 | for (integer j = 1; j <= my nx; j ++) { |
| 168 | const LPC_Frame lpc = & my d_frames [j]; |
| 169 | VECrc_from_lpc (rc.part (1, lpc -> nCoefficients), lpc -> a.part (1, lpc -> nCoefficients)); |
| 170 | thy z.column (j).part (1, lpc -> nCoefficients) <<= rc.get(); |
| 171 | } |
| 172 | return thee; |
| 173 | } catch (MelderError) { |
| 174 | Melder_throw (me, U": no Matrix with relection coefficients created."); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | autoMatrix LPC_downto_Matrix_area (LPC me) { |
| 179 | try { |
| 180 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 0.5 + my maxnCoefficients, my maxnCoefficients, 1.0, 1.0); |
| 181 | autoVEC rc = raw_VEC (my maxnCoefficients); |
| 182 | autoVEC area = raw_VEC (my maxnCoefficients); |
| 183 | for (integer j = 1; j <= my nx; j ++) { |
| 184 | const LPC_Frame lpc = & my d_frames [j]; |
| 185 | VECrc_from_lpc (rc.part (1, lpc -> nCoefficients), lpc -> a.part (1, lpc -> nCoefficients)); |
| 186 | VECarea_from_rc (area.part (1, lpc -> nCoefficients), rc.part (1, lpc -> nCoefficients)); |
| 187 | if (lpc -> nCoefficients < my maxnCoefficients) |
| 188 | area.part (lpc -> nCoefficients + 1, my maxnCoefficients) <<= 0.0; |
| 189 | thy z.column (j) <<= area.get(); |
| 190 | } |
| 191 | return thee; |
| 192 | } catch (MelderError) { |
| 193 | Melder_throw (me, U": no Matrix with areas created."); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* End of file LPC.cpp */ |