| 1 | #ifndef _SoundFrames_h_ |
| 2 | #define _SoundFrames_h_ |
| 3 | /* SoundFrames.h |
| 4 | * |
| 5 | * Copyright (C) 2024-2025 David Weenink |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | //#include "melder.h" |
| 22 | #include "NUMFourier.h" |
| 23 | #include "Sound_extensions.h" |
| 24 | #include "Spectrum.h" |
| 25 | |
| 26 | inline integer getSoundFrameSize_odd (double approximatePhysicalAnalysisWidth, double samplingPeriod) { |
| 27 | const double halfFrameDuration = 0.5 * approximatePhysicalAnalysisWidth; |
| 28 | const integer halfFrameSamples = Melder_ifloor (halfFrameDuration / samplingPeriod); |
| 29 | return 2 * halfFrameSamples + 1; |
| 30 | } |
| 31 | |
| 32 | inline integer getSoundFrameSize (double physicalAnalysisWidth, double samplingPeriod) { |
| 33 | Melder_assert (physicalAnalysisWidth > 0.0); |
| 34 | Melder_assert (samplingPeriod > 0.0); |
| 35 | return Melder_iround (physicalAnalysisWidth / samplingPeriod); |
| 36 | } |
| 37 | |
| 38 | inline double getPhysicalAnalysisWidth (double effectiveAnalysisWidth, kSound_windowShape windowShape) { |
| 39 | const double physicalAnalysisWidth = ( |
| 40 | windowShape == kSound_windowShape::RECTANGULAR || |
| 41 | windowShape == kSound_windowShape::TRIANGULAR || windowShape == kSound_windowShape::HAMMING || |
| 42 | windowShape == kSound_windowShape::HANNING ? effectiveAnalysisWidth : 2.0 * effectiveAnalysisWidth |
| 43 | ); |
| 44 | return physicalAnalysisWidth; |
| 45 | } |
| 46 | |
| 47 | Thing_define (SoundFrames, Thing) { |
| 48 | |
| 49 | constSound inputSound; |
| 50 | mutableSampled output; |
| 51 | double physicalAnalysisWidth; // depends on the effectiveAnalysiswidth and the window window shape |
| 52 | integer soundFrameSize; // determined by the physicalAnalysisWidth and the samplingFrequency of the Sound |
| 53 | autoSound frameAsSound; // the (possibly multichannel) frame as a Sound |
| 54 | autoVEC windowFunction; // the actual window used of size soundFrameSize |
| 55 | autoVEC soundFrame; // convenience: average of the channels |
| 56 | kSound_windowShape windowShape; // Type: Rectangular, triangular, hamming, etc.. |
| 57 | bool subtractChannelMean = true; // if true, the frame mean of each channel will be subtracted before windowing |
| 58 | |
| 59 | private: |
| 60 | |
| 61 | void initCommon (kSound_windowShape windowShape, bool subtractFrameMean); |
| 62 | |
| 63 | public: |
| 64 | |
| 65 | /* |
| 66 | Calculate the output sampling from the input parameters, |
| 67 | this is the default case |
| 68 | */ |
| 69 | void initForToSampled (constSound input, double effectiveAnalysisWidth, double timeStep, |
| 70 | kSound_windowShape windowShape, bool subtractChannelMean); |
| 71 | |
| 72 | /* |
| 73 | In special cases we need the sampling (x1, dx, nx) of the output Sampled: |
| 74 | like for the FormantPath. |
| 75 | */ |
| 76 | void initForIntoSampled (constSound input, mutableSampled output, double effectiveAnalysisWidth, |
| 77 | kSound_windowShape windowShape, bool subtractChannelMean); |
| 78 | |
| 79 | Sound getFrame (integer iframe); |
| 80 | |
| 81 | VEC getMonoFrame (integer iframe); |
| 82 | }; |
| 83 | |
| 84 | autoSoundFrames SoundFrames_createForIntoSampled (constSound input, mutableSampled output, double effectiveAnalysisWidth, |
| 85 | kSound_windowShape windowShape, bool subtractChannelMean); |
| 86 | |
| 87 | autoSoundFrames SoundFrames_createForToSampled (constSound input, double effectiveAnalysisWidth, double timeStep, |
| 88 | kSound_windowShape windowShape, bool subtractChannelMean); |
| 89 | |
| 90 | #if 0 |
| 91 | Thing_define (SoundFrameIntoSpectrum, SoundFrames) { |
| 92 | autoSpectrum spectrum; |
| 93 | integer fftInterpolationFactor = 1; // 0 = DFT, 1 = FFT, 2, 4, 8 FFT with extra zero's |
| 94 | integer numberOfFourierSamples; |
| 95 | autoVEC fourierSamples; // size = numberOfFourierSamples |
| 96 | autoVEC fourierSamples_channel; |
| 97 | autoVEC power_channelAveraged; |
| 98 | autoNUMFourierTable fourierTable; // of dimension numberOfFourierSamples; |
| 99 | |
| 100 | void init (constSound input, double effectiveAnalysisWidth, double timeStep, |
| 101 | kSound_windowShape windowShape, bool subtractChannelMean) |
| 102 | { |
| 103 | SoundFrameIntoSpectrum_Parent :: init (input, effectiveAnalysisWidth, timeStep, windowShape, subtractChannelMean); |
| 104 | init_spectralPart (); |
| 105 | } |
| 106 | |
| 107 | Spectrum getSpectrum () { |
| 108 | |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | |
| 113 | void init_spectralPart () { |
| 114 | our numberOfFourierSamples = our frameAsSound -> nx; |
| 115 | if (our fftInterpolationFactor > 0) { |
| 116 | our numberOfFourierSamples = Melder_iroundUpToPowerOfTwo (numberOfFourierSamples); |
| 117 | for (integer imultiply = fftInterpolationFactor; imultiply > 1; imultiply --) |
| 118 | our numberOfFourierSamples *= 2; |
| 119 | } |
| 120 | our fourierSamples = raw_VEC (numberOfFourierSamples); |
| 121 | our fourierSamples_channel = raw_VEC (numberOfFourierSamples); |
| 122 | our power_channelAveraged = raw_VEC (numberOfFourierSamples); |
| 123 | const integer numberOfFrequencies = numberOfFourierSamples / 2 + 1; |
| 124 | our fourierTable = NUMFourierTable_create (our numberOfFourierSamples); |
| 125 | our spectrum = Spectrum_create (0.5 / our frameAsSound -> dx, numberOfFrequencies); |
| 126 | our spectrum -> dx = 1.0 / (our frameAsSound -> dx * our numberOfFourierSamples); |
| 127 | } |
| 128 | |
| 129 | void soundFrameToPower_channelAveraged () { |
| 130 | const integer numberOfChannels = our frameAsSound -> ny; |
| 131 | power_channelAveraged.get() <<= 0.0; |
| 132 | for (integer ichannel = 1; ichannel <= numberOfChannels; ichannel ++) { |
| 133 | fourierSamples.part (1, soundFrameSize) <<= frameAsSound -> z.row (ichannel); |
| 134 | fourierSamples.part (soundFrameSize + 1, numberOfFourierSamples) <<= 0.0; |
| 135 | NUMfft_forward (fourierTable.get(), fourierSamples.get()); |
| 136 | for (integer i = 1; i <= numberOfFourierSamples; i ++) |
| 137 | power_channelAveraged [i] += fourierSamples [i] * fourierSamples [i]; |
| 138 | } |
| 139 | if (numberOfChannels > 1) |
| 140 | power_channelAveraged.get() /= numberOfChannels; |
| 141 | } |
| 142 | |
| 143 | }; |
| 144 | #endif /* 0 */ |
| 145 | |
| 146 | #endif /* _SoundFrames_h_ */ |