| 1 | /* ConstantQLog2FSpectrogram.cpp |
| 2 | * |
| 3 | * Copyright (C) 2021-2022 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 "Formula.h"cxx . . $ `<file>/sys/Formula.h`/ |
| 20 | #include "ConstantQSpectrograms.h"cxx . . $ `<file>/dwtools/ConstantQSpectrograms.h`/ |
| 21 | |
| 22 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (ConstantQLog2FSpectrogram, MultiSampledSpectrogram, 0); |
| 23 | |
| 24 | void structConstantQLog2FSpectrogramcxx . . $ structConstantQLog2FSpectrogram# :: v1_infocxx . . $ structConstantQLog2FSpectrogram#v1_info(49f6e7a06ebc5aa8). () { |
| 25 | ConstantQLog2FSpectrogram_Parentcxx . . $ ConstantQLog2FSpectrogram_Parent# :: v1_infocxx . . $ structMultiSampledSpectrogram#v1_info(49f6e7a06ebc5aa8). (); |
| 26 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Frequency resolution in bins: ", frequencyResolutionInBinscxx . . $ structMultiSampledSpectrogram#frequencyResolutionInBins.); |
| 27 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Quality factor Q: ", ConstantQLog2FSpectrogram_getQualityFactorcxx . . $ ConstantQLog2FSpectrogram_getQualityFactor(bdefd67d161a979e). (this)); |
| 28 | } |
| 29 | |
| 30 | double structConstantQLog2FSpectrogramcxx . . $ structConstantQLog2FSpectrogram# :: v_getValueAtSamplecxx . . $ structConstantQLog2FSpectrogram#v_getValueAtSample(8bbfc6ce471470c9). (integercxx . . $ integer# ifreq, integercxx . . $ integer# iframe , int unit) const { |
| 31 | FrequencyBincxx . . $ FrequencyBin# bin = frequencyBinscxx . . $ structMultiSampledSpectrogram#frequencyBins..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ifreq]; |
| 32 | const double value = bin -> v_getValueAtSamplecxx . . $ structFrequencyBin#v_getValueAtSample(8bbfc6ce471470c9). (iframe, 1, unit); |
| 33 | return ( isdefinedcxx . . $ isdefined(f8197d536af80c96). (value) ? ourcxx . . $ `melder/melder.h:42:9`! v_convertStandardToSpecialUnitcxx . . $ structFunction#v_convertStandardToSpecialUnit(d783654ce2b2ffd0). (value, iframe, unit) : undefinedcxx . . $ undefined. ); |
| 34 | } |
| 35 | |
| 36 | double structConstantQLog2FSpectrogramcxx . . $ structConstantQLog2FSpectrogram# :: v_myFrequencyUnitToHertzcxx . . $ structConstantQLog2FSpectrogram#v_myFrequencyUnitToHertz(7e3a3c6b59947c6d). (double log2_f) const { |
| 37 | return exp2cxx . . $ exp2(fee4f1d22bc8e617). (log2_f); |
| 38 | } |
| 39 | |
| 40 | double structConstantQLog2FSpectrogramcxx . . $ structConstantQLog2FSpectrogram# :: v_hertzToMyFrequencyUnitcxx . . $ structConstantQLog2FSpectrogram#v_hertzToMyFrequencyUnit(7e3a3c6b59947c6d). (double f_hz) const { |
| 41 | return ( f_hz > 0.0 ? log2cxx . . $ log2(fee4f1d22bc8e617). (f_hz) : undefinedcxx . . $ undefined. ); |
| 42 | } |
| 43 | |
| 44 | autoConstantQLog2FSpectrogramcxx . . $ autoConstantQLog2FSpectrogram# ConstantQLog2FSpectrogram_createcxx . . $ ConstantQLog2FSpectrogram_create(ec02a1924e4f27e8). (double tmin, double tmax, double f1, double fmax, integercxx . . $ integer# numberOfBinsPerOctave, double frequencyResolutionInBins) { |
| 45 | try { |
| 46 | autoConstantQLog2FSpectrogramcxx . . $ autoConstantQLog2FSpectrogram# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ConstantQLog2FSpectrogram); |
| 47 | const double log2_dy = 1.0 / numberOfBinsPerOctave; |
| 48 | const integercxx . . $ integer# numberOfBins = Melder_iroundDowncxx . . $ Melder_iroundDown(a6e4a4c8b00c35a3). (log2cxx . . $ log2(fee4f1d22bc8e617). (fmax / f1) * numberOfBinsPerOctave); |
| 49 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfBins > 1, |
| 50 | U"The number of bins should be larger than 1."); |
| 51 | const double log2_ymin = log2cxx . . $ log2(fee4f1d22bc8e617). (f1); |
| 52 | const double log2_f1 = log2_ymin + 0.5 * log2_dy, log2_ymax = log2cxx . . $ log2(fee4f1d22bc8e617). (fmax); |
| 53 | MultiSampledSpectrogram_initcxx . . $ MultiSampledSpectrogram_init(3992d859142ced2). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, log2_ymin, log2_ymax, numberOfBins, log2_dy, log2_f1, frequencyResolutionInBins); |
| 54 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 55 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 56 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Could not create ConstantQLog2FSpectrogram."); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | double ConstantQLog2FSpectrogram_getQualityFactorcxx . . $ ConstantQLog2FSpectrogram_getQualityFactor(bdefd67d161a979e). (ConstantQLog2FSpectrogramcxx . . $ ConstantQLog2FSpectrogram# me) { |
| 61 | const double a = exp2cxx . . $ exp2(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! frequencyResolutionInBinscxx . . $ structMultiSampledSpectrogram#frequencyResolutionInBins. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 62 | return 1.0 / (a - 1.0 / a); |
| 63 | } |
| 64 | |
| 65 | static void MultiSampledSpectrogram_setFrequencyTickscxx . . $ MultiSampledSpectrogram_setFrequencyTicks(a048925a5a734ebd). (MultiSampledSpectrogramcxx . . $ MultiSampledSpectrogram# me, Graphicscxx . . $ Graphics# g, double fmin, double fmax, double df) { |
| 66 | double f = fmin; |
| 67 | while (f <= fmax) { |
| 68 | const double f_hz = mycxx . . $ `melder/melder.h:44:9`! v_myFrequencyUnitToHertzcxx . . $ structMultiSampledSpectrogram#v_myFrequencyUnitToHertz(7e3a3c6b59947c6d). (f); |
| 69 | conststring32cxx . . $ conststring32# f_string = Melder_fixedcxx . . $ Melder_fixed(5b9062840a6ade99). (f_hz, 1); |
| 70 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, f, false, true, false, f_string); |
| 71 | f += df; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void ConstantQLog2FSpectrogram_paintcxx . . $ ConstantQLog2FSpectrogram_paint(7044c70a4c992fd2). (ConstantQLog2FSpectrogramcxx . . $ ConstantQLog2FSpectrogram# me, Graphicscxx . . $ Graphics# g, double tmin, double tmax, double fmin_hz, double fmax_hz, double dBRange, bool garnish) { |
| 76 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 77 | double fmin, fmax; |
| 78 | if (fmin_hz == 0.0 && fmax_hz == 0.0) { |
| 79 | fmin = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.; |
| 80 | fmax = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.; |
| 81 | } else { |
| 82 | fmin = ( fmin_hz > 0.0 ? mycxx . . $ `melder/melder.h:44:9`! v_hertzToMyFrequencyUnitcxx . . $ structConstantQLog2FSpectrogram#v_hertzToMyFrequencyUnit(7e3a3c6b59947c6d). (fmin_hz) : mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. ); |
| 83 | fmax = ( fmax_hz > 0.0 ? mycxx . . $ `melder/melder.h:44:9`! v_hertzToMyFrequencyUnitcxx . . $ structConstantQLog2FSpectrogram#v_hertzToMyFrequencyUnit(7e3a3c6b59947c6d). (fmax_hz) : mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. ); |
| 84 | } |
| 85 | if (! Function_intersectRangeWithDomaincxx . . $ Function_intersectRangeWithDomain(b58540e0674cd12). (me, & fmin, & fmax)) |
| 86 | return; |
| 87 | MultiSampledSpectrogram_paintInsidecxx . . $ MultiSampledSpectrogram_paintInside(52b1324802957f7). (me, g, tmin, tmax, fmin, fmax, dBRange); |
| 88 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 89 | if (garnish) { |
| 90 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 91 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Time (s)"); |
| 92 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 93 | Graphics_inqWindowcxx . . $ Graphics_inqWindow(962850f2031ce0d0). (g, & tmin, & tmax, & fmin, & fmax); |
| 94 | MultiSampledSpectrogram_setFrequencyTickscxx . . $ MultiSampledSpectrogram_setFrequencyTicks(a048925a5a734ebd). (me, g, fmin, fmax, 1.0); |
| 95 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Frequency (log__2_Hz)"); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | autoConstantQLog2FSpectrogramcxx . . $ autoConstantQLog2FSpectrogram# ConstantQLog2FSpectrogram_translateSpectrumcxx . . $ ConstantQLog2FSpectrogram_translateSpectrum(decf33055da53aef). (ConstantQLog2FSpectrogramcxx . . $ ConstantQLog2FSpectrogram# me, double fromTime, double toTime, double fromFrequency, double shiftNumberOfBins) { |
| 100 | try { |
| 101 | autoConstantQLog2FSpectrogramcxx . . $ autoConstantQLog2FSpectrogram# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 102 | if (shiftNumberOfBins == 0.0) |
| 103 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 104 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fabs (shiftNumberOfBins) < mycxx . . $ `melder/melder.h:44:9`! nx, |
| 105 | U"The shift should not be larger than the number of frequency bins (", mycxx . . $ `melder/melder.h:44:9`! nx, U")."); |
| 106 | fromFrequency = ( fromFrequency <= 0.0 ? (shiftNumberOfBins > 0.0 ? mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. : mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.) : mycxx . . $ `melder/melder.h:44:9`! v_hertzToMyFrequencyUnitcxx . . $ structConstantQLog2FSpectrogram#v_hertzToMyFrequencyUnit(7e3a3c6b59947c6d). (fromFrequency) ); |
| 107 | if (shiftNumberOfBins > 0.0) { |
| 108 | // start at the highest frequency and work down. |
| 109 | const integercxx . . $ integer# ifreqFrom = Sampled_xToHighIndexcxx . . $ Sampled_xToHighIndex(405c2491bc3636e9). (me, fromFrequency); |
| 110 | const integercxx . . $ integer# int_shiftNumberofBins = shiftNumberOfBins; |
| 111 | const integercxx . . $ integer# lowestBin = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., ifreqFrom - int_shiftNumberofBins); |
| 112 | for (integercxx . . $ integer# ifreq = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; ifreq >= lowestBin; ifreq --) { |
| 113 | FrequencyBincxx . . $ FrequencyBin# to_bin = thycxx . . $ `melder/melder.h:45:9`! frequencyBinscxx . . $ structMultiSampledSpectrogram#frequencyBins..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ifreq]; |
| 114 | const integercxx . . $ integer# from_index = ifreq - int_shiftNumberofBins; |
| 115 | integercxx . . $ integer# itmin, itmax; |
| 116 | if (Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (to_bin, fromTime, toTime, & itmin, & itmax) > 0 && from_index > 0) { |
| 117 | FrequencyBincxx . . $ FrequencyBin# from_bin = mycxx . . $ `melder/melder.h:44:9`! frequencyBinscxx . . $ structMultiSampledSpectrogram#frequencyBins..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).from_index]; |
| 118 | for (integercxx . . $ integer# index = itmin; index <= itmax; index ++) { |
| 119 | const double time = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (to_bin, index); |
| 120 | const double newValue = FrequencyBin_getValueAtXcxx . . $ FrequencyBin_getValueAtX(beabf3ea78b0ebe). (from_bin, time, kVector_valueInterpolationcxx . . $ kVector_valueInterpolation#::SINC70cxx . . $ kVector_valueInterpolation#SINC70.); |
| 121 | if (isdefinedcxx . . $ isdefined(f8197d536af80c96). (newValue)) |
| 122 | to_bin -> zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = newValue; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 129 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 130 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": shift not completed."); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (GaborSpectrogram, MultiSampledSpectrogram, 0); |
| 135 | |
| 136 | void structGaborSpectrogramcxx . . $ structGaborSpectrogram# :: v1_infocxx . . $ structGaborSpectrogram#v1_info(49f6e7a06ebc5aa8). () { |
| 137 | MultiSampledSpectrogram_Parentcxx . . $ MultiSampledSpectrogram_Parent# :: v1_infocxx . . $ structFunction#v1_info(49f6e7a06ebc5aa8). (); |
| 138 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Frequency resolution in bins: ", frequencyResolutionInBinscxx . . $ structMultiSampledSpectrogram#frequencyResolutionInBins.); |
| 139 | } |
| 140 | |
| 141 | void GaborSpectrogram_paintcxx . . $ GaborSpectrogram_paint(943fc4f4780261aa). (GaborSpectrogramcxx . . $ GaborSpectrogram# me, Graphicscxx . . $ Graphics# g, double tmin, double tmax, double fmin, double fmax, double dBRange, bool garnish) { |
| 142 | Function_bidirectionalAutowindowcxx . . $ Function_bidirectionalAutowindow(88b7840c59a57eaf). (me, & fmin, & fmax); |
| 143 | if (! Function_intersectRangeWithDomaincxx . . $ Function_intersectRangeWithDomain(b58540e0674cd12). (me, & fmin, & fmax)) |
| 144 | return; |
| 145 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 146 | MultiSampledSpectrogram_paintInsidecxx . . $ MultiSampledSpectrogram_paintInside(52b1324802957f7). (me, g, tmin, tmax, fmin, fmax, dBRange); |
| 147 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 148 | if (garnish) { |
| 149 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 150 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Time (s)"); |
| 151 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 152 | Graphics_inqWindowcxx . . $ Graphics_inqWindow(962850f2031ce0d0). (g, & tmin, & tmax, & fmin, & fmax); |
| 153 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 154 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Frequency (Hz)"); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | autoGaborSpectrogramcxx . . $ autoGaborSpectrogram# GaborSpectrogram_createcxx . . $ GaborSpectrogram_create(25d0239ed9e3fa48). (double tmin, double tmax, double fmax, double filterBandwidth, double df) { |
| 159 | try { |
| 160 | autoGaborSpectrogramcxx . . $ autoGaborSpectrogram# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (GaborSpectrogram); |
| 161 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (filterBandwidth > 0.0); |
| 162 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (df > 0.0); |
| 163 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (filterBandwidth <= fmax, |
| 164 | U"The filter bandwidth should not exceed ", fmax, U"."); |
| 165 | const integercxx . . $ integer# numberOfFrequencyBins = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((fmax - filterBandwidth) / df) + 1; |
| 166 | const double f1 = 0.5 * (fmax - (numberOfFrequencyBins - 1) * df); |
| 167 | const double frequencyResolutionBins = 0.5 * filterBandwidth / df; |
| 168 | MultiSampledSpectrogram_initcxx . . $ MultiSampledSpectrogram_init(3992d859142ced2). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, 0.0, fmax, numberOfFrequencyBins, df, f1, frequencyResolutionBins); |
| 169 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 170 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 171 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Could not create GaborSpectrogram."); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* End of file ConstantQSpectrograms.cpp */ |