| 1 |
/* Spectrum_to_Excitation.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2005,2011,2014-2020,2023 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 "Spectrum_to_Excitation.h" |
| 20 |
|
| 21 |
autoExcitation Spectrum_to_Excitation (Spectrum me, double dbark) { |
| 22 |
try { |
| 23 |
const integer nbark = Melder_iround (25.6 / dbark); |
| 24 |
const constVEC re = my z.row (1), im = my z.row (2); |
| 25 |
const autoVEC auditoryFilter = raw_VEC (nbark); |
| 26 |
for (integer i = 1; i <= nbark; i ++) { |
| 27 |
const double bark = dbark * (i - nbark/2) + 0.474; |
| 28 |
auditoryFilter [i] = pow (10, (1.581 + 0.75 * bark - 1.75 * sqrt (1 + bark * bark))); |
| 29 |
} |
| 30 |
/*const double filterArea = NUMsum (auditoryFilter.get()); |
| 31 |
auditoryFilter.all() /= filterArea;*/ |
| 32 |
const autoVEC rFreqs = raw_VEC (nbark + 1); |
| 33 |
const autoINTVEC iFreqs = raw_INTVEC (nbark + 1); |
| 34 |
for (integer i = 1; i <= nbark + 1; i ++) { |
| 35 |
rFreqs [i] = NUMbarkToHertz (dbark * (i - 1)); |
| 36 |
iFreqs [i] = Sampled_xToNearestIndex (me, rFreqs [i]); |
| 37 |
} |
| 38 |
const autoVEC inSig = zero_VEC (nbark); |
| 39 |
for (integer i = 1; i <= nbark; i ++) { |
| 40 |
const integer low = std::max (1_integer, iFreqs [i]); |
| 41 |
const integer high = std::min (iFreqs [i + 1] - 1, my nx); |
| 42 |
for (integer j = low; j <= high; j ++) |
| 43 |
inSig [i] += re [j] * re [j] + im [j] * im [j]; // Pa2 s2 |
| 44 |
|
| 45 |
/* An anti-undersampling correction. */ |
| 46 |
if (high >= low) |
| 47 |
inSig [i] *= 2.0 * (rFreqs [i + 1] - rFreqs [i]) / (high - low + 1) * my dx; // Pa2: power density in this band |
| 48 |
} |
| 49 |
|
| 50 |
/* Convolution with auditory (masking) filter. */ |
| 51 |
|
| 52 |
const autoVEC outSig = zero_VEC (2 * nbark); |
| 53 |
for (integer i = 1; i <= nbark; i ++) |
| 54 |
for (integer j = 1; j <= nbark; j ++) |
| 55 |
outSig [i + j] += inSig [i] * auditoryFilter [j]; |
| 56 |
|
| 57 |
autoExcitation thee = Excitation_create (dbark, nbark); |
| 58 |
for (integer i = 1; i <= nbark; i ++) |
| 59 |
thy z [1] [i] = NUMsoundPressureToPhon (sqrt (outSig [i + nbark/2]), Sampled_indexToX (thee.get(), i)); |
| 60 |
|
| 61 |
return thee; |
| 62 |
} catch (MelderError) { |
| 63 |
Melder_throw (me, U": not converted to Excitation."); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/* End of file Spectrum_to_Excitation.cpp */ |