eDSP  0.0.1
A cross-platform DSP library written in C++.
moving_median_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_median_filter.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 14/6/2018
21  */
22 #ifndef EDSP_FILTER_MOVING_MEDIAN_FILTER_H
23 #define EDSP_FILTER_MOVING_MEDIAN_FILTER_H
24 
27 
28 namespace edsp { namespace filter {
29 
42  template <typename T, typename Allocator = std::allocator<T>>
43  class moving_median {
44  public:
45  using size_type = std::size_t;
46  using value_type = T;
47 
52  explicit moving_median(size_type N);
53 
58  size_type size() const;
59 
64  void resize(size_type N);
65 
69  void reset();
70 
79  template <typename InputIt, typename OutputIt>
80  void filter(InputIt first, InputIt last, OutputIt d_first);
81 
82  private:
83  edsp::ring_buffer<T, Allocator> window_;
84  T accumulated_{0};
85  };
86 
87  template <typename T, typename Allocator>
89 
90  template <typename T, typename Allocator>
92  return window_.capacity();
93  }
94 
95  template <typename T, typename Allocator>
97  window_.clear();
98  }
99 
100  template <typename T, typename Allocator>
101  template <typename InputIt, typename OutputIt>
102  void moving_median<T, Allocator>::filter(InputIt first, InputIt last, OutputIt d_first) {
103  for (; first != last; ++d_first, ++first) {
104  window_.push_back(*first);
105  *d_first = statistics::median(window_.cbegin(), window_.cend());
106  }
107  }
108 
109  template <typename T, typename Allocator>
111  window_.resize(N);
112  }
113 
114 }} // namespace edsp::filter
115 
116 #endif // EDSP_FILTER_MOVING_MEDIAN_FILTER_H
moving_median(size_type N)
Creates a moving_median with a window of length N.
Definition: moving_median_filter.hpp:88
T value_type
Definition: moving_median_filter.hpp:46
void filter(InputIt first, InputIt last, OutputIt d_first)
Applies a moving median filter to the elements in the range [first, last) and stores the result in an...
Definition: moving_median_filter.hpp:102
void reset()
Reset the moving window to the original state.
Definition: moving_median_filter.hpp:96
constexpr meta::value_type_t< ForwardIt > median(ForwardIt first, ForwardIt last)
Computes the median of the range [first, last)
Definition: median.hpp:44
void resize(size_type N)
Resizes the moving window to the specified number of elements.
Definition: moving_median_filter.hpp:110
This class implement a cumulative moving median (rolling median or running median) filter...
Definition: moving_median_filter.hpp:43
std::size_t size_type
Definition: moving_median_filter.hpp:45
size_type size() const
Returns the size of the moving window.
Definition: moving_median_filter.hpp:91
Definition: amplifier.hpp:29