eDSP  0.0.1
A cross-platform DSP library written in C++.
convolution.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: convolution.hpp
19  * Date: 10/06/18
20  * Author: Mohammed Boujemaoui
21  */
22 
23 #ifndef EDSP_CONVOLUTION_HPP
24 #define EDSP_CONVOLUTION_HPP
25 
26 #include <edsp/spectral/dft.hpp>
27 #include <vector>
28 
29 namespace edsp { inline namespace spectral {
30 
48  template <typename InputIt, typename OutputIt, typename RAllocator = std::allocator<meta::value_type_t<InputIt>>,
49  typename CAllocator = std::allocator<std::complex<meta::value_type_t<OutputIt>>>>
50  inline void conv(InputIt first1, InputIt last1, InputIt first2, OutputIt d_first) {
51  meta::expects(std::distance(first1, last1) > 0, "Not expecting empty input");
52  using value_type = meta::value_type_t<InputIt>;
53  const auto size = std::distance(first1, last1);
54  const auto nfft = 2 * size;
55  fft_impl<value_type> fft_(nfft);
56  fft_impl<value_type> ifft_(nfft);
57 
58  std::vector<value_type, RAllocator> temp_input1(nfft, static_cast<value_type>(0)),
59  temp_input2(nfft, static_cast<value_type>(0)), temp_output(nfft);
60  std::copy(first1, last1, std::begin(temp_input1));
61  std::copy(first2, meta::advance(first2, size), std::begin(temp_input2));
62 
63  std::vector<std::complex<value_type>, CAllocator> fft_data1(make_fft_size(nfft));
64  std::vector<std::complex<value_type>, CAllocator> fft_data2(make_fft_size(nfft));
65 
66  fft_.dft(meta::data(temp_input1), meta::data(fft_data1));
67  fft_.dft(meta::data(temp_input2), meta::data(fft_data2));
68 
69  std::transform(std::cbegin(fft_data1), std::cend(fft_data1), std::cbegin(fft_data2), std::begin(fft_data1),
70  std::multiplies<>());
71 
72  ifft_.idft(meta::data(fft_data1), meta::data(temp_output));
73  ifft_.idft_scale(meta::data(temp_output));
74  std::copy(std::cbegin(temp_output), std::cbegin(temp_output) + size, d_first);
75  }
76 
77 }} // namespace edsp::spectral
78 
79 #endif // EDSP_CONVOLUTION_HPP
void conv(InputIt first1, InputIt last1, InputIt first2, OutputIt d_first)
Computes the convolution between the range [first1, last1) and the range [first2, last2)...
Definition: convolution.hpp:50
constexpr Integer make_fft_size(Integer real_size) noexcept
Computes the expected DFT size for a real-to-complex DFT transform.
Definition: dft.hpp:35
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29