| 1 | /* Harmonicity.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011,2012,2015-2020,2022,2024,2026 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 3 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. |
| 13 | * See the GNU 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 | #include "Graphics.h" |
| 20 | #include "Harmonicity.h" |
| 21 | |
| 22 | Thing_implement (Harmonicity, Vector, 2); |
| 23 | |
| 24 | static autoVEC Harmonicity_getSoundingValues (Harmonicity me, double tmin, double tmax) { |
| 25 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 26 | integer imin, imax; |
| 27 | integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax); |
| 28 | if (numberOfFrames < 1) |
| 29 | return autoVEC(); |
| 30 | autoVEC soundingValues = raw_VEC (numberOfFrames); |
| 31 | integer numberOfSoundingFrames = 0; |
| 32 | for (integer iframe = imin; iframe <= imax; iframe ++) |
| 33 | if (my z [1] [iframe] != -200.0) |
| 34 | soundingValues [++ numberOfSoundingFrames] = my z [1] [iframe]; |
| 35 | if (numberOfSoundingFrames < 1) |
| 36 | return autoVEC(); |
| 37 | soundingValues.size = numberOfSoundingFrames; // shrink (without reallocation) |
| 38 | return soundingValues; |
| 39 | } |
| 40 | |
| 41 | double Harmonicity_getMean (Harmonicity me, double tmin, double tmax) { |
| 42 | autoVEC soundingValues = Harmonicity_getSoundingValues (me, tmin, tmax); |
| 43 | return NUMmean (soundingValues.get()); |
| 44 | } |
| 45 | |
| 46 | double Harmonicity_getStandardDeviation (Harmonicity me, double tmin, double tmax) { |
| 47 | autoVEC soundingValues = Harmonicity_getSoundingValues (me, tmin, tmax); |
| 48 | return NUMstdev (soundingValues.get()); |
| 49 | } |
| 50 | |
| 51 | double Harmonicity_getQuantile (Harmonicity me, double quantile) { |
| 52 | autoVEC soundingValues = Harmonicity_getSoundingValues (me, 0.0, 0.0); |
| 53 | sort_e_VEC_inout (soundingValues.get()); |
| 54 | return NUMquantile (soundingValues.get(), quantile); |
| 55 | } |
| 56 | |
| 57 | void structHarmonicity :: v1_info () { |
| 58 | structDaata :: v1_info (); |
| 59 | MelderInfo_writeLine (U"Time domain:"); |
| 60 | MelderInfo_writeLine (U" Start time: ", our xmin, U" seconds"); |
| 61 | MelderInfo_writeLine (U" End time: ", our xmax, U" seconds"); |
| 62 | MelderInfo_writeLine (U" Total duration: ", our xmax - our xmin, U" seconds"); |
| 63 | autoVEC soundingValues = Harmonicity_getSoundingValues (this, 0.0, 0.0); |
| 64 | MelderInfo_writeLine (U"Time sampling:"); |
| 65 | MelderInfo_writeLine (U" Number of frames: ", our nx, U" (", soundingValues.size, U" sounding)"); |
| 66 | MelderInfo_writeLine (U" Time step: ", our dx, U" seconds"); |
| 67 | MelderInfo_writeLine (U" First frame centred at: ", our x1, U" seconds"); |
| 68 | if (soundingValues.size > 0) { |
| 69 | MelderInfo_writeLine (U"Periodicity-to-noise ratios of sounding frames:"); |
| 70 | sort_e_VEC_inout (soundingValues.get()); |
| 71 | MelderInfo_writeLine (U" Median ", Melder_single (NUMquantile (soundingValues.get(), 0.50)), U" dB"); |
| 72 | MelderInfo_writeLine (U" 10 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.10)), U" dB 90 %% = ", |
| 73 | Melder_single (NUMquantile (soundingValues.get(), 0.90)), U" dB"); |
| 74 | MelderInfo_writeLine (U" 16 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.16)), U" dB 84 %% = ", |
| 75 | Melder_single (NUMquantile (soundingValues.get(), 0.84)), U" dB"); |
| 76 | MelderInfo_writeLine (U" 25 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.25)), U" dB 75 %% = ", |
| 77 | Melder_single (NUMquantile (soundingValues.get(), 0.75)), U" dB"); |
| 78 | MelderInfo_writeLine (U"Minimum: ", Melder_single (soundingValues [1]), U" dB"); |
| 79 | MelderInfo_writeLine (U"Maximum: ", Melder_single (soundingValues [soundingValues.size]), U" dB"); |
| 80 | MelderGaussianStats stats = NUMmeanStdev (soundingValues.all()); |
| 81 | MelderInfo_writeLine (U"Average: ", Melder_single (stats.mean), U" dB"); |
| 82 | if (soundingValues.size > 1) |
| 83 | MelderInfo_writeLine (U"Standard deviation: ", Melder_single (stats.stdev), U" dB"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | autoHarmonicity Harmonicity_create (double tmin, double tmax, integer nt, double dt, double t1) { |
| 88 | try { |
| 89 | autoHarmonicity me = Thing_new (Harmonicity); |
| 90 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, 1.0, 1.0, 1, 1.0, 1.0); |
| 91 | return me; |
| 92 | } catch (MelderError) { |
| 93 | Melder_throw (U"Harmonicity not created."); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | autoMatrix Harmonicity_to_Matrix (Harmonicity me) { |
| 98 | try { |
| 99 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 100 | thy z.all() <<= my z.all(); |
| 101 | return thee; |
| 102 | } catch (MelderError) { |
| 103 | Melder_throw (me, U"not converted to Matrix."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | autoHarmonicity Matrix_to_Harmonicity (Matrix me) { |
| 108 | try { |
| 109 | autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, my nx, my dx, my x1); |
| 110 | thy z.all() <<= my z.row (1); |
| 111 | return thee; |
| 112 | } catch (MelderError) { |
| 113 | Melder_throw (me, U"not converted to Harmonicity."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* End of file Harmonicity.cpp */ |