Trait basic_dsp::RealVectorOps [] [src]

pub trait RealVectorOps<T>: DataVector<T> where T: RealNumber {
    type ComplexPartner;
    fn real_offset(self, factor: T) -> VecResult<Self>;
    fn real_scale(self, offset: T) -> VecResult<Self>;
    fn abs(self) -> VecResult<Self>;
    fn to_complex(self) -> VecResult<Self::ComplexPartner>;
    fn wrap(self, divisor: T) -> VecResult<Self>;
    fn unwrap(self, divisor: T) -> VecResult<Self>;
    fn real_dot_product(&self, factor: &Self) -> ScalarResult<T>;
    fn real_statistics(&self) -> Statistics<T>;
    fn real_statistics_splitted(&self, len: usize) -> Vec<Statistics<T>>;
    fn real_sum(&self) -> T;
    fn real_sum_sq(&self) -> T;
    fn map_inplace_real<A, F>(self, argument: A, map: F) -> VecResult<Self> where A: Sync + Copy + Send, F: Fn(T, usize, A) -> T + 'static + Sync;
    fn map_aggregate_real<A, FMap, FAggr, R>(&self, argument: A, map: FMap, aggregate: FAggr) -> ScalarResult<R> where A: Sync + Copy + Send, FMap: Fn(T, usize, A) -> R + 'static + Sync, FAggr: Fn(R, R) -> R + 'static + Sync + Send, R: Send;
}

Defines all operations which are valid on DataVectors containing real data.

Failures

All operations in this trait fail with VectorMustBeReal if the vector isn't in the real number space.

Associated Types

Required Methods

fn real_offset(self, factor: T) -> VecResult<Self>

Adds a scalar to the vector. See also OffsetOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, 2.0]);
let result = vector.real_offset(2.0).expect("Ignoring error handling in examples");
assert_eq!([3.0, 4.0], result.data());

fn real_scale(self, offset: T) -> VecResult<Self>

Multiplies the vector with a scalar. See also ScaleOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, 2.0]);
let result = vector.real_scale(4.0).expect("Ignoring error handling in examples");
assert_eq!([4.0, 8.0], result.data());

fn abs(self) -> VecResult<Self>

Gets the absolute value of all vector elements.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, -2.0]);
let result = vector.abs().expect("Ignoring error handling in examples");
assert_eq!([1.0, 2.0], result.data());

fn to_complex(self) -> VecResult<Self::ComplexPartner>

Converts the real vector into a complex vector.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, 2.0]);
let result = vector.to_complex().expect("Ignoring error handling in examples");
assert_eq!([1.0, 0.0, 2.0, 0.0], result.data());

fn wrap(self, divisor: T) -> VecResult<Self>

Each value in the vector is dividable by the divisor and the remainder is stored in the resulting vector. This the same a modulo operation or to phase wrapping.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
let result = vector.wrap(4.0).expect("Ignoring error handling in examples");
assert_eq!([1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0], result.data());

fn unwrap(self, divisor: T) -> VecResult<Self>

This function corrects the jumps in the given vector which occur due to wrap or modulo operations. This will undo a wrap operation only if the deltas are smaller than half the divisor.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps, DataVector};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0, 0.0]);
let result = vector.unwrap(4.0).expect("Ignoring error handling in examples");
assert_eq!([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], result.data());

fn real_dot_product(&self, factor: &Self) -> ScalarResult<T>

Calculates the dot product of self and factor. Self and factor remain unchanged. See also DotProductOps.

Failures

VecResult may report the following ErrorReason members:

  1. VectorMetaDataMustAgree: self and factor must be in the same domain and number space

Example

use basic_dsp::{RealTimeVector32, RealVectorOps};
let vector1 = RealTimeVector32::from_array(&[9.0, 2.0, 7.0]);
let vector2 = RealTimeVector32::from_array(&[4.0, 8.0, 10.0]);
let result = vector1.real_dot_product(&vector2).expect("Ignoring error handling in examples");
assert_eq!(122.0, result);

fn real_statistics(&self) -> Statistics<T>

Calculates the statistics of the data contained in the vector. See also StatisticsOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let result = vector.real_statistics();
assert_eq!(result.sum, 15.0);
assert_eq!(result.count, 5);
assert_eq!(result.average, 3.0);
assert!((result.rms - 3.3166).abs() < 1e-4);
assert_eq!(result.min, 1.0);
assert_eq!(result.min_index, 0);
assert_eq!(result.max, 5.0);
assert_eq!(result.max_index, 4);

fn real_statistics_splitted(&self, len: usize) -> Vec<Statistics<T>>

Calculates the statistics of the data contained in the vector as if the vector would have been split into len pieces. self.len should be dividable by len without a remainder, but this isn't enforced by the implementation. See also StatisticsOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0]);
let result = vector.real_statistics_splitted(2);
assert_eq!(result[0].sum, 4.0);
assert_eq!(result[1].sum, 6.0);

fn real_sum(&self) -> T

Calculates the sum of the data contained in the vector. See also StatisticsOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let result = vector.real_sum();
assert_eq!(result, 15.0);

fn real_sum_sq(&self) -> T

Calculates the sum of the data contained in the vector. See also StatisticsOps.

Example

use basic_dsp::{RealTimeVector32, RealVectorOps};
let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0, 5.0]);
let result = vector.real_sum_sq();
assert_eq!(result, 55.0);

fn map_inplace_real<A, F>(self, argument: A, map: F) -> VecResult<Self> where A: Sync + Copy + Send, F: Fn(T, usize, A) -> T + 'static + Sync

Transforms all vector elements using the function map.

Example

use basic_dsp::*;
let a = [1.0, 2.0, 3.0, 4.0, 5.0];
let c = RealTimeVector32::from_array(&a);
let r = c.map_inplace_real((), |v, i, _|v * i as f32).expect("Ignoring error handling in examples");
let expected = [0.0, 2.0, 6.0, 12.0, 20.0];
assert_eq!(r.data(), &expected);

fn map_aggregate_real<A, FMap, FAggr, R>(&self, argument: A, map: FMap, aggregate: FAggr) -> ScalarResult<R> where A: Sync + Copy + Send, FMap: Fn(T, usize, A) -> R + 'static + Sync, FAggr: Fn(R, R) -> R + 'static + Sync + Send, R: Send

Transforms all vector elements using the function map and then aggregates all the results with aggregate. aggregate must be a commutativity and associativity; that's because there is no guarantee that the numbers will be aggregated in any deterministic order.

Example

use basic_dsp::*;
let a = [1.0, 2.0, 3.0, 4.0, 5.0];
let c = RealTimeVector32::from_array(&a);
let r = c.map_aggregate_real(
     (), 
     |v, i, _|v as usize * i, 
     |a,b|a+b).expect("Ignoring error handling in examples");
assert_eq!(r, 40);

Implementors