2021-02-18 20:17:24 +08:00
|
|
|
pub use num::Complex;
|
2021-02-12 19:03:53 +08:00
|
|
|
|
2021-01-31 01:05:54 +08:00
|
|
|
use super::{atan2, cossin};
|
2021-01-21 23:12:59 +08:00
|
|
|
|
2021-02-19 16:29:38 +08:00
|
|
|
/// Complex extension trait offering DSP (fast, good accuracy) functionality.
|
|
|
|
pub trait ComplexExt<T, U> {
|
2021-02-18 20:17:24 +08:00
|
|
|
fn from_angle(angle: T) -> Self;
|
|
|
|
fn abs_sqr(&self) -> U;
|
|
|
|
fn log2(&self) -> T;
|
|
|
|
fn arg(&self) -> T;
|
2021-02-22 23:36:56 +08:00
|
|
|
fn saturating_add(&self, other: Self) -> Self;
|
|
|
|
fn saturating_sub(&self, other: Self) -> Self;
|
2021-02-18 20:17:24 +08:00
|
|
|
}
|
|
|
|
|
2021-02-19 16:29:38 +08:00
|
|
|
impl ComplexExt<i32, u32> for Complex<i32> {
|
2021-01-31 01:05:54 +08:00
|
|
|
/// Return a Complex on the unit circle given an angle.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-19 16:29:38 +08:00
|
|
|
/// use dsp::{Complex, ComplexExt};
|
2021-01-31 01:05:54 +08:00
|
|
|
/// Complex::<i32>::from_angle(0);
|
|
|
|
/// Complex::<i32>::from_angle(1 << 30); // pi/2
|
|
|
|
/// Complex::<i32>::from_angle(-1 << 30); // -pi/2
|
|
|
|
/// ```
|
2021-02-18 20:17:24 +08:00
|
|
|
fn from_angle(angle: i32) -> Self {
|
2021-02-01 19:07:03 +08:00
|
|
|
let (c, s) = cossin(angle);
|
2021-02-22 23:36:56 +08:00
|
|
|
Self::new(c, s)
|
2021-01-31 01:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the absolute square (the squared magnitude).
|
|
|
|
///
|
2021-02-12 01:14:28 +08:00
|
|
|
/// Note: Normalization is `1 << 32`, i.e. U0.32.
|
|
|
|
///
|
|
|
|
/// Note(panic): This will panic for `Complex(i32::MIN, i32::MIN)`
|
2021-01-31 01:05:54 +08:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-19 16:29:38 +08:00
|
|
|
/// use dsp::{Complex, ComplexExt};
|
2021-02-18 20:17:24 +08:00
|
|
|
/// assert_eq!(Complex::new(i32::MIN, 0).abs_sqr(), 1 << 31);
|
|
|
|
/// assert_eq!(Complex::new(i32::MAX, i32::MAX).abs_sqr(), u32::MAX - 3);
|
2021-01-31 01:05:54 +08:00
|
|
|
/// ```
|
2021-02-18 20:17:24 +08:00
|
|
|
fn abs_sqr(&self) -> u32 {
|
|
|
|
(((self.re as i64) * (self.re as i64)
|
|
|
|
+ (self.im as i64) * (self.im as i64))
|
2021-02-12 01:14:28 +08:00
|
|
|
>> 31) as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// log2(power) re full scale approximation
|
|
|
|
///
|
|
|
|
/// TODO: scale up, interpolate
|
|
|
|
///
|
|
|
|
/// Panic:
|
|
|
|
/// This will panic for `Complex(i32::MIN, i32::MIN)`
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-19 16:29:38 +08:00
|
|
|
/// use dsp::{Complex, ComplexExt};
|
2021-02-18 20:17:24 +08:00
|
|
|
/// 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);
|
|
|
|
/// assert_eq!(Complex::new(0, 0).log2(), -64);
|
2021-02-12 01:14:28 +08:00
|
|
|
/// ```
|
2021-02-18 20:17:24 +08:00
|
|
|
fn log2(&self) -> i32 {
|
|
|
|
let a = (self.re as i64) * (self.re as i64)
|
|
|
|
+ (self.im as i64) * (self.im as i64);
|
2021-02-12 01:14:28 +08:00
|
|
|
-(a.leading_zeros() as i32)
|
2021-01-21 23:12:59 +08:00
|
|
|
}
|
|
|
|
|
2021-01-31 01:05:54 +08:00
|
|
|
/// Return the angle.
|
|
|
|
///
|
|
|
|
/// Note: Normalization is `1 << 31 == pi`.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
2021-02-19 16:29:38 +08:00
|
|
|
/// use dsp::{Complex, ComplexExt};
|
2021-02-18 20:17:24 +08:00
|
|
|
/// 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);
|
|
|
|
/// assert_eq!(Complex::new(0, -1).arg(), -i32::MAX >> 1);
|
|
|
|
/// assert_eq!(Complex::new(0, 1).arg(), (i32::MAX >> 1) + 1);
|
|
|
|
/// assert_eq!(Complex::new(1, 1).arg(), (i32::MAX >> 2) + 1);
|
2021-01-31 01:05:54 +08:00
|
|
|
/// ```
|
2021-02-18 20:17:24 +08:00
|
|
|
fn arg(&self) -> i32 {
|
|
|
|
atan2(self.im, self.re)
|
2021-01-21 23:12:59 +08:00
|
|
|
}
|
2021-02-22 23:36:56 +08:00
|
|
|
|
|
|
|
fn saturating_add(&self, other: Self) -> Self {
|
2021-02-22 23:40:51 +08:00
|
|
|
Self::new(
|
|
|
|
self.re.saturating_add(other.re),
|
|
|
|
self.im.saturating_add(other.im),
|
|
|
|
)
|
2021-02-22 23:36:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn saturating_sub(&self, other: Self) -> Self {
|
2021-02-22 23:40:51 +08:00
|
|
|
Self::new(
|
|
|
|
self.re.saturating_sub(other.re),
|
|
|
|
self.im.saturating_sub(other.im),
|
|
|
|
)
|
2021-02-22 23:36:56 +08:00
|
|
|
}
|
2021-01-21 23:12:59 +08:00
|
|
|
}
|
2021-02-12 19:03:53 +08:00
|
|
|
|
2021-02-19 16:29:38 +08:00
|
|
|
/// Full scale fixed point multiplication.
|
2021-02-18 20:17:24 +08:00
|
|
|
pub trait MulScaled<T> {
|
|
|
|
fn mul_scaled(self, other: T) -> Self;
|
|
|
|
}
|
2021-02-12 19:03:53 +08:00
|
|
|
|
2021-02-18 20:17:24 +08:00
|
|
|
impl MulScaled<Complex<i32>> for Complex<i32> {
|
|
|
|
fn mul_scaled(self, other: Self) -> Self {
|
|
|
|
let a = self.re as i64;
|
|
|
|
let b = self.im as i64;
|
|
|
|
let c = other.re as i64;
|
|
|
|
let d = other.im as i64;
|
|
|
|
Complex {
|
2021-02-19 16:29:38 +08:00
|
|
|
re: ((a * c - b * d + (1 << 30)) >> 31) as i32,
|
|
|
|
im: ((b * c + a * d + (1 << 30)) >> 31) as i32,
|
2021-02-18 20:17:24 +08:00
|
|
|
}
|
2021-02-12 19:03:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:17:24 +08:00
|
|
|
impl MulScaled<i32> for Complex<i32> {
|
|
|
|
fn mul_scaled(self, other: i32) -> Self {
|
|
|
|
Complex {
|
2021-02-19 16:29:38 +08:00
|
|
|
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,
|
2021-02-18 20:17:24 +08:00
|
|
|
}
|
2021-02-12 19:03:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:17:24 +08:00
|
|
|
impl MulScaled<i16> for Complex<i32> {
|
|
|
|
fn mul_scaled(self, other: i16) -> Self {
|
|
|
|
Complex {
|
2021-02-19 16:29:38 +08:00
|
|
|
re: (other as i32 * (self.re >> 16) + (1 << 14)) >> 15,
|
|
|
|
im: (other as i32 * (self.im >> 16) + (1 << 14)) >> 15,
|
2021-02-18 20:17:24 +08:00
|
|
|
}
|
2021-02-12 19:03:53 +08:00
|
|
|
}
|
|
|
|
}
|