annotate trunk/src/Modules/NAP/ModuleHCL.cc @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents 30dde71d0230
children
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@318 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@318 7 // you may not use this file except in compliance with the License.
tomwalters@318 8 // You may obtain a copy of the License at
tomwalters@268 9 //
tomwalters@318 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@268 11 //
tomwalters@318 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@318 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@318 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@318 15 // See the License for the specific language governing permissions and
tomwalters@318 16 // limitations under the License.
tomwalters@268 17
tomwalters@268 18 /*!
tomwalters@268 19 * \file
tomwalters@268 20 * \brief Halfwave rectification, compression and lowpass filtering.
tomwalters@268 21 *
tomwalters@283 22 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@268 23 * \date created 2007/03/07
tomwalters@296 24 * \version \$Id$
tomwalters@268 25 */
tomwalters@268 26
tomwalters@287 27 #include <cmath>
tomwalters@268 28
tomwalters@268 29 #include "Modules/NAP/ModuleHCL.h"
tomwalters@268 30
tomwalters@268 31 namespace aimc {
tomwalters@268 32 ModuleHCL::ModuleHCL(Parameters *parameters) : Module(parameters) {
tomwalters@268 33 module_identifier_ = "hcl";
tomwalters@268 34 module_type_ = "nap";
tomwalters@268 35 module_description_ = "Halfwave rectification, compression "
tomwalters@268 36 "and lowpass filtering";
tomwalters@296 37 module_version_ = "$Id$";
tomwalters@268 38
tomwalters@290 39 do_lowpass_ = parameters_->DefaultBool("nap.do_lowpass", true);
tomwalters@273 40 do_log_ = parameters_->DefaultBool("nap.do_log_compression", false);
tomwalters@273 41 lowpass_cutoff_ = parameters_->DefaultFloat("nap.lowpass_cutoff", 1200.0);
tomwalters@273 42 lowpass_order_ = parameters_->DefaultInt("nap.lowpass_order", 2);
tomwalters@268 43 }
tomwalters@268 44
tomwalters@268 45 ModuleHCL::~ModuleHCL() {
tomwalters@268 46 }
tomwalters@268 47
tomwalters@268 48 bool ModuleHCL::InitializeInternal(const SignalBank &input) {
tomwalters@268 49 time_constant_ = 1.0f / (2.0f * M_PI * lowpass_cutoff_);
tomwalters@268 50 channel_count_ = input.channel_count();
tomwalters@268 51 output_.Initialize(input);
tomwalters@275 52 ResetInternal();
tomwalters@268 53 return true;
tomwalters@268 54 }
tomwalters@268 55
tomwalters@275 56 void ModuleHCL::ResetInternal() {
tomwalters@268 57 xn_ = 0.0f;
tomwalters@268 58 yn_ = 0.0f;
tomwalters@268 59 yns_.clear();
tomwalters@268 60 yns_.resize(channel_count_);
tomwalters@268 61 for (int c = 0; c < channel_count_; ++c) {
tomwalters@268 62 yns_[c].resize(lowpass_order_, 0.0f);
tomwalters@268 63 }
tomwalters@268 64 }
tomwalters@268 65
tomwalters@268 66 /* With do_log, the signal is first scaled up so that values <1.0 become
tomwalters@268 67 * negligible. This just rescales the sample values to fill the range of a
tomwalters@268 68 * 16-bit signed integer, then we lose the bottom bit of resolution. If the
tomwalters@268 69 * signal was sampled at 16-bit resolution, there shouldn't be anything to
tomwalters@268 70 * speak of there anyway. If it was sampled using a higher resolution, then
tomwalters@268 71 * some data will be discarded.
tomwalters@268 72 */
tomwalters@268 73 void ModuleHCL::Process(const SignalBank &input) {
tomwalters@268 74 output_.set_start_time(input.start_time());
tomwalters@268 75 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@268 76 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@268 77 if (input[c][i] < 0.0f) {
tomwalters@268 78 output_.set_sample(c, i, 0.0f);
tomwalters@268 79 } else {
tomwalters@268 80 float s = input[c][i];
tomwalters@268 81 if (do_log_) {
tomwalters@280 82 s *= pow(2.0f, 15);
tomwalters@268 83 if (s < 1.0f) s = 1.0f;
tomwalters@268 84 s = 20.0f * log10(s);
tomwalters@268 85 }
tomwalters@268 86 output_.set_sample(c, i, s);
tomwalters@268 87 }
tomwalters@268 88 }
tomwalters@268 89 if (do_lowpass_) {
tomwalters@280 90 float b = exp(-1.0f / (input.sample_rate() * time_constant_));
tomwalters@268 91 float gain = 1.0f / (1.0f - b);
tomwalters@268 92 for (int j = 0; j < lowpass_order_; j++) {
tomwalters@268 93 for (int k = 0; k < output_.buffer_length(); ++k) {
tomwalters@268 94 xn_ = output_[c][k];
tomwalters@268 95 yn_ = xn_ + b * yns_[c][j];
tomwalters@268 96 yns_[c][j] = yn_;
tomwalters@268 97 output_.set_sample(c, k, yn_ / gain);
tomwalters@268 98 }
tomwalters@268 99 }
tomwalters@268 100 }
tomwalters@268 101 }
tomwalters@268 102 PushOutput();
tomwalters@268 103 }
tomwalters@268 104 } // namespace aimc