annotate trunk/src/Modules/Strobes/ModuleParabola.cc @ 307:a9e7737cad19

-Added CVs as well as VCs to the gen features script
author tomwalters
date Fri, 26 Feb 2010 07:12:14 +0000
parents ed91095d9240
children 30dde71d0230
rev   line source
tomwalters@277 1 // Copyright 2007-2010, Thomas Walters
tomwalters@277 2 //
tomwalters@277 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@277 4 // http://www.acousticscale.org/AIMC
tomwalters@277 5 //
tomwalters@277 6 // This program is free software: you can redistribute it and/or modify
tomwalters@277 7 // it under the terms of the GNU General Public License as published by
tomwalters@277 8 // the Free Software Foundation, either version 3 of the License, or
tomwalters@277 9 // (at your option) any later version.
tomwalters@277 10 //
tomwalters@277 11 // This program is distributed in the hope that it will be useful,
tomwalters@277 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@277 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@277 14 // GNU General Public License for more details.
tomwalters@277 15 //
tomwalters@277 16 // You should have received a copy of the GNU General Public License
tomwalters@277 17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@277 18
tomwalters@277 19 /*!
tomwalters@277 20 * \file
tomwalters@277 21 * \brief Parabola strobe detection module - Using the 'parabloa' strobe
tomwalters@277 22 * criterion from the AIM-MAT sf2003 module
tomwalters@277 23 *
tomwalters@283 24 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@277 25 * \date created 2007/08/01
tomwalters@296 26 * \version \$Id$
tomwalters@277 27 */
tomwalters@277 28
tomwalters@293 29 #include <limits.h>
tomwalters@287 30 #include <cmath>
tomwalters@277 31
tomwalters@277 32 #include "Modules/Strobes/ModuleParabola.h"
tomwalters@277 33
tomwalters@277 34 namespace aimc {
tomwalters@277 35 ModuleParabola::ModuleParabola(Parameters *params) : Module(params) {
tomwalters@277 36 module_description_ = "sf2003 parabola algorithm";
tomwalters@277 37 module_identifier_ = "parabola";
tomwalters@277 38 module_type_ = "strobes";
tomwalters@277 39 module_version_ = "$Id$";
tomwalters@277 40
tomwalters@277 41 // Get data from parameters
tomwalters@277 42 height_ = parameters_->DefaultFloat("parabola.height", 1.2f);
tomwalters@277 43 parabw_ = parameters_->DefaultFloat("parabola.width_cycles", 1.5f);
tomwalters@277 44 strobe_decay_time_ = parameters_->DefaultFloat("parabla.strobe_decay_time",
tomwalters@277 45 0.02f);
tomwalters@277 46 channel_count_ = 0;
tomwalters@277 47 }
tomwalters@277 48
tomwalters@277 49 bool ModuleParabola::InitializeInternal(const SignalBank &input) {
tomwalters@277 50 output_.Initialize(input);
tomwalters@277 51 channel_count_ = input.channel_count();
tomwalters@277 52 sample_rate_ = input.sample_rate();
tomwalters@277 53
tomwalters@277 54 // Parameters for the parabola
tomwalters@277 55 parab_a_.resize(channel_count_);
tomwalters@277 56 parab_b_.resize(channel_count_);
tomwalters@277 57 parab_wnull_.resize(channel_count_);
tomwalters@277 58 parab_var_samples_.resize(channel_count_);
tomwalters@277 59
tomwalters@277 60 for (int ch = 0; ch < channel_count_; ++ch) {
tomwalters@277 61 parab_wnull_[ch] = parabw_ / input.centre_frequency(ch);
tomwalters@277 62 parab_var_samples_[ch] = floor(parab_wnull_[ch] * sample_rate_);
tomwalters@277 63 parab_a_[ch] = 4.0f * (1.0f - height_)
tomwalters@277 64 / (parab_wnull_[ch] * parab_wnull_[ch]);
tomwalters@277 65 parab_b_[ch] = -parab_wnull_[ch] / 2.0f;
tomwalters@277 66 }
tomwalters@277 67
tomwalters@277 68 // Number of samples over which the threshold should decay
tomwalters@277 69 strobe_decay_samples_ = floor(sample_rate_ * strobe_decay_time_);
tomwalters@277 70
tomwalters@277 71 // Prepare internal buffers
tomwalters@277 72 ResetInternal();
tomwalters@277 73
tomwalters@277 74 return true;
tomwalters@277 75 }
tomwalters@277 76
tomwalters@277 77 void ModuleParabola::ResetInternal() {
tomwalters@305 78 threshold_.clear();
tomwalters@277 79 threshold_.resize(channel_count_, 0.0f);
tomwalters@305 80 last_threshold_.clear();
tomwalters@277 81 last_threshold_.resize(channel_count_, 0.0f);
tomwalters@305 82 samples_since_last_strobe_.clear();
tomwalters@277 83 samples_since_last_strobe_.resize(channel_count_, 0);
tomwalters@277 84
tomwalters@305 85 prev_sample_.clear();
tomwalters@277 86 prev_sample_.resize(channel_count_, 10000.0f);
tomwalters@305 87 curr_sample_.clear();
tomwalters@277 88 curr_sample_.resize(channel_count_, 5000.0f);
tomwalters@305 89 next_sample_.clear();
tomwalters@277 90 next_sample_.resize(channel_count_, 0.0f);
tomwalters@277 91 }
tomwalters@277 92
tomwalters@277 93 void ModuleParabola::Process(const SignalBank &input) {
tomwalters@277 94 float decay_constant;
tomwalters@277 95
tomwalters@277 96 for (int ch = 0; ch < output_.channel_count(); ch++) {
tomwalters@277 97 output_.ResetStrobes(ch);
tomwalters@277 98 }
tomwalters@277 99 output_.set_start_time(input.start_time());
tomwalters@277 100
tomwalters@277 101 // Loop across samples first, then channels
tomwalters@277 102 for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@277 103 // Find strobes in each channel first
tomwalters@277 104 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@277 105 // Shift all the samples by one
tomwalters@277 106 // curr_sample is the sample at time (i - 1)
tomwalters@277 107 prev_sample_[ch] = curr_sample_[ch];
tomwalters@277 108 curr_sample_[ch] = next_sample_[ch];
tomwalters@277 109 next_sample_[ch] = input.sample(ch, i);
tomwalters@277 110
tomwalters@277 111 // Copy input signal to output signal
tomwalters@277 112 output_.set_sample(ch, i, input.sample(ch, i));
tomwalters@277 113
tomwalters@279 114 if (curr_sample_[ch] >= threshold_[ch]) {
tomwalters@279 115 threshold_[ch] = curr_sample_[ch];
tomwalters@279 116 if (prev_sample_[ch] < curr_sample_[ch]
tomwalters@279 117 && next_sample_[ch] < curr_sample_[ch]) {
tomwalters@279 118 // We have a strobe: set threshold and add strobe to the list
tomwalters@279 119 output_.AddStrobe(ch, i - 1);
tomwalters@279 120 last_threshold_[ch] = threshold_[ch];
tomwalters@279 121 parab_var_samples_[ch] =
tomwalters@279 122 floor(input.sample_rate()
tomwalters@279 123 * (parab_wnull_[ch] - (threshold_[ch]
tomwalters@279 124 - 2.0f * parab_a_[ch] *parab_b_[ch])
tomwalters@279 125 / (2.0f * parab_a_[ch])));
tomwalters@279 126 }
tomwalters@277 127 }
tomwalters@279 128 if (output_.strobe_count(ch) > 0) {
tomwalters@277 129 samples_since_last_strobe_[ch] = (i - 1)
tomwalters@277 130 - output_.strobe(ch, output_.strobe_count(ch) - 1);
tomwalters@277 131 } else {
tomwalters@279 132 samples_since_last_strobe_[ch] = UINT_MAX;
tomwalters@277 133 }
tomwalters@277 134
tomwalters@279 135 if (samples_since_last_strobe_[ch] > parab_var_samples_[ch]) {
tomwalters@279 136 decay_constant = last_threshold_[ch] / strobe_decay_samples_;
tomwalters@279 137 if (threshold_[ch] > decay_constant)
tomwalters@279 138 threshold_[ch] -= decay_constant;
tomwalters@279 139 else
tomwalters@279 140 threshold_[ch] = 0.0f;
tomwalters@277 141 } else {
tomwalters@279 142 threshold_[ch] = last_threshold_[ch]
tomwalters@279 143 * (parab_a_[ch] * pow((samples_since_last_strobe_[ch]
tomwalters@279 144 / input.sample_rate() + parab_b_[ch]),
tomwalters@279 145 2.0f) + height_);
tomwalters@277 146 }
tomwalters@277 147 }
tomwalters@277 148 }
tomwalters@277 149
tomwalters@277 150 PushOutput();
tomwalters@277 151 }
tomwalters@277 152
tomwalters@277 153
tomwalters@277 154
tomwalters@277 155 ModuleParabola::~ModuleParabola() {
tomwalters@277 156 }
tomwalters@277 157 } // namespace aimc