| 1 | /* Cochleagram.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2007,2008,2011,2012,2015-2020 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. |
| 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 "Cochleagram.h" |
| 21 | |
| 22 | Thing_implement (Cochleagram, Matrix, 2); |
| 23 | |
| 24 | autoCochleagram Cochleagram_create (double tmin, double tmax, integer nt, double dt, double t1, double df, integer nf) { |
| 25 | try { |
| 26 | autoCochleagram me = Thing_new (Cochleagram); |
| 27 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, 0.0, nf * df, nf, df, 0.5 * df); |
| 28 | return me; |
| 29 | } catch (MelderError) { |
| 30 | Melder_throw (U"Cochleagram with ", nt, U" times and ", nf, U" frequencies not created."); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void Cochleagram_paint (Cochleagram me, Graphics g, double tmin, double tmax, bool garnish) { |
| 35 | static double border [1 + 12] |
| 36 | { 0.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0 }; |
| 37 | try { |
| 38 | autoCochleagram copy = Data_copy (me); |
| 39 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 40 | integer itmin, itmax; |
| 41 | Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax); |
| 42 | for (integer iy = 2; iy < my ny; iy ++) |
| 43 | for (integer ix = itmin; ix <= itmax; ix ++) |
| 44 | if (my z [iy] [ix] > my z [iy - 1] [ix] && |
| 45 | my z [iy] [ix] > my z [iy + 1] [ix]) |
| 46 | { |
| 47 | copy -> z [iy - 1] [ix] += 10.0; |
| 48 | copy -> z [iy] [ix] += 10.0; |
| 49 | copy -> z [iy + 1] [ix] += 10.0; |
| 50 | } |
| 51 | Graphics_setInner (g); |
| 52 | Graphics_setWindow (g, tmin, tmax, 0.0, my ny * my dy); |
| 53 | Graphics_grey (g, copy -> z.part (1, my ny, itmin, itmax), |
| 54 | Matrix_columnToX (me, itmin), Matrix_columnToX (me, itmax), |
| 55 | 0.5 * my dy, (my ny - 0.5) * my dy, |
| 56 | 12, border); |
| 57 | Graphics_unsetInner (g); |
| 58 | if (garnish) { |
| 59 | Graphics_drawInnerBox (g); |
| 60 | Graphics_textBottom (g, true, U"Time (s)"); |
| 61 | Graphics_marksBottom (g, 2, true, true, false); |
| 62 | Graphics_textLeft (g, true, U"Place (Bark)"); |
| 63 | Graphics_marksLeftEvery (g, 1.0, 5.0, true, true, false); |
| 64 | } |
| 65 | } catch (MelderError) { |
| 66 | Melder_clearError (); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | double Cochleagram_difference (Cochleagram me, Cochleagram thee, double tmin, double tmax) { |
| 71 | try { |
| 72 | if (my nx != thy nx || my dx != thy dx || my x1 != thy x1) |
| 73 | Melder_throw (U"Unequal time samplings."); |
| 74 | Melder_require (my ny == thy ny, |
| 75 | U"Unequal numbers of frequencies."); |
| 76 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 77 | integer itmin, itmax; |
| 78 | integer nt = Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax); |
| 79 | Melder_require (nt > 0, |
| 80 | U"Window too short."); |
| 81 | longdouble diff = 0.0; |
| 82 | for (integer itime = itmin; itime <= itmax; itime ++) { |
| 83 | for (integer ifreq = 1; ifreq <= my ny; ifreq ++) { |
| 84 | double d = my z [ifreq] [itime] - thy z [ifreq] [itime]; |
| 85 | diff += d * d; |
| 86 | } |
| 87 | } |
| 88 | diff /= nt * my ny; |
| 89 | return sqrt ((double) diff); |
| 90 | } catch (MelderError) { |
| 91 | Melder_throw (me, U" & ", thee, U": difference not computed."); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | autoCochleagram Matrix_to_Cochleagram (Matrix me) { |
| 96 | try { |
| 97 | autoCochleagram thee = Cochleagram_create (my xmin, my xmax, my nx, my dx, my x1, my dy, my ny); |
| 98 | thy z.all() <<= my z.all(); |
| 99 | return thee; |
| 100 | } catch (MelderError) { |
| 101 | Melder_throw (me, U": not converted to Cochleagram."); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | autoMatrix Cochleagram_to_Matrix (Cochleagram me) { |
| 106 | try { |
| 107 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 108 | thy z.all() <<= my z.all(); |
| 109 | return thee; |
| 110 | } catch (MelderError) { |
| 111 | Melder_throw (me, U": not converted to Matrix."); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* End of file Cochleagram.cpp */ |