Trait basic_dsp::GenericVectorOperations
[−]
[src]
pub trait GenericVectorOperations<T>: DataVector<T> where T: RealNumber { fn add_vector(self, summand: &Self) -> VecResult<Self>; fn add_smaller_vector(self, summand: &Self) -> VecResult<Self>; fn subtract_vector(self, subtrahend: &Self) -> VecResult<Self>; fn subtract_smaller_vector(self, summand: &Self) -> VecResult<Self>; fn multiply_vector(self, factor: &Self) -> VecResult<Self>; fn multiply_smaller_vector(self, factor: &Self) -> VecResult<Self>; fn divide_vector(self, divisor: &Self) -> VecResult<Self>; fn divide_smaller_vector(self, divisor: &Self) -> VecResult<Self>; fn zero_pad(self, points: usize, option: PaddingOption) -> VecResult<Self>; fn reverse(self) -> VecResult<Self>; fn zero_interleave(self, factor: u32) -> VecResult<Self>; fn diff(self) -> VecResult<Self>; fn diff_with_start(self) -> VecResult<Self>; fn cum_sum(self) -> VecResult<Self>; fn sqrt(self) -> VecResult<Self>; fn square(self) -> VecResult<Self>; fn root(self, degree: T) -> VecResult<Self>; fn power(self, exponent: T) -> VecResult<Self>; fn logn(self) -> VecResult<Self>; fn expn(self) -> VecResult<Self>; fn log_base(self, base: T) -> VecResult<Self>; fn exp_base(self, base: T) -> VecResult<Self>; fn sin(self) -> VecResult<Self>; fn cos(self) -> VecResult<Self>; fn tan(self) -> VecResult<Self>; fn asin(self) -> VecResult<Self>; fn acos(self) -> VecResult<Self>; fn atan(self) -> VecResult<Self>; fn sinh(self) -> VecResult<Self>; fn cosh(self) -> VecResult<Self>; fn tanh(self) -> VecResult<Self>; fn asinh(self) -> VecResult<Self>; fn acosh(self) -> VecResult<Self>; fn atanh(self) -> VecResult<Self>; fn swap_halves(self) -> VecResult<Self>; fn split_into(&self, targets: &mut [Box<Self>]) -> VoidResult; fn merge(self, sources: &[Box<Self>]) -> VecResult<Self>; fn override_data(self, data: &[T]) -> VecResult<Self>; }
Defines all operations which are valid on all DataVectors
.
Required Methods
fn add_vector(self, summand: &Self) -> VecResult<Self>
Calculates the sum of self + summand
. It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
VectorsMustHaveTheSameSize
:self
andsummand
must have the same sizeVectorMetaDataMustAgree
:self
andsummand
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[1.0, 2.0]); let vector2 = RealTimeVector32::from_array(&[10.0, 11.0]); let result = vector1.add_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([11.0, 13.0], result.data());
fn add_smaller_vector(self, summand: &Self) -> VecResult<Self>
Calculates the sum of self + summand
. summand
may be smaller than self
as long
as self.len() % summand.len() == 0
. THe result is the same as it would be if
you would repeat summand
until it has the same length as self
.
It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
:self.points()
isn't dividable bysummand.points()
VectorMetaDataMustAgree
:self
andsummand
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[10.0, 11.0, 12.0, 13.0]); let vector2 = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector1.add_smaller_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([11.0, 13.0, 13.0, 15.0], result.data());
fn subtract_vector(self, subtrahend: &Self) -> VecResult<Self>
Calculates the difference of self - subtrahend
. It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
VectorsMustHaveTheSameSize
:self
andsubtrahend
must have the same sizeVectorMetaDataMustAgree
:self
andsubtrahend
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[1.0, 2.0]); let vector2 = RealTimeVector32::from_array(&[10.0, 11.0]); let result = vector1.subtract_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([-9.0, -9.0], result.data());
fn subtract_smaller_vector(self, summand: &Self) -> VecResult<Self>
Calculates the sum of self - subtrahend
. subtrahend
may be smaller than self
as long
as self.len() % subtrahend.len() == 0
. THe result is the same as it would be if
you would repeat subtrahend
until it has the same length as self
.
It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
:self.points()
isn't dividable bysubtrahend.points()
VectorMetaDataMustAgree
:self
andsubtrahend
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[10.0, 11.0, 12.0, 13.0]); let vector2 = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector1.subtract_smaller_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([9.0, 9.0, 11.0, 11.0], result.data());
fn multiply_vector(self, factor: &Self) -> VecResult<Self>
Calculates the product of self * factor
. It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
VectorsMustHaveTheSameSize
:self
andfactor
must have the same sizeVectorMetaDataMustAgree
:self
andfactor
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[1.0, 2.0]); let vector2 = RealTimeVector32::from_array(&[10.0, 11.0]); let result = vector1.multiply_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([10.0, 22.0], result.data());
fn multiply_smaller_vector(self, factor: &Self) -> VecResult<Self>
Calculates the sum of self - factor
. factor
may be smaller than self
as long
as self.len() % factor.len() == 0
. THe result is the same as it would be if
you would repeat factor
until it has the same length as self
.
It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
:self.points()
isn't dividable byfactor.points()
VectorMetaDataMustAgree
:self
andfactor
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[10.0, 11.0, 12.0, 13.0]); let vector2 = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector1.multiply_smaller_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([10.0, 22.0, 12.0, 26.0], result.data());
fn divide_vector(self, divisor: &Self) -> VecResult<Self>
Calculates the quotient of self / summand
. It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
VectorsMustHaveTheSameSize
:self
anddivisor
must have the same sizeVectorMetaDataMustAgree
:self
anddivisor
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[10.0, 22.0]); let vector2 = RealTimeVector32::from_array(&[2.0, 11.0]); let result = vector1.divide_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([5.0, 2.0], result.data());
fn divide_smaller_vector(self, divisor: &Self) -> VecResult<Self>
Calculates the sum of self - divisor
. divisor
may be smaller than self
as long
as self.len() % divisor.len() == 0
. THe result is the same as it would be if
you would repeat divisor
until it has the same length as self
.
It consumes self and returns the result.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
:self.points()
isn't dividable bydivisor.points()
VectorMetaDataMustAgree
:self
anddivisor
must be in the same domain and number space
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector1 = RealTimeVector32::from_array(&[10.0, 12.0, 12.0, 14.0]); let vector2 = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector1.divide_smaller_vector(&vector2).expect("Ignoring error handling in examples"); assert_eq!([10.0, 6.0, 12.0, 7.0], result.data());
fn zero_pad(self, points: usize, option: PaddingOption) -> VecResult<Self>
Appends zeros add the end of the vector until the vector has the size given in the points argument.
If points
smaller than the self.len()
then this operation won't do anything.
Note: Each point is two floating point numbers if the vector is complex.
Example
use basic_dsp::{PaddingOption, RealTimeVector32, ComplexTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector.zero_pad(4, PaddingOption::End).expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 0.0, 0.0], result.data()); let vector = ComplexTimeVector32::from_interleaved(&[1.0, 2.0]); let result = vector.zero_pad(2, PaddingOption::End).expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 0.0, 0.0], result.data());
fn reverse(self) -> VecResult<Self>
Reverses the data inside the vector.
fn zero_interleave(self, factor: u32) -> VecResult<Self>
Ineterleaves zeros factor - 1
times after every vector element, so that the resulting
vector will have a length of self.len() * factor
.
Note: Remember that each complex number consists of two floating points and interleaving will take that into account.
If factor is 0 (zero) then self
will be returned.
Example
use basic_dsp::{RealTimeVector32, ComplexTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0]); let result = vector.zero_interleave(2).expect("Ignoring error handling in examples"); assert_eq!([1.0, 0.0, 2.0, 0.0], result.data()); let vector = ComplexTimeVector32::from_interleaved(&[1.0, 2.0, 3.0, 4.0]); let result = vector.zero_interleave(2).expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 0.0, 0.0, 3.0, 4.0, 0.0, 0.0], result.data());
fn diff(self) -> VecResult<Self>
Calculates the delta of each elements to its previous element. This will decrease the vector length by one point.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[2.0, 3.0, 2.0, 6.0]); let result = vector.diff().expect("Ignoring error handling in examples"); assert_eq!([1.0, -1.0, 4.0], result.data());
fn diff_with_start(self) -> VecResult<Self>
Calculates the delta of each elements to its previous element. The first element will remain unchanged.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[2.0, 3.0, 2.0, 6.0]); let result = vector.diff_with_start().expect("Ignoring error handling in examples"); assert_eq!([2.0, 1.0, -1.0, 4.0], result.data());
fn cum_sum(self) -> VecResult<Self>
Calculates the cumulative sum of all elements. This operation undoes the diff_with_start
operation.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[2.0, 1.0, -1.0, 4.0]); let result = vector.cum_sum().expect("Ignoring error handling in examples"); assert_eq!([2.0, 3.0, 2.0, 6.0], result.data());
fn sqrt(self) -> VecResult<Self>
Gets the square root of all vector elements.
The sqrt of a negative number gives NaN and not a complex vector.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 4.0, 9.0, 16.0, 25.0]); let result = vector.sqrt().expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 3.0, 4.0, 5.0], result.data()); let vector = RealTimeVector32::from_array(&[-1.0]); let result = vector.sqrt().expect("Ignoring error handling in examples"); assert!(result[0].is_nan());
fn square(self) -> VecResult<Self>
Squares all vector elements.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0, 5.0]); let result = vector.square().expect("Ignoring error handling in examples"); assert_eq!([1.0, 4.0, 9.0, 16.0, 25.0], result.data());
fn root(self, degree: T) -> VecResult<Self>
Calculates the n-th root of every vector element.
If the result would be a complex number then the vector will contain a NaN instead. So the vector will never convert itself to a complex vector during this operation.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 8.0, 27.0]); let result = vector.root(3.0).expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 3.0], result.data());
fn power(self, exponent: T) -> VecResult<Self>
Raises every vector element to the given power.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0]); let result = vector.power(3.0).expect("Ignoring error handling in examples"); assert_eq!([1.0, 8.0, 27.0], result.data());
fn logn(self) -> VecResult<Self>
Calculates the natural logarithm to the base e for every vector element.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[2.718281828459045 , 7.389056, 20.085537]); let result = vector.logn().expect("Ignoring error handling in examples"); let actual = result.data(); let expected = &[1.0, 2.0, 3.0]; assert_eq!(actual.len(), expected.len()); for i in 0..actual.len() { assert!((actual[i] - expected[i]).abs() < 1e-4); }
fn expn(self) -> VecResult<Self>
Calculates the natural exponential to the base e for every vector element.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0]); let result = vector.expn().expect("Ignoring error handling in examples"); assert_eq!([2.71828182846, 7.389056, 20.085537], result.data());
fn log_base(self, base: T) -> VecResult<Self>
Calculates the logarithm to the given base for every vector element.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[10.0, 100.0, 1000.0]); let result = vector.log_base(10.0).expect("Ignoring error handling in examples"); assert_eq!([1.0, 2.0, 3.0], result.data());
fn exp_base(self, base: T) -> VecResult<Self>
Calculates the exponential to the given base for every vector element.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0]); let result = vector.exp_base(10.0).expect("Ignoring error handling in examples"); assert_eq!([10.0, 100.0, 1000.0], result.data());
fn sin(self) -> VecResult<Self>
Calculates the sine of each element in radians.
Example
use std::f32; use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[f32::consts::PI/2.0, -f32::consts::PI/2.0]); let result = vector.sin().expect("Ignoring error handling in examples"); assert_eq!([1.0, -1.0], result.data());
fn cos(self) -> VecResult<Self>
Calculates the cosine of each element in radians.
Example
use std::f32; use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[2.0 * f32::consts::PI, f32::consts::PI]); let result = vector.cos().expect("Ignoring error handling in examples"); assert_eq!([1.0, -1.0], result.data());
fn tan(self) -> VecResult<Self>
Calculates the tangent of each element in radians.
fn asin(self) -> VecResult<Self>
Calculates the principal value of the inverse sine of each element in radians.
fn acos(self) -> VecResult<Self>
Calculates the principal value of the inverse cosine of each element in radians.
fn atan(self) -> VecResult<Self>
Calculates the principal value of the inverse tangent of each element in radians.
fn sinh(self) -> VecResult<Self>
Calculates the hyperbolic sine each element in radians.
fn cosh(self) -> VecResult<Self>
Calculates the hyperbolic cosine each element in radians.
fn tanh(self) -> VecResult<Self>
Calculates the hyperbolic tangent each element in radians.
fn asinh(self) -> VecResult<Self>
Calculates the principal value of the inverse hyperbolic sine of each element in radians.
fn acosh(self) -> VecResult<Self>
Calculates the principal value of the inverse hyperbolic cosine of each element in radians.
fn atanh(self) -> VecResult<Self>
Calculates the principal value of the inverse hyperbolic tangent of each element in radians.
fn swap_halves(self) -> VecResult<Self>
This function swaps both halves of the vector. This operation is also called fft shift
Use it after a plain_fft
to get a spectrum which is centered at 0 Hz
.
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, 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.swap_halves().expect("Ignoring error handling in examples"); assert_eq!([5.0, 6.0, 7.0, 8.0, 1.0, 2.0, 3.0, 4.0], result.data());
fn split_into(&self, targets: &mut [Box<Self>]) -> VoidResult
Splits the vector into several smaller vectors. self.len()
must be dividable by
targets.len()
without a remainder and this conidition must be true too targets.len() > 0
.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
:self.points()
isn't dividable bytargets.len()
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]; let merge = RealTimeVector32::from_array(&a); let mut split = &mut [Box::new(RealTimeVector32::empty()), Box::new(RealTimeVector32::empty())]; merge.split_into(split).unwrap(); assert_eq!([1.0, 3.0, 5.0, 7.0, 9.0], split[0].data());
fn merge(self, sources: &[Box<Self>]) -> VecResult<Self>
Merges several vectors into self
. All vectors must have the same size and
at least one vector must be provided.
Failures
VecResult may report the following ErrorReason
members:
InvalidArgumentLength
: ifsources.len() == 0
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::empty(); let parts = &[ Box::new(RealTimeVector32::from_array(&[1.0, 2.0])), Box::new(RealTimeVector32::from_array(&[1.0, 2.0]))]; let merged = vector.merge(parts).expect("Ignoring error handling in examples"); assert_eq!([1.0, 1.0, 2.0, 2.0], merged.data());
fn override_data(self, data: &[T]) -> VecResult<Self>
Overrides the data in the vector with the given data. This may also change the vectors length (however not the allocated length).
Example
use basic_dsp::{RealTimeVector32, GenericVectorOperations, DataVector}; let vector = RealTimeVector32::from_array(&[1.0, 2.0, 3.0, 4.0]); let result = vector.override_data(&[5.0, 7.0]).expect("Ignoring error handling in examples"); assert_eq!(&[5.0, 7.0], result.data());
Implementors
impl GenericVectorOperations<f32> for GenericDataVector<f32>
impl GenericVectorOperations<f64> for GenericDataVector<f64>
impl GenericVectorOperations<f32> for RealTimeVector<f32>
impl GenericVectorOperations<f64> for RealTimeVector<f64>
impl GenericVectorOperations<f32> for RealFreqVector<f32>
impl GenericVectorOperations<f64> for RealFreqVector<f64>
impl GenericVectorOperations<f32> for ComplexTimeVector<f32>
impl GenericVectorOperations<f64> for ComplexTimeVector<f64>
impl GenericVectorOperations<f32> for ComplexFreqVector<f32>
impl GenericVectorOperations<f64> for ComplexFreqVector<f64>