annotate src/Modules/NAP/ModuleHCL.cc @ 11:bd370910aa05

-Added modules template -Changed header guard style to be more consistent with the Google style guide -Added Doyxfile to generate doxygen documentation -Added structure diagram -Updated swig script to reflect new modules -Changes Gaussians back to using floats and changed tolerance on tests - doubles are unnecessary here
author tomwalters
date Fri, 19 Feb 2010 12:15:56 +0000
parents fcbf85ce59fb
children b4cafba48e9d
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@11 23 * \author Thomas Walters <tom@acousticscale.org>
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@1 40 do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", false);
tomwalters@1 41 do_log_ = parameters_->DefaultBool("nap.do_log_compression", false);
tomwalters@1 42 lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0);
tomwalters@1 43 lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2);
tomwalters@0 44 }
tomwalters@0 45
tomwalters@0 46 ModuleHCL::~ModuleHCL() {
tomwalters@0 47 }
tomwalters@0 48
tomwalters@0 49 bool ModuleHCL::InitializeInternal(const SignalBank &input) {
tomwalters@0 50 time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_);
tomwalters@0 51 channel_count_ = input.channel_count();
tomwalters@0 52 output_.Initialize(input);
tomwalters@3 53 ResetInternal();
tomwalters@0 54 return true;
tomwalters@0 55 }
tomwalters@0 56
tomwalters@3 57 void ModuleHCL::ResetInternal() {
tomwalters@0 58 xn_ = 0.0f;
tomwalters@0 59 yn_ = 0.0f;
tomwalters@0 60 yns_.clear();
tomwalters@0 61 yns_.resize(channel_count_);
tomwalters@0 62 for (int c = 0; c < channel_count_; ++c) {
tomwalters@0 63 yns_[c].resize(lowpass_order_, 0.0f);
tomwalters@0 64 }
tomwalters@0 65 }
tomwalters@0 66
tomwalters@0 67 /* With do_log, the signal is first scaled up so that values <1.0 become
tomwalters@0 68 * negligible. This just rescales the sample values to fill the range of a
tomwalters@0 69 * 16-bit signed integer, then we lose the bottom bit of resolution. If the
tomwalters@0 70 * signal was sampled at 16-bit resolution, there shouldn't be anything to
tomwalters@0 71 * speak of there anyway. If it was sampled using a higher resolution, then
tomwalters@0 72 * some data will be discarded.
tomwalters@0 73 */
tomwalters@0 74 void ModuleHCL::Process(const SignalBank &input) {
tomwalters@0 75 output_.set_start_time(input.start_time());
tomwalters@0 76 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@0 77 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@0 78 if (input[c][i] < 0.0f) {
tomwalters@0 79 output_.set_sample(c, i, 0.0f);
tomwalters@0 80 } else {
tomwalters@0 81 float s = input[c][i];
tomwalters@0 82 if (do_log_) {
tomwalters@8 83 s *= pow(2.0f, 15);
tomwalters@0 84 if (s < 1.0f) s = 1.0f;
tomwalters@0 85 s = 20.0f * log10(s);
tomwalters@0 86 }
tomwalters@0 87 output_.set_sample(c, i, s);
tomwalters@0 88 }
tomwalters@0 89 }
tomwalters@0 90 if (do_lowpass_) {
tomwalters@8 91 float b = exp(-1.0f / (input.sample_rate() * time_constant_));
tomwalters@0 92 float gain = 1.0f / (1.0f - b);
tomwalters@0 93 for (int j = 0; j < lowpass_order_; j++) {
tomwalters@0 94 for (int k = 0; k < output_.buffer_length(); ++k) {
tomwalters@0 95 xn_ = output_[c][k];
tomwalters@0 96 yn_ = xn_ + b * yns_[c][j];
tomwalters@0 97 yns_[c][j] = yn_;
tomwalters@0 98 output_.set_sample(c, k, yn_ / gain);
tomwalters@0 99 }
tomwalters@0 100 }
tomwalters@0 101 }
tomwalters@0 102 }
tomwalters@0 103 PushOutput();
tomwalters@0 104 }
tomwalters@0 105 } // namespace aimc