| 1 | /* AnalyticSound.cpp |
| 2 | * |
| 3 | * Copyright (C) 2021-2022 David Weenink |
| 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 | #include "AnalyticSound.h"cxx . . $ `<file>/dwtools/AnalyticSound.h`/ |
| 20 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 21 | #include "Spectrum_extensions.h"cxx . . $ `<file>/dwtools/Spectrum_extensions.h`/ |
| 22 | |
| 23 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (AnalyticSound, Sound, 0); |
| 24 | |
| 25 | double structAnalyticSoundcxx . . $ structAnalyticSound# :: v_getValueAtSamplecxx . . $ structAnalyticSound#v_getValueAtSample(8bbfc6ce471470c9). (integercxx . . $ integer# isamp, integercxx . . $ integer# which, int /* unit */) const { |
| 26 | /* |
| 27 | Preconditions: |
| 28 | 1 <= isample <= nx |
| 29 | 0 <= which <= ny |
| 30 | */ |
| 31 | double value; |
| 32 | if (which == 0) |
| 33 | value = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (sqrcxx . . $ sqr(ada6a8422704cf8a). (zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]) + sqrcxx . . $ sqr(ada6a8422704cf8a). (zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp])); |
| 34 | else |
| 35 | value = zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).which] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]; |
| 36 | return value; |
| 37 | } |
| 38 | |
| 39 | autoAnalyticSoundcxx . . $ autoAnalyticSound# AnalyticSound_createcxx . . $ AnalyticSound_create(3663e8d9e13c92b4). (double xmin, double xmax, integercxx . . $ integer# nx, double dx, double x1) { |
| 40 | try { |
| 41 | autoAnalyticSoundcxx . . $ autoAnalyticSound# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (AnalyticSound); |
| 42 | Matrix_initcxx . . $ Matrix_init(9dd6c49f9830a091). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax, nx, dx, x1, 1.0, 2.0, 2, 1.0, 1.0); |
| 43 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 44 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 45 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"AnalyticSound not created."); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static void Spectrum_into_AnalyticSoundcxx . . $ Spectrum_into_AnalyticSound(7ad569b0d3db146d). (Spectrumcxx . . $ Spectrum# me, AnalyticSoundcxx . . $ AnalyticSound# thee) { |
| 50 | try { |
| 51 | autoSoundcxx . . $ autoSound# him = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (me); |
| 52 | const integercxx . . $ integer# nx = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx., thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.); |
| 53 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx) <<=cxx . . $ `operator<<=`(adea042513329390). hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx); |
| 54 | Spectrum_shiftPhaseBy90Degreescxx . . $ Spectrum_shiftPhaseBy90Degrees(9136b6455db7f37). (me); |
| 55 | him =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (me); |
| 56 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx) <<=cxx . . $ `operator<<=`(adea042513329390). hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx); |
| 57 | Spectrum_unshiftPhaseBy90Degreescxx . . $ Spectrum_unshiftPhaseBy90Degrees(9136b6455db7f37). (me); // restore original |
| 58 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 59 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": cannot ", thee); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Sound_into_AnalyticSoundcxx . . $ Sound_into_AnalyticSound(8c60cad3a877f832). (Soundcxx . . $ Sound# me, AnalyticSoundcxx . . $ AnalyticSound# thee) { |
| 64 | try { |
| 65 | const integercxx . . $ integer# nx = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.); |
| 66 | /* |
| 67 | Calculate the Hilbert transform |
| 68 | 20220208: the DFT can be very slow for prime numbers, wait for FFTW? |
| 69 | */ |
| 70 | autoSpectrumcxx . . $ autoSpectrum# spectrum = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (me, false); |
| 71 | Spectrum_shiftPhaseBy90Degreescxx . . $ Spectrum_shiftPhaseBy90Degrees(9136b6455db7f37). (spectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 72 | autoSoundcxx . . $ autoSound# hilbert = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (spectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 73 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx) <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx); |
| 74 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx) <<=cxx . . $ `operator<<=`(adea042513329390). hilbertcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nx); |
| 75 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 76 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not convert to AnalyticSound ", thee); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | autoAnalyticSoundcxx . . $ autoAnalyticSound# Sound_to_AnalyticSoundcxx . . $ Sound_to_AnalyticSound(c91414eca4bcdab1). (Soundcxx . . $ Sound# me) { |
| 81 | try { |
| 82 | autoAnalyticSoundcxx . . $ autoAnalyticSound# thee = AnalyticSound_createcxx . . $ AnalyticSound_create(3663e8d9e13c92b4). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.); |
| 83 | autoSpectrumcxx . . $ autoSpectrum# spectrum = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (me, false); |
| 84 | Sound_into_AnalyticSoundcxx . . $ Sound_into_AnalyticSound(8c60cad3a877f832). (me, thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 85 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 86 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 87 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not create AnalyticSound."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | autoSoundcxx . . $ autoSound# AnalyticSound_to_Soundcxx . . $ AnalyticSound_to_Sound(e40d05aa54df2f1e). (AnalyticSoundcxx . . $ AnalyticSound# me) { |
| 92 | try { |
| 93 | auto thee = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.); |
| 94 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 95 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2) <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2); |
| 96 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 97 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 98 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not create AnalyticSound."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | autoIntensitycxx . . $ autoIntensity# AnalyticSound_to_Intensitycxx . . $ AnalyticSound_to_Intensity(4b690d3b08e0f604). (AnalyticSoundcxx . . $ AnalyticSound# me) { |
| 103 | try { |
| 104 | autoIntensitycxx . . $ autoIntensity# thee = Intensity_createcxx . . $ Intensity_create(83c615748dcd7df). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.); |
| 105 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 106 | const double power_rehearingThreshold = stdcxx . . $ std/:: maxcxx . . $ std/max(2a338fce5842aa6a). (sqrcxx . . $ sqr(ada6a8422704cf8a). (mycxx . . $ `melder/melder.h:44:9`! v_getValueAtSamplecxx . . $ structAnalyticSound#v_getValueAtSample(8bbfc6ce471470c9). (i, 0, 0)) / 4.0e-10 , 1e-30); |
| 107 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 10.0 * log10cxx . . $ log10(fee4f1d22bc8e617). (power_rehearingThreshold); |
| 108 | } |
| 109 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 110 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 111 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not create Intensity."); |
| 112 | } |
| 113 | } |
| 114 | /* End of file AnalyticSound.h */ |