annotate src/Modules/Strobes/ModuleParabola.cc @ 654:a1b82b240328

Rename variables to be consistent with the rest of the library.
author ronw@google.com
date Thu, 27 Jun 2013 15:30:46 +0000
parents c5f5e9569863
children
rev   line source
tomwalters@5 1 // Copyright 2007-2010, Thomas Walters
tomwalters@5 2 //
tomwalters@5 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@5 4 // http://www.acousticscale.org/AIMC
tomwalters@5 5 //
tomwalters@45 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 7 // you may not use this file except in compliance with the License.
tomwalters@45 8 // You may obtain a copy of the License at
tomwalters@5 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@5 11 //
tomwalters@45 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@45 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 15 // See the License for the specific language governing permissions and
tomwalters@45 16 // limitations under the License.
tomwalters@5 17
tomwalters@5 18 /*!
tomwalters@5 19 * \file
tomwalters@5 20 * \brief Parabola strobe detection module - Using the 'parabloa' strobe
tomwalters@5 21 * criterion from the AIM-MAT sf2003 module
tomwalters@5 22 *
tomwalters@11 23 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@5 24 * \date created 2007/08/01
tomwalters@23 25 * \version \$Id$
tomwalters@5 26 */
tomwalters@5 27
tomwalters@21 28 #include <limits.h>
tomwalters@15 29 #include <cmath>
tomwalters@5 30
tomwalters@5 31 #include "Modules/Strobes/ModuleParabola.h"
tomwalters@5 32
tomwalters@5 33 namespace aimc {
tomwalters@5 34 ModuleParabola::ModuleParabola(Parameters *params) : Module(params) {
tomwalters@5 35 module_description_ = "sf2003 parabola algorithm";
tomwalters@5 36 module_identifier_ = "parabola";
tomwalters@5 37 module_type_ = "strobes";
tomwalters@5 38 module_version_ = "$Id$";
tomwalters@5 39
tomwalters@5 40 // Get data from parameters
tomwalters@5 41 height_ = parameters_->DefaultFloat("parabola.height", 1.2f);
tomwalters@5 42 parabw_ = parameters_->DefaultFloat("parabola.width_cycles", 1.5f);
tomwalters@5 43 strobe_decay_time_ = parameters_->DefaultFloat("parabla.strobe_decay_time",
tomwalters@5 44 0.02f);
tomwalters@5 45 channel_count_ = 0;
tomwalters@5 46 }
tomwalters@5 47
tomwalters@5 48 bool ModuleParabola::InitializeInternal(const SignalBank &input) {
tomwalters@5 49 output_.Initialize(input);
tomwalters@5 50 channel_count_ = input.channel_count();
tomwalters@5 51 sample_rate_ = input.sample_rate();
tomwalters@5 52
tomwalters@5 53 // Parameters for the parabola
tomwalters@5 54 parab_a_.resize(channel_count_);
tomwalters@5 55 parab_b_.resize(channel_count_);
tomwalters@5 56 parab_wnull_.resize(channel_count_);
tomwalters@5 57 parab_var_samples_.resize(channel_count_);
tomwalters@5 58
tomwalters@5 59 for (int ch = 0; ch < channel_count_; ++ch) {
tomwalters@5 60 parab_wnull_[ch] = parabw_ / input.centre_frequency(ch);
tomwalters@5 61 parab_var_samples_[ch] = floor(parab_wnull_[ch] * sample_rate_);
tomwalters@5 62 parab_a_[ch] = 4.0f * (1.0f - height_)
tomwalters@5 63 / (parab_wnull_[ch] * parab_wnull_[ch]);
tomwalters@5 64 parab_b_[ch] = -parab_wnull_[ch] / 2.0f;
tomwalters@5 65 }
tomwalters@5 66
tomwalters@5 67 // Number of samples over which the threshold should decay
tomwalters@5 68 strobe_decay_samples_ = floor(sample_rate_ * strobe_decay_time_);
tomwalters@5 69
tomwalters@5 70 // Prepare internal buffers
tomwalters@5 71 ResetInternal();
tomwalters@5 72
tomwalters@5 73 return true;
tomwalters@5 74 }
tomwalters@5 75
tomwalters@5 76 void ModuleParabola::ResetInternal() {
tomwalters@32 77 threshold_.clear();
tomwalters@5 78 threshold_.resize(channel_count_, 0.0f);
tomwalters@32 79 last_threshold_.clear();
tomwalters@5 80 last_threshold_.resize(channel_count_, 0.0f);
tomwalters@32 81 samples_since_last_strobe_.clear();
tomwalters@5 82 samples_since_last_strobe_.resize(channel_count_, 0);
tomwalters@5 83
tomwalters@32 84 prev_sample_.clear();
tomwalters@5 85 prev_sample_.resize(channel_count_, 10000.0f);
tomwalters@32 86 curr_sample_.clear();
tomwalters@5 87 curr_sample_.resize(channel_count_, 5000.0f);
tomwalters@32 88 next_sample_.clear();
tomwalters@5 89 next_sample_.resize(channel_count_, 0.0f);
tomwalters@5 90 }
tomwalters@5 91
tomwalters@5 92 void ModuleParabola::Process(const SignalBank &input) {
tomwalters@5 93 float decay_constant;
tomwalters@5 94
tomwalters@5 95 for (int ch = 0; ch < output_.channel_count(); ch++) {
tomwalters@5 96 output_.ResetStrobes(ch);
tomwalters@5 97 }
tomwalters@5 98 output_.set_start_time(input.start_time());
tomwalters@5 99
tomwalters@5 100 // Loop across samples first, then channels
tomwalters@5 101 for (int i = 0; i < input.buffer_length(); i++) {
tomwalters@5 102 // Find strobes in each channel first
tomwalters@5 103 for (int ch = 0; ch < input.channel_count(); ++ch) {
tomwalters@5 104 // Shift all the samples by one
tomwalters@5 105 // curr_sample is the sample at time (i - 1)
tomwalters@5 106 prev_sample_[ch] = curr_sample_[ch];
tomwalters@5 107 curr_sample_[ch] = next_sample_[ch];
tomwalters@5 108 next_sample_[ch] = input.sample(ch, i);
tomwalters@5 109
tomwalters@5 110 // Copy input signal to output signal
tomwalters@5 111 output_.set_sample(ch, i, input.sample(ch, i));
tomwalters@5 112
tomwalters@7 113 if (curr_sample_[ch] >= threshold_[ch]) {
tomwalters@7 114 threshold_[ch] = curr_sample_[ch];
tomwalters@7 115 if (prev_sample_[ch] < curr_sample_[ch]
tomwalters@7 116 && next_sample_[ch] < curr_sample_[ch]) {
tomwalters@7 117 // We have a strobe: set threshold and add strobe to the list
tomwalters@7 118 output_.AddStrobe(ch, i - 1);
tomwalters@7 119 last_threshold_[ch] = threshold_[ch];
tomwalters@7 120 parab_var_samples_[ch] =
tomwalters@7 121 floor(input.sample_rate()
tomwalters@7 122 * (parab_wnull_[ch] - (threshold_[ch]
tomwalters@7 123 - 2.0f * parab_a_[ch] *parab_b_[ch])
tomwalters@7 124 / (2.0f * parab_a_[ch])));
tomwalters@7 125 }
tomwalters@5 126 }
tomwalters@7 127 if (output_.strobe_count(ch) > 0) {
tomwalters@5 128 samples_since_last_strobe_[ch] = (i - 1)
tomwalters@5 129 - output_.strobe(ch, output_.strobe_count(ch) - 1);
tomwalters@5 130 } else {
tomwalters@7 131 samples_since_last_strobe_[ch] = UINT_MAX;
tomwalters@5 132 }
tomwalters@5 133
tomwalters@7 134 if (samples_since_last_strobe_[ch] > parab_var_samples_[ch]) {
tomwalters@7 135 decay_constant = last_threshold_[ch] / strobe_decay_samples_;
tomwalters@7 136 if (threshold_[ch] > decay_constant)
tomwalters@7 137 threshold_[ch] -= decay_constant;
tomwalters@7 138 else
tomwalters@7 139 threshold_[ch] = 0.0f;
tomwalters@5 140 } else {
tomwalters@7 141 threshold_[ch] = last_threshold_[ch]
tomwalters@7 142 * (parab_a_[ch] * pow((samples_since_last_strobe_[ch]
tomwalters@7 143 / input.sample_rate() + parab_b_[ch]),
tomwalters@7 144 2.0f) + height_);
tomwalters@5 145 }
tomwalters@5 146 }
tomwalters@5 147 }
tomwalters@5 148
tomwalters@5 149 PushOutput();
tomwalters@5 150 }
tomwalters@5 151
tomwalters@5 152
tomwalters@5 153
tomwalters@5 154 ModuleParabola::~ModuleParabola() {
tomwalters@5 155 }
tomwalters@5 156 } // namespace aimc