| 1 | /* LineSpectralFrequencies.cpp |
| 2 | * |
| 3 | * Copyright (C) 2016-2023 David Weenink |
| 4 | * |
| 5 | * This program 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 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This program 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 program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "LineSpectralFrequencies.h" |
| 21 | #include "NUM2.h" |
| 22 | |
| 23 | #include "oo_DESTROY.h" |
| 24 | #include "LineSpectralFrequencies_def.h" |
| 25 | #include "oo_COPY.h" |
| 26 | #include "LineSpectralFrequencies_def.h" |
| 27 | #include "oo_EQUAL.h" |
| 28 | #include "LineSpectralFrequencies_def.h" |
| 29 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 30 | #include "LineSpectralFrequencies_def.h" |
| 31 | #include "oo_WRITE_TEXT.h" |
| 32 | #include "LineSpectralFrequencies_def.h" |
| 33 | #include "oo_WRITE_BINARY.h" |
| 34 | #include "LineSpectralFrequencies_def.h" |
| 35 | #include "oo_READ_TEXT.h" |
| 36 | #include "LineSpectralFrequencies_def.h" |
| 37 | #include "oo_READ_BINARY.h" |
| 38 | #include "LineSpectralFrequencies_def.h" |
| 39 | #include "oo_DESCRIPTION.h" |
| 40 | #include "LineSpectralFrequencies_def.h" |
| 41 | |
| 42 | Thing_implement (LineSpectralFrequencies, Sampled, 1); |
| 43 | |
| 44 | void structLineSpectralFrequencies :: v1_info () { |
| 45 | structDaata :: v1_info (); |
| 46 | MelderInfo_writeLine (U"Time domain: ", xmin, U" to ", xmax, U" (s)."); |
| 47 | MelderInfo_writeLine (U"Number of frequencies: ", maximumNumberOfFrequencies); |
| 48 | MelderInfo_writeLine (U"Number of frames: ", nx); |
| 49 | MelderInfo_writeLine (U"Time step: ", dx, U" (s)."); |
| 50 | MelderInfo_writeLine (U"First frame at: ", x1, U" (s)."); |
| 51 | } |
| 52 | |
| 53 | void LineSpectralFrequencies_Frame_init (LineSpectralFrequencies_Frame me, integer numberOfFrequencies) { |
| 54 | my frequencies = zero_VEC (numberOfFrequencies); |
| 55 | my numberOfFrequencies = numberOfFrequencies; |
| 56 | } |
| 57 | |
| 58 | void LineSpectralFrequencies_init (LineSpectralFrequencies me, double tmin, double tmax, integer nt, double dt, double t1, integer numberOfFrequencies, double maximumFrequency) { |
| 59 | my maximumFrequency = maximumFrequency; |
| 60 | my maximumNumberOfFrequencies = numberOfFrequencies; |
| 61 | Sampled_init (me, tmin, tmax, nt, dt, t1); |
| 62 | my d_frames = newvectorzero <structLineSpectralFrequencies_Frame> (nt); |
| 63 | } |
| 64 | |
| 65 | autoLineSpectralFrequencies LineSpectralFrequencies_create (double tmin, double tmax, integer nt, double dt, double t1, integer numberOfFrequencies, double maximumFrequency) { |
| 66 | try { |
| 67 | autoLineSpectralFrequencies me = Thing_new (LineSpectralFrequencies); |
| 68 | LineSpectralFrequencies_init (me.get(), tmin, tmax, nt, dt, t1, numberOfFrequencies, maximumFrequency); |
| 69 | return me; |
| 70 | } catch (MelderError) { |
| 71 | Melder_throw (U"LineSpectralFrequencies not created."); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void LineSpectralFrequencies_drawFrequencies (LineSpectralFrequencies me, Graphics g, double tmin, double tmax, double fmin, double fmax, bool garnish) { |
| 76 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 77 | integer itmin, itmax; |
| 78 | if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax)) |
| 79 | return; |
| 80 | if (fmax <= fmin) { |
| 81 | const integer numberOfSelected = itmax - itmin + 1; |
| 82 | autoVEC f1 = raw_VEC (numberOfSelected); |
| 83 | autoVEC f2 = raw_VEC (numberOfSelected); |
| 84 | for (integer iframe = itmin; iframe <= itmax; iframe ++) { |
| 85 | f1 [iframe - itmin + 1] = my d_frames [iframe]. frequencies [1]; |
| 86 | f2 [iframe - itmin + 1] = my d_frames [iframe]. frequencies [my d_frames [iframe] . numberOfFrequencies]; |
| 87 | } |
| 88 | double f1max, f2min; |
| 89 | NUMextrema_u (f1.get(), & fmin, & f1max); |
| 90 | NUMextrema_u (f2.get(), & f2min, & fmax); |
| 91 | } |
| 92 | if (isundef (fmin) || isundef (fmax)) |
| 93 | return; |
| 94 | if (fmax == fmin) { |
| 95 | fmin = 0.0; |
| 96 | fmax += 0.5; |
| 97 | } |
| 98 | |
| 99 | Graphics_setInner (g); |
| 100 | Graphics_setWindow (g, tmin, tmax, fmin, fmax); |
| 101 | for (integer iframe = itmin; iframe <= itmax; iframe ++) { |
| 102 | const LineSpectralFrequencies_Frame lsf = & my d_frames [iframe]; |
| 103 | const double x = Sampled_indexToX (me, iframe); |
| 104 | for (integer ifreq = 1; ifreq <= lsf -> numberOfFrequencies; ifreq ++) { |
| 105 | const double y = lsf -> frequencies [ifreq]; |
| 106 | if (y >= fmin && y <= fmax) |
| 107 | Graphics_speckle (g, x, y); |
| 108 | } |
| 109 | } |
| 110 | Graphics_unsetInner (g); |
| 111 | if (garnish) { |
| 112 | Graphics_drawInnerBox (g); |
| 113 | Graphics_textBottom (g, true, U"Time (seconds)"); |
| 114 | Graphics_textLeft (g, true, U"Frequency (Hz)"); |
| 115 | Graphics_marksBottom (g, 2, true, true, false); |
| 116 | Graphics_marksLeft (g, 2, true, true, false); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | autoMatrix LineSpectralFrequencies_downto_Matrix (LineSpectralFrequencies me) { |
| 121 | try { |
| 122 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 0.5 + my maximumNumberOfFrequencies, my maximumNumberOfFrequencies, 1.0, 1.0); |
| 123 | for (integer j = 1; j <= my nx; j ++) { |
| 124 | const LineSpectralFrequencies_Frame lsf = & my d_frames [j]; |
| 125 | thy z.column (j).part (1, lsf -> numberOfFrequencies) <<= lsf -> frequencies.get(); |
| 126 | } |
| 127 | return thee; |
| 128 | } catch (MelderError) { |
| 129 | Melder_throw (me, U": no Matrix with linear prediction coefficients created."); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* End of file LineSpectralFrequencies.cpp */ |