fix sinusoidal

This commit is contained in:
bolero-MURAKAMI 2012-05-01 11:32:48 +09:00
parent bafd1004dd
commit 51a7a4c86c
4 changed files with 55 additions and 17 deletions

View file

@ -36,12 +36,14 @@ namespace sprout {
difference_type index_; difference_type index_;
value_type frequency_; value_type frequency_;
value_type amplitude_; value_type amplitude_;
value_type phase_;
value_type d_; value_type d_;
private: private:
explicit SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& other, difference_type index) explicit SPROUT_CONSTEXPR sinusoid_iterator(sinusoid_iterator const& other, difference_type index)
: index_(index) : index_(index)
, frequency_(other.frequency_) , frequency_(other.frequency_)
, amplitude_(other.amplitude_) , amplitude_(other.amplitude_)
, phase_(other.phase_)
, d_(other.d_) , d_(other.d_)
{} {}
public: public:
@ -49,17 +51,20 @@ namespace sprout {
: index_() : index_()
, frequency_(1) , frequency_(1)
, amplitude_(1) , amplitude_(1)
, phase_(0)
, d_(value_type(2) * sprout::math::pi<value_type>()) , d_(value_type(2) * sprout::math::pi<value_type>())
{} {}
sinusoid_iterator(sinusoid_iterator const&) = default; sinusoid_iterator(sinusoid_iterator const&) = default;
explicit SPROUT_CONSTEXPR sinusoid_iterator( explicit SPROUT_CONSTEXPR sinusoid_iterator(
difference_type index, difference_type index,
value_type const& frequency = 1, value_type const& frequency = 1,
value_type const& amplitude = 1 value_type const& amplitude = 1,
value_type const& phase = 0
) )
: index_(index) : index_(index)
, frequency_(frequency) , frequency_(frequency)
, amplitude_(amplitude) , amplitude_(amplitude)
, phase_(phase)
, d_(value_type(2) * sprout::math::pi<value_type>() * frequency) , d_(value_type(2) * sprout::math::pi<value_type>() * frequency)
{} {}
template<typename U> template<typename U>
@ -67,6 +72,7 @@ namespace sprout {
: index_(it.index_) : index_(it.index_)
, frequency_(it.frequency_) , frequency_(it.frequency_)
, amplitude_(it.amplitude_) , amplitude_(it.amplitude_)
, phase_(it.phase_)
, d_(it.d_) , d_(it.d_)
{} {}
template<typename U> template<typename U>
@ -84,6 +90,9 @@ namespace sprout {
SPROUT_CONSTEXPR value_type const& amplitude() const { SPROUT_CONSTEXPR value_type const& amplitude() const {
return amplitude_; return amplitude_;
} }
SPROUT_CONSTEXPR value_type const& phase() const {
return phase_;
}
SPROUT_CONSTEXPR reference operator*() const { SPROUT_CONSTEXPR reference operator*() const {
using std::sin; using std::sin;
return amplitude_ * sin(d_ * value_type(index_)); return amplitude_ * sin(d_ * value_type(index_));
@ -137,9 +146,10 @@ namespace sprout {
} }
void swap(sinusoid_iterator& other) { void swap(sinusoid_iterator& other) {
using std::swap; using std::swap;
swap(index_, other.index_);
swap(frequency_, other.frequency_); swap(frequency_, other.frequency_);
swap(amplitude_, other.amplitude_); swap(amplitude_, other.amplitude_);
swap(index_, other.index_); swap(phase_, other.phase_);
swap(d_, other.d_); swap(d_, other.d_);
} }
}; };

View file

@ -16,11 +16,12 @@ namespace sprout {
Container const& cont, Container const& cont,
typename sprout::container_traits<Container>::value_type const& frequency, typename sprout::container_traits<Container>::value_type const& frequency,
typename sprout::container_traits<Container>::value_type const& amplitude, typename sprout::container_traits<Container>::value_type const& amplitude,
typename sprout::container_traits<Container>::value_type const& phase,
typename sprout::container_traits<Container>::difference_type offset typename sprout::container_traits<Container>::difference_type offset
) )
{ {
return sprout::sub_copy( 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,
offset + sprout::size(cont) offset + sprout::size(cont)
); );
@ -33,7 +34,8 @@ namespace sprout {
inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm<Container>::type sinusoid( inline SPROUT_CONSTEXPR typename sprout::fit::result_of::algorithm<Container>::type sinusoid(
Container const& cont, Container const& cont,
typename sprout::container_traits<Container>::value_type const& frequency = 1, typename sprout::container_traits<Container>::value_type const& frequency = 1,
typename sprout::container_traits<Container>::value_type const& amplitude = 1 typename sprout::container_traits<Container>::value_type const& amplitude = 1,
typename sprout::container_traits<Container>::value_type const& phase = 0
) )
{ {
return sprout::fit::detail::sinusoid_impl(cont, frequency, amplitude, sprout::internal_begin_offset(cont)); return sprout::fit::detail::sinusoid_impl(cont, frequency, amplitude, sprout::internal_begin_offset(cont));

View file

@ -19,6 +19,7 @@ namespace sprout {
Container const& cont, Container const& cont,
typename sprout::container_traits<Container>::value_type const& d, typename sprout::container_traits<Container>::value_type const& d,
typename sprout::container_traits<Container>::value_type const& amplitude, typename sprout::container_traits<Container>::value_type const& amplitude,
typename sprout::container_traits<Container>::value_type const& phase,
sprout::index_tuple<Indexes...>, sprout::index_tuple<Indexes...>,
typename sprout::container_traits<Container>::difference_type offset, typename sprout::container_traits<Container>::difference_type offset,
typename sprout::container_traits<Container>::size_type size typename sprout::container_traits<Container>::size_type size
@ -30,7 +31,7 @@ namespace sprout {
cont, cont,
size, size,
(Indexes >= offset && Indexes < offset + 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) : *sprout::next(sprout::internal_begin(cont), Indexes)
)... )...
); );
@ -40,7 +41,8 @@ namespace sprout {
sinusoid( sinusoid(
Container const& cont, Container const& cont,
typename sprout::container_traits<Container>::value_type const& frequency, typename sprout::container_traits<Container>::value_type const& frequency,
typename sprout::container_traits<Container>::value_type const& amplitude typename sprout::container_traits<Container>::value_type const& amplitude,
typename sprout::container_traits<Container>::value_type const& phase
) )
{ {
typedef typename sprout::container_traits<Container>::value_type value_type; typedef typename sprout::container_traits<Container>::value_type value_type;
@ -48,6 +50,7 @@ namespace sprout {
cont, cont,
value_type(2) * sprout::math::pi<value_type>() * frequency / value_type(sprout::size(cont)), value_type(2) * sprout::math::pi<value_type>() * frequency / value_type(sprout::size(cont)),
amplitude, amplitude,
phase,
sprout::container_indexes<Container>::make(), sprout::container_indexes<Container>::make(),
sprout::internal_begin_offset(cont), sprout::internal_begin_offset(cont),
sprout::size(cont) sprout::size(cont)
@ -62,10 +65,11 @@ namespace sprout {
sinusoid( sinusoid(
Container const& cont, Container const& cont,
typename sprout::container_traits<Container>::value_type const& frequency = 1, typename sprout::container_traits<Container>::value_type const& frequency = 1,
typename sprout::container_traits<Container>::value_type const& amplitude = 1 typename sprout::container_traits<Container>::value_type const& amplitude = 1,
typename sprout::container_traits<Container>::value_type const& phase = 0
) )
{ {
return sprout::fixed::detail::sinusoid(cont, frequency, amplitude); return sprout::fixed::detail::sinusoid(cont, frequency, amplitude, phase);
} }
} // namespace fixed } // namespace fixed

View file

@ -39,13 +39,23 @@ namespace sprout {
explicit SPROUT_CONSTEXPR sinusoidal_range( explicit SPROUT_CONSTEXPR sinusoidal_range(
range_type& range, range_type& range,
value_type const& frequency = 1, value_type const& frequency = 1,
value_type const& amplitude = 1 value_type const& amplitude = 1,
value_type const& phase = 0
) )
: base_type( : base_type(
iterator(0, frequency, amplitude), iterator(0, frequency, amplitude, phase),
iterator(sprout::size(range), frequency, amplitude) 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<typename Value> template<typename Value>
@ -66,13 +76,23 @@ namespace sprout {
sinusoidal_range(sinusoidal_range const&) = default; sinusoidal_range(sinusoidal_range const&) = default;
explicit SPROUT_CONSTEXPR sinusoidal_range( explicit SPROUT_CONSTEXPR sinusoidal_range(
value_type const& frequency = 1, value_type const& frequency = 1,
value_type const& amplitude = 1 value_type const& amplitude = 1,
value_type const& phase = 0
) )
: base_type( : base_type(
iterator(0, frequency, amplitude), iterator(0, frequency, amplitude, phase),
iterator(-1, frequency, amplitude) 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<Value> SPROUT_CONSTEXPR sprout::adaptors::sinusoidal_range<Value>
operator()( operator()(
Value const& frequency = 1, Value const& frequency = 1,
Value const& amplitude = 1 Value const& amplitude = 1,
Value const& phase = 0
) )
{ {
return sprout::adaptors::sinusoidal_range<Value>(frequency, amplitude); return sprout::adaptors::sinusoidal_range<Value>(frequency, amplitude, phase);
} }
}; };
@ -113,7 +134,8 @@ namespace sprout {
>( >(
sprout::lvalue_forward<Range>(lhs), sprout::lvalue_forward<Range>(lhs),
rhs.frequency(), rhs.frequency(),
rhs.amplitude() rhs.amplitude(),
rhs.phase()
); );
} }
} // namespace adaptors } // namespace adaptors