Trait basic_dsp_vector::ApproximatedOps

source ·
pub trait ApproximatedOps<T>
where T: RealNumber,
{ // Required methods fn ln_approx(&mut self); fn exp_approx(&mut self); fn sin_approx(&mut self); fn cos_approx(&mut self); fn log_approx(&mut self, base: T); fn expf_approx(&mut self, base: T); fn powf_approx(&mut self, exponent: T); }
Expand description

Recommended to be only used with the CPU feature flags sse or avx.

This trait provides alternative implementations for some standard functions which are less accurate but perform faster. Those approximations are written for SSE2 or AVX2 processors. If a processor supports neither of those instruction sets, then the standard functions will be used.

Information on the error of the approximation and their performance are rough numbers. A detailed table can be obtained by running the approx_accuracy example.

§Failures

If one of the methods is called on complex data then self.len() will be set to 0. To avoid this it’s recommended to use the to_real_time_vec, to_real_freq_vec to_complex_time_vec and to_complex_freq_vec constructor methods since the resulting types will already check at compile time (using the type system) that the data is real.

Required Methods§

source

fn ln_approx(&mut self)

Computes the principal value approximation of natural logarithm of every element in the vector.

Error should be below 1% as long as the values in the vector are larger than 1. Single core performance should be about 5x as fast.

§Example
use basic_dsp_vector::*;
let mut vector = vec!(2.718281828459045, 7.389056, 20.085537).to_real_time_vec();
vector.ln_approx();
let actual = &vector[0..];
let expected = &[1.0, 2.0, 3.0];
assert_eq!(actual.len(), expected.len());
for i in 0..actual.len() {
       assert!(f64::abs(actual[i] - expected[i]) < 1e-2);
}
source

fn exp_approx(&mut self)

Calculates the natural exponential approximation for every vector element.

Error should be less than 1%`` as long as the values in the vector are small (e.g. in the range between -10 and 10). Single core performance should be about 50%` faster.

§Example
use basic_dsp_vector::*;
let mut vector = vec!(1.0, 2.0, 3.0).to_real_time_vec();
vector.exp_approx();
let actual = &vector[0..];
let expected = &[2.718281828459045, 7.389056, 20.085537];
assert_eq!(actual.len(), expected.len());
for i in 0..actual.len() {
       assert!(f64::abs(actual[i] - expected[i]) < 1e-4);
}
source

fn sin_approx(&mut self)

Calculates the sine approximation of each element in radians.

Error should be below 1E-6. Single core performance should be about 2x as fast.

§Example
use std::f32;
use basic_dsp_vector::*;
let mut vector = vec!(f32::consts::PI/2.0, -f32::consts::PI/2.0).to_real_time_vec();
vector.sin_approx();
assert_eq!([1.0, -1.0], vector[..]);
source

fn cos_approx(&mut self)

Calculates the cosine approximation of each element in radians

Error should be below 1E-6. Single core performance should be about 2x as fast.

§Example
use std::f32;
use basic_dsp_vector::*;
let mut vector = vec!(2.0 * f32::consts::PI, f32::consts::PI).to_real_time_vec();
vector.cos_approx();
assert_eq!([1.0, -1.0], vector[..]);
source

fn log_approx(&mut self, base: T)

Calculates the approximated logarithm to the given base for every vector element.

Error should be below 1% as long as the values in the vector are larger than 1. Single core performance should be about 5x as fast.

§Example
use basic_dsp_vector::*;
let mut vector = vec!(10.0, 100.0, 1000.0).to_real_time_vec();
vector.log_approx(10.0);
let actual = &vector[0..];
let expected = &[1.0, 2.0, 3.0];
assert_eq!(actual.len(), expected.len());
for i in 0..actual.len() {
       assert!(f64::abs(actual[i] - expected[i]) < 1e-4);
}
source

fn expf_approx(&mut self, base: T)

Calculates the approximated exponential to the given base for every vector element.

Error should be less than 1%`` as long as the values in the vector are small (e.g. in the range between -10 and 10). Single core performance should be about 5x` as fast.

§Example
use basic_dsp_vector::*;
use std::f32;
let vector: Vec<f32> = vec!(1.0, 2.0, 3.0);
let mut vector = vector.to_real_time_vec();
vector.expf_approx(10.0);
assert!((vector[0] - 10.0).abs() < 1e-3);
assert!((vector[1] - 100.0).abs() < 1e-3);
assert!((vector[2] - 1000.0).abs() < 1e-3);
source

fn powf_approx(&mut self, exponent: T)

Raises every vector element to approximately a floating point power.

Error should be less than 1%`` as long as the values in the vector are really small (e.g. in the range between -0.1 and 0.1). Single core performance should be about 5x` as fast.

§Example
use basic_dsp_vector::*;
use std::f32;
let vector: Vec<f32> = vec!(1.0, 2.0, 3.0);
let mut vector = vector.to_real_time_vec();
vector.powf_approx(3.0);
assert!((vector[0] - 1.0).abs() < 1e-3);
assert!((vector[1] - 8.0).abs() < 1e-3);
assert!((vector[2] - 27.0).abs() < 1e-3);

Implementors§

source§

impl<S, T, N, D> ApproximatedOps<T> for DspVec<S, T, N, D>
where S: ToSliceMut<T>, T: RealNumber, N: RealNumberSpace, D: Domain,