eDSP  0.0.1
A cross-platform DSP library written in C++.
moving_average_filter.hpp
Go to the documentation of this file.
1 /*
2  * eDSP, A cross-platform Digital Signal Processing library written in modern C++.
3  * Copyright (C) 2018 Mohammed Boujemaoui Boulaghmoudi, All rights reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation, either version 3 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along width
16  * this program. If not, see <http://www.gnu.org/licenses/>
17  *
18  * File: moving_average_filter.hpp
19  * Date: 14/6/2018
20  * Authors: Mohammed Boujemaoui
21  */
22 #ifndef EDSP_FILTER_MOVING_AVERAGE_FILTER_H
23 #define EDSP_FILTER_MOVING_AVERAGE_FILTER_H
24 
26 
27 namespace edsp { namespace filter {
28 
47  template <typename T, typename Allocator = std::allocator<T>>
49  public:
50  using size_type = std::size_t;
51  using value_type = T;
52 
57  explicit moving_average(size_type N);
58 
63  size_type size() const;
64 
69  void resize(size_type N);
70 
74  void reset();
75 
84  template <typename InputIt, typename OutputIt>
85  void filter(InputIt first, InputIt last, OutputIt d_first);
86 
92 
93  private:
94  edsp::ring_buffer<T, Allocator> window_;
95  T accumulated_{0};
96  };
97 
98  template <typename T, typename Allocator>
100 
101  template <typename T, typename Allocator>
103  return window_.capacity();
104  }
105 
106  template <typename T, typename Allocator>
108  window_.clear();
109  }
110 
111  template <typename T, typename Allocator>
112  template <typename InputIt, typename OutputIt>
113  void moving_average<T, Allocator>::filter(InputIt first, InputIt last, OutputIt d_first) {
114  if (window_.full()) {
115  for (; first != last; ++d_first, ++first) {
116  accumulated_ -= window_.front();
117  accumulated_ += *first;
118  window_.push_back(*first);
119  *d_first = accumulated_ / static_cast<T>(window_.size());
120  }
121  } else {
122  for (; first != last; ++d_first, ++first) {
123  accumulated_ += *first;
124  window_.push_back(*first);
125  *d_first = accumulated_ / static_cast<T>(window_.size());
126  }
127  };
128  }
129 
130  template <typename T, typename Allocator>
132  window_.resize(N);
133  }
134 
135  template<typename T, typename Allocator>
137  if (window_.full()) {
138  accumulated_ -= window_.front();
139  accumulated_ += tick;
140  window_.push_back(tick);
141  } else {
142  accumulated_ += tick;
143  window_.push_back(tick);
144  };
145  return accumulated_ / static_cast<T>(window_.size());
146  }
147 
148  }} // namespace edsp::filter
149 
150 #endif // EDSP_FILTER_MOVING_AVERAGE_FILTER_H
void resize(size_type N)
Resizes the moving window to the specified number of elements.
Definition: moving_average_filter.hpp:131
T value_type
Definition: moving_average_filter.hpp:51
value_type tick(value_type tick)
Applies a moving average filter to the single element.
Definition: moving_average_filter.hpp:136
void reset()
Reset the moving window to the original state.
Definition: moving_average_filter.hpp:107
void filter(InputIt first, InputIt last, OutputIt d_first)
Applies a moving average filter to the elements in the range [first, last) and stores the result in a...
Definition: moving_average_filter.hpp:113
std::size_t size_type
Definition: moving_average_filter.hpp:50
logger & reset(logger &stream)
Resets the logger to default configuration.
Definition: logger.hpp:416
moving_average(size_type N)
Creates a moving_average with a window of length N.
Definition: moving_average_filter.hpp:99
size_type size() const
Returns the size of the moving window.
Definition: moving_average_filter.hpp:102
This class implement a cumulative moving average (rolling average or running average) filter...
Definition: moving_average_filter.hpp:48
Definition: amplifier.hpp:29