eDSP  0.0.1
A cross-platform DSP library written in C++.
hilbert.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: hartley.hpp
19  * Author: Mohammed Boujemaoui
20  * Date: 3/8/2018
21  */
22 #ifndef EDSP_HILBERT_HPP
23 #define EDSP_HILBERT_HPP
24 
25 #include <edsp/spectral/internal/fft_impl.hpp>
27 #include <edsp/math/numeric.hpp>
28 #include <vector>
29 
30 namespace edsp { inline namespace spectral {
31 
50  template <typename InputIt, typename OutputIt,
51  typename Allocator = std::allocator<std::complex<meta::value_type_t<InputIt>>>>
52  inline void hilbert(InputIt first, InputIt last, OutputIt d_first) {
53  using value_type = meta::value_type_t<InputIt>;
54  const auto nfft = static_cast<typename fft_impl<value_type>::size_type>(std::distance(first, last));
55 
56  std::vector<std::complex<value_type>, Allocator> input_data(nfft);
57  std::vector<std::complex<value_type>, Allocator> complex_data(nfft);
58  edsp::real2complex(first, last, std::begin(input_data));
59 
60  fft_impl<value_type> fft(nfft);
61  fft.dft(meta::data(input_data), meta::data(complex_data));
62 
63  const auto limit_1 = math::is_even(nfft) ? nfft / 2 : (nfft + 1) / 2;
64  const auto limit_2 = math::is_even(nfft) ? limit_1 + 1 : limit_1;
65  for (auto i = 1; i < limit_1; ++i) {
66  complex_data[i] *= 2;
67  }
68 
69  for (auto i = limit_2; i < nfft; ++i) {
70  complex_data[i] = std::complex<value_type>(0, 0);
71  }
72 
73  fft_impl<value_type> ifft(nfft);
74  ifft.idft(meta::data(complex_data), &(*d_first));
75  ifft.idft_scale(&(*d_first));
76  }
77 
78 }} // namespace edsp::spectral
79 
80 #endif // EDSP_HIRTLEY_HPP
constexpr bool is_even(T x)
Determines if the number is even.
Definition: numeric.hpp:59
constexpr std::complex< T > real2complex(T real, T imag=static_cast< T >(0)) noexcept
Converts a real scalar to an equivalent complex number.
Definition: real2complex.hpp:38
void hilbert(InputIt first, InputIt last, OutputIt d_first)
Computes the Discrete-Time analytic signal using Hilbert transform of the range [first, last) and stores the result in another range, beginning at d_first.
Definition: hilbert.hpp:52
constexpr T distance(T x, T y) noexcept
Computes the distance between x and y.
Definition: numeric.hpp:328
Definition: amplifier.hpp:29