add compost utility

This commit is contained in:
bolero-MURAKAMI 2012-12-07 02:31:16 +09:00
parent 24d2a229f3
commit b44f7c8f2a
14 changed files with 670 additions and 29 deletions

View file

@ -3,5 +3,7 @@
#include <sprout/config.hpp>
#include <sprout/compost/utility/equal_temperament.hpp>
#include <sprout/compost/utility/rosenberg.hpp>
#include <sprout/compost/utility/iir_filter.hpp>
#endif // #ifndef SPROUT_COMPOST_UTILITY_HPP

View file

@ -0,0 +1,562 @@
#ifndef SPROUT_COMPOST_UTILITY_IIR_FILTER_HPP
#define SPROUT_COMPOST_UTILITY_IIR_FILTER_HPP
#include <sprout/config.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/algorithm/fixed/result_of.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/math/tan.hpp>
#include <sprout/math/sqrt.hpp>
#include <sprout/math/constants.hpp>
namespace sprout {
namespace compost {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::float_promote<T>::type
iir_fc(T const& fc) {
typedef typename sprout::float_promote<T>::type type;
using sprout::tan;
return tan(sprout::math::pi<type>() * fc) / (2 * sprout::math::pi<type>());
}
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::float_promote<T>::type
iir_g(T const& g) {
typedef typename sprout::float_promote<T>::type type;
return g + 1;
}
} // namespace detail
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_lpf_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
x2 / y,
2 * x2 / y,
x2 / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_lpf_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x) {
return sprout::compost::detail::iir_lpf_impl_2<Result>(
fc, q, a, b,
x, x * x, 1 + x / q + x * x
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_lpf_impl(T const& fc, T const& q, A const& a, B const& b) {
return sprout::compost::detail::iir_lpf_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_lpf
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_lpf(T const& fc, T const& q, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_lpf_impl<result_type>(sprout::compost::detail::iir_fc(fc), q, a, b);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_hpf_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
1 / y,
-2 / y,
1 / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_hpf_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x) {
return sprout::compost::detail::iir_hpf_impl_2<Result>(
fc, q, a, b,
x, x * x, 1 + x / q + x * x
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_hpf_impl(T const& fc, T const& q, A const& a, B const& b) {
return sprout::compost::detail::iir_hpf_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_hpf
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_hpf(T const& fc, T const& q, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_hpf_impl<result_type>(sprout::compost::detail::iir_fc(fc), q, a, b);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bpf_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x + x2) / y
),
sprout::remake<B>(
b, 3,
x / y,
T(0),
-x / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bpf_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2) {
return sprout::compost::detail::iir_bpf_impl_2<Result>(
fc, q, a, b,
x, x2, 1 + x + x2
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bpf_impl(T const& fc1, T const& fc2, A const& a, B const& b) {
return sprout::compost::detail::iir_bpf_impl_1<Result>(
fc1, fc2, a, b,
2 * sprout::math::pi<T>() * (fc2 - fc1),
4 * sprout::math::pi<T>() * fc1 * fc2
);
}
} // namespace detail
//
// iir_bpf
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_bpf(T const& fc1, T const& fc2, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_bpf_impl<result_type>(
sprout::compost::detail::iir_fc(fc1), sprout::compost::detail::iir_fc(fc2),
a, b
);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bef_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x + x2) / y
),
sprout::remake<B>(
b, 3,
(x2 + 1) / y,
(2 * x2 - 2) / y,
(x2 + 1) / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bef_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2) {
return sprout::compost::detail::iir_bef_impl_2<Result>(
fc, q, a, b,
x, x2, 1 + x + x2
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_bef_impl(T const& fc1, T const& fc2, A const& a, B const& b) {
return sprout::compost::detail::iir_bef_impl_1<Result>(
fc1, fc2, a, b,
2 * sprout::math::pi<T>() * (fc2 - fc1),
4 * sprout::math::pi<T>() * fc1 * fc2
);
}
} // namespace detail
//
// iir_bef
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_bef(T const& fc1, T const& fc2, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_bef_impl<result_type>(
sprout::compost::detail::iir_fc(fc1), sprout::compost::detail::iir_fc(fc2),
a, b
);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_resonator_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
x / q / y,
T(0),
-x / q / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_resonator_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x) {
return sprout::compost::detail::iir_resonator_impl_2<Result>(
fc, q, a, b,
x, x * x, 1 + x / q + x * x
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_resonator_impl(T const& fc, T const& q, A const& a, B const& b) {
return sprout::compost::detail::iir_resonator_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_resonator
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_resonator(T const& fc, T const& q, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_resonator_impl<result_type>(sprout::compost::detail::iir_fc(fc), q, a, b);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_notch_impl_2(T const& fc, T const& q, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
(x2 + 1) / y,
(2 * x2 + 2) / y,
(x2 + 1) / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_notch_impl_1(T const& fc, T const& q, A const& a, B const& b, T const& x) {
return sprout::compost::detail::iir_notch_impl_2<Result>(
fc, q, a, b,
x, x * x, 1 + x / q + x * x
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_notch_impl(T const& fc, T const& q, A const& a, B const& b) {
return sprout::compost::detail::iir_notch_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_notch
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_notch(T const& fc, T const& q, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_notch_impl<result_type>(sprout::compost::detail::iir_fc(fc), q, a, b);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_low_shelving_impl_2(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x, T const& x2, T const& y, T const& g_) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
(1 + g_ * x / q + g * x2) / y,
(2 * g * x2 - 2) / y,
(1 + g_ * x / q + g * x2) / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_low_shelving_impl_1(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x) {
using sprout::sqrt;
return sprout::compost::detail::iir_low_shelving_impl_2<Result>(
fc, q, g, a, b,
x, x * x, 1 + x / q + x * x, sqrt(g)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_low_shelving_impl(T const& fc, T const& q, T const& g, A const& a, B const& b) {
return sprout::compost::detail::iir_low_shelving_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_low_shelving
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_low_shelving(T const& fc, T const& q, T const& g, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_low_shelving_impl<result_type>(
sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g),
a, b
);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_high_shelving_impl_2(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x, T const& x2, T const& y, T const& g_) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
(g + g_ * x / q + x2) / y,
(2 * x2 - 2 * g) / y,
(g + g_ * x / q + x2) / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_high_shelving_impl_1(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x) {
using sprout::sqrt;
return sprout::compost::detail::iir_high_shelving_impl_2<Result>(
fc, q, g, a, b,
x, x * x, 1 + x / q + x * x, sqrt(g)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_high_shelving_impl(T const& fc, T const& q, T const& g, A const& a, B const& b) {
return sprout::compost::detail::iir_high_shelving_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_high_shelving
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_high_shelving(T const& fc, T const& q, T const& g, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_high_shelving_impl<result_type>(
sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g),
a, b
);
}
namespace detail {
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_peaking_impl_2(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x, T const& x2, T const& y) {
return Result(
sprout::remake<A>(
a, 3,
T(1),
(2 * x2 - 2) / y,
(1 - x / q + x2) / y
),
sprout::remake<B>(
b, 3,
(1 + x / q + g + x2) / y,
(2 * x2 - 2) / y,
(1 + x / q + g + x2) / y
)
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_peaking_impl_1(T const& fc, T const& q, T const& g, A const& a, B const& b, T const& x) {
return sprout::compost::detail::iir_peaking_impl_2<Result>(
fc, q, g, a, b,
x, x * x, 1 + x / q + x * x
);
}
template<typename Result, typename T, typename A, typename B>
inline SPROUT_CONSTEXPR Result
iir_peaking_impl(T const& fc, T const& q, T const& g, A const& a, B const& b) {
return sprout::compost::detail::iir_peaking_impl_1<Result>(
fc, q, a, b,
2 * sprout::math::pi<T>() * fc
);
}
} // namespace detail
//
// iir_peaking
//
template<typename T, typename A, typename B>
inline SPROUT_CONSTEXPR sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
>
iir_peaking(T const& fc, T const& q, T const& g, A const& a, B const& b) {
typedef sprout::pair<
typename sprout::fixed::result_of::algorithm<A>::type,
typename sprout::fixed::result_of::algorithm<B>::type
> result_type;
return sprout::compost::detail::iir_peaking_impl<result_type>(
sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g),
a, b
);
}
namespace detail {
template<typename Result, typename T, typename DelayA, typename DelayB>
inline SPROUT_CONSTEXPR Result
apply_iir_impl(T const& base, DelayA const& da, DelayB const& db, T const& sample) {
return Result(
sample,
typename Result::second_type(
sprout::remake<DelayA>(da, 2, sprout::tuples::get<1>(da), sample),
sprout::remake<DelayB>(db, 2, sprout::tuples::get<1>(db), base)
)
);
}
} // namespace detail
//
// apply_iir
//
template<typename T, typename A, typename B, typename DelayA, typename DelayB>
inline SPROUT_CONSTEXPR sprout::pair<
T,
sprout::pair<
typename sprout::fixed::result_of::algorithm<DelayA>::type,
typename sprout::fixed::result_of::algorithm<DelayB>::type
>
>
apply_iir(T const& base, A const& a, B const& b, DelayA const& da, DelayB const& db) {
typedef sprout::pair<
T,
sprout::pair<
typename sprout::fixed::result_of::algorithm<DelayA>::type,
typename sprout::fixed::result_of::algorithm<DelayB>::type
>
> result_type;
return sprout::compost::detail::apply_iir_impl<result_type>(
base, da, db,
sprout::tuples::get<0>(b) * base
+ sprout::tuples::get<1>(b) * sprout::tuples::get<0>(db)
+ sprout::tuples::get<2>(b) * sprout::tuples::get<1>(db)
- sprout::tuples::get<1>(a) * sprout::tuples::get<0>(da)
- sprout::tuples::get<2>(a) * sprout::tuples::get<1>(da)
);
}
} // namespace compost
} // namespace sprout
#endif // #ifndef SPROUT_COMPOST_UTILITY_IIR_FILTER_HPP

View file

@ -0,0 +1,29 @@
#ifndef SPROUT_COMPOST_UTILITY_ROSENBERG_HPP
#define SPROUT_COMPOST_UTILITY_ROSENBERG_HPP
#include <sprout/config.hpp>
#include <sprout/detail/pow.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/math/comparison.hpp>
namespace sprout {
namespace compost {
//
// rosenberg_value
// Rosenberg 波は声門開大期と閉小期が周期の40%16%となる非対称形の波形でありτ1 が開大期τ2が閉小期を示す
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::float_promote<T>::type
rosenberg_value(T x, T tau1, T tau2) {
typedef typename sprout::float_promote<T>::type type;
return x >= 0 && sprout::math::less_equal(x, tau1)
? 3 * sprout::detail::pow2<type>(x / tau1) - 2 * sprout::detail::pow3<type>(x / tau1)
: sprout::math::greater(x, tau1) && sprout::math::less_equal(x, tau1 + tau2)
? 1 - sprout::detail::pow2<type>((x - tau1) / tau2)
: 0
;
}
} // namespace compost
} // namespace sprout
#endif // #ifndef SPROUT_COMPOST_UTILITY_ROSENBERG_HPP

28
sprout/detail/pow.hpp Normal file
View file

@ -0,0 +1,28 @@
#ifndef SPROUT_DETAIL_POW_HPP
#define SPROUT_DETAIL_POW_HPP
#include <sprout/config.hpp>
namespace sprout {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
pow2(T const& x) {
return x * x;
}
template<typename T>
inline SPROUT_CONSTEXPR T
pow3(T const& x) {
return x * x * x;
}
template<typename T>
inline SPROUT_CONSTEXPR T
pow_n(T const& x, int n) {
return n == 1 ? x
: n % 2 ? x * sprout::detail::pow2(sprout::detail::pow_n(x, n / 2))
: sprout::detail::pow2(sprout::detail::pow_n(x, n / 2))
;
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_POW_HPP

View file

@ -89,7 +89,9 @@ namespace sprout {
return phase_;
}
SPROUT_CONSTEXPR reference operator*() const {
return amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_) + phase_)
;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &operator*()();
@ -129,7 +131,9 @@ namespace sprout {
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_ + n) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::sawtooth_value(frequency_ * value_type(index_ + n) + phase_)
;
}
SPROUT_CONSTEXPR sawtooth_iterator next() const {
return sawtooth_iterator(*this, index_ + 1);

View file

@ -96,7 +96,9 @@ namespace sprout {
}
SPROUT_CONSTEXPR reference operator*() const {
using sprout::sin;
return amplitude_ * sin(d_ * value_type(index_) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sin(d_ * value_type(index_) + phase_)
;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &operator*()();
@ -137,7 +139,9 @@ namespace sprout {
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
using sprout::sin;
return amplitude_ * sin(d_ * value_type(index_ + n) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sin(d_ * value_type(index_ + n) + phase_)
;
}
SPROUT_CONSTEXPR sinusoid_iterator next() const {
return sinusoid_iterator(*this, index_ + 1);

View file

@ -98,7 +98,9 @@ namespace sprout {
return duty_;
}
SPROUT_CONSTEXPR reference operator*() const {
return amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_) + phase_, duty_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_) + phase_, duty_)
;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &operator*()();
@ -138,7 +140,9 @@ namespace sprout {
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_ + n) + phase_, duty_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_ + n) + phase_, duty_)
;
}
SPROUT_CONSTEXPR square_iterator next() const {
return square_iterator(*this, index_ + 1);

View file

@ -89,7 +89,9 @@ namespace sprout {
return phase_;
}
SPROUT_CONSTEXPR reference operator*() const {
return amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_) + phase_)
;
}
SPROUT_CONSTEXPR pointer operator->() const {
return &operator*()();
@ -129,7 +131,9 @@ namespace sprout {
return *this;
}
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
return amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_ + n) + phase_);
return amplitude_ == 0 ? 0
: amplitude_ * sprout::fixed::detail::triangle_value(frequency_ * value_type(index_ + n) + phase_)
;
}
SPROUT_CONSTEXPR triangle_iterator next() const {
return triangle_iterator(*this, index_ + 1);

View file

@ -164,7 +164,7 @@ namespace sprout {
return !(lhs < rhs);
}
template<typename Iterator1, typename T1, typename Iterator2, typename T2>
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1, T1>() - std::declval<Iterator2, T2>())
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1>() - std::declval<Iterator2>())
operator-(sprout::valued_iterator<Iterator1, T1> const& lhs, sprout::valued_iterator<Iterator2, T2> const& rhs) {
return lhs.base() - rhs.base();
}

View file

@ -5,6 +5,7 @@
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/detail/pow.hpp>
#include <sprout/math/detail/config.hpp>
#include <sprout/math/factorial.hpp>
#include <sprout/math/constants.hpp>
@ -16,28 +17,23 @@ namespace sprout {
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR T
cos_impl_1(T x, T tmp, std::size_t n, T x2n) {
return 2 * n > sprout::math::factorial_limit<T>() ? tmp
: sprout::math::detail::cos_impl_1(
x,
tmp + (n % 2 ? -1 : 1) * x2n / sprout::math::factorial<T>(2 * n),
n + 1,
x2n * x * x
)
cos_impl_1(T x2, std::size_t first, std::size_t last) {
return last - first == 1
? (first % 2 ? -1 : 1) * sprout::detail::pow_n(x2, first) / sprout::math::factorial<T>(2 * first)
: sprout::math::detail::cos_impl_1(x2, first, first + (last - first) / 2)
+ sprout::math::detail::cos_impl_1(x2, first + (last - first) / 2, last)
;
}
template<typename FloatType>
inline SPROUT_CONSTEXPR FloatType
cos_impl(FloatType x) {
typedef double type;
return static_cast<FloatType>(sprout::math::detail::cos_impl_1(
static_cast<type>(x),
type(1),
1,
static_cast<type>(x) * static_cast<type>(x)
))
;
return static_cast<FloatType>(
type(1) + sprout::math::detail::cos_impl_1(
static_cast<type>(x) * static_cast<type>(x),
1, sprout::math::factorial_limit<type>() / 2 + 1
)
);
}
template<

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_RANGE_ADAPTOR_SAWTOOTH_WAVE_HPP
#define SPROUT_RANGE_ADAPTOR_SAWTOOTH_WAVE_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/pit.hpp>
@ -71,6 +72,7 @@ namespace sprout {
> base_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
public:
sawtooth_wave_range() = default;
sawtooth_wave_range(sawtooth_wave_range const&) = default;
@ -81,7 +83,7 @@ namespace sprout {
)
: base_type(
iterator(0, frequency, amplitude, phase),
iterator(-1, frequency, amplitude, phase)
iterator(std::numeric_limits<difference_type>::max(), frequency, amplitude, phase)
)
{}
SPROUT_CONSTEXPR value_type const& frequency() const {

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_RANGE_ADAPTOR_SINUSOIDAL_HPP
#define SPROUT_RANGE_ADAPTOR_SINUSOIDAL_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/pit.hpp>
@ -71,6 +72,7 @@ namespace sprout {
> base_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
public:
sinusoidal_range() = default;
sinusoidal_range(sinusoidal_range const&) = default;
@ -81,7 +83,7 @@ namespace sprout {
)
: base_type(
iterator(0, frequency, amplitude, phase),
iterator(-1, frequency, amplitude, phase)
iterator(std::numeric_limits<difference_type>::max(), frequency, amplitude, phase)
)
{}
SPROUT_CONSTEXPR value_type frequency() const {

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_RANGE_ADAPTOR_SQUARE_WAVE_HPP
#define SPROUT_RANGE_ADAPTOR_SQUARE_WAVE_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/pit.hpp>
@ -75,6 +76,7 @@ namespace sprout {
> base_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
public:
square_wave_range() = default;
square_wave_range(square_wave_range const&) = default;
@ -86,7 +88,7 @@ namespace sprout {
)
: base_type(
iterator(0, frequency, amplitude, phase, duty),
iterator(-1, frequency, amplitude, phase, duty)
iterator(std::numeric_limits<difference_type>::max(), frequency, amplitude, phase, duty)
)
{}
SPROUT_CONSTEXPR value_type frequency() const {

View file

@ -1,6 +1,7 @@
#ifndef SPROUT_RANGE_ADAPTOR_TRIANGLE_WAVE_HPP
#define SPROUT_RANGE_ADAPTOR_TRIANGLE_WAVE_HPP
#include <limits>
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/pit.hpp>
@ -71,6 +72,7 @@ namespace sprout {
> base_type;
typedef typename base_type::iterator iterator;
typedef typename base_type::value_type value_type;
typedef typename base_type::difference_type difference_type;
public:
triangle_wave_range() = default;
triangle_wave_range(triangle_wave_range const&) = default;
@ -81,7 +83,7 @@ namespace sprout {
)
: base_type(
iterator(0, frequency, amplitude, phase),
iterator(-1, frequency, amplitude, phase)
iterator(std::numeric_limits<difference_type>::max(), frequency, amplitude, phase)
)
{}
SPROUT_CONSTEXPR value_type frequency() const {