Nsound  0.9.4
FilterMovingAverage.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FilterMovingAverage.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006 to Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------////
28 
29 #include <Nsound/AudioStream.h>
30 #include <Nsound/Buffer.h>
31 #include <Nsound/FilterDelay.h>
33 
34 using namespace Nsound;
35 
36 //-----------------------------------------------------------------------------
38 FilterMovingAverage(uint32 n_samples_to_average)
39  :
40  Filter(1.0),
41  init_sum_(true),
42  n_samples_to_average_(n_samples_to_average),
43  running_sum_(0.0),
44  delay_(NULL)
45 {
47 
48  reset();
49 }
50 
51 //-----------------------------------------------------------------------------
54  :
55  Filter(1.0),
56  init_sum_(copy.init_sum_),
57  n_samples_to_average_(copy.n_samples_to_average_),
58  running_sum_(copy.running_sum_),
59  delay_(new FilterDelay(*copy.delay_))
60 {
61 }
62 
63 //-----------------------------------------------------------------------------
66 {
67  delete delay_;
68 }
69 
72 filter(const AudioStream & x)
73 {
75  return Filter::filter(x);
76 }
77 
78 Buffer
80 filter(const Buffer & x)
81 {
83  return Filter::filter(x);
84 }
85 
86 float64
88 filter(const float64 & x)
89 {
90  if(init_sum_)
91  {
92  init_sum_ = false;
93 
94  for(uint32 i = 0; i < static_cast<uint32>(n_samples_to_average_); ++i)
95  {
96  delay_->filter(x);
97  }
98 
99  running_sum_ += (n_samples_to_average_ - 1.0) * x;
100  }
101 
102  float64 last_nth_x = delay_->filter(x);
103 
104  running_sum_ += x - last_nth_x;
105 
107 }
108 
109 //-----------------------------------------------------------------------------
113 {
114  if(this == & rhs)
115  {
116  return *this;
117  }
118  init_sum_ = rhs.init_sum_;
121 
122  *delay_ = *rhs.delay_;
123 
124  return *this;
125 }
126 
127 void
130 {
131  init_sum_ = true;
132  running_sum_ = 0;
133  delay_->reset();
134 }
unsigned int uint32
Definition: Nsound.h:153
FilterMovingAverage(uint32 n_samples_to_average)
FilterMovingAverage & operator=(const FilterMovingAverage &rhs)
double float64
Definition: Nsound.h:146
AudioStream filter(const AudioStream &x)
Definition: FilterDelay.cc:98
Base class for IIR Filters, defines the interface.
Definition: Filter.h:49
AudioStream filter(const AudioStream &x)
Definition: Filter.cc:53
AudioStream filter(const AudioStream &x)
A Buffer for storing audio samples.
Definition: Buffer.h:60
A class for filtering audio in the frequecy domain.
Definition: FilterDelay.h:47