| 1 | /* CC.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2020 David Weenink, 2024 Paul Boersma |
| 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 20011016 removed some causes for compiler warnings |
| 21 | djmw 20020315 GPL header |
| 22 | djmw 20061212 Changed info to Melder_writeLine<x> format. |
| 23 | djmw 20071012 Added: oo_CAN_WRITE_AS_ENCODING.h |
| 24 | djmw 20080122 float -> double |
| 25 | djmw 20080513 CC_getValue |
| 26 | */ |
| 27 | |
| 28 | #include "CC.h" |
| 29 | #include "NUM2.h" |
| 30 | |
| 31 | #include "oo_DESTROY.h" |
| 32 | #include "CC_def.h" |
| 33 | #include "oo_COPY.h" |
| 34 | #include "CC_def.h" |
| 35 | #include "oo_EQUAL.h" |
| 36 | #include "CC_def.h" |
| 37 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 38 | #include "CC_def.h" |
| 39 | #include "oo_WRITE_TEXT.h" |
| 40 | #include "CC_def.h" |
| 41 | #include "oo_WRITE_BINARY.h" |
| 42 | #include "CC_def.h" |
| 43 | #include "oo_READ_TEXT.h" |
| 44 | #include "CC_def.h" |
| 45 | #include "oo_READ_BINARY.h" |
| 46 | #include "CC_def.h" |
| 47 | #include "oo_DESCRIPTION.h" |
| 48 | #include "CC_def.h" |
| 49 | |
| 50 | Thing_implement (CC, Sampled, 1); |
| 51 | |
| 52 | integer CC_getMaximumNumberOfCoefficientsUsed (CC me) { |
| 53 | integer numberOfCoefficients = 0; |
| 54 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 55 | const CC_Frame cf = & my frame [iframe]; |
| 56 | numberOfCoefficients = std::max (cf -> numberOfCoefficients, numberOfCoefficients); |
| 57 | } |
| 58 | return numberOfCoefficients; |
| 59 | } |
| 60 | |
| 61 | void structCC :: v1_info () { |
| 62 | structDaata :: v1_info (); |
| 63 | MelderInfo_writeLine (U"Time domain:", xmin, U" to ", xmax, U" seconds"); |
| 64 | MelderInfo_writeLine (U"Number of frames: ", nx); |
| 65 | MelderInfo_writeLine (U"Time step: ", dx, U" seconds"); |
| 66 | MelderInfo_writeLine (U"First frame at: ", x1, U" seconds"); |
| 67 | MelderInfo_writeLine (U"Maximum number of coefficients possible: ", maximumNumberOfCoefficients); |
| 68 | MelderInfo_writeLine (U"Maximum number of coefficients used: ", CC_getMaximumNumberOfCoefficientsUsed (this)); |
| 69 | } |
| 70 | |
| 71 | void CC_Frame_init (CC_Frame me, integer numberOfCoefficients) { |
| 72 | my c = zero_VEC (numberOfCoefficients); |
| 73 | my numberOfCoefficients = numberOfCoefficients; |
| 74 | } |
| 75 | |
| 76 | void CC_init (CC me, double tmin, double tmax, integer nt, double dt, double t1, integer maximumNumberOfCoefficients, double fmin, double fmax) { |
| 77 | my fmin = fmin; |
| 78 | my fmax = fmax; |
| 79 | my maximumNumberOfCoefficients = maximumNumberOfCoefficients; |
| 80 | Sampled_init (me, tmin, tmax, nt, dt, t1); |
| 81 | my frame = newvectorzero <structCC_Frame> (nt); |
| 82 | } |
| 83 | |
| 84 | autoMatrix CC_to_Matrix (CC me) { |
| 85 | try { |
| 86 | /* |
| 87 | Find number of coefficients by quering all frames. |
| 88 | We cannot use maximumNumberOfCoefficients because this number is only used to calculate the inverse |
| 89 | */ |
| 90 | integer numberOfCoefficients = 0; |
| 91 | for (integer i = 1; i <= my nx; i ++) { |
| 92 | const CC_Frame cf = & my frame [i]; |
| 93 | if (cf -> numberOfCoefficients > numberOfCoefficients) |
| 94 | numberOfCoefficients = cf -> numberOfCoefficients; |
| 95 | } |
| 96 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, numberOfCoefficients, numberOfCoefficients, 1.0, 1.0); |
| 97 | |
| 98 | for (integer i = 1; i <= my nx; i ++) { |
| 99 | const CC_Frame cf = & my frame [i]; |
| 100 | thy z.column (i).part (1, cf -> numberOfCoefficients) <<= cf -> c.get().part (1, cf -> numberOfCoefficients); |
| 101 | } |
| 102 | return thee; |
| 103 | } catch (MelderError) { |
| 104 | Melder_throw (me, U": not converted to Matrix."); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void CC_paint (CC me, Graphics g, double xmin, double xmax, integer cmin, integer cmax, double minimum, double maximum, bool garnish) { |
| 109 | autoMatrix thee = CC_to_Matrix (me); |
| 110 | |
| 111 | Matrix_paintCells (thee.get(), g, xmin, xmax, cmin, cmax, minimum, maximum); |
| 112 | |
| 113 | if (garnish) { |
| 114 | Graphics_marksBottom (g, 2, true, true, false); |
| 115 | Graphics_textBottom (g, true, U"Time (s)"); |
| 116 | Graphics_marksLeft (g, 2, true, true, false); |
| 117 | Graphics_textLeft (g, true, U"Coefficients"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void CC_drawC0 (CC me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool /* garnish */) { |
| 122 | if (xmin >= xmax) { |
| 123 | xmin = my xmin; |
| 124 | xmax = my xmax; |
| 125 | } |
| 126 | integer bframe, eframe; |
| 127 | integer numberOfSelected = Sampled_getWindowSamples (me, xmin, xmax, & bframe, & eframe); |
| 128 | if (numberOfSelected <= 0) |
| 129 | return; |
| 130 | autoVEC c = raw_VEC (numberOfSelected); |
| 131 | for (integer i = 1; i <= numberOfSelected; i ++) { |
| 132 | const CC_Frame cf = & my frame [bframe + i - 1]; |
| 133 | c [i] = cf -> c0; |
| 134 | } |
| 135 | if (ymin >= ymax) { |
| 136 | NUMextrema_u (c.get(), & ymin, & ymax); |
| 137 | if (isundef (ymin) || isundef (ymax)) |
| 138 | return; |
| 139 | if (ymax <= ymin) { |
| 140 | ymin -= 1.0; |
| 141 | ymax += 1.0; |
| 142 | } |
| 143 | } else |
| 144 | VECclip_inplace (ymin, c.get(), ymax); |
| 145 | |
| 146 | Graphics_setInner (g); |
| 147 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 148 | Graphics_function (g, c.asArgumentToFunctionThatExpectsOneBasedArray(), 1, numberOfSelected, xmin, xmax); |
| 149 | Graphics_unsetInner (g); |
| 150 | } |
| 151 | |
| 152 | void CC_getNumberOfCoefficients_extrema (CC me, integer startFrame, integer endFrame, integer *out_min, integer *out_max) { |
| 153 | |
| 154 | Melder_assert (startFrame <= endFrame); |
| 155 | |
| 156 | if (startFrame == 0 && endFrame == 0) { |
| 157 | startFrame = 1; |
| 158 | endFrame = my nx; |
| 159 | } |
| 160 | if (startFrame < 1) |
| 161 | startFrame = 1; |
| 162 | if (endFrame > my nx) |
| 163 | endFrame = my nx; |
| 164 | |
| 165 | integer min = my maximumNumberOfCoefficients; |
| 166 | integer max = 0; |
| 167 | |
| 168 | for (integer i = startFrame; i <= endFrame; i ++) { |
| 169 | const CC_Frame f = & my frame [i]; |
| 170 | const integer nc = f -> numberOfCoefficients; |
| 171 | if (nc < min) |
| 172 | min = nc; |
| 173 | if (nc > max) |
| 174 | max = nc; |
| 175 | } |
| 176 | if (out_min) |
| 177 | *out_min = min; |
| 178 | if (out_max) |
| 179 | *out_max = max; |
| 180 | } |
| 181 | |
| 182 | integer CC_getMinimumNumberOfCoefficients (CC me, integer startFrame, integer endFrame) { |
| 183 | integer min, max; |
| 184 | CC_getNumberOfCoefficients_extrema (me, startFrame, endFrame, & min, & max); |
| 185 | return min; |
| 186 | } |
| 187 | |
| 188 | integer CC_getMaximumNumberOfCoefficients (CC me, integer startFrame, integer endFrame) { |
| 189 | integer min, max; |
| 190 | CC_getNumberOfCoefficients_extrema (me, startFrame, endFrame, & min, & max); |
| 191 | return max; |
| 192 | } |
| 193 | |
| 194 | integer CC_getNumberOfCoefficients (CC me, integer frameNumber) { |
| 195 | if (frameNumber < 1 || frameNumber > my nx) |
| 196 | return 0; |
| 197 | const CC_Frame cf = & me -> frame [frameNumber]; |
| 198 | return cf -> numberOfCoefficients; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | double CC_getValueInFrame (CC me, integer frameNumber, integer coeffNumber) { |
| 203 | if (frameNumber < 1 || frameNumber > my nx) |
| 204 | return undefined; |
| 205 | const CC_Frame cf = & me -> frame [frameNumber]; |
| 206 | return ( coeffNumber > cf -> numberOfCoefficients ? undefined : cf -> c [coeffNumber] ); |
| 207 | } |
| 208 | |
| 209 | double CC_getValue (CC me, double t, integer coeffNumber) { |
| 210 | const integer frameNumber = Sampled_xToNearestIndex (me, t); |
| 211 | if (frameNumber < 1 || frameNumber > my nx) |
| 212 | return undefined; |
| 213 | const CC_Frame cf = & me -> frame [frameNumber]; |
| 214 | return ( coeffNumber > cf -> numberOfCoefficients ? undefined : cf -> c [coeffNumber] ); |
| 215 | } |
| 216 | |
| 217 | double CC_getC0ValueInFrame (CC me, integer frameNumber) { |
| 218 | if (frameNumber < 1 || frameNumber > my nx) |
| 219 | return undefined; |
| 220 | const CC_Frame cf = & me -> frame [frameNumber]; |
| 221 | return cf -> c0; |
| 222 | } |
| 223 | |
| 224 | /* End of file CC.cpp */ |