| 1 | /* Sound_to_PointProcess.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011,2014-2018,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 "Sound_to_PointProcess.h" |
| 20 | #include "Sound_to_Pitch.h" |
| 21 | #include "Pitch_to_PointProcess.h" |
| 22 | |
| 23 | autoPointProcess Sound_to_PointProcess_extrema (Sound me, integer channelNumber, kVector_peakInterpolation peakInterpolationType, bool includeMaxima, bool includeMinima) { |
| 24 | try { |
| 25 | /* |
| 26 | * Pass 1: count the extrema. There may be a maximum and minimum in the same interval! |
| 27 | */ |
| 28 | integer numberOfMaxima = 0; |
| 29 | integer numberOfMinima = 0; |
| 30 | constVEC y = my z.row (channelNumber); |
| 31 | for (integer i = 2; i <= my nx - 1; i ++) { |
| 32 | if (includeMaxima && y [i] > y [i - 1] && y [i] >= y [i + 1]) |
| 33 | numberOfMaxima ++; |
| 34 | if (includeMinima && y [i] <= y [i - 1] && y [i] < y [i + 1]) |
| 35 | numberOfMinima ++; |
| 36 | } |
| 37 | /* |
| 38 | * Create the empty result. |
| 39 | */ |
| 40 | autoPointProcess thee = PointProcess_create (my xmin, my xmax, numberOfMaxima + numberOfMinima); |
| 41 | /* |
| 42 | * Pass 2: compute and register the extrema. |
| 43 | */ |
| 44 | const integer interpolationDepth = kVector_peakInterpolation_to_interpolationDepth (peakInterpolationType); |
| 45 | for (integer i = 2; i <= my nx - 1; i ++) { |
| 46 | double time, i_real; |
| 47 | if (includeMaxima && y [i] > y [i - 1] && y [i] >= y [i + 1]) { |
| 48 | (void) NUMimproveMaximum (y, i, interpolationDepth, & i_real); |
| 49 | time = my x1 + (i_real - 1.0) * my dx; |
| 50 | PointProcess_addPoint (thee.get(), time); |
| 51 | } |
| 52 | if (includeMinima && y [i] <= y [i - 1] && y [i] < y [i + 1]) { |
| 53 | (void) NUMimproveMinimum (y, i, interpolationDepth, & i_real); |
| 54 | time = my x1 + (i_real - 1.0) * my dx; |
| 55 | PointProcess_addPoint (thee.get(), time); |
| 56 | } |
| 57 | } |
| 58 | return thee; |
| 59 | } catch (MelderError) { |
| 60 | Melder_throw (me, U": extrema not computed."); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | autoPointProcess Sound_to_PointProcess_maxima (Sound me, integer channel, kVector_peakInterpolation peakInterpolationType) { |
| 65 | return Sound_to_PointProcess_extrema (me, channel, peakInterpolationType, true, false); |
| 66 | } |
| 67 | autoPointProcess Sound_to_PointProcess_minima (Sound me, integer channel, kVector_peakInterpolation peakInterpolationType) { |
| 68 | return Sound_to_PointProcess_extrema (me, channel, peakInterpolationType, false, true); |
| 69 | } |
| 70 | autoPointProcess Sound_to_PointProcess_allExtrema (Sound me, integer channel, kVector_peakInterpolation peakInterpolationType) { |
| 71 | return Sound_to_PointProcess_extrema (me, channel, peakInterpolationType, true, true); |
| 72 | } |
| 73 | |
| 74 | autoPointProcess Sound_to_PointProcess_zeroes (Sound me, integer channel, bool includeRaisers, bool includeFallers) { |
| 75 | try { |
| 76 | /* |
| 77 | * Pass 1: count the zeroes. |
| 78 | */ |
| 79 | integer numberOfRaisers = 0; |
| 80 | integer numberOfFallers = 0; |
| 81 | double *y = & my z [channel] [0]; |
| 82 | for (integer i = 2; i <= my nx; i ++) { |
| 83 | if (includeRaisers && y [i - 1] < 0.0 && y [i] >= 0.0) numberOfRaisers ++; |
| 84 | if (includeFallers && y [i - 1] >= 0.0 && y [i] < 0.0) numberOfFallers ++; |
| 85 | } |
| 86 | /* |
| 87 | * Create the empty result. |
| 88 | */ |
| 89 | autoPointProcess thee = PointProcess_create (my xmin, my xmax, numberOfRaisers + numberOfFallers); |
| 90 | /* |
| 91 | * Pass 2: compute and register the zeroes. |
| 92 | */ |
| 93 | for (integer i = 2; i <= my nx; i ++) { |
| 94 | if ((includeRaisers && y [i - 1] < 0.0 && y [i] >= 0.0) || |
| 95 | (includeFallers && y [i - 1] >= 0.0 && y [i] < 0.0)) |
| 96 | { |
| 97 | double time = Sampled_indexToX (me, i - 1) + my dx * y [i - 1] / (y [i - 1] - y [i]); // linear |
| 98 | PointProcess_addPoint (thee.get(), time); |
| 99 | } |
| 100 | } |
| 101 | return thee; |
| 102 | } catch (MelderError) { |
| 103 | Melder_throw (me, U": zeroes not computed."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | autoPointProcess Sound_to_PointProcess_periodic_cc (Sound me, double fmin, double fmax) { |
| 108 | try { |
| 109 | autoPitch pitch = Sound_to_Pitch (me, 0.0, fmin, fmax); |
| 110 | autoPointProcess thee = Sound_Pitch_to_PointProcess_cc (me, pitch.get()); |
| 111 | return thee; |
| 112 | } catch (MelderError) { |
| 113 | Melder_throw (me, U": periodic pulses (cc) not computed."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | autoPointProcess Sound_to_PointProcess_periodic_peaks (Sound me, double fmin, double fmax, bool includeMaxima, bool includeMinima) { |
| 118 | try { |
| 119 | autoPitch pitch = Sound_to_Pitch (me, 0.0, fmin, fmax); |
| 120 | autoPointProcess thee = Sound_Pitch_to_PointProcess_peaks (me, pitch.get(), includeMaxima, includeMinima); |
| 121 | return thee; |
| 122 | } catch (MelderError) { |
| 123 | Melder_throw (me, U": periodic pulses (peaks) not computed."); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* End of file Sound_to_PointProcess.cpp */ |