00001 00002 // 00003 // $Id: FilterMovingAverage.cc 479 2010-05-16 23:32:34Z weegreenblobbie $ 00004 // 00005 // Copyright (c) 2006 to Present Nick Hilton 00006 // 00007 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.') 00008 // 00010 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 // This program is distributed in the hope that it will be useful, 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 // GNU Library General Public License for more details. 00022 // 00023 // You should have received a copy of the GNU General Public License 00024 // along with this program; if not, write to the Free Software 00025 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00026 // 00028 00029 #include <Nsound/AudioStream.h> 00030 #include <Nsound/Buffer.h> 00031 #include <Nsound/FilterDelay.h> 00032 #include <Nsound/FilterMovingAverage.h> 00033 00034 using namespace Nsound; 00035 00037 FilterMovingAverage:: 00038 FilterMovingAverage(uint32 n_samples_to_average) 00039 : 00040 Filter(1.0), 00041 n_samples_to_average_(n_samples_to_average), 00042 running_sum_(0.0), 00043 delay_(NULL) 00044 { 00045 delay_ = new FilterDelay(1.0, n_samples_to_average_); 00046 00047 reset(); 00048 } 00049 00051 FilterMovingAverage:: 00052 FilterMovingAverage(const FilterMovingAverage & copy) 00053 : 00054 Filter(1.0), 00055 n_samples_to_average_(copy.n_samples_to_average_), 00056 running_sum_(copy.running_sum_), 00057 delay_(new FilterDelay(*copy.delay_)) 00058 { 00059 } 00060 00062 FilterMovingAverage:: 00063 ~FilterMovingAverage() 00064 { 00065 delete delay_; 00066 } 00067 00069 AudioStream 00070 FilterMovingAverage:: 00071 filter(const AudioStream & x) 00072 { 00073 FilterMovingAverage::reset(); 00074 return Filter::filter(x); 00075 } 00076 00078 Buffer 00079 FilterMovingAverage:: 00080 filter(const Buffer & x) 00081 { 00082 FilterMovingAverage::reset(); 00083 return Filter::filter(x); 00084 } 00085 00087 float64 00088 FilterMovingAverage:: 00089 filter(const float64 & x) 00090 { 00091 float64 last_nth_x = delay_->filter(x); 00092 00093 running_sum_ += x - last_nth_x; 00094 00095 return running_sum_ / n_samples_to_average_; 00096 } 00097 00099 FilterMovingAverage & 00100 FilterMovingAverage:: 00101 operator=(const FilterMovingAverage & rhs) 00102 { 00103 if(this == & rhs) 00104 { 00105 return *this; 00106 } 00107 00108 n_samples_to_average_ = rhs.n_samples_to_average_; 00109 running_sum_ = rhs.running_sum_; 00110 00111 *delay_ = *rhs.delay_; 00112 00113 return *this; 00114 } 00115 00117 void 00118 FilterMovingAverage:: 00119 reset() 00120 { 00121 running_sum_ = 0.0; 00122 delay_->reset(); 00123 }
1.6.3