From ccec9d7fed72a262912a12097969b14980d1b5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 19 Feb 2021 09:29:38 +0100 Subject: [PATCH] complex: rename extension trait, fix MulScaled --- dsp/src/complex.rs | 39 ++++++++++++++------------------------ dsp/src/lockin.rs | 2 +- src/bin/lockin-external.rs | 2 +- src/bin/lockin-internal.rs | 2 +- 4 files changed, 17 insertions(+), 28 deletions(-) diff --git a/dsp/src/complex.rs b/dsp/src/complex.rs index 42d736b..3349460 100644 --- a/dsp/src/complex.rs +++ b/dsp/src/complex.rs @@ -2,33 +2,21 @@ pub use num::Complex; use super::{atan2, cossin}; -pub trait Map { - fn map(&self, func: F) -> Self; -} - -impl T, T: Copy> Map for Complex { - fn map(&self, func: F) -> Self { - Complex { - re: func(self.re), - im: func(self.im), - } - } -} - -pub trait FastInt { +/// Complex extension trait offering DSP (fast, good accuracy) functionality. +pub trait ComplexExt { fn from_angle(angle: T) -> Self; fn abs_sqr(&self) -> U; fn log2(&self) -> T; fn arg(&self) -> T; } -impl FastInt for Complex { +impl ComplexExt for Complex { /// Return a Complex on the unit circle given an angle. /// /// Example: /// /// ``` - /// use dsp::{Complex, FastInt}; + /// use dsp::{Complex, ComplexExt}; /// Complex::::from_angle(0); /// Complex::::from_angle(1 << 30); // pi/2 /// Complex::::from_angle(-1 << 30); // -pi/2 @@ -47,7 +35,7 @@ impl FastInt for Complex { /// Example: /// /// ``` - /// use dsp::{Complex, FastInt}; + /// use dsp::{Complex, ComplexExt}; /// assert_eq!(Complex::new(i32::MIN, 0).abs_sqr(), 1 << 31); /// assert_eq!(Complex::new(i32::MAX, i32::MAX).abs_sqr(), u32::MAX - 3); /// ``` @@ -67,7 +55,7 @@ impl FastInt for Complex { /// Example: /// /// ``` - /// use dsp::{Complex, FastInt}; + /// use dsp::{Complex, ComplexExt}; /// assert_eq!(Complex::new(i32::MAX, i32::MAX).log2(), -1); /// assert_eq!(Complex::new(i32::MAX, 0).log2(), -2); /// assert_eq!(Complex::new(1, 0).log2(), -63); @@ -86,7 +74,7 @@ impl FastInt for Complex { /// Example: /// /// ``` - /// use dsp::{Complex, FastInt}; + /// use dsp::{Complex, ComplexExt}; /// assert_eq!(Complex::new(1, 0).arg(), 0); /// assert_eq!(Complex::new(-i32::MAX, 1).arg(), i32::MAX); /// assert_eq!(Complex::new(-i32::MAX, -1).arg(), -i32::MAX); @@ -99,6 +87,7 @@ impl FastInt for Complex { } } +/// Full scale fixed point multiplication. pub trait MulScaled { fn mul_scaled(self, other: T) -> Self; } @@ -110,8 +99,8 @@ impl MulScaled> for Complex { let c = other.re as i64; let d = other.im as i64; Complex { - re: ((a * c - b * d + (1 << 31)) >> 32) as i32, - im: ((b * c + a * d + (1 << 31)) >> 32) as i32, + re: ((a * c - b * d + (1 << 30)) >> 31) as i32, + im: ((b * c + a * d + (1 << 30)) >> 31) as i32, } } } @@ -119,8 +108,8 @@ impl MulScaled> for Complex { impl MulScaled for Complex { fn mul_scaled(self, other: i32) -> Self { Complex { - re: ((other as i64 * self.re as i64 + (1 << 31)) >> 32) as i32, - im: ((other as i64 * self.im as i64 + (1 << 31)) >> 32) as i32, + re: ((other as i64 * self.re as i64 + (1 << 30)) >> 31) as i32, + im: ((other as i64 * self.im as i64 + (1 << 30)) >> 31) as i32, } } } @@ -128,8 +117,8 @@ impl MulScaled for Complex { impl MulScaled for Complex { fn mul_scaled(self, other: i16) -> Self { Complex { - re: (other as i32 * (self.re >> 16) + (1 << 15)) >> 16, - im: (other as i32 * (self.im >> 16) + (1 << 15)) >> 16, + re: (other as i32 * (self.re >> 16) + (1 << 14)) >> 15, + im: (other as i32 * (self.im >> 16) + (1 << 14)) >> 15, } } } diff --git a/dsp/src/lockin.rs b/dsp/src/lockin.rs index f0a2767..6a977b4 100644 --- a/dsp/src/lockin.rs +++ b/dsp/src/lockin.rs @@ -1,4 +1,4 @@ -use super::{Complex, FastInt, Lowpass, MulScaled}; +use super::{Complex, ComplexExt, Lowpass, MulScaled}; use generic_array::typenum::U2; #[derive(Clone, Default)] diff --git a/src/bin/lockin-external.rs b/src/bin/lockin-external.rs index b40037f..3d7ff0b 100644 --- a/src/bin/lockin-external.rs +++ b/src/bin/lockin-external.rs @@ -6,7 +6,7 @@ use stm32h7xx_hal as hal; use stabilizer::{hardware, hardware::design_parameters}; -use dsp::{Accu, Complex, FastInt, Lockin, RPLL}; +use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL}; use hardware::{ Adc0Input, Adc1Input, Dac0Output, Dac1Output, InputStamper, AFE0, AFE1, }; diff --git a/src/bin/lockin-internal.rs b/src/bin/lockin-internal.rs index 3539993..1f9df6e 100644 --- a/src/bin/lockin-internal.rs +++ b/src/bin/lockin-internal.rs @@ -2,7 +2,7 @@ #![no_std] #![no_main] -use dsp::{Accu, Complex, FastInt, Lockin}; +use dsp::{Accu, Complex, ComplexExt, Lockin}; use hardware::{Adc1Input, Dac0Output, Dac1Output, AFE0, AFE1}; use stabilizer::{hardware, hardware::design_parameters};