From 51a7a4c86c60101cc354faece1005787c15a6ac6 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 1 May 2012 11:32:48 +0900 Subject: [PATCH] fix sinusoidal --- sprout/iterator/sinusoid_iterator.hpp | 14 ++++++++-- sprout/numeric/dft/fit/sinusoid.hpp | 6 ++-- sprout/numeric/dft/fixed/sinusoid.hpp | 12 +++++--- sprout/range/adaptor/sinusoidal.hpp | 40 +++++++++++++++++++++------ 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/sprout/iterator/sinusoid_iterator.hpp b/sprout/iterator/sinusoid_iterator.hpp index aff9171c..40fe8b6e 100644 --- a/sprout/iterator/sinusoid_iterator.hpp +++ b/sprout/iterator/sinusoid_iterator.hpp @@ -36,12 +36,14 @@ namespace sprout { difference_type index_; value_type frequency_; value_type amplitude_; + value_type phase_; value_type d_; private: explicit SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& other, difference_type index) : index_(index) , frequency_(other.frequency_) , amplitude_(other.amplitude_) + , phase_(other.phase_) , d_(other.d_) {} public: @@ -49,17 +51,20 @@ namespace sprout { : index_() , frequency_(1) , amplitude_(1) + , phase_(0) , d_(value_type(2) * sprout::math::pi()) {} sinusoid_iterator(sinusoid_iterator const&) = default; explicit SPROUT_CONSTEXPR sinusoid_iterator( difference_type index, value_type const& frequency = 1, - value_type const& amplitude = 1 + value_type const& amplitude = 1, + value_type const& phase = 0 ) : index_(index) , frequency_(frequency) , amplitude_(amplitude) + , phase_(phase) , d_(value_type(2) * sprout::math::pi() * frequency) {} template @@ -67,6 +72,7 @@ namespace sprout { : index_(it.index_) , frequency_(it.frequency_) , amplitude_(it.amplitude_) + , phase_(it.phase_) , d_(it.d_) {} template @@ -84,6 +90,9 @@ namespace sprout { SPROUT_CONSTEXPR value_type const& amplitude() const { return amplitude_; } + SPROUT_CONSTEXPR value_type const& phase() const { + return phase_; + } SPROUT_CONSTEXPR reference operator*() const { using std::sin; return amplitude_ * sin(d_ * value_type(index_)); @@ -137,9 +146,10 @@ namespace sprout { } void swap(sinusoid_iterator& other) { using std::swap; + swap(index_, other.index_); swap(frequency_, other.frequency_); swap(amplitude_, other.amplitude_); - swap(index_, other.index_); + swap(phase_, other.phase_); swap(d_, other.d_); } }; diff --git a/sprout/numeric/dft/fit/sinusoid.hpp b/sprout/numeric/dft/fit/sinusoid.hpp index 44738493..acb6b6e7 100644 --- a/sprout/numeric/dft/fit/sinusoid.hpp +++ b/sprout/numeric/dft/fit/sinusoid.hpp @@ -16,11 +16,12 @@ namespace sprout { Container const& cont, typename sprout::container_traits::value_type const& frequency, typename sprout::container_traits::value_type const& amplitude, + typename sprout::container_traits::value_type const& phase, typename sprout::container_traits::difference_type offset ) { return sprout::sub_copy( - sprout::get_internal(sprout::fixed::sinusoid(cont, frequency, amplitude)), + sprout::get_internal(sprout::fixed::sinusoid(cont, frequency, amplitude, phase)), offset, offset + sprout::size(cont) ); @@ -33,7 +34,8 @@ namespace sprout { inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm::type sinusoid( Container const& cont, typename sprout::container_traits::value_type const& frequency = 1, - typename sprout::container_traits::value_type const& amplitude = 1 + typename sprout::container_traits::value_type const& amplitude = 1, + typename sprout::container_traits::value_type const& phase = 0 ) { return sprout::fit::detail::sinusoid_impl(cont, frequency, amplitude, sprout::internal_begin_offset(cont)); diff --git a/sprout/numeric/dft/fixed/sinusoid.hpp b/sprout/numeric/dft/fixed/sinusoid.hpp index 05fee29b..0339ef6d 100644 --- a/sprout/numeric/dft/fixed/sinusoid.hpp +++ b/sprout/numeric/dft/fixed/sinusoid.hpp @@ -19,6 +19,7 @@ namespace sprout { Container const& cont, typename sprout::container_traits::value_type const& d, typename sprout::container_traits::value_type const& amplitude, + typename sprout::container_traits::value_type const& phase, sprout::index_tuple, typename sprout::container_traits::difference_type offset, typename sprout::container_traits::size_type size @@ -30,7 +31,7 @@ namespace sprout { cont, size, (Indexes >= offset && Indexes < offset + size - ? amplitude * sin(d * value_type(Indexes)) + ? amplitude * sin(d * value_type(Indexes) + phase) : *sprout::next(sprout::internal_begin(cont), Indexes) )... ); @@ -40,7 +41,8 @@ namespace sprout { sinusoid( Container const& cont, typename sprout::container_traits::value_type const& frequency, - typename sprout::container_traits::value_type const& amplitude + typename sprout::container_traits::value_type const& amplitude, + typename sprout::container_traits::value_type const& phase ) { typedef typename sprout::container_traits::value_type value_type; @@ -48,6 +50,7 @@ namespace sprout { cont, value_type(2) * sprout::math::pi() * frequency / value_type(sprout::size(cont)), amplitude, + phase, sprout::container_indexes::make(), sprout::internal_begin_offset(cont), sprout::size(cont) @@ -62,10 +65,11 @@ namespace sprout { sinusoid( Container const& cont, typename sprout::container_traits::value_type const& frequency = 1, - typename sprout::container_traits::value_type const& amplitude = 1 + typename sprout::container_traits::value_type const& amplitude = 1, + typename sprout::container_traits::value_type const& phase = 0 ) { - return sprout::fixed::detail::sinusoid(cont, frequency, amplitude); + return sprout::fixed::detail::sinusoid(cont, frequency, amplitude, phase); } } // namespace fixed diff --git a/sprout/range/adaptor/sinusoidal.hpp b/sprout/range/adaptor/sinusoidal.hpp index d8ec2f0f..14508ede 100644 --- a/sprout/range/adaptor/sinusoidal.hpp +++ b/sprout/range/adaptor/sinusoidal.hpp @@ -39,13 +39,23 @@ namespace sprout { explicit SPROUT_CONSTEXPR sinusoidal_range( range_type& range, value_type const& frequency = 1, - value_type const& amplitude = 1 + value_type const& amplitude = 1, + value_type const& phase = 0 ) : base_type( - iterator(0, frequency, amplitude), - iterator(sprout::size(range), frequency, amplitude) + iterator(0, frequency, amplitude, phase), + iterator(sprout::size(range), frequency, amplitude, phase) ) {} + SPROUT_CONSTEXPR value_type const& frequency() const { + return base_type::begin().frequency(); + } + SPROUT_CONSTEXPR value_type const& amplitude() const { + return base_type::begin().amplitude(); + } + SPROUT_CONSTEXPR value_type const& phase() const { + return base_type::begin().phase(); + } }; template @@ -66,13 +76,23 @@ namespace sprout { sinusoidal_range(sinusoidal_range const&) = default; explicit SPROUT_CONSTEXPR sinusoidal_range( value_type const& frequency = 1, - value_type const& amplitude = 1 + value_type const& amplitude = 1, + value_type const& phase = 0 ) : base_type( - iterator(0, frequency, amplitude), - iterator(-1, frequency, amplitude) + iterator(0, frequency, amplitude, phase), + iterator(-1, frequency, amplitude, phase) ) {} + SPROUT_CONSTEXPR value_type const& frequency() const { + return base_type::begin().frequency(); + } + SPROUT_CONSTEXPR value_type const& amplitude() const { + return base_type::begin().amplitude(); + } + SPROUT_CONSTEXPR value_type const& phase() const { + return base_type::begin().phase(); + } }; // @@ -84,10 +104,11 @@ namespace sprout { SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range operator()( Value const& frequency = 1, - Value const& amplitude = 1 + Value const& amplitude = 1, + Value const& phase = 0 ) { - return sprout::adaptors::sinusoidal_range(frequency, amplitude); + return sprout::adaptors::sinusoidal_range(frequency, amplitude, phase); } }; @@ -113,7 +134,8 @@ namespace sprout { >( sprout::lvalue_forward(lhs), rhs.frequency(), - rhs.amplitude() + rhs.amplitude(), + rhs.phase() ); } } // namespace adaptors