Debian Codegraph @

dwtools/SPINET.cpp

raw
1 /* SPINET.cpp
2 *
3 * Copyright (C) 1993-2020 David Weenink, 2015 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 20020813 GPL header
21 djmw 20061212 Changed info to Melder_writeLine<x> format.
22 djmw 20071012 Added: oo_CAN_WRITE_AS_ENCODING.h
23 djmw 20080122 float -> double
24 djmw 20110304 Thing_new
25 */
26
27 #include "SPINET.h"
28 #include "Sound_extensions.h"
29 #include "NUM2.h"
30
31 #include "oo_DESTROY.h"
32 #include "SPINET_def.h"
33 #include "oo_COPY.h"
34 #include "SPINET_def.h"
35 #include "oo_EQUAL.h"
36 #include "SPINET_def.h"
37 #include "oo_CAN_WRITE_AS_ENCODING.h"
38 #include "SPINET_def.h"
39 #include "oo_WRITE_TEXT.h"
40 #include "SPINET_def.h"
41 #include "oo_WRITE_BINARY.h"
42 #include "SPINET_def.h"
43 #include "oo_READ_TEXT.h"
44 #include "SPINET_def.h"
45 #include "oo_READ_BINARY.h"
46 #include "SPINET_def.h"
47 #include "oo_DESCRIPTION.h"
48 #include "SPINET_def.h"
49
50 Thing_implement (SPINET, SampledXY, 0);
51
52 static integer SampledXY_getWindowExtrema (SampledXY me, constMAT z, integer ixmin, integer ixmax, integer iymin, integer iymax, double *out_minimum, double *out_maximum) {
53 /*
54 Function:
55 compute the minimum and maximum values of z over all samples inside [ixmin, ixmax] * [iymin, iymax].
56 Arguments:
57 if ixmin = 0, start at first column; if ixmax = 0, end at last column (same for iymin and iymax).
58 Return value:
59 the number of samples inside the window.
60 Postconditions:
61 if result == 0, *minimum and *maximum are not changed;
62 */
63
64 if (ixmin == 0)
65 ixmin = 1;
66 if (ixmax == 0)
67 ixmax = my nx;
68 if (iymin == 0)
69 iymin = 1;
70 if (iymax == 0)
71 iymax = my ny;
72 if (ixmin > ixmax || iymin > iymax)
73 return 0;
74 MelderExtremaWithInit extrema;
75 for (integer iy = iymin; iy <= iymax; iy ++)
76 for (integer ix = ixmin; ix <= ixmax; ix ++)
77 extrema.update (z [iy] [ix]);
78 if (out_minimum)
79 *out_minimum = extrema.min;
80 if (out_maximum)
81 *out_maximum = extrema.max;
82 return (ixmax - ixmin + 1) * (iymax - iymin + 1);
83 }
84
85 void structSPINET :: v1_info () {
86 structDaata :: v1_info ();
87 double miny, maxy, mins, maxs;
88 if (! SampledXY_getWindowExtrema (this, y.get(), 1, nx, 1, ny, & miny, & maxy) ||
89 ! SampledXY_getWindowExtrema (this, s.get(), 1, nx, 1, ny, & mins, & maxs))
90 return;
91
92 MelderInfo_writeLine (U"Minimum power: ", miny);
93 MelderInfo_writeLine (U"Maximum power: ", maxy);
94 MelderInfo_writeLine (U"Minimum power rectified: ", mins);
95 MelderInfo_writeLine (U"Maximum power rectified: ", maxs);
96 }
97
98 autoSPINET SPINET_create (double tmin, double tmax, integer nt, double dt, double t1, double minimumFrequency, double maximumFrequency, integer nFilters, double excitationErbProportion, double inhibitionErbProportion) {
99 try {
100 autoSPINET me = Thing_new (SPINET);
101 const double minErb = NUMhertzToErb (minimumFrequency);
102 const double maxErb = NUMhertzToErb (maximumFrequency);
103 const double dErb = (maxErb - minErb) / nFilters;
104 SampledXY_init (me.get(), tmin, tmax, nt, dt, t1, minErb - dErb / 2.0, maxErb + dErb / 2.0, nFilters, dErb, minErb);
105 my y = zero_MAT (nFilters, nt);
106 my s = zero_MAT (nFilters, nt);
107 my gamma = 4;
108 my excitationErbProportion = excitationErbProportion;
109 my inhibitionErbProportion = inhibitionErbProportion;
110 return me;
111 } catch (MelderError) {
112 Melder_throw (U"SPINET not created.");
113 }
114 }
115
116 void SPINET_spectralRepresentation (SPINET me, Graphics g, double fromTime, double toTime, double fromErb, double toErb, double minimum, double maximum, bool enhanced, bool garnish) {
117
118 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
119 thy z.all() <<= enhanced ? my s.all() : my y.all();
120
121 Matrix_paintCells (thee.get(), g, fromTime, toTime, fromErb, toErb, minimum, maximum);
122 if (garnish) {
123 Graphics_drawInnerBox (g);
124 Graphics_textBottom (g, true, U"Time (s)");
125 Graphics_marksBottom (g, 2, true, true, false);
126 Graphics_textLeft (g, true, U"Frequency (ERB)");
127 Graphics_marksLeft (g, 2, true, true, false);
128 Graphics_textTop (g, false, enhanced ? U"Cooperative interaction output" : U"Gammatone filterbank output");
129 }
130 }
131
132 void SPINET_drawSpectrum (SPINET me, Graphics g, double time, double fromErb, double toErb, double minimum, double maximum, bool enhanced, bool garnish) {
133 const integer icol = Sampled_xToLowIndex (me, time); // ppgb: don't use Sampled2_xToColumn for integer rounding
134 if (icol < 1 || icol > my nx)
135 return;
136 if (toErb <= fromErb) {
137 fromErb = my ymin;
138 toErb = my ymax;
139 }
140 integer ifmin, ifmax;
141 SampledXY_getWindowSamplesY (me, fromErb, toErb, & ifmin, & ifmax);
142 autoVEC spec = column_VEC (enhanced ? my s.get() : my y.get(), icol);
143
144 if (maximum <= minimum)
145 NUMextrema_u (spec.part (ifmin, ifmax), & minimum, & maximum);
146 if (isundef (minimum) || isundef (maximum))
147 return;
148 if (maximum <= minimum) {
149 minimum -= 1;
150 maximum += 1;
151 }
152 VECclip_inplace (minimum, spec.part (ifmin, ifmax), maximum);
153
154 Graphics_setInner (g);
155 Graphics_setWindow (g, fromErb, toErb, minimum, maximum);
156 Graphics_function (g, & spec [0], ifmin, ifmax, SampledXY_indexToY (me, ifmin), SampledXY_indexToY (me, ifmax));
157 Graphics_unsetInner (g);
158 if (garnish) {
159 Graphics_drawInnerBox (g);
160 Graphics_textBottom (g, true, U"Frequency (ERB)");
161 Graphics_marksBottom (g, 2, true, true, false);
162 Graphics_textLeft (g, true, U"strength");
163 Graphics_marksLeft (g, 2, true, true, false);
164 }
165 }
166
167 /* End of file SPINET.cpp */