annotate trunk/src/Support/SignalBank.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 733a11a65f3d
children
rev   line source
tomwalters@268 1 // Copyright 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 /*! \file
tomwalters@268 19 * \brief
tomwalters@268 20 */
tomwalters@268 21
tomwalters@268 22 /*! \author: Thomas Walters <tom@acousticscale.org>
tomwalters@268 23 * \date 2010/01/23
tomwalters@268 24 * \version \$Id$
tomwalters@268 25 */
tomwalters@268 26
tomwalters@268 27 #include "Support/SignalBank.h"
tomwalters@268 28
tomwalters@268 29 namespace aimc {
tomwalters@268 30 using std::deque;
tomwalters@268 31 using std::vector;
tomwalters@268 32
tomwalters@268 33 SignalBank::SignalBank() {
tomwalters@268 34 sample_rate_ = 0.0f;
tomwalters@268 35 start_time_ = 0;
tomwalters@268 36 channel_count_ = 0;
tomwalters@268 37 buffer_length_ = 0;
tomwalters@268 38 initialized_ = false;
tomwalters@268 39 }
tomwalters@268 40
tomwalters@268 41 SignalBank::~SignalBank() {
tomwalters@268 42 }
tomwalters@268 43
tomwalters@268 44 bool SignalBank::Initialize(int channel_count,
tomwalters@268 45 int signal_length,
tomwalters@268 46 float sample_rate) {
tomwalters@268 47 if (channel_count < 1)
tomwalters@268 48 return false;
tomwalters@268 49 if (signal_length < 1)
tomwalters@268 50 return false;
tomwalters@268 51 if (sample_rate < 0.0f)
tomwalters@268 52 return false;
tomwalters@268 53
tomwalters@268 54 start_time_ = 0;
tomwalters@268 55 sample_rate_ = sample_rate;
tomwalters@268 56 buffer_length_ = signal_length;
tomwalters@268 57 channel_count_ = channel_count;
tomwalters@268 58 signals_.resize(channel_count_);
tomwalters@268 59 strobes_.resize(channel_count_);
tomwalters@268 60 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 61 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 62 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@268 63 }
tomwalters@268 64 initialized_ = true;
tomwalters@268 65 return true;
tomwalters@268 66 }
tomwalters@268 67
tomwalters@268 68 bool SignalBank::Initialize(const SignalBank &input) {
tomwalters@268 69 if (input.channel_count() < 1)
tomwalters@268 70 return false;
tomwalters@268 71 if (input.buffer_length() < 1)
tomwalters@268 72 return false;
tomwalters@268 73 if (input.sample_rate() < 0.0f)
tomwalters@268 74 return false;
tomwalters@268 75
tomwalters@268 76 start_time_ = input.start_time();
tomwalters@268 77 sample_rate_ = input.sample_rate();
tomwalters@268 78 buffer_length_ = input.buffer_length();
tomwalters@268 79 channel_count_ = input.channel_count();
tomwalters@268 80
tomwalters@268 81 signals_.resize(channel_count_);
tomwalters@268 82 strobes_.resize(channel_count_);
tomwalters@268 83
tomwalters@268 84 centre_frequencies_.resize(channel_count_, 0.0f);
tomwalters@268 85 for (int i = 0; i < channel_count_; ++i) {
tomwalters@277 86 centre_frequencies_[i] = input.centre_frequency(i);
tomwalters@268 87 }
tomwalters@268 88
tomwalters@268 89 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 90 signals_[i].resize(buffer_length_, 0.0f);
tomwalters@277 91 strobes_[i].resize(0);
tomwalters@268 92 }
tomwalters@268 93 initialized_ = true;
tomwalters@268 94 return true;
tomwalters@268 95 }
tomwalters@268 96
tom@420 97 void SignalBank::Clear() {
tom@420 98 for (int i = 0; i < channel_count_; ++i) {
tom@420 99 signals_[i].assign(buffer_length_, 0.0f);
tom@420 100 strobes_[i].resize(0);
tom@420 101 }
tom@420 102 }
tom@420 103
tomwalters@268 104 bool SignalBank::Validate() const {
tomwalters@268 105 if (sample_rate_ <= 0.0f)
tomwalters@268 106 return false;
tomwalters@268 107
tomwalters@268 108 if (static_cast<int>(signals_.size()) < 1)
tomwalters@268 109 return false;
tomwalters@268 110
tomwalters@268 111 if (static_cast<int>(signals_.size()) != channel_count_)
tomwalters@268 112 return false;
tomwalters@268 113
tomwalters@268 114 if (static_cast<int>(strobes_.size()) != channel_count_)
tomwalters@268 115 return false;
tomwalters@268 116
tomwalters@268 117 for (int i = 0; i < channel_count_; ++i) {
tomwalters@268 118 if (static_cast<int>(signals_[i].size()) != buffer_length_)
tomwalters@268 119 return false;
tomwalters@268 120 }
tomwalters@268 121 return true;
tomwalters@268 122 }
tomwalters@268 123
tomwalters@277 124 int SignalBank::strobe(int channel, int index) const {
tomwalters@277 125 return strobes_[channel][index];
tomwalters@277 126 }
tomwalters@277 127
tomwalters@277 128 int SignalBank::strobe_count(int channel) const {
tomwalters@277 129 return strobes_[channel].size();
tomwalters@277 130 }
tomwalters@277 131
tomwalters@277 132 void SignalBank::AddStrobe(int channel, int time) {
tomwalters@277 133 strobes_[channel].push_back(time);
tomwalters@277 134 }
tomwalters@277 135
tomwalters@277 136 void SignalBank::ResetStrobes(int channel) {
tomwalters@277 137 strobes_[channel].resize(0);
tomwalters@268 138 }
tomwalters@268 139
tomwalters@268 140 float SignalBank::sample_rate() const {
tomwalters@268 141 return sample_rate_;
tomwalters@268 142 }
tomwalters@268 143
tomwalters@268 144 int SignalBank::buffer_length() const {
tomwalters@268 145 return buffer_length_;
tomwalters@268 146 }
tomwalters@268 147
tomwalters@268 148 int SignalBank::start_time() const {
tomwalters@268 149 return start_time_;
tomwalters@268 150 }
tomwalters@268 151
tomwalters@268 152 void SignalBank::set_start_time(int start_time) {
tomwalters@268 153 start_time_ = start_time;
tomwalters@268 154 }
tomwalters@268 155
tomwalters@277 156 float SignalBank::centre_frequency(int i) const {
tomwalters@268 157 if (i < channel_count_)
tomwalters@268 158 return centre_frequencies_[i];
tomwalters@268 159 else
tomwalters@268 160 return 0.0f;
tomwalters@268 161 }
tomwalters@268 162
tomwalters@268 163 void SignalBank::set_centre_frequency(int i, float cf) {
tomwalters@268 164 if (i < channel_count_)
tomwalters@268 165 centre_frequencies_[i] = cf;
tomwalters@268 166 }
tomwalters@268 167
tomwalters@268 168 bool SignalBank::initialized() const {
tomwalters@268 169 return initialized_;
tomwalters@268 170 }
tomwalters@268 171
tomwalters@268 172 int SignalBank::channel_count() const {
tomwalters@268 173 return channel_count_;
tomwalters@268 174 }
tomwalters@268 175 }