| 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" |
| 20 |
#include "ConstantQSpectrograms.h" |
| 21 |
|
| 22 |
Thing_implement (ConstantQLog2FSpectrogram, MultiSampledSpectrogram, 0); |
| 23 |
|
| 24 |
void structConstantQLog2FSpectrogram :: v1_info () { |
| 25 |
ConstantQLog2FSpectrogram_Parent :: v1_info (); |
| 26 |
MelderInfo_writeLine (U"Frequency resolution in bins: ", frequencyResolutionInBins); |
| 27 |
MelderInfo_writeLine (U"Quality factor Q: ", ConstantQLog2FSpectrogram_getQualityFactor (this)); |
| 28 |
} |
| 29 |
|
| 30 |
double structConstantQLog2FSpectrogram :: v_getValueAtSample (integer ifreq, integer iframe , int unit) const { |
| 31 |
FrequencyBin bin = frequencyBins.at [ifreq]; |
| 32 |
const double value = bin -> v_getValueAtSample (iframe, 1, unit); |
| 33 |
return ( isdefined (value) ? our v_convertStandardToSpecialUnit (value, iframe, unit) : undefined ); |
| 34 |
} |
| 35 |
|
| 36 |
double structConstantQLog2FSpectrogram :: v_myFrequencyUnitToHertz (double log2_f) const { |
| 37 |
return exp2 (log2_f); |
| 38 |
} |
| 39 |
|
| 40 |
double structConstantQLog2FSpectrogram :: v_hertzToMyFrequencyUnit (double f_hz) const { |
| 41 |
return ( f_hz > 0.0 ? log2 (f_hz) : undefined ); |
| 42 |
} |
| 43 |
|
| 44 |
autoConstantQLog2FSpectrogram ConstantQLog2FSpectrogram_create (double tmin, double tmax, double f1, double fmax, integer numberOfBinsPerOctave, double frequencyResolutionInBins) { |
| 45 |
try { |
| 46 |
autoConstantQLog2FSpectrogram me = Thing_new (ConstantQLog2FSpectrogram); |
| 47 |
const double log2_dy = 1.0 / numberOfBinsPerOctave; |
| 48 |
const integer numberOfBins = Melder_iroundDown (log2 (fmax / f1) * numberOfBinsPerOctave); |
| 49 |
Melder_require (numberOfBins > 1, |
| 50 |
U"The number of bins should be larger than 1."); |
| 51 |
const double log2_ymin = log2 (f1); |
| 52 |
const double log2_f1 = log2_ymin + 0.5 * log2_dy, log2_ymax = log2 (fmax); |
| 53 |
MultiSampledSpectrogram_init (me.get(), tmin, tmax, log2_ymin, log2_ymax, numberOfBins, log2_dy, log2_f1, frequencyResolutionInBins); |
| 54 |
return me; |
| 55 |
} catch (MelderError) { |
| 56 |
Melder_throw (U"Could not create ConstantQLog2FSpectrogram."); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
double ConstantQLog2FSpectrogram_getQualityFactor (ConstantQLog2FSpectrogram me) { |
| 61 |
const double a = exp2 (my frequencyResolutionInBins * my dx); |
| 62 |
return 1.0 / (a - 1.0 / a); |
| 63 |
} |
| 64 |
|
| 65 |
static void MultiSampledSpectrogram_setFrequencyTicks (MultiSampledSpectrogram me, Graphics g, double fmin, double fmax, double df) { |
| 66 |
double f = fmin; |
| 67 |
while (f <= fmax) { |
| 68 |
const double f_hz = my v_myFrequencyUnitToHertz (f); |
| 69 |
conststring32 f_string = Melder_fixed (f_hz, 1); |
| 70 |
Graphics_markLeft (g, f, false, true, false, f_string); |
| 71 |
f += df; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void ConstantQLog2FSpectrogram_paint (ConstantQLog2FSpectrogram me, Graphics g, double tmin, double tmax, double fmin_hz, double fmax_hz, double dBRange, bool garnish) { |
| 76 |
Graphics_setInner (g); |
| 77 |
double fmin, fmax; |
| 78 |
if (fmin_hz == 0.0 && fmax_hz == 0.0) { |
| 79 |
fmin = my xmin; |
| 80 |
fmax = my xmax; |
| 81 |
} else { |
| 82 |
fmin = ( fmin_hz > 0.0 ? my v_hertzToMyFrequencyUnit (fmin_hz) : my xmin ); |
| 83 |
fmax = ( fmax_hz > 0.0 ? my v_hertzToMyFrequencyUnit (fmax_hz) : my xmin ); |
| 84 |
} |
| 85 |
if (! Function_intersectRangeWithDomain (me, & fmin, & fmax)) |
| 86 |
return; |
| 87 |
MultiSampledSpectrogram_paintInside (me, g, tmin, tmax, fmin, fmax, dBRange); |
| 88 |
Graphics_unsetInner (g); |
| 89 |
if (garnish) { |
| 90 |
Graphics_drawInnerBox (g); |
| 91 |
Graphics_textBottom (g, true, U"Time (s)"); |
| 92 |
Graphics_marksBottom (g, 2, true, true, false); |
| 93 |
Graphics_inqWindow (g, & tmin, & tmax, & fmin, & fmax); |
| 94 |
MultiSampledSpectrogram_setFrequencyTicks (me, g, fmin, fmax, 1.0); |
| 95 |
Graphics_textLeft (g, true, U"Frequency (log__2_Hz)"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
autoConstantQLog2FSpectrogram ConstantQLog2FSpectrogram_translateSpectrum (ConstantQLog2FSpectrogram me, double fromTime, double toTime, double fromFrequency, double shiftNumberOfBins) { |
| 100 |
try { |
| 101 |
autoConstantQLog2FSpectrogram thee = Data_copy (me); |
| 102 |
if (shiftNumberOfBins == 0.0) |
| 103 |
return thee; |
| 104 |
Melder_require (fabs (shiftNumberOfBins) < my nx, |
| 105 |
U"The shift should not be larger than the number of frequency bins (", my nx, U")."); |
| 106 |
fromFrequency = ( fromFrequency <= 0.0 ? (shiftNumberOfBins > 0.0 ? my x1 : my xmax) : my v_hertzToMyFrequencyUnit (fromFrequency) ); |
| 107 |
if (shiftNumberOfBins > 0.0) { |
| 108 |
// start at the highest frequency and work down. |
| 109 |
const integer ifreqFrom = Sampled_xToHighIndex (me, fromFrequency); |
| 110 |
const integer int_shiftNumberofBins = shiftNumberOfBins; |
| 111 |
const integer lowestBin = std::max (1_integer, ifreqFrom - int_shiftNumberofBins); |
| 112 |
for (integer ifreq = my nx; ifreq >= lowestBin; ifreq --) { |
| 113 |
FrequencyBin to_bin = thy frequencyBins.at [ifreq]; |
| 114 |
const integer from_index = ifreq - int_shiftNumberofBins; |
| 115 |
integer itmin, itmax; |
| 116 |
if (Sampled_getWindowSamples (to_bin, fromTime, toTime, & itmin, & itmax) > 0 && from_index > 0) { |
| 117 |
FrequencyBin from_bin = my frequencyBins.at [from_index]; |
| 118 |
for (integer index = itmin; index <= itmax; index ++) { |
| 119 |
const double time = Sampled_indexToX (to_bin, index); |
| 120 |
const double newValue = FrequencyBin_getValueAtX (from_bin, time, kVector_valueInterpolation::SINC70); |
| 121 |
if (isdefined (newValue)) |
| 122 |
to_bin -> z [1] [index] = newValue; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return thee; |
| 129 |
} catch (MelderError) { |
| 130 |
Melder_throw (me, U": shift not completed."); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
Thing_implement (GaborSpectrogram, MultiSampledSpectrogram, 0); |
| 135 |
|
| 136 |
void structGaborSpectrogram :: v1_info () { |
| 137 |
MultiSampledSpectrogram_Parent :: v1_info (); |
| 138 |
MelderInfo_writeLine (U"Frequency resolution in bins: ", frequencyResolutionInBins); |
| 139 |
} |
| 140 |
|
| 141 |
void GaborSpectrogram_paint (GaborSpectrogram me, Graphics g, double tmin, double tmax, double fmin, double fmax, double dBRange, bool garnish) { |
| 142 |
Function_bidirectionalAutowindow (me, & fmin, & fmax); |
| 143 |
if (! Function_intersectRangeWithDomain (me, & fmin, & fmax)) |
| 144 |
return; |
| 145 |
Graphics_setInner (g); |
| 146 |
MultiSampledSpectrogram_paintInside (me, g, tmin, tmax, fmin, fmax, dBRange); |
| 147 |
Graphics_unsetInner (g); |
| 148 |
if (garnish) { |
| 149 |
Graphics_drawInnerBox (g); |
| 150 |
Graphics_textBottom (g, true, U"Time (s)"); |
| 151 |
Graphics_marksBottom (g, 2, true, true, false); |
| 152 |
Graphics_inqWindow (g, & tmin, & tmax, & fmin, & fmax); |
| 153 |
Graphics_marksLeft (g, 2, true, true, false); |
| 154 |
Graphics_textLeft (g, true, U"Frequency (Hz)"); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
autoGaborSpectrogram GaborSpectrogram_create (double tmin, double tmax, double fmax, double filterBandwidth, double df) { |
| 159 |
try { |
| 160 |
autoGaborSpectrogram me = Thing_new (GaborSpectrogram); |
| 161 |
Melder_assert (filterBandwidth > 0.0); |
| 162 |
Melder_assert (df > 0.0); |
| 163 |
Melder_require (filterBandwidth <= fmax, |
| 164 |
U"The filter bandwidth should not exceed ", fmax, U"."); |
| 165 |
const integer numberOfFrequencyBins = Melder_ifloor ((fmax - filterBandwidth) / df) + 1; |
| 166 |
const double f1 = 0.5 * (fmax - (numberOfFrequencyBins - 1) * df); |
| 167 |
const double frequencyResolutionBins = 0.5 * filterBandwidth / df; |
| 168 |
MultiSampledSpectrogram_init (me.get(), tmin, tmax, 0.0, fmax, numberOfFrequencyBins, df, f1, frequencyResolutionBins); |
| 169 |
return me; |
| 170 |
} catch (MelderError) { |
| 171 |
Melder_throw (U"Could not create GaborSpectrogram."); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/* End of file ConstantQSpectrograms.cpp */ |