| 1 | /* Sound_to_Harmonicity_GNE.cpp |
| 2 | * |
| 3 | * Copyright (C) 1999-2012,2015-2021 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 | /* a replication of: |
| 20 | D. Michaelis, T. Gramss & H.W. Strube (1997): |
| 21 | "Glottal-to-noise excitation ratio -- a new measure |
| 22 | for describing pathological voices." |
| 23 | ACUSTICA - acta acustica 83: 700-706. |
| 24 | henceforth abbreviated as "MGS". |
| 25 | */ |
| 26 | |
| 27 | #include "Sound_to_Harmonicity.h"cxx . . $ `<file>/fon/Sound_to_Harmonicity.h`/ |
| 28 | #include "Sound_and_LPC.h"cxx . . $ `<file>/LPC/Sound_and_LPC.h`/ |
| 29 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 30 | |
| 31 | static void bandFiltercxx . . $ bandFilter(bf752cabfe9883ae). (Spectrumcxx . . $ Spectrum# me, double fmid, double bandwidth) { |
| 32 | double *re = & mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0], *im = & mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0]; |
| 33 | double fmin = fmid - bandwidth / 2.0, fmax = fmid + bandwidth / 2.0; |
| 34 | double twopibybandwidth = 2.0 * NUMpicxx . . $ NUMpi. / bandwidth; |
| 35 | for (integercxx . . $ integer# col = 1; col <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; col ++) { |
| 36 | double x = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + (col - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 37 | if (x < fmin || x > fmax) { |
| 38 | re [col] = 0.0; |
| 39 | im [col] = 0.0; |
| 40 | } else { |
| 41 | double factor = 0.5 + 0.5 * coscxx . . $ cos(fee4f1d22bc8e617). (twopibybandwidth * (x - fmid)); |
| 42 | re [col] *= factor; |
| 43 | im [col] *= factor; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | autoMatrixcxx . . $ autoMatrix# Sound_to_Harmonicity_GNEcxx . . $ Sound_to_Harmonicity_GNE(8e823b2def6fea16). (Soundcxx . . $ Sound# me, |
| 49 | double fmin, // 500 Hz |
| 50 | double fmax, // 4500 Hz |
| 51 | double bandwidth, // 1000 Hz |
| 52 | double step) // 80 Hz |
| 53 | { |
| 54 | try { |
| 55 | autoSoundcxx . . $ autoSound# envelopecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8). [1+100]; |
| 56 | integercxx . . $ integer# nenvelopes = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((fmax - fmin) / step); |
| 57 | for (integercxx . . $ integer# ienvelope = 1; ienvelope <= 100; ienvelope ++) |
| 58 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (! envelope [ienvelope].get()); |
| 59 | |
| 60 | /* |
| 61 | * Step 1: down-sampling to 10 kHz, |
| 62 | * in order to be able to flatten the spectrum |
| 63 | * (since the human voice does not contain much above 5 kHz). |
| 64 | */ |
| 65 | autoSoundcxx . . $ autoSound# original10k = Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (me, 10000, 500); |
| 66 | Vector_subtractMeancxx . . $ Vector_subtractMean(8ab8c8e01f03cabf). (original10k.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 67 | double duration = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.; |
| 68 | |
| 69 | /* |
| 70 | * Step 2: inverse filtering of the speech signal |
| 71 | * by 13th-order "autocorrelation method" |
| 72 | * with a Gaussian (not Hann, like MGS!) window of 30 ms length |
| 73 | * and 10 ms shift between successive frames. |
| 74 | * Since we need a spectrally flat signal (not an approximation |
| 75 | * of the source signal), we must turn the pre-emphasis off |
| 76 | * (by setting its turnover point at 1,000,000,000 Hz); |
| 77 | * otherwise, the pre-emphasis would cause an overestimation |
| 78 | * in the LPC object of the high frequencies, so that inverse |
| 79 | * filtering would yield weakened high frequencies. |
| 80 | */ |
| 81 | autoLPCcxx . . $ autoLPC# lpc = Sound_to_LPC_autocxx . . $ Sound_to_LPC_auto(ed41325f50f429a4). (original10k.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 13, 30e-3, 10e-3, 1e9); |
| 82 | autoSoundcxx . . $ autoSound# flat = LPC_Sound_filterInversecxx . . $ LPC_Sound_filterInverse(f4bce7e21db85b5). (lpc.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), original10k.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 83 | autoSpectrumcxx . . $ autoSpectrum# flatSpectrum = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (flat.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); |
| 84 | autoSpectrumcxx . . $ autoSpectrum# hilbertSpectrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (flatSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 85 | for (integercxx . . $ integer# col = 1; col <= hilbertSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; col ++) { |
| 86 | hilbertSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col] = flatSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col]; |
| 87 | hilbertSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col] = - flatSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col]; |
| 88 | } |
| 89 | double fmid = fmin; |
| 90 | integercxx . . $ integer# ienvelope = 1; |
| 91 | autoMelderMonitorcxx . . $ autoMelderMonitor# monitorcxx . . $ autoMelderMonitor#autoMelderMonitor(ff00d524ab0355ec). (U"Computing Hilbert envelopes..."); |
| 92 | while (fmid <= fmax) { |
| 93 | /* |
| 94 | * Step 3: calculate Hilbert envelopes of bands. |
| 95 | */ |
| 96 | autoSpectrumcxx . . $ autoSpectrum# bandSpectrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (flatSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 97 | autoSpectrumcxx . . $ autoSpectrum# hilbertBandSpectrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (hilbertSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 98 | /* |
| 99 | * 3a: Filter both the spectrum of the original flat sound and its Hilbert transform. |
| 100 | */ |
| 101 | bandFiltercxx . . $ bandFilter(bf752cabfe9883ae). (bandSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), fmid, bandwidth); |
| 102 | bandFiltercxx . . $ bandFilter(bf752cabfe9883ae). (hilbertBandSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), fmid, bandwidth); |
| 103 | /* |
| 104 | * 3b: Create both the band-filtered flat sound and its Hilbert transform. |
| 105 | */ |
| 106 | autoSoundcxx . . $ autoSound# band = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (bandSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 107 | /*if (graphics) { |
| 108 | Graphics_beginMovieFrame (graphics, & Melder_WHITE); |
| 109 | Spectrum_draw (bandSpectrum, graphics, 0, 5000, 0, 0, true); |
| 110 | Graphics_endMovieFrame (graphics, 0.0); |
| 111 | }*/ |
| 112 | Melder_monitorcxx . . $ Melder_monitor(71ac93a27c32a1fa). (ienvelope / (nenvelopes + 1.0), U"Computing Hilbert envelope ", ienvelope, U"..."); |
| 113 | autoSoundcxx . . $ autoSound# hilbertBand = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (hilbertBandSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 114 | envelope [ienvelope] =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Sound_extractPartcxx . . $ Sound_extractPart(e1651eb055dd2f09). (band.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, duration, kSound_windowShapecxx . . $ kSound_windowShape#::RECTANGULARcxx . . $ kSound_windowShape#RECTANGULAR., 1.0, true); |
| 115 | /* |
| 116 | * 3c: Compute the Hilbert envelope of the band-passed flat signal. |
| 117 | */ |
| 118 | for (integercxx . . $ integer# col = 1; col <= envelope [ienvelope] ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; col ++) { |
| 119 | double self = envelope [ienvelope] ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col], other = hilbertBand ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col]; |
| 120 | envelope [ienvelope] ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col] = hypotcxx . . $ hypot(b71928d39fde4295). (self, other); |
| 121 | } |
| 122 | Vector_subtractMeancxx . . $ Vector_subtractMean(8ab8c8e01f03cabf). (envelope [ienvelope].getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 123 | /* |
| 124 | * Next band. |
| 125 | */ |
| 126 | fmid += step; |
| 127 | ienvelope += 1; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Step 4: crosscorrelation |
| 132 | */ |
| 133 | nenvelopes = ienvelope - 1; |
| 134 | autoMatrixcxx . . $ autoMatrix# cc = Matrix_createSimplecxx . . $ Matrix_createSimple(1d1e893fc69cdb2f). (nenvelopes, nenvelopes); |
| 135 | for (integercxx . . $ integer# row = 2; row <= nenvelopes; row ++) { |
| 136 | for (integercxx . . $ integer# col = 1; col <= row - 1; col ++) { |
| 137 | autoSoundcxx . . $ autoSound# crossCorrelation = Sounds_crossCorrelate_shortcxx . . $ Sounds_crossCorrelate_short(619b4afbd70d9931). (envelope [row].getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), envelope [col].getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), -3.1e-4, 3.1e-4, true); |
| 138 | /* |
| 139 | * Step 5: the maximum of each correlation function |
| 140 | */ |
| 141 | double ccmax = Vector_getMaximumcxx . . $ Vector_getMaximum(9e5d98b21939784c). (crossCorrelation.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.0, 0.0, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: NONEcxx . . $ kVector_peakInterpolation#NONE.); |
| 142 | cc ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).row] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col] = ccmax; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Step 6: maximum of the maxima, ignoring those too close to the diagonal. |
| 148 | */ |
| 149 | for (integercxx . . $ integer# row = 2; row <= nenvelopes; row ++) { |
| 150 | for (integercxx . . $ integer# col = 1; col <= row - 1; col ++) { |
| 151 | if (integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (row - col) < bandwidth / 2.0 / step) { |
| 152 | cc ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).row] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).col] = 0.0; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return cccxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 158 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 159 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Harmonicity (GNE)."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* End of file Sound_to_Harmonicity_GNE.cpp */ |