| 1 |
/* Sound_and_Spectrum.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2005,2011,2012,2015-2021,2025 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 "Sound_and_Spectrum.h" |
| 20 |
#include "NUM2.h" |
| 21 |
|
| 22 |
autoSpectrum Sound_to_Spectrum (Sound me, bool fast) { |
| 23 |
try { |
| 24 |
const integer numberOfFourierSamples = ( fast ? Melder_iroundUpToPowerOfTwo (my nx) : my nx ); |
| 25 |
const integer numberOfChannels = my ny; |
| 26 |
const integer numberOfFrequencies = numberOfFourierSamples / 2 + 1; // 4 samples -> cos0 cos1 sin1 cos2; 5 samples -> cos0 cos1 sin1 cos2 sin2 |
| 27 |
|
| 28 |
autoVEC data = raw_VEC (numberOfFourierSamples); // raw, so make sure to fill each sample |
| 29 |
columnMeans_VEC_out (data.part (1, my nx), my z.all()); |
| 30 |
data.part (my nx + 1, numberOfFourierSamples) <<= 0.0; |
| 31 |
|
| 32 |
autoNUMFourierTable fourierTable = NUMFourierTable_create (numberOfFourierSamples); |
| 33 |
NUMfft_forward (fourierTable.get(), data.get()); |
| 34 |
|
| 35 |
autoSpectrum thee = Spectrum_create (0.5 / my dx, numberOfFrequencies); |
| 36 |
thy dx = 1.0 / (my dx * numberOfFourierSamples); // override, just in case numberOfFourierSamples is odd |
| 37 |
const VEC re = thy z.row (1); |
| 38 |
const VEC im = thy z.row (2); |
| 39 |
const double scaling = my dx; |
| 40 |
re [1] = data [1] * scaling; |
| 41 |
im [1] = 0.0; |
| 42 |
for (integer i = 2; i < numberOfFrequencies; i ++) { |
| 43 |
re [i] = data [i + i - 2] * scaling; // data [2], data [4], ... |
| 44 |
im [i] = data [i + i - 1] * scaling; // data [3], data [5], ... |
| 45 |
} |
| 46 |
if ((numberOfFourierSamples & 1) != 0) { |
| 47 |
if (numberOfFourierSamples > 1) { |
| 48 |
re [numberOfFrequencies] = data [numberOfFourierSamples - 1] * scaling; |
| 49 |
im [numberOfFrequencies] = data [numberOfFourierSamples] * scaling; |
| 50 |
} |
| 51 |
} else { |
| 52 |
re [numberOfFrequencies] = data [numberOfFourierSamples] * scaling; |
| 53 |
im [numberOfFrequencies] = 0.0; |
| 54 |
} |
| 55 |
return thee; |
| 56 |
} catch (MelderError) { |
| 57 |
Melder_throw (me, U": not converted to Spectrum."); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
autoSound Spectrum_to_Sound (Spectrum me) { |
| 62 |
try { |
| 63 |
const constVEC re = my z.row (1), im = my z.row (2); |
| 64 |
const double lastFrequency = my x1 + (my nx - 1) * my dx; |
| 65 |
const bool originalNumberOfSamplesProbablyOdd = ( im [my nx] != 0.0 || my xmax - lastFrequency > 0.25 * my dx ); |
| 66 |
if (my x1 != 0.0) |
| 67 |
Melder_throw (U"A Fourier-transformable Spectrum must have a first frequency of 0 Hz, not ", my x1, U" Hz."); |
| 68 |
const integer numberOfSamples = 2 * my nx - ( originalNumberOfSamplesProbablyOdd ? 1 : 2 ); |
| 69 |
autoSound thee = Sound_createSimple (1, 1.0 / my dx, numberOfSamples * my dx); |
| 70 |
const VEC amp = thy z.row (1); |
| 71 |
const double scaling = my dx; |
| 72 |
amp [1] = re [1] * scaling; |
| 73 |
for (integer i = 2; i < my nx; i ++) { |
| 74 |
amp [i + i - 1] = re [i] * scaling; |
| 75 |
amp [i + i] = im [i] * scaling; |
| 76 |
} |
| 77 |
if (originalNumberOfSamplesProbablyOdd) { |
| 78 |
amp [numberOfSamples] = re [my nx] * scaling; |
| 79 |
if (numberOfSamples > 1) |
| 80 |
amp [2] = im [my nx] * scaling; |
| 81 |
} else { |
| 82 |
amp [2] = re [my nx] * scaling; |
| 83 |
} |
| 84 |
NUMrealft (amp, -1); |
| 85 |
return thee; |
| 86 |
} catch (MelderError) { |
| 87 |
Melder_throw (me, U": not converted to Sound."); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
autoSpectrum Spectrum_lpcSmoothing (Spectrum me, int numberOfPeaks, double preemphasisFrequency) { |
| 92 |
try { |
| 93 |
const integer numberOfCoefficients = 2 * numberOfPeaks; |
| 94 |
|
| 95 |
autoSound sound = Spectrum_to_Sound (me); |
| 96 |
VECpreemphasize_f_inplace (sound -> z.row (1), sound -> dx, preemphasisFrequency); |
| 97 |
autoVEC a = raw_VEC (numberOfCoefficients); |
| 98 |
const double gain = VECburg (a.get(), sound -> z.row (1)); |
| 99 |
for (integer i = 1; i <= numberOfCoefficients; i ++) |
| 100 |
a [i] = - a [i]; |
| 101 |
autoSpectrum thee = Data_copy (me); |
| 102 |
|
| 103 |
const integer nfft = 2 * (thy nx - 1); |
| 104 |
const integer ndata = Melder_clippedRight (numberOfCoefficients, nfft - 1); |
| 105 |
const double scale = 10.0 * (gain > 0.0 ? sqrt (gain) : 1.0) / numberOfCoefficients; |
| 106 |
autoVEC data = zero_VEC (nfft); |
| 107 |
data [1] = 1.0; |
| 108 |
for (integer i = 1; i <= ndata; i ++) |
| 109 |
data [i + 1] = a [i]; |
| 110 |
NUMrealft (data.get(), 1); |
| 111 |
const VEC re = thy z.row (1); |
| 112 |
const VEC im = thy z.row (2); |
| 113 |
re [1] = scale / data [1]; |
| 114 |
im [1] = 0.0; |
| 115 |
const integer halfnfft = nfft / 2; |
| 116 |
for (integer i = 2; i <= halfnfft; i ++) { |
| 117 |
const double realPart = data [i + i - 1], imaginaryPart = data [i + i]; |
| 118 |
re [i] = scale / hypot (realPart, imaginaryPart) / (1.0 + thy dx * (i - 1) / preemphasisFrequency); |
| 119 |
im [i] = 0.0; |
| 120 |
} |
| 121 |
re [halfnfft + 1] = scale / data [2] / (1.0 + thy dx * halfnfft / preemphasisFrequency); |
| 122 |
im [halfnfft + 1] = 0.0; |
| 123 |
return thee; |
| 124 |
} catch (MelderError) { |
| 125 |
Melder_throw (me, U": not smoothed."); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
autoSound Sound_filter_formula (Sound me, conststring32 formula, Interpreter interpreter) { |
| 130 |
try { |
| 131 |
autoSound thee = Data_copy (me); |
| 132 |
if (my ny == 1) { |
| 133 |
autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 134 |
Matrix_formula (spec.get(), formula, interpreter, nullptr); |
| 135 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 136 |
thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 137 |
} else { |
| 138 |
for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 139 |
autoSound channel = Sound_extractChannel (me, ichan); |
| 140 |
autoSpectrum spec = Sound_to_Spectrum (channel.get(), true); |
| 141 |
Matrix_formula (spec.get(), formula, interpreter, nullptr); |
| 142 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 143 |
thy z.row (ichan) <<= his z.row (1).part (1, thy nx); |
| 144 |
} |
| 145 |
} |
| 146 |
return thee; |
| 147 |
} catch (MelderError) { |
| 148 |
Melder_throw (me, U": not filtered (with formula)."); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
autoSound Sound_filter_passHannBand (Sound me, double fmin, double fmax, double smooth) { |
| 153 |
try { |
| 154 |
autoSound thee = Data_copy (me); |
| 155 |
if (my ny == 1) { |
| 156 |
autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 157 |
Spectrum_passHannBand (spec.get(), fmin, fmax, smooth); |
| 158 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 159 |
thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 160 |
} else { |
| 161 |
for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 162 |
autoSound channel = Sound_extractChannel (me, ichan); |
| 163 |
autoSpectrum spec = Sound_to_Spectrum (channel.get(), true); |
| 164 |
Spectrum_passHannBand (spec.get(), fmin, fmax, smooth); |
| 165 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 166 |
thy z.row (ichan) <<= his z.row (1).part (1, thy nx); |
| 167 |
} |
| 168 |
} |
| 169 |
return thee; |
| 170 |
} catch (MelderError) { |
| 171 |
Melder_throw (me, U": not filtered (pass Hann band)."); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
autoSound Sound_filter_stopHannBand (Sound me, double fmin, double fmax, double smooth) { |
| 176 |
try { |
| 177 |
autoSound thee = Data_copy (me); |
| 178 |
if (my ny == 1) { |
| 179 |
autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 180 |
Spectrum_stopHannBand (spec.get(), fmin, fmax, smooth); |
| 181 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 182 |
thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 183 |
} else { |
| 184 |
for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 185 |
autoSound channel = Sound_extractChannel (me, ichan); |
| 186 |
autoSpectrum spec = Sound_to_Spectrum (channel.get(), true); |
| 187 |
Spectrum_stopHannBand (spec.get(), fmin, fmax, smooth); |
| 188 |
autoSound him = Spectrum_to_Sound (spec.get()); |
| 189 |
thy z.row (ichan) <<= his z.row (1).part (1, thy nx); |
| 190 |
} |
| 191 |
} |
| 192 |
return thee; |
| 193 |
} catch (MelderError) { |
| 194 |
Melder_throw (me, U": not filtered (stop Hann band)."); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* End of file Sound_and_Spectrum.cpp */ |