annotate src/Modules/SNR/ModuleNoise.cc @ 87:0f4fcd0e5ebe

- AWS
author tomwalters
date Thu, 12 Aug 2010 14:08:24 +0000
parents bee31e7ebf4b
children
rev   line source
tomwalters@32 1 // Copyright 2010, Thomas Walters
tomwalters@32 2 //
tomwalters@32 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@32 4 // http://www.acousticscale.org/AIMC
tomwalters@32 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@32 9 //
tomwalters@45 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@32 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@32 17
tomwalters@32 18 /*!
tomwalters@32 19 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@32 20 * \date created 2010/02/24
tomwalters@32 21 * \version \$Id$
tomwalters@32 22 */
tomwalters@32 23
tomwalters@32 24 // Use the boost RNGs to generate Gaussian noise
tomwalters@32 25 #include <boost/random.hpp>
tomwalters@32 26 #include <math.h>
tomwalters@32 27
tomwalters@32 28 #include "Modules/SNR/ModuleNoise.h"
tomwalters@32 29
tomwalters@32 30 namespace aimc {
tomwalters@32 31 ModuleNoise::ModuleNoise(Parameters *params) :
tomwalters@32 32 Module(params),
tomwalters@32 33 gaussian_variate_(boost::mt19937(),
tomwalters@32 34 boost::normal_distribution<float>(0.0f, 1.0f)) {
tomwalters@32 35 module_description_ = "Adds noise to a signal";
tomwalters@32 36 module_identifier_ = "noise";
tomwalters@32 37 module_type_ = "snr";
tomwalters@32 38 module_version_ = "$Id$";
tomwalters@32 39
tomwalters@84 40 pink_ = parameters_->DefaultBool("noise.pink", true);
tomwalters@32 41 // Noise level relative to unit-variance Gaussian noise (ie. 0dB will give a
tomwalters@32 42 // noise with an RMS level of 1.0)
tomwalters@32 43 float snr_db = parameters_->DefaultFloat("noise.level_db", 0.0f);
tomwalters@32 44 multiplier_ = pow(10.0f, snr_db / 20.0f);
tomwalters@32 45 }
tomwalters@32 46
tomwalters@32 47 ModuleNoise::~ModuleNoise() {
tomwalters@32 48 }
tomwalters@32 49
tomwalters@32 50 bool ModuleNoise::InitializeInternal(const SignalBank &input) {
tomwalters@32 51 // Copy the parameters of the input signal bank into internal variables, so
tomwalters@32 52 // that they can be checked later.
tomwalters@32 53 sample_rate_ = input.sample_rate();
tomwalters@32 54 buffer_length_ = input.buffer_length();
tomwalters@32 55 channel_count_ = input.channel_count();
tomwalters@32 56
tomwalters@32 57 output_.Initialize(input);
tomwalters@84 58 ResetInternal();
tomwalters@32 59 return true;
tomwalters@32 60 }
tomwalters@32 61
tomwalters@32 62 void ModuleNoise::ResetInternal() {
tomwalters@84 63 s0_ = 0.0f;
tomwalters@84 64 s1_ = 0.0f;
tomwalters@84 65 s2_ = 0.0f;
tomwalters@32 66 }
tomwalters@32 67
tomwalters@32 68 void ModuleNoise::Process(const SignalBank &input) {
tomwalters@32 69 // Check to see if the module has been initialized. If not, processing
tomwalters@32 70 // should not continue.
tomwalters@32 71 if (!initialized_) {
tomwalters@32 72 LOG_ERROR(_T("Module %s not initialized."), module_identifier_.c_str());
tomwalters@32 73 return;
tomwalters@32 74 }
tomwalters@32 75
tomwalters@32 76 // Check that ths input this time is the same as the input passed to
tomwalters@32 77 // Initialize()
tomwalters@32 78 if (buffer_length_ != input.buffer_length()
tomwalters@32 79 || channel_count_ != input.channel_count()) {
tomwalters@32 80 LOG_ERROR(_T("Mismatch between input to Initialize() and input to "
tomwalters@32 81 "Process() in module %s."), module_identifier_.c_str());
tomwalters@32 82 return;
tomwalters@32 83 }
tomwalters@32 84
tomwalters@32 85 for (int c = 0; c < input.channel_count(); ++c) {
tomwalters@32 86 for (int i = 0; i < input.buffer_length(); ++i) {
tomwalters@32 87 float s = input[c][i];
tomwalters@84 88 float n = gaussian_variate_();
tomwalters@84 89 if (pink_) {
tomwalters@84 90 // Pink noise filter coefficients from
tomwalters@84 91 // ccrma.stanford.edu/~jos/sasp/Example_Synthesis_1_F_Noise.html
tomwalters@84 92 // Smith, Julius O. Spectral Audio Signal Processing, October 2008
tomwalters@84 93 // Draft, http://ccrma.stanford.edu/~jos/sasp/, online book,
tomwalters@84 94 // accessed 2010-02-27.
tomwalters@84 95 float f = 0.049922035 * n + s0_;
tomwalters@84 96 s0_ = -0.095993537 * n - (-2.494956002 * f) + s1_;
tomwalters@84 97 s1_ = 0.050612699 * n - (2.017265875 * f) + s2_;
tomwalters@84 98 s2_ = -0.004408786 * n - (-0.522189400 * f);
tomwalters@84 99 n = f;
tomwalters@84 100 }
tomwalters@84 101 s += multiplier_ * n;
tomwalters@32 102 output_.set_sample(c, i, s);
tomwalters@32 103 }
tomwalters@32 104 }
tomwalters@32 105 PushOutput();
tomwalters@32 106 }
tomwalters@32 107 } // namespace aimc
tomwalters@32 108