eDSP  0.0.1
A cross-platform DSP library written in C++.
biquad_cascade.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: biquad_cascade.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 30/8/2018
21  */
22 #ifndef EDSP_BIQUAD_CASCADE_HPP
23 #define EDSP_BIQUAD_CASCADE_HPP
24 
25 #include <edsp/meta/expects.hpp>
26 #include <edsp/meta/ensure.hpp>
27 #include <edsp/filter/biquad.hpp>
28 #include <array>
29 #include <edsp/meta/iterator.hpp>
30 
31 namespace edsp { namespace filter {
32 
42  template <typename T, std::size_t N>
44  public:
47  using const_reference = const value_type&;
48  using iterator = value_type*;
49  using const_iterator = const value_type*;
50  using size_type = std::size_t;
51 
55  constexpr biquad_cascade() = default;
56 
60  ~biquad_cascade() = default;
61 
66  constexpr size_type size() const noexcept;
67 
73  constexpr size_type max_size() const noexcept;
74 
82  constexpr size_type capacity() const noexcept;
83 
90  constexpr void clear();
91 
95  constexpr void reset();
96 
102  constexpr const_reference operator[](size_type index) const noexcept;
103 
109  constexpr reference operator[](size_type index) noexcept;
110 
115  constexpr iterator begin() noexcept;
116 
121  constexpr iterator end() noexcept;
122 
127  constexpr const_iterator cbegin() const noexcept;
128 
133  constexpr const_iterator begin() const noexcept;
134 
139  constexpr const_iterator cend() const noexcept;
140 
145  constexpr const_iterator end() const noexcept;
146 
156  template <typename InputIt, typename OutputIt>
157  constexpr void filter(InputIt first, InputIt last, OutputIt d_first);
158 
164  constexpr T tick(T value) noexcept;
165 
170  constexpr void push_back(const biquad<T>& biquad);
171 
176  template <typename... Arg>
177  constexpr void emplace_back(Arg... arg);
178 
179  private:
180  std::size_t num_stage_{0};
181  std::array<biquad<T>, N> cascade_{};
182  };
183 
184  template <typename T, size_t N>
185  constexpr typename biquad_cascade<T, N>::size_type biquad_cascade<T, N>::size() const noexcept {
186  return num_stage_;
187  }
188 
189  template <typename T, size_t N>
191  return N;
192  }
193 
194  template <typename T, size_t N>
195  template <typename... Arg>
196  constexpr void biquad_cascade<T, N>::emplace_back(Arg... arg) {
197  meta::ensure(num_stage_ < N, "No space available");
198  cascade_[num_stage_] = biquad<T>(arg...);
199  num_stage_++;
200  }
201 
202  template <typename T, size_t N>
204  meta::ensure(num_stage_ < N, "No space available");
205  cascade_[num_stage_] = biquad;
206  num_stage_++;
207  }
208 
209  template <typename T, size_t N>
210  constexpr T biquad_cascade<T, N>::tick(T value) noexcept {
211  for (auto i = 0ul; i < num_stage_; ++i) {
212  value = cascade_[i].tick(value);
213  }
214  return value;
215  }
216 
217  template <typename T, size_t N>
218  template <typename InputIt, typename OutputIt>
219  constexpr void biquad_cascade<T, N>::filter(InputIt first, InputIt last, OutputIt d_first) {
220  for (; first != last; ++first, ++d_first) {
221  *d_first = tick(*first);
222  }
223  }
224 
225  template <typename T, size_t N>
227  return std::cbegin(cascade_) + size();
228  }
229 
230  template <typename T, size_t N>
232  return std::cbegin(cascade_) + size();
233  }
234 
235  template <typename T, size_t N>
237  return std::cbegin(cascade_);
238  }
239 
240  template <typename T, size_t N>
242  return std::cbegin(cascade_);
243  }
244 
245  template <typename T, size_t N>
247  return std::begin(cascade_) + size();
248  }
249 
250  template <typename T, size_t N>
252  return std::begin(cascade_);
253  }
254 
255  template <typename T, size_t N>
258  return cascade_[index];
259  }
260 
261  template <typename T, size_t N>
263  operator[](biquad_cascade::size_type index) const noexcept {
264  return cascade_[index];
265  }
266 
267  template <typename T, size_t N>
268  constexpr void biquad_cascade<T, N>::reset() {
269  for (auto i = 0ul; i < num_stage_; ++i) {
270  cascade_[i].reset();
271  }
272  }
273 
274  template <typename T, size_t N>
275  constexpr void biquad_cascade<T, N>::clear() {
276  num_stage_ = 0;
277  }
278 
279  template <typename T, size_t N>
281  return N;
282  }
283 }} // namespace edsp::filter
284 #endif // EDSP_BIQUAD_CASCADE_HPP
constexpr const_iterator cbegin() const noexcept
Returns a constant iterator to the first element of the container.
Definition: biquad_cascade.hpp:241
constexpr iterator begin() noexcept
Returns an iterator to the first element of the container.
Definition: biquad_cascade.hpp:251
constexpr T tick(T value) noexcept
Computes the output of filtering one digital time-step.
Definition: biquad_cascade.hpp:210
constexpr biquad_cascade()=default
Creates an empty biquad_cascade.
constexpr const_reference operator[](size_type index) const noexcept
Returns a constant reference to the Biquad at specified location pos. No bounds checking is performed...
Definition: biquad_cascade.hpp:263
constexpr void clear()
Clear the contents of the BiquadCascade.
Definition: biquad_cascade.hpp:275
constexpr size_type max_size() const noexcept
Returns the maximum number of Biquads the BiquadCascade is able to hold.
Definition: biquad_cascade.hpp:190
constexpr iterator end() noexcept
Returns an iterator to the last element of the container.
Definition: biquad_cascade.hpp:246
This Biquad class implements a second-order recursive linear filter, containing two poles and two zer...
Definition: biquad.hpp:71
The BiquadCascade class implements a cascade of Biquad filters.
Definition: biquad_cascade.hpp:43
constexpr size_type capacity() const noexcept
Returns the capacity the BiquadCascade.
Definition: biquad_cascade.hpp:280
constexpr void emplace_back(Arg... arg)
Construct a Biquad in-place at the end.
Definition: biquad_cascade.hpp:196
constexpr void push_back(const biquad< T > &biquad)
Appends the given Biquad at the end.
Definition: biquad_cascade.hpp:203
constexpr const_iterator cend() const noexcept
Returns a constant iterator to the last element of the container.
Definition: biquad_cascade.hpp:231
~biquad_cascade()=default
Default destructor.
Definition: amplifier.hpp:29
std::size_t size_type
Definition: biquad_cascade.hpp:50
constexpr size_type size() const noexcept
Returns the number of Biquads.
Definition: biquad_cascade.hpp:185
constexpr void reset()
Reset all the internal Biquads to the original state.
Definition: biquad_cascade.hpp:268
constexpr void filter(InputIt first, InputIt last, OutputIt d_first)
Filters the signal in the range [first, last) and stores the result in another range, beginning at d_first.
Definition: biquad_cascade.hpp:219