From f671e0c94252568fb00cfc709ac5dfa81e43ffa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 22 Feb 2021 16:36:56 +0100 Subject: [PATCH 1/2] complex: add saturating_add/sub --- dsp/src/complex.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/dsp/src/complex.rs b/dsp/src/complex.rs index 3349460..dc36349 100644 --- a/dsp/src/complex.rs +++ b/dsp/src/complex.rs @@ -8,6 +8,8 @@ pub trait ComplexExt { fn abs_sqr(&self) -> U; fn log2(&self) -> T; fn arg(&self) -> T; + fn saturating_add(&self, other: Self) -> Self; + fn saturating_sub(&self, other: Self) -> Self; } impl ComplexExt for Complex { @@ -23,7 +25,7 @@ impl ComplexExt for Complex { /// ``` fn from_angle(angle: i32) -> Self { let (c, s) = cossin(angle); - Self { re: c, im: s } + Self::new(c, s) } /// Return the absolute square (the squared magnitude). @@ -85,6 +87,15 @@ impl ComplexExt for Complex { fn arg(&self) -> i32 { atan2(self.im, self.re) } + + fn saturating_add(&self, other: Self) -> Self { + Self::new(self.re.saturating_add(other.re), self.im.saturating_add(other.im)) + } + + fn saturating_sub(&self, other: Self) -> Self { + Self::new(self.re.saturating_sub(other.re), self.im.saturating_sub(other.im)) + } + } /// Full scale fixed point multiplication. From ffdf4026fbdbf9a899aec7025b5710542ced6870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 22 Feb 2021 16:40:51 +0100 Subject: [PATCH 2/2] fmt --- dsp/src/complex.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/dsp/src/complex.rs b/dsp/src/complex.rs index dc36349..1eb003f 100644 --- a/dsp/src/complex.rs +++ b/dsp/src/complex.rs @@ -89,13 +89,18 @@ impl ComplexExt for Complex { } fn saturating_add(&self, other: Self) -> Self { - Self::new(self.re.saturating_add(other.re), self.im.saturating_add(other.im)) + Self::new( + self.re.saturating_add(other.re), + self.im.saturating_add(other.im), + ) } fn saturating_sub(&self, other: Self) -> Self { - Self::new(self.re.saturating_sub(other.re), self.im.saturating_sub(other.im)) + Self::new( + self.re.saturating_sub(other.re), + self.im.saturating_sub(other.im), + ) } - } /// Full scale fixed point multiplication.