annotate trunk/src/Modules/NAP/ModuleHCL.cc @ 273:c26222c51fb7

- Fixed the python SWIG wrappers - Added stub test for the Gaussian features, and test data - Fixed build errors
author tomwalters
date Mon, 15 Feb 2010 20:37:26 +0000
parents e14c70d1b171
children ce2bab04f155
rev   line source
tomwalters@268 1 // Copyright 2007-2010, Thomas Walters
tomwalters@268 2 //
tomwalters@268 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@268 4 // http://www.acousticscale.org/AIMC
tomwalters@268 5 //
tomwalters@268 6 // This program is free software: you can redistribute it and/or modify
tomwalters@268 7 // it under the terms of the GNU General Public License as published by
tomwalters@268 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@268 9 // (at your option) any later version.
tomwalters@268 10 //
tomwalters@268 11 // This program is distributed in the hope that it will be useful,
tomwalters@268 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@268 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@268 14 // GNU General Public License for more details.
tomwalters@268 15 //
tomwalters@268 16 // You should have received a copy of the GNU General Public License
tomwalters@268 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@268 18
tomwalters@268 19 /*!
tomwalters@268 20 * \file
tomwalters@268 21 * \brief Halfwave rectification, compression and lowpass filtering.
tomwalters@268 22 *
tomwalters@268 23 * \author Tom Walters <tcw24@cam.ac.uk>
tomwalters@268 24 * \date created 2007/03/07
tomwalters@268 25 * \version \$Id: ModuleHCL.cc 4 2010-02-03 18:44:58Z tcw $
tomwalters@268 26 */
tomwalters@268 27
tomwalters@268 28 #include <math.h>
tomwalters@268 29
tomwalters@268 30 #include "Modules/NAP/ModuleHCL.h"
tomwalters@268 31
tomwalters@268 32 namespace aimc {
tomwalters@268 33 ModuleHCL::ModuleHCL(Parameters *parameters) : Module(parameters) {
tomwalters@268 34 module_identifier_ = "hcl";
tomwalters@268 35 module_type_ = "nap";
tomwalters@268 36 module_description_ = "Halfwave rectification, compression "
tomwalters@268 37 "and lowpass filtering";
tomwalters@268 38 module_version_ = "$Id: ModuleHCL.cc 4 2010-02-03 18:44:58Z tcw $";
tomwalters@268 39
tomwalters@273 40 do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", false);
tomwalters@273 41 do_log_ = parameters_->DefaultBool("nap.do_log_compression", false);
tomwalters@273 42 lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0);
tomwalters@273 43 lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2);
tomwalters@268 44 }
tomwalters@268 45
tomwalters@268 46 ModuleHCL::~ModuleHCL() {
tomwalters@268 47 }
tomwalters@268 48
tomwalters@268 49 bool ModuleHCL::InitializeInternal(const SignalBank &input) {
tomwalters@268 50 time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_);
tomwalters@268 51 channel_count_ = input.channel_count();
tomwalters@268 52 output_.Initialize(input);
tomwalters@268 53 Reset();
tomwalters@268 54 return true;
tomwalters@268 55 }
tomwalters@268 56
tomwalters@268 57 void ModuleHCL::Reset() {
tomwalters@268 58 xn_ = 0.0f;
tomwalters@268 59 yn_ = 0.0f;
tomwalters@268 60 yns_.clear();
tomwalters@268 61 yns_.resize(channel_count_);
tomwalters@268 62 for (int c = 0; c < channel_count_; ++c) {
tomwalters@268 63 yns_[c].resize(lowpass_order_, 0.0f);
tomwalters@268 64 }
tomwalters@268 65 }
tomwalters@268 66
tomwalters@268 67 /* With do_log, the signal is first scaled up so that values <1.0 become
tomwalters@268 68 * negligible. This just rescales the sample values to fill the range of a
tomwalters@268 69 * 16-bit signed integer, then we lose the bottom bit of resolution. If the
tomwalters@268 70 * signal was sampled at 16-bit resolution, there shouldn't be anything to
tomwalters@268 71 * speak of there anyway. If it was sampled using a higher resolution, then
tomwalters@268 72 * some data will be discarded.
tomwalters@268 73 */
tomwalters@268 74 void ModuleHCL::Process(const SignalBank &input) {
tomwalters@268 75 output_.set_start_time(input.start_time());
tomwalters@268 76 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@268 77 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@268 78 if (input[c][i] < 0.0f) {
tomwalters@268 79 output_.set_sample(c, i, 0.0f);
tomwalters@268 80 } else {
tomwalters@268 81 float s = input[c][i];
tomwalters@268 82 if (do_log_) {
tomwalters@268 83 s *= pow(2.0f,15);
tomwalters@268 84 if (s < 1.0f) s = 1.0f;
tomwalters@268 85 s = 20.0f * log10(s);
tomwalters@268 86 }
tomwalters@268 87 output_.set_sample(c, i, s);
tomwalters@268 88 }
tomwalters@268 89 }
tomwalters@268 90 if (do_lowpass_) {
tomwalters@268 91 float b = exp( -1.0f / (input.sample_rate() * time_constant_));
tomwalters@268 92 float gain = 1.0f / (1.0f - b);
tomwalters@268 93 for (int j = 0; j < lowpass_order_; j++) {
tomwalters@268 94 for (int k = 0; k < output_.buffer_length(); ++k) {
tomwalters@268 95 xn_ = output_[c][k];
tomwalters@268 96 yn_ = xn_ + b * yns_[c][j];
tomwalters@268 97 yns_[c][j] = yn_;
tomwalters@268 98 output_.set_sample(c, k, yn_ / gain);
tomwalters@268 99 }
tomwalters@268 100 }
tomwalters@268 101 }
tomwalters@268 102 }
tomwalters@268 103 PushOutput();
tomwalters@268 104 }
tomwalters@268 105 } // namespace aimc