eDSP  0.0.1
A cross-platform DSP library written in C++.
moving_rms_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_rms_filter.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 2/8/2018
21  */
22 #ifndef EDSP_MOVING_RMS_FILTER_HPP
23 #define EDSP_MOVING_RMS_FILTER_HPP
24 
26 #include <cmath>
27 
28 namespace edsp { namespace filter {
29 
48  template <typename T, typename Allocator = std::allocator<T>>
49  class moving_rms {
50  public:
51  using size_type = std::size_t;
52  using value_type = T;
53 
58  explicit moving_rms(size_type N);
59 
64  size_type size() const;
65 
70  void resize(size_type N);
71 
75  void reset();
76 
85  template <typename InputIt, typename OutputIt>
86  void filter(InputIt first, InputIt last, OutputIt d_first);
87 
88  private:
89  edsp::ring_buffer<T, Allocator> window_;
90  T accumulated_{0};
91  };
92 
93  template <typename T, typename Allocator>
95 
96  template <typename T, typename Allocator>
98  return window_.capacity();
99  }
100 
101  template <typename T, typename Allocator>
103  window_.clear();
104  }
105 
106  template <typename T, typename Allocator>
107  template <typename InputIt, typename OutputIt>
108  void moving_rms<T, Allocator>::filter(InputIt first, InputIt last, OutputIt d_first) {
109  if (window_.full()) {
110  for (; first != last; ++d_first, ++first) {
111  const auto sqr = (*first) * (*first);
112  accumulated_ -= window_.front();
113  accumulated_ += sqr;
114  window_.push_back(sqr)* d_first = std::sqrt(accumulated_ / static_cast<T>(window_.size()));
115  }
116  } else {
117  for (; first != last; ++d_first, ++first) {
118  const auto sqr = (*first) * (*first);
119  accumulated_ += sqr;
120  window_.push_back(sqr)* d_first = std::sqrt(accumulated_ / static_cast<T>(window_.size()));
121  }
122  };
123  }
124 
125  template <typename T, typename Allocator>
127  window_.resize(N);
128  }
129 
130 }} // namespace edsp::filter
131 
132 #endif // EDSP_MOVING_RMS_FILTER_HPP
moving_rms(size_type N)
Creates a moving_rms with a window of length N.
Definition: moving_rms_filter.hpp:94
This class implement a cumulative moving rms (rolling rms or running rms) filter. ...
Definition: moving_rms_filter.hpp:49
void resize(size_type N)
Resizes the moving window to the specified number of elements.
Definition: moving_rms_filter.hpp:126
size_type size() const
Returns the size of the moving window.
Definition: moving_rms_filter.hpp:97
void reset()
Reset the moving window to the original state.
Definition: moving_rms_filter.hpp:102
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_rms_filter.hpp:108
Definition: amplifier.hpp:29
T value_type
Definition: moving_rms_filter.hpp:52
std::size_t size_type
Definition: moving_rms_filter.hpp:51