| 1 | /* Sound_and_Spectrogram_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2024 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 | /* |
| 20 | djmw 20010718 |
| 21 | djmw 20020813 GPL header. |
| 22 | djmw 20041124 Changed call to Sound_to_Spectrum. |
| 23 | djmw 20070103 Sound interface changes |
| 24 | djmw 20071107 Errors/warnings text changes |
| 25 | djmw 20071202 Melder_warning<n> |
| 26 | */ |
| 27 | |
| 28 | #include "Sound_and_Spectrogram_extensions.h" |
| 29 | #include "Sound_extensions.h" |
| 30 | #include "Sound_and_Spectrum.h" |
| 31 | #include "Sound_to_Pitch.h" |
| 32 | #include "Vector.h" |
| 33 | #include "NUM2.h" |
| 34 | |
| 35 | autoSound BandFilterSpectrogram_as_Sound (BandFilterSpectrogram me, int to_dB); |
| 36 | |
| 37 | /* |
| 38 | The gaussian(x) = (exp(-48*((i-(n+1)/2)/(n+1))^2)-exp(-12))/(1-exp(-12)); |
| 39 | For power we need the area under the square of this window: |
| 40 | Integrate (gaussian(i)^2,i=1..n) = |
| 41 | (sqrt(Pi)*sqrt(3)*sqrt(2)*erf(2*(n-1)*sqrt(3)*sqrt(2)/(n+1))*(n+1) + 24*exp(-24)*(n-1)+ |
| 42 | -4*sqrt(Pi)*sqrt(3)*exp(-12)*erf(2*(n-1)*sqrt(3)/(n+1))*(n+1))/ (24 * (-1+exp(-12))^2), |
| 43 | where erf(x) = 1 - erfc(x) and n is the windowLength in samples. |
| 44 | To compare with the rectangular window we need to divide this by the window width (n -1) x 1^2. |
| 45 | */ |
| 46 | static void _Spectrogram_windowCorrection (Spectrogram me, integer numberOfSamples_window) { |
| 47 | double windowFactor = 1.0; |
| 48 | if (numberOfSamples_window > 1) { |
| 49 | const double e12 = exp (-12); |
| 50 | const double denum = (e12 - 1) * (e12 - 1.0) * 24 * (numberOfSamples_window - 1); |
| 51 | const double arg1 = 2.0 * NUMsqrt3 * (numberOfSamples_window - 1) / (numberOfSamples_window + 1); |
| 52 | const double arg2 = arg1 * NUMsqrt2; |
| 53 | const double p2 = NUMsqrtpi * NUMsqrt3 * NUMsqrt2 * (1 - NUMerfcc (arg2)) * (numberOfSamples_window + 1); |
| 54 | const double p1 = 4 * NUMsqrtpi * NUMsqrt3 * e12 * (1 - NUMerfcc (arg1)) * (numberOfSamples_window + 1); |
| 55 | windowFactor = (p2 - p1 + 24 * (numberOfSamples_window - 1) * e12 * e12) / denum; |
| 56 | } |
| 57 | my z.get() /= windowFactor; |
| 58 | } |
| 59 | |
| 60 | static autoSpectrum Sound_to_Spectrum_power (Sound me) { |
| 61 | try { |
| 62 | autoSpectrum thee = Sound_to_Spectrum (me, true); |
| 63 | double scale = 2.0 * thy dx / (my xmax - my xmin); |
| 64 | /* |
| 65 | factor '2' because we combine positive and negative frequencies |
| 66 | thy dx : width of frequency bin |
| 67 | my xmax - my xmin : duration of sound |
| 68 | */ |
| 69 | VEC re = thy z.row (1), im = thy z.row (2); |
| 70 | for (integer i = 1; i <= thy nx; i ++) { |
| 71 | const double power = scale * (re [i] * re [i] + im [i] * im [i]); |
| 72 | re [i] = power; |
| 73 | im [i] = 0.0; |
| 74 | } |
| 75 | /* |
| 76 | Correction of frequency bins at 0 Hz and nyquist: don't count for two. |
| 77 | */ |
| 78 | re [1] *= 0.5; |
| 79 | re [thy nx] *= 0.5; |
| 80 | return thee; |
| 81 | } catch (MelderError) { |
| 82 | Melder_throw (me, U": no Spectrum with spectral power created."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void Sound_into_BarkSpectrogram_frame (Sound me, BarkSpectrogram thee, integer frame) { |
| 87 | autoSpectrum him = Sound_to_Spectrum_power (me); |
| 88 | integer numberOfFrequencies = his nx; |
| 89 | |
| 90 | autoVEC z = raw_VEC (numberOfFrequencies); |
| 91 | for (integer ifreq = 1; ifreq <= numberOfFrequencies; ifreq ++) { |
| 92 | const double frequency_Hz = his x1 + (ifreq - 1) * his dx; |
| 93 | z [ifreq] = thy v_hertzToFrequency (frequency_Hz); |
| 94 | } |
| 95 | |
| 96 | for (integer i = 1; i <= thy ny; i ++) { |
| 97 | const double z0 = thy y1 + (i - 1) * thy dy; |
| 98 | constVEC pow = his z.row (1); // TODO ?? |
| 99 | longdouble p = 0.0; |
| 100 | for (integer ifreq = 1; ifreq <= numberOfFrequencies; ifreq ++) { |
| 101 | /* |
| 102 | Sekey & Hanson filter is defined in the power domain. |
| 103 | We therefore multiply the power with a (and not a^2). |
| 104 | integral (F(z),z=0..25) = 1.58/9 |
| 105 | */ |
| 106 | const double a = NUMsekeyhansonfilter_amplitude (z0, z [ifreq]); |
| 107 | p += a * pow [ifreq] ; |
| 108 | } |
| 109 | thy z [i] [frame] = double (p); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | autoBarkSpectrogram Sound_to_BarkSpectrogram (Sound me, double analysisWidth, double dt, double f1_bark, double fmax_bark, double df_bark) { |
| 114 | try { |
| 115 | const double samplingFrequency = 1.0 / my dx, nyquist = 0.5 * samplingFrequency; |
| 116 | const double windowDuration = 2.0 * analysisWidth; /* gaussian window */ |
| 117 | const double zmax = NUMhertzToBark (nyquist); |
| 118 | double fmin_bark = 0.0; |
| 119 | |
| 120 | // Check defaults. |
| 121 | |
| 122 | if (f1_bark <= 0.0) |
| 123 | f1_bark = 1.0; |
| 124 | if (fmax_bark <= 0.0) |
| 125 | fmax_bark = zmax; |
| 126 | if (df_bark <= 0.0) |
| 127 | df_bark = 1.0; |
| 128 | |
| 129 | fmax_bark = std::min (fmax_bark, zmax); |
| 130 | const integer numberOfFilters = Melder_iround ( (fmax_bark - f1_bark) / df_bark); |
| 131 | Melder_require (numberOfFilters > 0, |
| 132 | U"The combination of filter parameters is not valid."); |
| 133 | |
| 134 | integer numberOfFrames; |
| 135 | double t1; |
| 136 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 137 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 138 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 139 | autoBarkSpectrogram thee = BarkSpectrogram_create (my xmin, my xmax, numberOfFrames, dt, t1, fmin_bark, fmax_bark, numberOfFilters, df_bark, f1_bark); |
| 140 | |
| 141 | autoMelderProgress progess (U"Sound to BarkSpectrogram..."); |
| 142 | |
| 143 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 144 | const double t = Sampled_indexToX (thee.get(), iframe); |
| 145 | |
| 146 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2.0); |
| 147 | Sounds_multiply (sframe.get(), window.get()); |
| 148 | Sound_into_BarkSpectrogram_frame (sframe.get(), thee.get(), iframe); |
| 149 | |
| 150 | if (iframe % 10 == 1) |
| 151 | Melder_progress ( (double) iframe / numberOfFrames, U"BarkSpectrogram analysis: frame ", |
| 152 | iframe, U" from ", numberOfFrames, U"."); |
| 153 | } |
| 154 | |
| 155 | _Spectrogram_windowCorrection ((Spectrogram) thee.get(), window -> nx); |
| 156 | |
| 157 | return thee; |
| 158 | } catch (MelderError) { |
| 159 | Melder_throw (me, U": no BarkSpectrogram created."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void Sound_into_MelSpectrogram_frame (Sound me, MelSpectrogram thee, integer frame) { |
| 164 | autoSpectrum him = Sound_to_Spectrum_power (me); |
| 165 | |
| 166 | for (integer ifilter = 1; ifilter <= thy ny; ifilter ++) { |
| 167 | longdouble power = 0.0; |
| 168 | const double fc_mel = thy y1 + (ifilter - 1) * thy dy; |
| 169 | const double fc_hz = thy v_frequencyToHertz (fc_mel); |
| 170 | const double fl_hz = thy v_frequencyToHertz (std::max (fc_mel - thy dy, 0.0)); |
| 171 | const double fh_hz = thy v_frequencyToHertz (std::min (fc_mel + thy dy, his xmax)); |
| 172 | integer ifrom, ito; |
| 173 | Sampled_getWindowSamples (him.get(), fl_hz, fh_hz, & ifrom, & ito); |
| 174 | for (integer i = ifrom; i <= ito; i ++) { |
| 175 | /* |
| 176 | Bin with a triangular filter the power (= amplitude-squared) |
| 177 | */ |
| 178 | const double f = his x1 + (i - 1) * his dx; |
| 179 | const double a = NUMtriangularfilter_amplitude (fl_hz, fc_hz, fh_hz, f); |
| 180 | power += a * his z [1] [i]; |
| 181 | } |
| 182 | thy z [ifilter] [frame] = double (power); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | autoMelSpectrogram Sound_to_MelSpectrogram (Sound me, double analysisWidth, double dt, double f1_mel, double fmax_mel, double df_mel) { |
| 187 | try { |
| 188 | const double samplingFrequency = 1.0 / my dx, nyquist = 0.5 * samplingFrequency; |
| 189 | const double windowDuration = 2.0 * analysisWidth; // Gaussian window |
| 190 | double fmin_mel = 0.0; |
| 191 | const double fbottom = NUMhertzToMel2 (100.0), fceiling = NUMhertzToMel2 (nyquist); |
| 192 | |
| 193 | // Check defaults. |
| 194 | |
| 195 | if (fmax_mel <= 0.0 || fmax_mel > fceiling) |
| 196 | fmax_mel = fceiling; |
| 197 | if (fmax_mel <= f1_mel) { |
| 198 | f1_mel = fbottom; |
| 199 | fmax_mel = fceiling; |
| 200 | } |
| 201 | if (f1_mel <= 0.0) |
| 202 | f1_mel = fbottom; |
| 203 | if (df_mel <= 0.0) |
| 204 | df_mel = 100.0; |
| 205 | |
| 206 | // Determine the number of filters. |
| 207 | |
| 208 | const integer numberOfFilters = Melder_iround ((fmax_mel - f1_mel) / df_mel); |
| 209 | |
| 210 | |
| 211 | integer numberOfFrames; |
| 212 | double t1; |
| 213 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 214 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 215 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 216 | autoMelSpectrogram thee = MelSpectrogram_create (my xmin, my xmax, numberOfFrames, dt, t1, fmin_mel, fmax_mel, numberOfFilters, df_mel, f1_mel); |
| 217 | |
| 218 | autoMelderProgress progress (U"Sound to MelSpectrogram..."); |
| 219 | |
| 220 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 221 | const double t = Sampled_indexToX (thee.get(), iframe); |
| 222 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2.0); |
| 223 | Sounds_multiply (sframe.get(), window.get()); |
| 224 | Sound_into_MelSpectrogram_frame (sframe.get(), thee.get(), iframe); |
| 225 | |
| 226 | if (iframe % 10 == 1) |
| 227 | Melder_progress ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ", numberOfFrames, U"."); |
| 228 | } |
| 229 | |
| 230 | _Spectrogram_windowCorrection ((Spectrogram) thee.get(), window -> nx); |
| 231 | |
| 232 | return thee; |
| 233 | } catch (MelderError) { |
| 234 | Melder_throw (me, U": No MelSpectrogram created."); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | Analog formant filter response : |
| 240 | H(f) = i f B / (f1^2 - f^2 + i f B) |
| 241 | */ |
| 242 | static int Sound_into_Spectrogram_frame (Sound me, Spectrogram thee, integer frame, double bw) { |
| 243 | Melder_assert (bw > 0.0); |
| 244 | autoSpectrum him = Sound_to_Spectrum_power (me); |
| 245 | |
| 246 | for (integer ifilter = 1; ifilter <= thy ny; ifilter ++) { |
| 247 | const double fc = thy y1 + (ifilter - 1) * thy dy; |
| 248 | constVEC pow = his z.row (1); |
| 249 | double p = 0.0; |
| 250 | for (integer ifreq = 1; ifreq <= his nx; ifreq ++) { |
| 251 | /* |
| 252 | H(f) = ifB / (fc^2 - f^2 + ifB) |
| 253 | H(f)| = fB / sqrt ((fc^2 - f^2)^2 + f^2B^2) |
| 254 | |H(f)|^2 = f^2B^2 / ((fc^2 - f^2)^2 + f^2B^2) |
| 255 | = 1 / (((fc^2 - f^2) /fB)^2 + 1) |
| 256 | */ |
| 257 | const double f = his x1 + (ifreq - 1) * his dx; |
| 258 | const double a = NUMformantfilter_amplitude (fc, bw, f); |
| 259 | p += a * pow [ifreq]; |
| 260 | } |
| 261 | thy z [ifilter] [frame] = p; |
| 262 | } |
| 263 | return 1; |
| 264 | } |
| 265 | |
| 266 | autoSpectrogram Sound_to_Spectrogram_pitchDependent (Sound me, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw, |
| 267 | double pitchFloor, double pitchCeiling) |
| 268 | { |
| 269 | try { |
| 270 | autoPitch thee = Sound_to_Pitch (me, dt, pitchFloor, pitchCeiling); |
| 271 | return Sound_Pitch_to_Spectrogram (me, thee.get(), analysisWidth, dt, f1_hz, fmax_hz, df_hz, relative_bw); |
| 272 | } catch (MelderError) { |
| 273 | Melder_throw (me, U": no Spectrogram created."); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | autoSpectrogram Sound_Pitch_to_Spectrogram (Sound me, Pitch thee, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw) { |
| 278 | try { |
| 279 | const double windowDuration = 2.0 * analysisWidth; /* gaussian window */ |
| 280 | const double nyquist = 0.5 / my dx, samplingFrequency = 1.0 / my dx, fmin_hz = 0.0; |
| 281 | |
| 282 | Melder_require (my xmin >= thy xmin && my xmax <= thy xmax, |
| 283 | U"The domain of the Sound should be included in the domain of the Pitch."); |
| 284 | |
| 285 | double f0_median = Pitch_getQuantile (thee, thy xmin, thy xmax, 0.5, kPitch_unit::HERTZ); |
| 286 | |
| 287 | if (isundef (f0_median) || f0_median == 0.0) { |
| 288 | f0_median = 100.0; |
| 289 | Melder_warning (U"Pitch values undefined. Bandwidth fixed to 100 Hz. "); |
| 290 | } |
| 291 | |
| 292 | if (f1_hz <= 0.0) |
| 293 | f1_hz = 100.0; |
| 294 | if (fmax_hz <= 0.0) |
| 295 | fmax_hz = nyquist; |
| 296 | if (df_hz <= 0.0) |
| 297 | df_hz = f0_median / 2.0; |
| 298 | if (relative_bw <= 0.0) |
| 299 | relative_bw = 1.1; |
| 300 | |
| 301 | fmax_hz = std::min (fmax_hz, nyquist); |
| 302 | const integer numberOfFilters = Melder_iround ( (fmax_hz - f1_hz) / df_hz); |
| 303 | |
| 304 | double t1; |
| 305 | integer numberOfFrames, numberOfUndefinedPitchFrames = 0; |
| 306 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 307 | autoSpectrogram him = Spectrogram_create (my xmin, my xmax, numberOfFrames, dt, t1, fmin_hz, fmax_hz, numberOfFilters, df_hz, f1_hz); |
| 308 | |
| 309 | // Temporary objects |
| 310 | |
| 311 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 312 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 313 | autoMelderProgress progress (U"Sound & Pitch: To Spectrogram..."); |
| 314 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 315 | const double t = Sampled_indexToX (him.get(), iframe); |
| 316 | double f0 = Pitch_getValueAtTime (thee, t, kPitch_unit::HERTZ, 0); |
| 317 | |
| 318 | if (isundef (f0) || f0 == 0.0) { |
| 319 | numberOfUndefinedPitchFrames ++; |
| 320 | f0 = f0_median; |
| 321 | } |
| 322 | const double b = relative_bw * f0; |
| 323 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2.0); |
| 324 | Sounds_multiply (sframe.get(), window.get()); |
| 325 | |
| 326 | Sound_into_Spectrogram_frame (sframe.get(), him.get(), iframe, b); |
| 327 | |
| 328 | if (iframe % 10 == 1) |
| 329 | Melder_progress ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ", |
| 330 | numberOfFrames, U"."); |
| 331 | } |
| 332 | |
| 333 | _Spectrogram_windowCorrection (him.get(), window -> nx); |
| 334 | |
| 335 | return him; |
| 336 | } catch (MelderError) { |
| 337 | Melder_throw (U"FormantFilter not created from Pitch & FormantFilter."); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | autoSound BandFilterSpectrogram_as_Sound (BandFilterSpectrogram me, int unit) { |
| 342 | try { |
| 343 | autoSound thee = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1); |
| 344 | for (integer i = 1; i <= my ny; i ++) |
| 345 | for (integer j = 1; j <= my nx; j ++) |
| 346 | thy z [i] [j] = my v_getValueAtSample (j, i, unit); |
| 347 | return thee; |
| 348 | } catch (MelderError) { |
| 349 | Melder_throw (me, U": no Sound created."); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | autoSound BandFilterSpectrograms_crossCorrelate (BandFilterSpectrogram me, BandFilterSpectrogram thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) { |
| 354 | try { |
| 355 | autoSound sme = BandFilterSpectrogram_as_Sound (me, 1); // to dB |
| 356 | autoSound sthee = BandFilterSpectrogram_as_Sound (thee, 1); |
| 357 | autoSound cc = Sounds_crossCorrelate (sme.get(), sthee.get(), scaling, signalOutsideTimeDomain); |
| 358 | return cc; |
| 359 | } catch (MelderError) { |
| 360 | Melder_throw (me, U" and ", thee, U" not cross-correlated."); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | autoSound BandFilterSpectrograms_convolve (BandFilterSpectrogram me, BandFilterSpectrogram thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) { |
| 365 | try { |
| 366 | autoSound sme = BandFilterSpectrogram_as_Sound (me, 1); // to dB |
| 367 | autoSound sthee = BandFilterSpectrogram_as_Sound (thee, 1); |
| 368 | autoSound cc = Sounds_convolve (sme.get(), sthee.get(), scaling, signalOutsideTimeDomain); |
| 369 | return cc; |
| 370 | } catch (MelderError) { |
| 371 | Melder_throw (me, U" and ", thee, U" not convolved."); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* End of file Sound_and_Spectrogram_extensions.cpp */ |