| 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" |
| 20 | #include "Sound_and_Spectrum.h" |
| 21 | #include "Spectrum_extensions.h" |
| 22 | |
| 23 | Thing_implement (AnalyticSound, Sound, 0); |
| 24 | |
| 25 | double structAnalyticSound :: v_getValueAtSample (integer isamp, 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 = sqrt (sqr (z [1] [isamp]) + sqr (z [2] [isamp])); |
| 34 | else |
| 35 | value = z [which] [isamp]; |
| 36 | return value; |
| 37 | } |
| 38 | |
| 39 | autoAnalyticSound AnalyticSound_create (double xmin, double xmax, integer nx, double dx, double x1) { |
| 40 | try { |
| 41 | autoAnalyticSound me = Thing_new (AnalyticSound); |
| 42 | Matrix_init (me.get(), xmin, xmax, nx, dx, x1, 1.0, 2.0, 2, 1.0, 1.0); |
| 43 | return me; |
| 44 | } catch (MelderError) { |
| 45 | Melder_throw (U"AnalyticSound not created."); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static void Spectrum_into_AnalyticSound (Spectrum me, AnalyticSound thee) { |
| 50 | try { |
| 51 | autoSound him = Spectrum_to_Sound (me); |
| 52 | const integer nx = std::min (his nx, thy nx); |
| 53 | thy z.row (1).part (1, nx) <<= his z.row (1).part (1, nx); |
| 54 | Spectrum_shiftPhaseBy90Degrees (me); |
| 55 | him = Spectrum_to_Sound (me); |
| 56 | thy z.row (2).part (1, nx) <<= his z.row (1).part (1, nx); |
| 57 | Spectrum_unshiftPhaseBy90Degrees (me); // restore original |
| 58 | } catch (MelderError) { |
| 59 | Melder_throw (me, U": cannot ", thee); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void Sound_into_AnalyticSound (Sound me, AnalyticSound thee) { |
| 64 | try { |
| 65 | const integer nx = std::min (my nx, thy nx); |
| 66 | /* |
| 67 | Calculate the Hilbert transform |
| 68 | 20220208: the DFT can be very slow for prime numbers, wait for FFTW? |
| 69 | */ |
| 70 | autoSpectrum spectrum = Sound_to_Spectrum (me, false); |
| 71 | Spectrum_shiftPhaseBy90Degrees (spectrum.get()); |
| 72 | autoSound hilbert = Spectrum_to_Sound (spectrum.get()); |
| 73 | thy z.row (1).part (1, nx) <<= my z.row (1).part (1, nx); |
| 74 | thy z.row (2).part (1, nx) <<= hilbert -> z.row (1).part (1, nx); |
| 75 | } catch (MelderError) { |
| 76 | Melder_throw (me, U": could not convert to AnalyticSound ", thee); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | autoAnalyticSound Sound_to_AnalyticSound (Sound me) { |
| 81 | try { |
| 82 | autoAnalyticSound thee = AnalyticSound_create (my xmin, my xmax, my nx, my dx, my x1); |
| 83 | autoSpectrum spectrum = Sound_to_Spectrum (me, false); |
| 84 | Sound_into_AnalyticSound (me, thee.get()); |
| 85 | return thee; |
| 86 | } catch (MelderError) { |
| 87 | Melder_throw (me, U": could not create AnalyticSound."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | autoSound AnalyticSound_to_Sound (AnalyticSound me) { |
| 92 | try { |
| 93 | auto thee = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1); |
| 94 | thy z.row (1) <<= my z.row (1); |
| 95 | thy z.row (2) <<= my z.row (2); |
| 96 | return thee; |
| 97 | } catch (MelderError) { |
| 98 | Melder_throw (me, U": could not create AnalyticSound."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | autoIntensity AnalyticSound_to_Intensity (AnalyticSound me) { |
| 103 | try { |
| 104 | autoIntensity thee = Intensity_create (my xmin, my xmax, my nx, my dx, my x1); |
| 105 | for (integer i = 1; i <= my nx; i ++) { |
| 106 | const double power_rehearingThreshold = std:: max (sqr (my v_getValueAtSample (i, 0, 0)) / 4.0e-10 , 1e-30); |
| 107 | thy z [1] [i] = 10.0 * log10 (power_rehearingThreshold); |
| 108 | } |
| 109 | return thee; |
| 110 | } catch (MelderError) { |
| 111 | Melder_throw (me, U": could not create Intensity."); |
| 112 | } |
| 113 | } |
| 114 | /* End of file AnalyticSound.h */ |