| 1 | /* ComplexSpectrogram.cpp |
| 2 | * |
| 3 | * Copyright (C) 2014-2019 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 "ComplexSpectrogram.h" |
| 20 | #include "Sound_and_Spectrum.h" |
| 21 | |
| 22 | #include "oo_DESTROY.h" |
| 23 | #include "ComplexSpectrogram_def.h" |
| 24 | #include "oo_COPY.h" |
| 25 | #include "ComplexSpectrogram_def.h" |
| 26 | #include "oo_EQUAL.h" |
| 27 | #include "ComplexSpectrogram_def.h" |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 | #include "ComplexSpectrogram_def.h" |
| 30 | #include "oo_WRITE_TEXT.h" |
| 31 | #include "ComplexSpectrogram_def.h" |
| 32 | #include "oo_WRITE_BINARY.h" |
| 33 | #include "ComplexSpectrogram_def.h" |
| 34 | #include "oo_READ_TEXT.h" |
| 35 | #include "ComplexSpectrogram_def.h" |
| 36 | #include "oo_READ_BINARY.h" |
| 37 | #include "ComplexSpectrogram_def.h" |
| 38 | #include "oo_DESCRIPTION.h" |
| 39 | #include "ComplexSpectrogram_def.h" |
| 40 | |
| 41 | Thing_implement (ComplexSpectrogram, Matrix, 2); |
| 42 | |
| 43 | |
| 44 | autoComplexSpectrogram ComplexSpectrogram_create (double tmin, double tmax, integer nt, double dt, |
| 45 | double t1, double fmin, double fmax, integer nf, double df, double f1) |
| 46 | { |
| 47 | try { |
| 48 | autoComplexSpectrogram me = Thing_new (ComplexSpectrogram); |
| 49 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1); |
| 50 | my phase = zero_MAT (my ny, my nx); |
| 51 | return me; |
| 52 | } catch (MelderError) { |
| 53 | Melder_throw (U"ComplexSpectrogram not created."); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static autoVEC newVEChanning (integer size) { |
| 58 | autoVEC window = raw_VEC (size); |
| 59 | for (integer i = 1; i <= size; i ++) |
| 60 | window [i] = 0.5 * (1.0 - cos (NUM2pi * (i - 0.5) / size)); |
| 61 | return window; |
| 62 | } |
| 63 | |
| 64 | autoComplexSpectrogram Sound_to_ComplexSpectrogram (Sound me, double windowLength, double maximumFrequency ) { |
| 65 | try { |
| 66 | Melder_require (my ny == 1, |
| 67 | U"The sound should have onely one channel."); |
| 68 | const double samplingFrequency = 1.0 / my dx, nyquistFrequency = 0.5 / my dx; |
| 69 | const double myDuration = my xmax - my xmin; |
| 70 | Melder_require (windowLength <= myDuration, |
| 71 | U"The sound is too short: it should be at least as long as one window length."); |
| 72 | if (maximumFrequency == 0.0) |
| 73 | maximumFrequency = nyquistFrequency; |
| 74 | Melder_require (maximumFrequency <= nyquistFrequency, |
| 75 | U"The maximum frequency should not exceed ", nyquistFrequency, U" Hz."); |
| 76 | integer nsamp_window = Melder_ifloor (windowLength / my dx); |
| 77 | autoVEC window = newVEChanning (nsamp_window); |
| 78 | const integer halfnsamp_window = nsamp_window / 2 - 1; |
| 79 | nsamp_window = halfnsamp_window * 2; |
| 80 | |
| 81 | Melder_require (nsamp_window > 1, |
| 82 | U"There should be at least two samples in the window."); |
| 83 | |
| 84 | integer numberOfFrames; |
| 85 | const double timeStep = 0.5 * windowLength; |
| 86 | double t1; |
| 87 | Sampled_shortTermAnalysis (me, windowLength, timeStep, & numberOfFrames, & t1); |
| 88 | |
| 89 | // Compute sampling of the spectrum |
| 90 | |
| 91 | const integer numberOfFrequencies = halfnsamp_window + 1; |
| 92 | const double df = samplingFrequency / (numberOfFrequencies - 1); |
| 93 | |
| 94 | autoComplexSpectrogram thee = ComplexSpectrogram_create (my xmin, my xmax, numberOfFrames, timeStep, t1, 0.0, maximumFrequency, numberOfFrequencies, df, 0.0); |
| 95 | |
| 96 | autoSound analysisWindow = Sound_create (1, 0.0, nsamp_window * my dx, nsamp_window, my dx, 0.5 * my dx); |
| 97 | |
| 98 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 99 | const double tmid = Sampled_indexToX (thee.get(), iframe); |
| 100 | const integer leftSample = Sampled_xToLowIndex (me, tmid); |
| 101 | const double rightSample = leftSample + 1; |
| 102 | const integer startSample = rightSample - halfnsamp_window; |
| 103 | const integer endSample = startSample + nsamp_window - 1; |
| 104 | Melder_assert (startSample >= 1); |
| 105 | Melder_assert (endSample <= my nx); |
| 106 | |
| 107 | analysisWindow -> z.row (1) <<= my z.row (1).part (startSample, endSample); |
| 108 | |
| 109 | analysisWindow -> z.row (1) *= window.part (1, nsamp_window); |
| 110 | autoSpectrum spec = Sound_to_Spectrum (analysisWindow.get(), false); |
| 111 | |
| 112 | thy z [1] [iframe] = spec -> z [1] [1] * spec -> z [1] [1]; |
| 113 | thy phase [1] [iframe] = 0.0; |
| 114 | for (integer ifreq = 2; ifreq <= numberOfFrequencies - 1; ifreq ++) { |
| 115 | const double x = spec -> z [1] [ifreq], y = spec -> z [2] [ifreq]; |
| 116 | thy z [ifreq] [iframe] = x * x + y * y; // power |
| 117 | thy phase [ifreq] [iframe] = atan2 (y, x); // phase [-pi,+pi] |
| 118 | } |
| 119 | // even number of samples |
| 120 | thy z [numberOfFrequencies] [iframe] = spec -> z [1] [numberOfFrequencies] * spec -> z [1] [numberOfFrequencies]; |
| 121 | thy phase [numberOfFrequencies] [iframe] = 0.0; |
| 122 | } |
| 123 | return thee; |
| 124 | } catch (MelderError) { |
| 125 | Melder_throw (me, U": no ComplexSpectrogram created."); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | autoSound ComplexSpectrogram_to_Sound (ComplexSpectrogram me, double stretchFactor) { |
| 130 | try { |
| 131 | /* |
| 132 | original number of samples is odd: imaginary part of last spectral value is zero -> |
| 133 | phase is either zero or +/-pi |
| 134 | */ |
| 135 | const double samplingFrequency = 2.0 * my ymax; |
| 136 | const double lastFrequency = my y1 + (my ny - 1) * my dy, lastPhase = my phase [my ny] [1]; |
| 137 | const bool originalNumberOfSamplesProbablyOdd = ( lastPhase != 0.0 && lastPhase != NUMpi && lastPhase != -NUMpi || |
| 138 | my ymax - lastFrequency > 0.25 * my dx ); |
| 139 | Melder_require (my y1 == 0.0, |
| 140 | U"A Fourier-transformable ComplexSpectrogram should have a first frequency of 0 Hz, not ", my y1, U" Hz."); |
| 141 | |
| 142 | const integer nsamp_window = 2 * my ny - ( originalNumberOfSamplesProbablyOdd ? 1 : 2 ); |
| 143 | const integer halfnsamp_window = nsamp_window / 2; |
| 144 | const double synthesisWindowDuration = nsamp_window / samplingFrequency; |
| 145 | autoSpectrum spectrum = Spectrum_create (my ymax, my ny); |
| 146 | autoSound synthesisWindow = Sound_createSimple (1, synthesisWindowDuration, samplingFrequency); |
| 147 | const double newDuration = (my xmax - my xmin) * stretchFactor; |
| 148 | autoSound thee = Sound_createSimple (1, newDuration, samplingFrequency); //TODO |
| 149 | //double thyStartTime; |
| 150 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 151 | // "original" sound : |
| 152 | const double tmid = Sampled_indexToX (me, iframe); |
| 153 | const integer leftSample = Sampled_xToLowIndex (thee.get(), tmid); |
| 154 | const integer rightSample = leftSample + 1; |
| 155 | const integer startSample = rightSample - halfnsamp_window; |
| 156 | const integer endSample = std::min (startSample + nsamp_window - 1, thy nx); |
| 157 | |
| 158 | //const double startTime = Sampled_indexToX (thee.get(), startSample); |
| 159 | //if (iframe == 1) |
| 160 | // thyStartTime = Sampled_indexToX (thee.get(), startSample); |
| 161 | //integer endSample = leftSample + halfnsamp_window; |
| 162 | // New Sound with stretch |
| 163 | //const integer thyStartSample = Sampled_xToLowIndex (thee.get(), thyStartTime); |
| 164 | //const double thyEndTime = thyStartTime + my dx * stretchFactor; |
| 165 | //const integer thyEndSample = Sampled_xToLowIndex (thee.get(), thyEndTime); |
| 166 | //double extraTime = (thyStartSample - startSample + 1) * thy dx; |
| 167 | //const double extraTime = thyStartTime - startTime; |
| 168 | spectrum -> z [1] [1] = sqrt (my z [1] [iframe]); |
| 169 | for (integer ifreq = 2; ifreq <= my ny; ifreq ++) { |
| 170 | //const double f = my y1 + (ifreq - 1) * my dy; |
| 171 | const double a = sqrt (my z [ifreq] [iframe]); |
| 172 | //double dummy; |
| 173 | const double phi = my phase [ifreq] [iframe]; // + extraPhase; |
| 174 | spectrum -> z [1] [ifreq] = a * cos (phi); |
| 175 | spectrum -> z [2] [ifreq] = a * sin (phi); |
| 176 | } |
| 177 | autoSound synthesis = Spectrum_to_Sound (spectrum.get()); |
| 178 | |
| 179 | thy z.row (1).part (startSample, endSample) += 0.5 * synthesis -> z.row (1).part (1, nsamp_window); |
| 180 | } |
| 181 | return thee; |
| 182 | } catch (MelderError) { |
| 183 | Melder_throw (me, U": no Sound created."); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #if 0 |
| 188 | static autoSound ComplexSpectrogram_to_Sound2 (ComplexSpectrogram me, double stretchFactor) { |
| 189 | try { |
| 190 | /* original number of samples is odd: imaginary part of last spectral value is zero -> |
| 191 | * phase is either zero or pi |
| 192 | */ |
| 193 | const double pi = atan2 (0.0, - 0.5); |
| 194 | const double samplingFrequency = 2.0 * my ymax; |
| 195 | const double lastFrequency = my y1 + (my ny - 1) * my dy; |
| 196 | int originalNumberOfSamplesProbablyOdd = (my phase [my ny] [1] != 0.0 && my phase [my ny] [1] != pi) || my ymax - lastFrequency > 0.25 * my dx; |
| 197 | if (my y1 != 0.0) { |
| 198 | Melder_throw (U"A Fourier-transformable Spectrum must have a first frequency of 0 Hz, not ", my y1, U" Hz."); |
| 199 | } |
| 200 | const integer numberOfSamples = 2 * my ny - (originalNumberOfSamplesProbablyOdd ? 1 : 2 ); |
| 201 | const double synthesisWindowDuration = numberOfSamples / samplingFrequency; |
| 202 | autoSpectrum spectrum = Spectrum_create (my ymax, my ny); |
| 203 | autoSound synthesisWindow = Sound_createSimple (1, synthesisWindowDuration, samplingFrequency); |
| 204 | const integer stepSizeSamples = my dx * samplingFrequency * stretchFactor; |
| 205 | const double newDuration = (my xmax - my xmin) * stretchFactor + 0.05; |
| 206 | autoSound thee = Sound_createSimple (1, newDuration, samplingFrequency); //TODO |
| 207 | integer istart = 1, iend = istart + stepSizeSamples - 1; |
| 208 | for (integer iframe = 1; iframe <= my nx; iframe++) { |
| 209 | spectrum -> z [1] [1] = sqrt (my z [1] [iframe]); |
| 210 | for (integer ifreq = 2; ifreq <= my ny; ifreq ++) { |
| 211 | const double f = my y1 + (ifreq - 1) * my dy; |
| 212 | const double a = sqrt (my z [ifreq] [iframe]); |
| 213 | const double extraPhase = 2.0 * pi * (stretchFactor - 1.0) * my dx * f; |
| 214 | const double phi = my phase [ifreq] [iframe] + extraPhase; |
| 215 | spectrum -> z [1] [ifreq] = a * cos (phi); |
| 216 | spectrum -> z [2] [ifreq] = a * sin (phi); |
| 217 | } |
| 218 | autoSound synthesis = Spectrum_to_Sound (spectrum.get()); |
| 219 | for (integer j = istart; j <= iend; j ++) |
| 220 | thy z [1] [j] = synthesis -> z [1] [j - istart + 1]; |
| 221 | istart = iend + 1; |
| 222 | iend = istart + stepSizeSamples - 1; |
| 223 | } |
| 224 | return thee; |
| 225 | } catch (MelderError) { |
| 226 | Melder_throw (me, U": no Sound created."); |
| 227 | } |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | autoSpectrogram ComplexSpectrogram_to_Spectrogram (ComplexSpectrogram me) { |
| 232 | try { |
| 233 | autoSpectrogram thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 234 | thy z.all() <<= my z.all(); |
| 235 | return thee; |
| 236 | } catch (MelderError) { |
| 237 | Melder_throw (me, U": not converted to Spectrogram."); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void ComplexSpectrogram_Spectrogram_replaceAmplitudes (ComplexSpectrogram me, Spectrogram thee) { |
| 242 | Melder_require (my nx == thy nx && my ny == thy ny, |
| 243 | U"The numbers of cells in the ComplexSpectrogram and Spectrogram should be equal."); |
| 244 | my z.all() <<= thy z.all(); |
| 245 | } |
| 246 | |
| 247 | autoSpectrum ComplexSpectrogram_to_Spectrum (ComplexSpectrogram me, double time) { |
| 248 | try { |
| 249 | integer iframe = Sampled_xToLowIndex (me, time); |
| 250 | if (iframe < 1) |
| 251 | iframe = 1; |
| 252 | if (iframe > my nx) |
| 253 | iframe = my nx; |
| 254 | autoSpectrum thee = Spectrum_create (my ymax, my ny); |
| 255 | for (integer ifreq = 1; ifreq <= my ny; ifreq ++) { |
| 256 | const double a = sqrt (my z [ifreq] [iframe]); |
| 257 | const double phi = my phase [ifreq] [iframe]; |
| 258 | thy z [1] [ifreq] = a * cos (phi); |
| 259 | thy z [2] [ifreq] = a * sin (phi); |
| 260 | } |
| 261 | return thee; |
| 262 | } catch (MelderError) { |
| 263 | Melder_throw (me, U": no Spectrum created."); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* End of file ComplexSpectrogram.cpp */ |