annotate src/Modules/NAP/ModuleHCL.cc @ 0:582cbe817f2c

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