From cc48f3585a8c11ed4f8a1f9afbb0fab860fe31e6 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sun, 20 Apr 2014 15:25:25 +0900 Subject: [PATCH] add c++14 constexpr spectrum and wave genarator algorithms --- sprout/iterator/sawtooth_iterator.hpp | 6 +-- sprout/iterator/sinusoid_iterator.hpp | 13 +++--- sprout/iterator/square_iterator.hpp | 6 +-- sprout/iterator/triangle_iterator.hpp | 6 +-- sprout/numeric/dft/amplitude_spectrum.hpp | 1 + sprout/numeric/dft/cxx14.hpp | 15 +++++++ .../numeric/dft/cxx14/amplitude_spectrum.hpp | 33 +++++++++++++++ sprout/numeric/dft/cxx14/phase_spectrum.hpp | 33 +++++++++++++++ sprout/numeric/dft/cxx14/sawtooth.hpp | 38 ++++++++++++++++++ sprout/numeric/dft/cxx14/sinusoid.hpp | 40 +++++++++++++++++++ sprout/numeric/dft/cxx14/spectrum.hpp | 31 ++++++++++++++ sprout/numeric/dft/cxx14/square.hpp | 38 ++++++++++++++++++ sprout/numeric/dft/cxx14/triangle.hpp | 39 ++++++++++++++++++ sprout/numeric/dft/cxx14/wave.hpp | 17 ++++++++ sprout/numeric/dft/detail/sawtooth.hpp | 30 ++++++++++++++ sprout/numeric/dft/detail/sinusoid.hpp | 36 +++++++++++++++++ sprout/numeric/dft/detail/square.hpp | 29 ++++++++++++++ sprout/numeric/dft/detail/triangle.hpp | 34 ++++++++++++++++ sprout/numeric/dft/fit/spectrum.hpp | 3 +- .../numeric/dft/fixed/amplitude_spectrum.hpp | 33 ++++++++++----- sprout/numeric/dft/fixed/phase_spectrum.hpp | 33 ++++++++++----- sprout/numeric/dft/fixed/sawtooth.hpp | 12 +----- sprout/numeric/dft/fixed/sinusoid.hpp | 8 ++-- sprout/numeric/dft/fixed/spectrum.hpp | 20 ++++++++-- sprout/numeric/dft/fixed/square.hpp | 11 +---- sprout/numeric/dft/fixed/triangle.hpp | 15 +------ sprout/numeric/dft/modifying.hpp | 1 + sprout/numeric/dft/phase_spectrum.hpp | 1 + sprout/numeric/dft/spectrum.hpp | 1 + 29 files changed, 506 insertions(+), 77 deletions(-) create mode 100644 sprout/numeric/dft/cxx14.hpp create mode 100644 sprout/numeric/dft/cxx14/amplitude_spectrum.hpp create mode 100644 sprout/numeric/dft/cxx14/phase_spectrum.hpp create mode 100644 sprout/numeric/dft/cxx14/sawtooth.hpp create mode 100644 sprout/numeric/dft/cxx14/sinusoid.hpp create mode 100644 sprout/numeric/dft/cxx14/spectrum.hpp create mode 100644 sprout/numeric/dft/cxx14/square.hpp create mode 100644 sprout/numeric/dft/cxx14/triangle.hpp create mode 100644 sprout/numeric/dft/cxx14/wave.hpp create mode 100644 sprout/numeric/dft/detail/sawtooth.hpp create mode 100644 sprout/numeric/dft/detail/sinusoid.hpp create mode 100644 sprout/numeric/dft/detail/square.hpp create mode 100644 sprout/numeric/dft/detail/triangle.hpp diff --git a/sprout/iterator/sawtooth_iterator.hpp b/sprout/iterator/sawtooth_iterator.hpp index 00306819..5fbd44a9 100644 --- a/sprout/iterator/sawtooth_iterator.hpp +++ b/sprout/iterator/sawtooth_iterator.hpp @@ -16,8 +16,8 @@ #include #include #include -#include #include +#include namespace sprout { // @@ -97,7 +97,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator*() const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_) + phase_) + : sprout::detail::sawtooth_value(frequency_, amplitude_, phase_, index_) ; } SPROUT_CONSTEXPR pointer operator->() const { @@ -139,7 +139,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_ + n) + phase_) + : sprout::detail::sawtooth_value(frequency_, amplitude_, phase_, index_ + n) ; } SPROUT_CONSTEXPR sawtooth_iterator next() const { diff --git a/sprout/iterator/sinusoid_iterator.hpp b/sprout/iterator/sinusoid_iterator.hpp index 08805b0b..eca429ff 100644 --- a/sprout/iterator/sinusoid_iterator.hpp +++ b/sprout/iterator/sinusoid_iterator.hpp @@ -16,9 +16,8 @@ #include #include #include -#include -#include #include +#include namespace sprout { // @@ -60,7 +59,7 @@ namespace sprout { , frequency_(1) , amplitude_(1) , phase_(0) - , d_(sprout::math::two_pi()) + , d_(sprout::detail::sinusoid_value_d(frequency_)) {} sinusoid_iterator(sinusoid_iterator const&) = default; explicit SPROUT_CONSTEXPR sinusoid_iterator( @@ -73,7 +72,7 @@ namespace sprout { , frequency_(frequency) , amplitude_(amplitude) , phase_(phase) - , d_(sprout::math::two_pi() * frequency) + , d_(sprout::detail::sinusoid_value_d(frequency_)) {} template SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& it) @@ -102,9 +101,8 @@ namespace sprout { return phase_; } SPROUT_CONSTEXPR reference operator*() const { - using sprout::sin; return amplitude_ == 0 ? 0 - : amplitude_ * sin(d_ * value_type(index_) + phase_) + : sprout::detail::sinusoid_value(amplitude_, phase_, d_, index_) ; } SPROUT_CONSTEXPR pointer operator->() const { @@ -145,9 +143,8 @@ namespace sprout { return *this; } SPROUT_CONSTEXPR reference operator[](difference_type n) const { - using sprout::sin; return amplitude_ == 0 ? 0 - : amplitude_ * sin(d_ * value_type(index_ + n) + phase_) + : sprout::detail::sinusoid_value(amplitude_, phase_, d_, index_ + n) ; } SPROUT_CONSTEXPR sinusoid_iterator next() const { diff --git a/sprout/iterator/square_iterator.hpp b/sprout/iterator/square_iterator.hpp index fd2b3cd9..83ea85c4 100644 --- a/sprout/iterator/square_iterator.hpp +++ b/sprout/iterator/square_iterator.hpp @@ -16,8 +16,8 @@ #include #include #include -#include #include +#include namespace sprout { // @@ -106,7 +106,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator*() const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_) + phase_, duty_) + : sprout::detail::square_value(frequency_, amplitude_, phase_, duty_, index_) ; } SPROUT_CONSTEXPR pointer operator->() const { @@ -148,7 +148,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_ + n) + phase_, duty_) + : sprout::detail::square_value(frequency_, amplitude_, phase_, duty_, index_ + n) ; } SPROUT_CONSTEXPR square_iterator next() const { diff --git a/sprout/iterator/triangle_iterator.hpp b/sprout/iterator/triangle_iterator.hpp index 26161f1d..43a0bf54 100644 --- a/sprout/iterator/triangle_iterator.hpp +++ b/sprout/iterator/triangle_iterator.hpp @@ -16,8 +16,8 @@ #include #include #include -#include #include +#include namespace sprout { // @@ -97,7 +97,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator*() const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_) + phase_) + : sprout::detail::triangle_value(frequency_, amplitude_, phase_, index_) ; } SPROUT_CONSTEXPR pointer operator->() const { @@ -139,7 +139,7 @@ namespace sprout { } SPROUT_CONSTEXPR reference operator[](difference_type n) const { return amplitude_ == 0 ? 0 - : amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_ + n) + phase_) + : sprout::detail::triangle_value(frequency_, amplitude_, phase_, index_ + n) ; } SPROUT_CONSTEXPR triangle_iterator next() const { diff --git a/sprout/numeric/dft/amplitude_spectrum.hpp b/sprout/numeric/dft/amplitude_spectrum.hpp index ed5b571c..e6915f24 100644 --- a/sprout/numeric/dft/amplitude_spectrum.hpp +++ b/sprout/numeric/dft/amplitude_spectrum.hpp @@ -11,5 +11,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_NUMERIC_DFT_AMPLITUDE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/cxx14.hpp b/sprout/numeric/dft/cxx14.hpp new file mode 100644 index 00000000..74e56802 --- /dev/null +++ b/sprout/numeric/dft/cxx14.hpp @@ -0,0 +1,15 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_HPP +#define SPROUT_NUMERIC_DFT_CXX14_HPP + +#include +#include +#include + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_HPP diff --git a/sprout/numeric/dft/cxx14/amplitude_spectrum.hpp b/sprout/numeric/dft/cxx14/amplitude_spectrum.hpp new file mode 100644 index 00000000..bee80385 --- /dev/null +++ b/sprout/numeric/dft/cxx14/amplitude_spectrum.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_AMPLITUDE_SPECTRUM_HPP +#define SPROUT_NUMERIC_DFT_CXX14_AMPLITUDE_SPECTRUM_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // amplitude_spectrum + // + template< + typename InputIterator, typename OutputIterator, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR OutputIterator + amplitude_spectrum(InputIterator first, InputIterator last, OutputIterator result) { + while (first != last) { + *result++ = sprout::amplitude_spectrum_value(*first++); + } + return result; + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_AMPLITUDE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/cxx14/phase_spectrum.hpp b/sprout/numeric/dft/cxx14/phase_spectrum.hpp new file mode 100644 index 00000000..d16e2e14 --- /dev/null +++ b/sprout/numeric/dft/cxx14/phase_spectrum.hpp @@ -0,0 +1,33 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_PHASE_SPECTRUM_HPP +#define SPROUT_NUMERIC_DFT_CXX14_PHASE_SPECTRUM_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // phase_spectrum + // + template< + typename InputIterator, typename OutputIterator, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR OutputIterator + phase_spectrum(InputIterator first, InputIterator last, OutputIterator result) { + while (first != last) { + *result++ = sprout::phase_spectrum_value(*first++); + } + return result; + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_PHASE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/cxx14/sawtooth.hpp b/sprout/numeric/dft/cxx14/sawtooth.hpp new file mode 100644 index 00000000..64c14805 --- /dev/null +++ b/sprout/numeric/dft/cxx14/sawtooth.hpp @@ -0,0 +1,38 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP +#define SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // sawtooth + // + template + inline SPROUT_CXX14_CONSTEXPR void + sawtooth( + ForwardIterator first, ForwardIterator last, + typename std::iterator_traits::value_type const& frequency = 1, + typename std::iterator_traits::value_type const& amplitude = 1, + typename std::iterator_traits::value_type const& phase = 0 + ) + { + typedef typename std::iterator_traits::difference_type difference_type; + difference_type i = 0; + while (first != last) { + *first++ = sprout::detail::sawtooth_value(frequency, amplitude, phase, i); + ++i; + } + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP diff --git a/sprout/numeric/dft/cxx14/sinusoid.hpp b/sprout/numeric/dft/cxx14/sinusoid.hpp new file mode 100644 index 00000000..90b2bf3c --- /dev/null +++ b/sprout/numeric/dft/cxx14/sinusoid.hpp @@ -0,0 +1,40 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_SINUSOID_HPP +#define SPROUT_NUMERIC_DFT_CXX14_SINUSOID_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // sinusoid + // + template + inline SPROUT_CXX14_CONSTEXPR void + sinusoid( + ForwardIterator first, ForwardIterator last, + typename std::iterator_traits::value_type const& frequency = 1, + typename std::iterator_traits::value_type const& amplitude = 1, + typename std::iterator_traits::value_type const& phase = 0 + ) + { + typedef typename std::iterator_traits::value_type value_type; + typedef typename std::iterator_traits::difference_type difference_type; + value_type const d = sprout::detail::sinusoid_value_d(frequency, sprout::distance(first, last)); + difference_type i = 0; + while (first != last) { + *first++ = sprout::detail::sinusoid_value(amplitude, phase, d, i); + ++i; + } + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_SINUSOID_HPP diff --git a/sprout/numeric/dft/cxx14/spectrum.hpp b/sprout/numeric/dft/cxx14/spectrum.hpp new file mode 100644 index 00000000..cb7be51c --- /dev/null +++ b/sprout/numeric/dft/cxx14/spectrum.hpp @@ -0,0 +1,31 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_SPECTRUM_HPP +#define SPROUT_NUMERIC_DFT_CXX14_SPECTRUM_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + // + // spectrum + // + template< + typename InputIterator, typename OutputIterator, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CXX14_CONSTEXPR OutputIterator + spectrum(InputIterator first, InputIterator last, OutputIterator result) { + return sprout::amplitude_spectrum(first, last, result); + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_SPECTRUM_HPP diff --git a/sprout/numeric/dft/cxx14/square.hpp b/sprout/numeric/dft/cxx14/square.hpp new file mode 100644 index 00000000..f891685c --- /dev/null +++ b/sprout/numeric/dft/cxx14/square.hpp @@ -0,0 +1,38 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP +#define SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // square + // + template + inline SPROUT_CXX14_CONSTEXPR void + square( + ForwardIterator first, ForwardIterator last, + typename std::iterator_traits::value_type const& frequency = 1, + typename std::iterator_traits::value_type const& amplitude = 1, + typename std::iterator_traits::value_type const& phase = 0 + ) + { + typedef typename std::iterator_traits::difference_type difference_type; + difference_type i = 0; + while (first != last) { + *first++ = sprout::detail::square_value(frequency, amplitude, phase, i); + ++i; + } + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP diff --git a/sprout/numeric/dft/cxx14/triangle.hpp b/sprout/numeric/dft/cxx14/triangle.hpp new file mode 100644 index 00000000..8ea7f983 --- /dev/null +++ b/sprout/numeric/dft/cxx14/triangle.hpp @@ -0,0 +1,39 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP +#define SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // triangle + // + template + inline SPROUT_CXX14_CONSTEXPR void + triangle( + ForwardIterator first, ForwardIterator last, + typename std::iterator_traits::value_type const& frequency = 1, + typename std::iterator_traits::value_type const& amplitude = 1, + typename std::iterator_traits::value_type const& phase = 0, + typename std::iterator_traits::value_type const& duty = 0.5 + ) + { + typedef typename std::iterator_traits::difference_type difference_type; + difference_type i = 0; + while (first != last) { + *first++ = sprout::detail::triangle_value(frequency, amplitude, phase, duty, i); + ++i; + } + } +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_SAWTOOTH_HPP diff --git a/sprout/numeric/dft/cxx14/wave.hpp b/sprout/numeric/dft/cxx14/wave.hpp new file mode 100644 index 00000000..5290e407 --- /dev/null +++ b/sprout/numeric/dft/cxx14/wave.hpp @@ -0,0 +1,17 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_CXX14_WAVE_HPP +#define SPROUT_NUMERIC_DFT_CXX14_WAVE_HPP + +#include +#include +#include +#include +#include + +#endif // #ifndef SPROUT_NUMERIC_DFT_CXX14_WAVE_HPP diff --git a/sprout/numeric/dft/detail/sawtooth.hpp b/sprout/numeric/dft/detail/sawtooth.hpp new file mode 100644 index 00000000..99b342e5 --- /dev/null +++ b/sprout/numeric/dft/detail/sawtooth.hpp @@ -0,0 +1,30 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_DETAIL_SAWTOOTH_HPP +#define SPROUT_NUMERIC_DFT_DETAIL_SAWTOOTH_HPP + +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR T + sawtooth_value_impl(T const& t) { + using sprout::floor; + return 2 * (t - floor(t + T(0.5))); + } + template + inline SPROUT_CONSTEXPR T + sawtooth_value(T const& frequency, T const& amplitude, T const& phase, Index i) { + return amplitude * sprout::detail::sawtooth_value_impl(frequency * T(i) + phase); + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_DETAIL_SAWTOOTH_HPP diff --git a/sprout/numeric/dft/detail/sinusoid.hpp b/sprout/numeric/dft/detail/sinusoid.hpp new file mode 100644 index 00000000..c63ebf78 --- /dev/null +++ b/sprout/numeric/dft/detail/sinusoid.hpp @@ -0,0 +1,36 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_DETAIL_SINUSOID_HPP +#define SPROUT_NUMERIC_DFT_DETAIL_SINUSOID_HPP + +#include +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR T + sinusoid_value(T const& amplitude, T const& phase, T const& d, Index i) { + using sprout::sin; + return amplitude * sin(d * T(i) + phase); + } + template + inline SPROUT_CONSTEXPR T + sinusoid_value_d(T const& frequency, Size size) { + return sprout::math::two_pi() * frequency / T(size); + } + template + inline SPROUT_CONSTEXPR T + sinusoid_value_d(T const& frequency) { + return sprout::math::two_pi() * frequency; + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_DETAIL_SINUSOID_HPP diff --git a/sprout/numeric/dft/detail/square.hpp b/sprout/numeric/dft/detail/square.hpp new file mode 100644 index 00000000..7a57fe4f --- /dev/null +++ b/sprout/numeric/dft/detail/square.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_DETAIL_SQUARE_HPP +#define SPROUT_NUMERIC_DFT_DETAIL_SQUARE_HPP + +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR T + square_value_impl(T const& t, T const& duty) { + return sprout::detail::sawtooth_value_impl(t) - sprout::detail::sawtooth_value_impl(t - duty); + } + template + inline SPROUT_CONSTEXPR T + square_value(T const& frequency, T const& amplitude, T const& phase, T const& duty, Index i) { + return amplitude * sprout::detail::square_value_impl(frequency * T(i) + phase, duty); + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_DETAIL_SQUARE_HPP diff --git a/sprout/numeric/dft/detail/triangle.hpp b/sprout/numeric/dft/detail/triangle.hpp new file mode 100644 index 00000000..2acf8e51 --- /dev/null +++ b/sprout/numeric/dft/detail/triangle.hpp @@ -0,0 +1,34 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_NUMERIC_DFT_DETAIL_TRINAGLE_HPP +#define SPROUT_NUMERIC_DFT_DETAIL_TRINAGLE_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR T + triangle_value_impl(T const& t) { + typedef double elem_type; + using sprout::sin; + using sprout::asin; + return T(sprout::math::two_div_pi()) * asin(sin(T(sprout::math::two_pi()) * t)); + } + template + inline SPROUT_CONSTEXPR T + triangle_value(T const& frequency, T const& amplitude, T const& phase, Index i) { + return amplitude * sprout::detail::triangle_value_impl(frequency * T(i) + phase); + } + } // namespace detail +} // namespace sprout + +#endif // #ifndef SPROUT_NUMERIC_DFT_DETAIL_TRINAGLE_HPP diff --git a/sprout/numeric/dft/fit/spectrum.hpp b/sprout/numeric/dft/fit/spectrum.hpp index 48010bc2..38656f36 100644 --- a/sprout/numeric/dft/fit/spectrum.hpp +++ b/sprout/numeric/dft/fit/spectrum.hpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace sprout { namespace fit { @@ -31,6 +32,4 @@ namespace sprout { } // namespace fit } // namespace sprout -#include - #endif // #ifndef SPROUT_NUMERIC_DFT_FIT_SPECTRUM_HPP diff --git a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp index 8243820f..2d59a1e8 100644 --- a/sprout/numeric/dft/fixed/amplitude_spectrum.hpp +++ b/sprout/numeric/dft/fixed/amplitude_spectrum.hpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -60,26 +62,26 @@ namespace sprout { ); } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::container_traits::static_size == sizeof...(Args), typename sprout::fixed::results::algorithm::type >::type amplitude_spectrum_impl( - ForwardIterator, ForwardIterator, Result const& result, + InputIterator, InputIterator, Result const& result, typename sprout::container_traits::size_type, Args const&... args ) { return sprout::remake(result, sprout::size(result), args...); } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::container_traits::static_size != sizeof...(Args), typename sprout::fixed::results::algorithm::type >::type amplitude_spectrum_impl( - ForwardIterator first, ForwardIterator last, Result const& result, + InputIterator first, InputIterator last, Result const& result, typename sprout::container_traits::size_type size, Args const&... args ) @@ -92,10 +94,10 @@ namespace sprout { : sprout::detail::container_complate(result, args...) ; } - template + template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type amplitude_spectrum( - ForwardIterator first, ForwardIterator last, Result const& result, + InputIterator first, InputIterator last, Result const& result, std::forward_iterator_tag* ) { @@ -134,14 +136,27 @@ namespace sprout { return sprout::fixed::detail::amplitude_spectrum(first, last, result); } - template + template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type - amplitude_spectrum(ForwardIterator first, ForwardIterator last) { + amplitude_spectrum(InputIterator first, InputIterator last) { return sprout::fixed::amplitude_spectrum(first, last, sprout::pit()); } } // namespace fixed - using sprout::fixed::amplitude_spectrum; + template< + typename InputIterator, typename Result, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + amplitude_spectrum(InputIterator first, InputIterator last, Result const& result) { + return sprout::fixed::amplitude_spectrum(first, last, result); + } + + template + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + amplitude_spectrum(InputIterator first, InputIterator last) { + return sprout::fixed::amplitude_spectrum(first, last); + } } // namespace sprout #endif // #ifndef SPROUT_NUMERIC_DFT_FIXED_AMPLITUDE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/fixed/phase_spectrum.hpp b/sprout/numeric/dft/fixed/phase_spectrum.hpp index 3281e7d8..82a97ecb 100644 --- a/sprout/numeric/dft/fixed/phase_spectrum.hpp +++ b/sprout/numeric/dft/fixed/phase_spectrum.hpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -60,26 +62,26 @@ namespace sprout { ); } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::container_traits::static_size == sizeof...(Args), typename sprout::fixed::results::algorithm::type >::type phase_spectrum_impl( - ForwardIterator, ForwardIterator, Result const& result, + InputIterator, InputIterator, Result const& result, typename sprout::container_traits::size_type, Args const&... args ) { return sprout::remake(result, sprout::size(result), args...); } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< sprout::container_traits::static_size != sizeof...(Args), typename sprout::fixed::results::algorithm::type >::type phase_spectrum_impl( - ForwardIterator first, ForwardIterator last, Result const& result, + InputIterator first, InputIterator last, Result const& result, typename sprout::container_traits::size_type size, Args const&... args ) @@ -92,10 +94,10 @@ namespace sprout { : sprout::detail::container_complate(result, args...) ; } - template + template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type phase_spectrum( - ForwardIterator first, ForwardIterator last, Result const& result, + InputIterator first, InputIterator last, Result const& result, std::forward_iterator_tag* ) { @@ -134,14 +136,27 @@ namespace sprout { return sprout::fixed::detail::phase_spectrum(first, last, result); } - template + template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type - phase_spectrum(ForwardIterator first, ForwardIterator last) { + phase_spectrum(InputIterator first, InputIterator last) { return sprout::fixed::phase_spectrum(first, last, sprout::pit()); } } // namespace fixed - using sprout::fixed::phase_spectrum; + template< + typename InputIterator, typename Result, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + phase_spectrum(InputIterator first, InputIterator last, Result const& result) { + return sprout::fixed::phase_spectrum(first, last, result); + } + + template + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + phase_spectrum(InputIterator first, InputIterator last) { + return sprout::fixed::phase_spectrum(first, last); + } } // namespace sprout #endif // #ifndef SPROUT_NUMERIC_DFT_FIXED_PHASE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/fixed/sawtooth.hpp b/sprout/numeric/dft/fixed/sawtooth.hpp index 0cf81412..b1524b87 100644 --- a/sprout/numeric/dft/fixed/sawtooth.hpp +++ b/sprout/numeric/dft/fixed/sawtooth.hpp @@ -15,19 +15,12 @@ #include #include #include -#include #include +#include namespace sprout { namespace fixed { namespace detail { - template - inline SPROUT_CONSTEXPR T - sawtooth_value(T const& t) { - using sprout::floor; - return 2 * (t - floor(t + T(0.5))); - } - template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type sawtooth_impl( @@ -40,11 +33,10 @@ namespace sprout { typename sprout::container_traits::size_type size ) { - typedef typename sprout::container_traits::value_type value_type; return sprout::remake( cont, size, (Indexes >= offset && Indexes < offset + size - ? amplitude * sprout::fixed::detail::sawtooth_value(frequency * value_type(Indexes) + phase) + ? sprout::detail::sawtooth_value(frequency, amplitude, phase, Indexes) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); diff --git a/sprout/numeric/dft/fixed/sinusoid.hpp b/sprout/numeric/dft/fixed/sinusoid.hpp index 9d71736c..463d44d8 100644 --- a/sprout/numeric/dft/fixed/sinusoid.hpp +++ b/sprout/numeric/dft/fixed/sinusoid.hpp @@ -15,9 +15,8 @@ #include #include #include -#include -#include #include +#include namespace sprout { namespace fixed { @@ -35,11 +34,10 @@ namespace sprout { ) { typedef typename sprout::container_traits::value_type value_type; - using sprout::sin; return sprout::remake( cont, size, (Indexes >= offset && Indexes < offset + size - ? amplitude * sin(d * value_type(Indexes) + phase) + ? sprout::detail::sinusoid_value(amplitude, phase, d, Indexes) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); @@ -56,7 +54,7 @@ namespace sprout { typedef typename sprout::container_traits::value_type value_type; return sprout::fixed::detail::sinusoid_impl( cont, - sprout::math::two_pi() * frequency / value_type(sprout::size(cont)), + sprout::detail::sinusoid_value_d(frequency, sprout::size(cont)), amplitude, phase, sprout::container_indexes::make(), diff --git a/sprout/numeric/dft/fixed/spectrum.hpp b/sprout/numeric/dft/fixed/spectrum.hpp index a4be3d89..baa6b78d 100644 --- a/sprout/numeric/dft/fixed/spectrum.hpp +++ b/sprout/numeric/dft/fixed/spectrum.hpp @@ -9,8 +9,11 @@ #define SPROUT_NUMERIC_DFT_FIXED_SPECTRUM_HPP #include +#include +#include #include #include +#include namespace sprout { namespace fixed { @@ -30,9 +33,20 @@ namespace sprout { } } // namespace fixed - using sprout::fixed::spectrum; + template< + typename InputIterator, typename Result, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + spectrum(InputIterator first, InputIterator last, Result const& result) { + return sprout::fixed::spectrum(first, last, result); + } + + template + inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type + spectrum(InputIterator first, InputIterator last) { + return sprout::fixed::spectrum(first, last); + } } // namespace sprout -#include - #endif // #ifndef SPROUT_NUMERIC_DFT_FIXED_SPECTRUM_HPP diff --git a/sprout/numeric/dft/fixed/square.hpp b/sprout/numeric/dft/fixed/square.hpp index d3b8ee5d..6bca908e 100644 --- a/sprout/numeric/dft/fixed/square.hpp +++ b/sprout/numeric/dft/fixed/square.hpp @@ -15,18 +15,12 @@ #include #include #include -#include #include +#include namespace sprout { namespace fixed { namespace detail { - template - inline SPROUT_CONSTEXPR T - square_value(T const& t, T const& d) { - return sprout::fixed::detail::sawtooth_value(t) - sprout::fixed::detail::sawtooth_value(t - d); - } - template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type square_impl( @@ -40,11 +34,10 @@ namespace sprout { typename sprout::container_traits::size_type size ) { - typedef typename sprout::container_traits::value_type value_type; return sprout::remake( cont, size, (Indexes >= offset && Indexes < offset + size - ? amplitude * sprout::fixed::detail::square_value(frequency * value_type(Indexes) + phase, duty) + ? sprout::detail::square_value(frequency, amplitude, phase, duty, Indexes) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); diff --git a/sprout/numeric/dft/fixed/triangle.hpp b/sprout/numeric/dft/fixed/triangle.hpp index a680d03c..a4b64f12 100644 --- a/sprout/numeric/dft/fixed/triangle.hpp +++ b/sprout/numeric/dft/fixed/triangle.hpp @@ -15,22 +15,12 @@ #include #include #include -#include -#include -#include #include +#include namespace sprout { namespace fixed { namespace detail { - template - inline SPROUT_CONSTEXPR T - triangle_value(T const& t) { - using sprout::sin; - using sprout::asin; - return T(sprout::math::two_div_pi()) * asin(sin(T(sprout::math::two_pi()) * t)); - } - template inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm::type triangle_impl( @@ -43,11 +33,10 @@ namespace sprout { typename sprout::container_traits::size_type size ) { - typedef typename sprout::container_traits::value_type value_type; return sprout::remake( cont, size, (Indexes >= offset && Indexes < offset + size - ? amplitude * sprout::fixed::detail::triangle_value(frequency * value_type(Indexes) + phase) + ? sprout::detail::triangle_value(frequency, amplitude, phase, Indexes) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); diff --git a/sprout/numeric/dft/modifying.hpp b/sprout/numeric/dft/modifying.hpp index e101bc94..efd71792 100644 --- a/sprout/numeric/dft/modifying.hpp +++ b/sprout/numeric/dft/modifying.hpp @@ -11,5 +11,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_NUMERIC_DFT_MODIFYIING_HPP diff --git a/sprout/numeric/dft/phase_spectrum.hpp b/sprout/numeric/dft/phase_spectrum.hpp index fbb2cd67..adafcf58 100644 --- a/sprout/numeric/dft/phase_spectrum.hpp +++ b/sprout/numeric/dft/phase_spectrum.hpp @@ -11,5 +11,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_NUMERIC_DFT_PHASE_SPECTRUM_HPP diff --git a/sprout/numeric/dft/spectrum.hpp b/sprout/numeric/dft/spectrum.hpp index 4c124b1c..c2ebea3a 100644 --- a/sprout/numeric/dft/spectrum.hpp +++ b/sprout/numeric/dft/spectrum.hpp @@ -11,5 +11,6 @@ #include #include #include +#include #endif // #ifndef SPROUT_NUMERIC_DFT_SPECTRUM_HPP