diff --git a/sprout/compost/utility.hpp b/sprout/compost/utility.hpp index d9b1318f..e24800c9 100644 --- a/sprout/compost/utility.hpp +++ b/sprout/compost/utility.hpp @@ -3,5 +3,7 @@ #include #include +#include +#include #endif // #ifndef SPROUT_COMPOST_UTILITY_HPP diff --git a/sprout/compost/utility/iir_filter.hpp b/sprout/compost/utility/iir_filter.hpp new file mode 100644 index 00000000..4a860c50 --- /dev/null +++ b/sprout/compost/utility/iir_filter.hpp @@ -0,0 +1,562 @@ +#ifndef SPROUT_COMPOST_UTILITY_IIR_FILTER_HPP +#define SPROUT_COMPOST_UTILITY_IIR_FILTER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace compost { + namespace detail { + template + inline SPROUT_CONSTEXPR typename sprout::float_promote::type + iir_fc(T const& fc) { + typedef typename sprout::float_promote::type type; + using sprout::tan; + return tan(sprout::math::pi() * fc) / (2 * sprout::math::pi()); + } + template + inline SPROUT_CONSTEXPR typename sprout::float_promote::type + iir_g(T const& g) { + typedef typename sprout::float_promote::type type; + return g + 1; + } + } // namespace detail + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + x2 / y, + 2 * x2 / y, + x2 / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x * x, 1 + x / q + x * x + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_lpf + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_lpf(T const& fc, T const& q, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_lpf_impl(sprout::compost::detail::iir_fc(fc), q, a, b); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + 1 / y, + -2 / y, + 1 / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x * x, 1 + x / q + x * x + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_hpf + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_hpf(T const& fc, T const& q, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_hpf_impl(sprout::compost::detail::iir_fc(fc), q, a, b); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x + x2) / y + ), + sprout::remake( + b, 3, + x / y, + T(0), + -x / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x2, 1 + x + x2 + ); + } + template + 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( + fc1, fc2, a, b, + 2 * sprout::math::pi() * (fc2 - fc1), + 4 * sprout::math::pi() * fc1 * fc2 + ); + } + } // namespace detail + // + // iir_bpf + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_bpf(T const& fc1, T const& fc2, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_bpf_impl( + sprout::compost::detail::iir_fc(fc1), sprout::compost::detail::iir_fc(fc2), + a, b + ); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x + x2) / y + ), + sprout::remake( + b, 3, + (x2 + 1) / y, + (2 * x2 - 2) / y, + (x2 + 1) / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x2, 1 + x + x2 + ); + } + template + 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( + fc1, fc2, a, b, + 2 * sprout::math::pi() * (fc2 - fc1), + 4 * sprout::math::pi() * fc1 * fc2 + ); + } + } // namespace detail + // + // iir_bef + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_bef(T const& fc1, T const& fc2, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_bef_impl( + sprout::compost::detail::iir_fc(fc1), sprout::compost::detail::iir_fc(fc2), + a, b + ); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + x / q / y, + T(0), + -x / q / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x * x, 1 + x / q + x * x + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_resonator + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_resonator(T const& fc, T const& q, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_resonator_impl(sprout::compost::detail::iir_fc(fc), q, a, b); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + (x2 + 1) / y, + (2 * x2 + 2) / y, + (x2 + 1) / y + ) + ); + } + template + 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( + fc, q, a, b, + x, x * x, 1 + x / q + x * x + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_notch + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > + iir_notch(T const& fc, T const& q, A const& a, B const& b) { + typedef sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_notch_impl(sprout::compost::detail::iir_fc(fc), q, a, b); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + (1 + g_ * x / q + g * x2) / y, + (2 * g * x2 - 2) / y, + (1 + g_ * x / q + g * x2) / y + ) + ); + } + template + 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( + fc, q, g, a, b, + x, x * x, 1 + x / q + x * x, sqrt(g) + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_low_shelving + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::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::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_low_shelving_impl( + sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g), + a, b + ); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + (g + g_ * x / q + x2) / y, + (2 * x2 - 2 * g) / y, + (g + g_ * x / q + x2) / y + ) + ); + } + template + 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( + fc, q, g, a, b, + x, x * x, 1 + x / q + x * x, sqrt(g) + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_high_shelving + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::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::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_high_shelving_impl( + sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g), + a, b + ); + } + + namespace detail { + template + 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, 3, + T(1), + (2 * x2 - 2) / y, + (1 - x / q + x2) / y + ), + sprout::remake( + b, 3, + (1 + x / q + g + x2) / y, + (2 * x2 - 2) / y, + (1 + x / q + g + x2) / y + ) + ); + } + template + 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( + fc, q, g, a, b, + x, x * x, 1 + x / q + x * x + ); + } + template + 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( + fc, q, a, b, + 2 * sprout::math::pi() * fc + ); + } + } // namespace detail + // + // iir_peaking + // + template + inline SPROUT_CONSTEXPR sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::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::type, + typename sprout::fixed::result_of::algorithm::type + > result_type; + return sprout::compost::detail::iir_peaking_impl( + sprout::compost::detail::iir_fc(fc), q, sprout::compost::detail::iir_g(g), + a, b + ); + } + + namespace detail { + template + 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(da, 2, sprout::tuples::get<1>(da), sample), + sprout::remake(db, 2, sprout::tuples::get<1>(db), base) + ) + ); + } + } // namespace detail + // + // apply_iir + // + template + inline SPROUT_CONSTEXPR sprout::pair< + T, + sprout::pair< + typename sprout::fixed::result_of::algorithm::type, + typename sprout::fixed::result_of::algorithm::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::type, + typename sprout::fixed::result_of::algorithm::type + > + > result_type; + return sprout::compost::detail::apply_iir_impl( + 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 diff --git a/sprout/compost/utility/rosenberg.hpp b/sprout/compost/utility/rosenberg.hpp new file mode 100644 index 00000000..d584b737 --- /dev/null +++ b/sprout/compost/utility/rosenberg.hpp @@ -0,0 +1,29 @@ +#ifndef SPROUT_COMPOST_UTILITY_ROSENBERG_HPP +#define SPROUT_COMPOST_UTILITY_ROSENBERG_HPP + +#include +#include +#include +#include + +namespace sprout { + namespace compost { + // + // rosenberg_value + // Rosenberg 波は,声門開大期と閉小期が周期の40%,16%となる非対称形の波形であり,τ1 が開大期,τ2が閉小期を示す. + // + template + inline SPROUT_CONSTEXPR typename sprout::float_promote::type + rosenberg_value(T x, T tau1, T tau2) { + typedef typename sprout::float_promote::type type; + return x >= 0 && sprout::math::less_equal(x, tau1) + ? 3 * sprout::detail::pow2(x / tau1) - 2 * sprout::detail::pow3(x / tau1) + : sprout::math::greater(x, tau1) && sprout::math::less_equal(x, tau1 + tau2) + ? 1 - sprout::detail::pow2((x - tau1) / tau2) + : 0 + ; + } + } // namespace compost +} // namespace sprout + +#endif // #ifndef SPROUT_COMPOST_UTILITY_ROSENBERG_HPP diff --git a/sprout/detail/pow.hpp b/sprout/detail/pow.hpp new file mode 100644 index 00000000..6afd2be8 --- /dev/null +++ b/sprout/detail/pow.hpp @@ -0,0 +1,28 @@ +#ifndef SPROUT_DETAIL_POW_HPP +#define SPROUT_DETAIL_POW_HPP +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR T + pow2(T const& x) { + return x * x; + } + template + inline SPROUT_CONSTEXPR T + pow3(T const& x) { + return x * x * x; + } + template + 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 diff --git a/sprout/iterator/sawtooth_iterator.hpp b/sprout/iterator/sawtooth_iterator.hpp index 68f622ee..dc8b98e7 100644 --- a/sprout/iterator/sawtooth_iterator.hpp +++ b/sprout/iterator/sawtooth_iterator.hpp @@ -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); diff --git a/sprout/iterator/sinusoid_iterator.hpp b/sprout/iterator/sinusoid_iterator.hpp index 09379d69..a6d618dc 100644 --- a/sprout/iterator/sinusoid_iterator.hpp +++ b/sprout/iterator/sinusoid_iterator.hpp @@ -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); diff --git a/sprout/iterator/square_iterator.hpp b/sprout/iterator/square_iterator.hpp index 7cd34917..f6bc0ad4 100644 --- a/sprout/iterator/square_iterator.hpp +++ b/sprout/iterator/square_iterator.hpp @@ -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); diff --git a/sprout/iterator/triangle_iterator.hpp b/sprout/iterator/triangle_iterator.hpp index 3fec7361..fa4a2019 100644 --- a/sprout/iterator/triangle_iterator.hpp +++ b/sprout/iterator/triangle_iterator.hpp @@ -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); diff --git a/sprout/iterator/valued_iterator.hpp b/sprout/iterator/valued_iterator.hpp index 7b59ff54..9b378bb5 100644 --- a/sprout/iterator/valued_iterator.hpp +++ b/sprout/iterator/valued_iterator.hpp @@ -164,7 +164,7 @@ namespace sprout { return !(lhs < rhs); } template - inline SPROUT_CONSTEXPR decltype(std::declval() - std::declval()) + inline SPROUT_CONSTEXPR decltype(std::declval() - std::declval()) operator-(sprout::valued_iterator const& lhs, sprout::valued_iterator const& rhs) { return lhs.base() - rhs.base(); } diff --git a/sprout/math/cos.hpp b/sprout/math/cos.hpp index 9d3c82bd..c3707a8e 100644 --- a/sprout/math/cos.hpp +++ b/sprout/math/cos.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -16,28 +17,23 @@ namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR T - cos_impl_1(T x, T tmp, std::size_t n, T x2n) { - return 2 * n > sprout::math::factorial_limit() ? tmp - : sprout::math::detail::cos_impl_1( - x, - tmp + (n % 2 ? -1 : 1) * x2n / sprout::math::factorial(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(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 inline SPROUT_CONSTEXPR FloatType cos_impl(FloatType x) { typedef double type; - return static_cast(sprout::math::detail::cos_impl_1( - static_cast(x), - type(1), - 1, - static_cast(x) * static_cast(x) - )) - ; + return static_cast( + type(1) + sprout::math::detail::cos_impl_1( + static_cast(x) * static_cast(x), + 1, sprout::math::factorial_limit() / 2 + 1 + ) + ); } template< diff --git a/sprout/range/adaptor/sawtooth_wave.hpp b/sprout/range/adaptor/sawtooth_wave.hpp index a69e25b9..bd73be9e 100644 --- a/sprout/range/adaptor/sawtooth_wave.hpp +++ b/sprout/range/adaptor/sawtooth_wave.hpp @@ -1,6 +1,7 @@ #ifndef SPROUT_RANGE_ADAPTOR_SAWTOOTH_WAVE_HPP #define SPROUT_RANGE_ADAPTOR_SAWTOOTH_WAVE_HPP +#include #include #include #include @@ -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::max(), frequency, amplitude, phase) ) {} SPROUT_CONSTEXPR value_type const& frequency() const { diff --git a/sprout/range/adaptor/sinusoidal.hpp b/sprout/range/adaptor/sinusoidal.hpp index 6553a55a..56942104 100644 --- a/sprout/range/adaptor/sinusoidal.hpp +++ b/sprout/range/adaptor/sinusoidal.hpp @@ -1,6 +1,7 @@ #ifndef SPROUT_RANGE_ADAPTOR_SINUSOIDAL_HPP #define SPROUT_RANGE_ADAPTOR_SINUSOIDAL_HPP +#include #include #include #include @@ -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::max(), frequency, amplitude, phase) ) {} SPROUT_CONSTEXPR value_type frequency() const { diff --git a/sprout/range/adaptor/square_wave.hpp b/sprout/range/adaptor/square_wave.hpp index 0bdc7f51..10bae837 100644 --- a/sprout/range/adaptor/square_wave.hpp +++ b/sprout/range/adaptor/square_wave.hpp @@ -1,6 +1,7 @@ #ifndef SPROUT_RANGE_ADAPTOR_SQUARE_WAVE_HPP #define SPROUT_RANGE_ADAPTOR_SQUARE_WAVE_HPP +#include #include #include #include @@ -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::max(), frequency, amplitude, phase, duty) ) {} SPROUT_CONSTEXPR value_type frequency() const { diff --git a/sprout/range/adaptor/triangle_wave.hpp b/sprout/range/adaptor/triangle_wave.hpp index 00d83976..c76fb259 100644 --- a/sprout/range/adaptor/triangle_wave.hpp +++ b/sprout/range/adaptor/triangle_wave.hpp @@ -1,6 +1,7 @@ #ifndef SPROUT_RANGE_ADAPTOR_TRIANGLE_WAVE_HPP #define SPROUT_RANGE_ADAPTOR_TRIANGLE_WAVE_HPP +#include #include #include #include @@ -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::max(), frequency, amplitude, phase) ) {} SPROUT_CONSTEXPR value_type frequency() const {