annotate trunk/src/Modules/NAP/ModuleHCL.cc @ 268:e14c70d1b171

- Initial add of support code and modules. Not everything is working yet.
author tomwalters
date Fri, 12 Feb 2010 12:31:23 +0000
parents
children c26222c51fb7
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@268 40 parameters_->SetDefault("nap.do_lowpass", "false");
tomwalters@268 41 parameters_->SetDefault("nap.do_log_compression", "false");
tomwalters@268 42 parameters_->SetDefault("nap.lowpass_cutoff", "1200.0");
tomwalters@268 43 parameters_->SetDefault("nap.lowpass_order", "2");
tomwalters@268 44
tomwalters@268 45 do_lowpass_ = parameters_->GetBool("nap.do_lowpass");
tomwalters@268 46 do_log_ = parameters_->GetBool("nap.do_log_compression");
tomwalters@268 47 lowpass_cutoff_ = parameters_->GetFloat("nap.lowpass_cutoff");
tomwalters@268 48 lowpass_order_ = parameters_->GetInt("nap.lowpass_order");
tomwalters@268 49 }
tomwalters@268 50
tomwalters@268 51 ModuleHCL::~ModuleHCL() {
tomwalters@268 52 }
tomwalters@268 53
tomwalters@268 54 bool ModuleHCL::InitializeInternal(const SignalBank &input) {
tomwalters@268 55 time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_);
tomwalters@268 56 channel_count_ = input.channel_count();
tomwalters@268 57 output_.Initialize(input);
tomwalters@268 58 Reset();
tomwalters@268 59 return true;
tomwalters@268 60 }
tomwalters@268 61
tomwalters@268 62 void ModuleHCL::Reset() {
tomwalters@268 63 xn_ = 0.0f;
tomwalters@268 64 yn_ = 0.0f;
tomwalters@268 65 yns_.clear();
tomwalters@268 66 yns_.resize(channel_count_);
tomwalters@268 67 for (int c = 0; c < channel_count_; ++c) {
tomwalters@268 68 yns_[c].resize(lowpass_order_, 0.0f);
tomwalters@268 69 }
tomwalters@268 70 }
tomwalters@268 71
tomwalters@268 72 /* With do_log, the signal is first scaled up so that values <1.0 become
tomwalters@268 73 * negligible. This just rescales the sample values to fill the range of a
tomwalters@268 74 * 16-bit signed integer, then we lose the bottom bit of resolution. If the
tomwalters@268 75 * signal was sampled at 16-bit resolution, there shouldn't be anything to
tomwalters@268 76 * speak of there anyway. If it was sampled using a higher resolution, then
tomwalters@268 77 * some data will be discarded.
tomwalters@268 78 */
tomwalters@268 79 void ModuleHCL::Process(const SignalBank &input) {
tomwalters@268 80 output_.set_start_time(input.start_time());
tomwalters@268 81 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@268 82 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@268 83 if (input[c][i] < 0.0f) {
tomwalters@268 84 output_.set_sample(c, i, 0.0f);
tomwalters@268 85 } else {
tomwalters@268 86 float s = input[c][i];
tomwalters@268 87 if (do_log_) {
tomwalters@268 88 s *= pow(2.0f,15);
tomwalters@268 89 if (s < 1.0f) s = 1.0f;
tomwalters@268 90 s = 20.0f * log10(s);
tomwalters@268 91 }
tomwalters@268 92 output_.set_sample(c, i, s);
tomwalters@268 93 }
tomwalters@268 94 }
tomwalters@268 95 if (do_lowpass_) {
tomwalters@268 96 float b = exp( -1.0f / (input.sample_rate() * time_constant_));
tomwalters@268 97 float gain = 1.0f / (1.0f - b);
tomwalters@268 98 for (int j = 0; j < lowpass_order_; j++) {
tomwalters@268 99 for (int k = 0; k < output_.buffer_length(); ++k) {
tomwalters@268 100 xn_ = output_[c][k];
tomwalters@268 101 yn_ = xn_ + b * yns_[c][j];
tomwalters@268 102 yns_[c][j] = yn_;
tomwalters@268 103 output_.set_sample(c, k, yn_ / gain);
tomwalters@268 104 }
tomwalters@268 105 }
tomwalters@268 106 }
tomwalters@268 107 }
tomwalters@268 108 PushOutput();
tomwalters@268 109 }
tomwalters@268 110 } // namespace aimc