annotate trunk/src/Modules/BMM/ModuleGammatone.cc @ 287:4b3a43b543dd

-<math.h> replaced wit <cmath> where possible -SSI support added but not yet tested
author tomwalters
date Fri, 19 Feb 2010 15:19:27 +0000
parents e55d0c225a57
children 34993448961f
rev   line source
tomwalters@276 1 // Copyright 2009-2010, Thomas Walters
tomwalters@276 2 //
tomwalters@276 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@276 4 // http://www.acousticscale.org/AIMC
tomwalters@276 5 //
tomwalters@276 6 // This program is free software: you can redistribute it and/or modify
tomwalters@276 7 // it under the terms of the GNU General Public License as published by
tomwalters@276 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@276 9 // (at your option) any later version.
tomwalters@276 10 //
tomwalters@276 11 // This program is distributed in the hope that it will be useful,
tomwalters@276 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@276 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@276 14 // GNU General Public License for more details.
tomwalters@276 15 //
tomwalters@276 16 // You should have received a copy of the GNU General Public License
tomwalters@276 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@276 18
tomwalters@276 19 /*! \file
tomwalters@276 20 * \brief Slaney's gammatone filterbank
tomwalters@276 21 *
tomwalters@276 22 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@276 23 * \date created 2009/11/13
tomwalters@276 24 * \version \$Id$
tomwalters@276 25 */
tomwalters@276 26
tomwalters@287 27 #include <cmath>
tomwalters@276 28 #include <complex>
tomwalters@277 29 #include "Support/ERBTools.h"
tomwalters@276 30
tomwalters@276 31 #include "Modules/BMM/ModuleGammatone.h"
tomwalters@276 32
tomwalters@276 33 namespace aimc {
tomwalters@277 34 using std::vector;
tomwalters@276 35 using std::complex;
tomwalters@276 36 ModuleGammatone::ModuleGammatone(Parameters *params) : Module(params) {
tomwalters@277 37 module_identifier_ = "gt";
tomwalters@276 38 module_type_ = "bmm";
tomwalters@276 39 module_description_ = "Gammatone filterbank (Slaney's IIR gammatone)";
tomwalters@276 40 module_version_ = "$Id$";
tomwalters@276 41
tomwalters@276 42 num_channels_ = parameters_->DefaultInt("gtfb.channel_count", 200);
tomwalters@276 43 min_frequency_ = parameters_->DefaultFloat("gtfb.min_frequency", 86.0f);
tomwalters@276 44 max_frequency_ = parameters_->DefaultFloat("gtfb.max_frequency", 16000.0f);
tomwalters@276 45 }
tomwalters@276 46
tomwalters@277 47 ModuleGammatone::~ModuleGammatone() {
tomwalters@277 48 }
tomwalters@277 49
tomwalters@277 50 void ModuleGammatone::ResetInternal() {
tomwalters@277 51 state_.resize(num_channels_);
tomwalters@277 52 for (int i = 0; i < num_channels_; ++i) {
tomwalters@277 53 state_[i].resize(9, 0.0f);
tomwalters@277 54 }
tomwalters@277 55 }
tomwalters@277 56
tomwalters@276 57 bool ModuleGammatone::InitializeInternal(const SignalBank& input) {
tomwalters@276 58 // Calculate number of channels, and centre frequencies
tomwalters@277 59 float erb_max = ERBTools::Freq2ERB(max_frequency_);
tomwalters@277 60 float erb_min = ERBTools::Freq2ERB(min_frequency_);
tomwalters@277 61 float delta_erb = (erb_max - erb_min) / (num_channels_ - 1);
tomwalters@277 62
tomwalters@277 63 centre_frequencies_.resize(num_channels_);
tomwalters@277 64 float erb_current = erb_min;
tomwalters@277 65
tomwalters@277 66 for (int i = 0; i < num_channels_; ++i) {
tomwalters@280 67 centre_frequencies_[i] = ERBTools::ERB2Freq(erb_current);
tomwalters@280 68 erb_current += delta_erb;
tomwalters@277 69 }
tomwalters@276 70
tomwalters@276 71 forward_.resize(num_channels_);
tomwalters@277 72 back_.resize(num_channels_);
tomwalters@277 73 state_.resize(num_channels_);
tomwalters@276 74
tomwalters@276 75 for (int ch = 0; ch < num_channels_; ++ch) {
tomwalters@277 76 float cf = centre_frequencies_[ch];
tomwalters@277 77 float erb = ERBTools::Freq2ERBw(cf);
tomwalters@276 78
tomwalters@276 79 // Sample interval
tomwalters@277 80 float dt = 1.0f / input.sample_rate();
tomwalters@276 81
tomwalters@276 82 // Bandwidth parameter
tomwalters@276 83 float b = 1.019f * 2.0f * M_PI * erb;
tomwalters@276 84
tomwalters@280 85 // All of the following expressions are derived in Apple TR #35, "An
tomwalters@280 86 // Efficient Implementation of the Patterson-Holdsworth Cochlear
tomwalters@276 87 // Filter Bank".
tomwalters@276 88
tomwalters@276 89 // Calculate the gain:
tomwalters@277 90 float cpt = cf * M_PI * dt;
tomwalters@277 91 complex<float> exponent(0.0f, 2.0f * cpt);
tomwalters@277 92 complex<float> ec = exp(2.0f * exponent);
tomwalters@277 93 complex<float> two_cf_pi_t(2.0f * cpt, 0.0f);
tomwalters@277 94 complex<float> two_pow(pow(2.0f, (3.0f / 2.0f)), 0.0f);
tomwalters@277 95 complex<float> p = -2.0f * ec * dt
tomwalters@277 96 + 2.0f * exp(-(b * dt) + exponent) * dt;
tomwalters@277 97 complex<float> b_dt(b * dt, 0.0f);
tomwalters@276 98
tomwalters@276 99 float gain = abs(
tomwalters@277 100 (p * (cos(two_cf_pi_t) - sqrt(3.0f - two_pow) * sin(two_cf_pi_t)))
tomwalters@277 101 * (p * (cos(two_cf_pi_t) + sqrt(3.0f - two_pow) * sin(two_cf_pi_t)))
tomwalters@277 102 * (p * (cos(two_cf_pi_t) - sqrt(3.0f + two_pow) * sin(two_cf_pi_t)))
tomwalters@277 103 * (p * (cos(two_cf_pi_t) + sqrt(3.0f + two_pow) * sin(two_cf_pi_t)))
tomwalters@277 104 / pow(-2.0f / exp(2.0f * b_dt) - 2.0f * ec + 2.0f * (1.0f + ec)
tomwalters@277 105 / exp(b_dt), 4.0f));
tomwalters@276 106
tomwalters@276 107 // The filter coefficients themselves:
tomwalters@277 108 const int coeff_count = 9;
tomwalters@277 109 forward_[ch].resize(coeff_count, 0.0f);
tomwalters@277 110 back_[ch].resize(coeff_count, 0.0f);
tomwalters@277 111 state_[ch].resize(coeff_count, 0.0f);
tomwalters@276 112
tomwalters@277 113 forward_[ch][0] = pow(dt, 4.0f) / gain;
tomwalters@277 114 forward_[ch][1] = (-4.0f * pow(dt, 4.0f) * cos(2.0f * cpt)
tomwalters@276 115 / exp(b * dt) / gain);
tomwalters@277 116 forward_[ch][2] = (6.0f * pow(dt, 4.0f) * cos(4.0f * cpt)
tomwalters@276 117 / exp(2.0f * b * dt) / gain);
tomwalters@277 118 forward_[ch][3] = (-4.0f * pow(dt, 4.0f) * cos(6.0f * cpt)
tomwalters@276 119 / exp(3.0f * b * dt) / gain);
tomwalters@277 120 forward_[ch][4] = (pow(dt, 4.0f) * cos(8.0f * cpt)
tomwalters@276 121 / exp(4.0f * b * dt) / gain);
tomwalters@277 122 // Note: the remainder of the forward vector is zero-padded
tomwalters@276 123
tomwalters@277 124 back_[ch][0] = 1.0f;
tomwalters@277 125 back_[ch][1] = -8.0f * cos(2.0f * cpt) / exp(b * dt);
tomwalters@277 126 back_[ch][2] = (4.0f * (4.0f + 3.0f * cos(4.0f * cpt))
tomwalters@277 127 / exp(2.0f * b * dt));
tomwalters@277 128 back_[ch][3] = (-8.0f * (6.0f * cos(2.0f * cpt) + cos(6.0f * cpt))
tomwalters@277 129 / exp(3.0f * b * dt));
tomwalters@277 130 back_[ch][4] = (2.0f * (18.0f + 16.0f * cos(4.0f * cpt) + cos(8.0f * cpt))
tomwalters@277 131 / exp(4.0f * b * dt));
tomwalters@277 132 back_[ch][5] = (-8.0f * (6.0f * cos(2.0f * cpt) + cos(6.0f * cpt))
tomwalters@277 133 / exp(5.0f * b * dt));
tomwalters@277 134 back_[ch][6] = (4.0f * (4.0f + 3.0f * cos(4.0f * cpt))
tomwalters@277 135 / exp(6.0f * b * dt));
tomwalters@277 136 back_[ch][7] = -8.0f * cos(2.0f * cpt) / exp(7.0f * b * dt);
tomwalters@277 137 back_[ch][8] = exp(-8.0f * b * dt);
tomwalters@276 138 }
tomwalters@277 139 output_.Initialize(num_channels_,
tomwalters@277 140 input.buffer_length(),
tomwalters@277 141 input.sample_rate());
tomwalters@277 142 return true;
tomwalters@276 143 }
tomwalters@277 144
tomwalters@277 145 void ModuleGammatone::Process(const SignalBank &input) {
tomwalters@277 146 output_.set_start_time(input.start_time());
tomwalters@277 147 int audio_channel = 0;
tomwalters@277 148
tomwalters@277 149 vector<vector<float> >::iterator b = forward_.begin();
tomwalters@277 150 vector<vector<float> >::iterator a = back_.begin();
tomwalters@277 151 vector<vector<float> >::iterator s = state_.begin();
tomwalters@277 152
tomwalters@277 153 for (int ch = 0; ch < num_channels_; ++ch, ++a, ++b, ++s) {
tomwalters@277 154 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@280 155 // Direct-form-II IIR filter
tomwalters@277 156 float in = input.sample(audio_channel, i);
tomwalters@277 157 float out = (*b)[0] * in + (*s)[0];
tomwalters@277 158 for (unsigned int stage = 1; stage < s->size(); ++stage)
tomwalters@277 159 (*s)[stage - 1] = (*b)[stage] * in - (*a)[stage] * out + (*s)[stage];
tomwalters@277 160 output_.set_sample(ch, i, out);
tomwalters@277 161 }
tomwalters@277 162 }
tomwalters@277 163 PushOutput();
tomwalters@277 164 }
tomwalters@277 165
tomwalters@280 166 } // namespace aimc