FilterBank.h 7.2 KB

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