summaryrefslogtreecommitdiff log msg author committer range
diff options
 context: 12345678910152025303540 space: includeignore mode: unifiedssdiffstat only
-rw-r--r--src/num_traits.rs8
-rw-r--r--src/sinusoid.rs35
2 files changed, 39 insertions, 4 deletions
 diff --git a/src/num_traits.rs b/src/num_traits.rsindex 030e5c4..938bc9a 100644--- a/src/num_traits.rs+++ b/src/num_traits.rs@@ -99,6 +99,8 @@ pub trait FractionOps { fn recip(self) -> Self; fn pi() -> Self; fn two_pi() -> Self;+ fn half_pi() -> Self;+ fn zero() -> Self; } macro_rules! impl_fraction_float {@@ -113,6 +115,12 @@ macro_rules! impl_fraction_float { fn two_pi() -> Self { 2.0 * \$pi }+ fn half_pi() -> Self {+ \$pi / 2.0+ }+ fn zero() -> Self {+ 0.0+ } } } }diff --git a/src/sinusoid.rs b/src/sinusoid.rsindex 51deae2..8316707 100644--- a/src/sinusoid.rs+++ b/src/sinusoid.rs@@ -1,5 +1,5 @@ use std::cmp::{PartialOrd};-use ::num_traits::{Trig, Pow, ArithmeticOps, FractionOps};+use ::num_traits::{Trig, Pow, ArithmeticOps, SignedArithmeticOps, FractionOps}; use ::complex::Complex; /// A data structure representing a sinusoid. AKA the sin or cos functions.@@ -10,9 +10,9 @@ use ::complex::Complex; /// floats. #[derive(Debug, Clone, PartialEq)] pub struct Sinusoid {- amplitude: T,- frequency: T,- phase: T+ pub amplitude: T,+ pub frequency: T,+ pub phase: T } impl Sinusoid {@@ -69,6 +69,33 @@ impl Sinusoid where T: Trig + Pow + ArithmeticOps + Copy { pub fn to_phasor(self) -> Complex { Complex::from_polar(self.amplitude, self.phase) }++}++impl Sinusoid where T: Trig + Pow + SignedArithmeticOps + FractionOps + Copy {+ /// Splits the sinusoid into cos and sin components+ ///+ /// ```+ /// use worthe_signals::sinusoid::Sinusoid;+ /// use std::f32;+ ///+ /// let cos_component = Sinusoid::new(-3.0 as f32, 1.0, 0.0);+ /// let sin_component = Sinusoid::new(4.0 as f32, 1.0, -f32::consts::FRAC_PI_2);+ /// let combined = cos_component.clone().add(sin_component.clone()).expect("I know this won't panic because the frequencies are hardcoded to be the same");+ ///+ /// let (recovered_cos, recovered_sin) = combined.to_orthogonal_components();+ ///+ /// //there is a small loss of precision when going back and forth between split components and one sinusoid.+ /// assert!((cos_component.amplitude - recovered_cos.amplitude).abs() < 0.000001);+ /// assert!((sin_component.amplitude - recovered_sin.amplitude).abs() < 0.000001);+ /// ```+ pub fn to_orthogonal_components(self) -> (Self, Self) {+ let frequency = self.frequency;+ let phasor = self.to_phasor();+ let cos = Sinusoid::new(phasor.real, frequency, T::zero());+ let sin = Sinusoid::new(-phasor.imag, frequency, -T::half_pi());+ (cos, sin)+ } } impl Sinusoid where T: Trig + Pow + ArithmeticOps + PartialEq + Copy {