| 1 | #ifndef _FilterBank_h_ |
| 2 | #define _FilterBank_h_ |
| 3 | /* FilterBank.h |
| 4 | * |
| 5 | * Copyright (C) 1993-2017 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 2 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 | /* |
| 22 | djmw 20010609 |
| 23 | djmw 20020813 GPL header |
| 24 | djmw 20040702 Corrected MELTOBARK formula. |
| 25 | djmw 20110306 Latest modification. |
| 26 | */ |
| 27 | |
| 28 | #include "TableOfReal.h" |
| 29 | #include "Spectrum.h" |
| 30 | #include "MFCC.h" |
| 31 | #include "Intensity.h" |
| 32 | #include "PCA.h" |
| 33 | #include "Pitch.h" |
| 34 | #include "Spectrogram_extensions.h" |
| 35 | |
| 36 | #define HZTOMEL(x) NUMhertzToMel2(x) |
| 37 | #define MELTOHZ(x) NUMmelToHertz2(x) |
| 38 | |
| 39 | #define FilterBank_DBREF 4e-10 |
| 40 | #define FilterBank_DBFAC 1.0 |
| 41 | #define FilterBank_DBFLOOR -20 |
| 42 | |
| 43 | #define FilterBank_HERTZ 1 |
| 44 | #define FilterBank_BARK 2 |
| 45 | #define FilterBank_MEL 3 |
| 46 | |
| 47 | // FilterBank, BarkFilter, MelFilter are deprecated as of october 2014. |
| 48 | // New types are BandFilterSpectrogram, BarkSpectrogram and MelSpectrogram. |
| 49 | // This interface is maintained because older scripts still have to work. |
| 50 | |
| 51 | Thing_define (FilterBank, Matrix) { |
| 52 | virtual int v_getFrequencyScale () |
| 53 | { return FilterBank_HERTZ; } |
| 54 | virtual conststring32 v_getFrequencyUnit () |
| 55 | { return U"Hz"; } // compatibility with BandFilterSpectrogram |
| 56 | }; |
| 57 | |
| 58 | Thing_define (BarkFilter, FilterBank) { |
| 59 | int v_getFrequencyScale () |
| 60 | override { return FilterBank_BARK; } |
| 61 | conststring32 v_getFrequencyUnit () |
| 62 | override { return U"bark"; } |
| 63 | }; |
| 64 | |
| 65 | /* |
| 66 | Interpretation: |
| 67 | xmin, xmax, x1, dx, nx like Sampled. |
| 68 | ymin, ymax lowest and highest frequencies in Barks. |
| 69 | y1 mid of first filter (bark). |
| 70 | dy distance between filters (bark). |
| 71 | ny the number of filters. |
| 72 | */ |
| 73 | |
| 74 | double FilterBank_getFrequencyInHertz (FilterBank me, double f, int scale_from); |
| 75 | double FilterBank_getFrequencyInBark (FilterBank me, double f, int scale_from); |
| 76 | double FilterBank_getFrequencyInMel (FilterBank me, double f, int scale_from); |
| 77 | |
| 78 | int FilterBank_getFrequencyScale (FilterBank me); |
| 79 | |
| 80 | void FilterBank_drawFrequencyScales (FilterBank me, Graphics g, int horizontalScale, double xmin, |
| 81 | double xmax, int verticalScale, double ymin, double ymax, bool garnish); |
| 82 | |
| 83 | void BarkFilter_drawSekeyHansonFilterFunctions (BarkFilter me, Graphics g, |
| 84 | int freqScale, int fromFilter, int toFilter, double zmin, double zmax, |
| 85 | int dbScale, double ymin, double ymax, bool garnish); |
| 86 | |
| 87 | void FilterBank_drawTimeSlice (FilterBank me, Graphics g, double t, double fmin, double fmax, |
| 88 | double min, double max, conststring32 xlabel, bool garnish); |
| 89 | |
| 90 | void FilterBank_paint (FilterBank me, Graphics g, double xmin, double xmax, double ymin, double ymax, double minimum, double maximum, bool garnish); |
| 91 | |
| 92 | autoBarkFilter BarkFilter_create (double tmin, double tmax, integer nt, double dt, double t1, |
| 93 | double fmin, double fmax, integer nf, double df, double f1); |
| 94 | |
| 95 | autoBarkFilter Matrix_to_BarkFilter (Matrix me); |
| 96 | |
| 97 | Thing_define (MelFilter, FilterBank) { |
| 98 | int v_getFrequencyScale () |
| 99 | override { return FilterBank_MEL; } |
| 100 | conststring32 v_getFrequencyUnit () |
| 101 | override { return U"mel"; } |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | Interpretation: |
| 106 | xmin, xmax, x1, dx, nx like Sampled. |
| 107 | ymin, ymax lowest and highest frequencies in mels. |
| 108 | y1 mid of first filter (mels). |
| 109 | dy distance between filters (mel). |
| 110 | ny the number of filters. |
| 111 | */ |
| 112 | |
| 113 | autoMelFilter MelFilter_create (double tmin, double tmax, integer nt, double dt, |
| 114 | double t1, double fmin, double fmax, integer nf, double df, double f1); |
| 115 | |
| 116 | autoMelFilter Matrix_to_MelFilter (Matrix); |
| 117 | |
| 118 | void MelFilter_drawFilterFunctions (MelFilter me, Graphics g, |
| 119 | int freqScale, int fromFilter, int toFilter, double zmin, double zmax, |
| 120 | int dbScale, double ymin, double ymax, bool garnish); |
| 121 | |
| 122 | autoMFCC MelFilter_to_MFCC (MelFilter me, integer numberOfCoefficients); |
| 123 | |
| 124 | Thing_define (FormantFilter, FilterBank) { |
| 125 | conststring32 v_getFrequencyUnit () |
| 126 | override { return U"Hz"; } |
| 127 | }; |
| 128 | |
| 129 | autoFormantFilter FormantFilter_create (double tmin, double tmax, integer nt, |
| 130 | double dt, double t1, double fmin, double fmax, integer nf, double df, |
| 131 | double f1); |
| 132 | |
| 133 | void FormantFilter_drawFilterFunctions (FormantFilter me, Graphics g, double bandwidth, |
| 134 | int freqScale, int fromFilter, int toFilter, double zmin, double zmax, |
| 135 | int dbScale, double ymin, double ymax, bool garnish); |
| 136 | |
| 137 | autoFormantFilter Matrix_to_FormantFilter (Matrix me); |
| 138 | |
| 139 | void FilterBank_equalizeIntensities (FilterBank me, double intensity_db); |
| 140 | |
| 141 | autoMatrix FilterBank_to_Matrix (FilterBank me); |
| 142 | /* Generieke ...Filters_to_Matrix */ |
| 143 | |
| 144 | autoSpectrum FormantFilter_to_Spectrum_slice (FormantFilter me, double t); |
| 145 | /* |
| 146 | Calculate amplitude spectrum at time t. |
| 147 | power [i] = ref * 10 ^ (my z [i] [t] / 10) |
| 148 | spec -> z [1] [i] = sqrt (power [i]) = sqrt (ref) * 10 ^ (my z [i] [t] / (2*10)) |
| 149 | spec -> z [2] [i] = 0 |
| 150 | */ |
| 151 | |
| 152 | autoIntensity FilterBank_to_Intensity (FilterBank me); |
| 153 | |
| 154 | void FilterBank_PCA_drawComponent (FilterBank me, PCA thee, Graphics g, integer component, double dblevel, |
| 155 | double frequencyOffset, double scale, double tmin, double tmax, double fmin, double fmax); |
| 156 | |
| 157 | // Convert old types to new types |
| 158 | |
| 159 | autoMelSpectrogram MelFilter_to_MelSpectrogram (MelFilter me); |
| 160 | |
| 161 | autoBarkSpectrogram BarkFilter_to_BarkSpectrogram (BarkFilter me); |
| 162 | |
| 163 | autoSpectrogram FormantFilter_to_Spectrogram (FormantFilter me); |
| 164 | |
| 165 | |
| 166 | autoMFCC MelFilter_to_MFCC (MelFilter me, integer numberOfCoefficients); |
| 167 | /* |
| 168 | Calculates the Cosine Transform of the filterbank values. |
| 169 | */ |
| 170 | |
| 171 | autoMelFilter MFCC_to_MelFilter (MFCC me, integer firstCoefficient, integer lastCoefficient); |
| 172 | /* |
| 173 | Calculates the Inverse CT of cepstral coefficients. |
| 174 | */ |
| 175 | |
| 176 | autoBarkFilter Sound_to_BarkFilter (Sound me, double analysisWidth, double dt, |
| 177 | double f1_bark, double fmax_bark, double df_bark); |
| 178 | /* |
| 179 | Filtering with filters on a Bark scale as defined by |
| 180 | Andrew Sekey & Brian Hanson (1984), "Improved 1-Bark bandwidth |
| 181 | "auditory filter", Jasa 75, 1902-1904. |
| 182 | Although not explicitely stated the filter function is defined in the |
| 183 | power domain. |
| 184 | 10 log F(z) = 15.8 + 7.5(z + 0.5) - 17.5 * sqrt(1 + (z + 0.5)^2) |
| 185 | */ |
| 186 | |
| 187 | autoMelFilter Sound_to_MelFilter (Sound me, double analysisWidth, double dt, |
| 188 | double f1_mel, double fmax_mel, double df_mel); |
| 189 | |
| 190 | autoFormantFilter Sound_to_FormantFilter (Sound me, double analysisWidth, |
| 191 | double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw, |
| 192 | double pitchFloor, double pitchCeiling); |
| 193 | |
| 194 | autoFormantFilter Sound_Pitch_to_FormantFilter (Sound me, Pitch thee, |
| 195 | double analysisWidth, double dt, double f1_hz, double fmax_hz, |
| 196 | double df_hz, double relative_bw); |
| 197 | |
| 198 | autoSound FilterBanks_crossCorrelate (FilterBank me, FilterBank thee, enum kSounds_convolve_scaling scaling, |
| 199 | enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain); |
| 200 | |
| 201 | autoSound FilterBanks_convolve (FilterBank me, FilterBank thee, enum kSounds_convolve_scaling scaling, |
| 202 | enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain); |
| 203 | |
| 204 | #endif /* _FilterBank_h_ */ |