Debian Codegraph @

fon/SoundSet.cpp

raw symbols SCIP
1 /* SoundSet.cpp
2 *
3 * Copyright (C) 2019-2021,2023 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 "SoundSet.h"
20
21 Thing_implement (SoundSet, Ordered, 0);
22
23 integer SoundSet_getMinimumNumberOfSamples (SoundSet me) {
24 integer result = INTEGER_MAX;
25 for (integer isound = 1; isound <= my size; isound ++)
26 if (my at [isound] -> nx < result)
27 result = my at [isound] -> nx;
28 return result;
29 }
30
31 autoMAT SoundSet_getRandomizedPatterns (SoundSet me, integer numberOfPatterns, integer patternSize) {
32 try {
33 const integer minimumNumberOfSamples = SoundSet_getMinimumNumberOfSamples (me);
34 Melder_require (patternSize <= minimumNumberOfSamples,
35 U"The pattern size cannot be ", patternSize, U", because there is a Sound that is only ", minimumNumberOfSamples, U" samples long.");
36 autoMAT result = zero_MAT (numberOfPatterns, patternSize);
37 for (integer ipattern = 1; ipattern <= numberOfPatterns; ipattern ++) {
38 const integer soundNumber = NUMrandomInteger (1, my size);
39 Sound sound = my at [soundNumber];
40 const integer numberOfSamples = sound -> nx;
41 const integer endSample = NUMrandomInteger (patternSize, numberOfSamples);
42 const integer startSample = endSample - (patternSize - 1);
43 Melder_assert (startSample >= 1);
44 const constVEC samples = sound -> z.row (1);
45 result.row (ipattern) <<= samples.part (startSample, endSample);
46 }
47 return result;
48 } catch (MelderError) {
49 Melder_throw (me, U": no randomize patterns gotten.");
50 }
51 }
52
53 void SoundSet_Table_getRandomizedPatterns (SoundSet me, Table thee, conststring32 columnName, integer numberOfPatterns, integer inputSize, integer outputSize,
54 autoPatternList *out_inputs, autoPatternList *out_outputs)
55 {
56 try {
57 Melder_require (thy rows.size == my size,
58 U"The number of rows of ", thee, U" should be equal to the number of elements of ", me);
59 const integer columnNumber = Table_columnNameToNumber_e (thee, columnName);
60 const integer minimumNumberOfSamples = SoundSet_getMinimumNumberOfSamples (me);
61 Melder_require (inputSize <= minimumNumberOfSamples,
62 U"The input size cannot be ", inputSize, U", because there is a Sound that is only ", minimumNumberOfSamples, U" samples long.");
63 Table_numericize_a (thee, columnNumber);
64 autoPatternList inputs = PatternList_create (numberOfPatterns, inputSize);
65 autoPatternList outputs = PatternList_create (numberOfPatterns, outputSize);
66 for (integer ipattern = 1; ipattern <= numberOfPatterns; ipattern ++) {
67 const integer soundNumber = NUMrandomInteger (1, my size);
68 Sound sound = my at [soundNumber];
69 const integer numberOfSamples = sound -> nx;
70 const integer endSample = NUMrandomInteger (inputSize, numberOfSamples);
71 const integer startSample = endSample - (inputSize - 1);
72 Melder_assert (startSample >= 1);
73 const constVEC samples = sound -> z.row (1);
74 inputs -> z.row (ipattern) <<= samples.part (startSample, endSample);
75 integer classNumber = Melder_iround (thy rows.at [soundNumber] -> cells [columnNumber]. number);
76 Melder_require (classNumber >= 1 && classNumber <= outputSize,
77 U"The class number should be between 1 and ", outputSize, U", not ", classNumber, U".");
78 outputs -> z [ipattern] [classNumber] = 1.0;
79 }
80 if (out_inputs)
81 *out_inputs = inputs.move();
82 if (out_outputs)
83 *out_outputs = outputs.move();
84 } catch (MelderError) {
85 Melder_throw (me, U" and ", thee, U": no randomized patterns gotten.");
86 }
87 }
88
89 /* End of file SoundSet.cpp */