From b374ec63bcd0d1634ee6c8cdfe7e96268ade3ab1 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Tue, 21 Feb 2017 20:38:58 +0200 Subject: Added breaking sinusoids into orthogonal components It looks this has been sitting in my directory for a while, and I just forgot to push it. --- src/sinusoid.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'src/sinusoid.rs') diff --git a/src/sinusoid.rs b/src/sinusoid.rs index 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 { -- cgit v1.2.3