Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rj/saturating complex #280

Merged
merged 2 commits into from
Feb 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion dsp/src/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub trait ComplexExt<T, U> {
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<i32, u32> for Complex<i32> {
Expand All @@ -23,7 +25,7 @@ impl ComplexExt<i32, u32> for Complex<i32> {
/// ```
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).
Expand Down Expand Up @@ -85,6 +87,20 @@ impl ComplexExt<i32, u32> for Complex<i32> {
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.
Expand Down