pub struct Signal { /* private fields */ }Expand description
A complex baseband waveform, [n_streams, n_samples], with its sample rate.
Implementations§
Source§impl Signal
impl Signal
Sourcepub fn new(data: Array2<C64>, sample_rate: Hertz) -> Signal
pub fn new(data: Array2<C64>, sample_rate: Hertz) -> Signal
A waveform from its samples ([n_streams, n_samples]) and its sample rate.
Sourcepub fn zeros(n_streams: usize, n_samples: usize, sample_rate: Hertz) -> Signal
pub fn zeros(n_streams: usize, n_samples: usize, sample_rate: Hertz) -> Signal
n_streams streams of n_samples zeros.
Sourcepub fn from_streams(streams: &[Vec<C64>], sample_rate: Hertz) -> Result<Signal>
pub fn from_streams(streams: &[Vec<C64>], sample_rate: Hertz) -> Result<Signal>
A waveform from several streams of equal length.
§Errors
Error::Shape if the streams have different lengths.
Sourcepub fn sample_rate(&self) -> Hertz
pub fn sample_rate(&self) -> Hertz
Sample rate.
Sourcepub fn stream_view(&self, i: usize) -> ArrayView1<'_, C64>
pub fn stream_view(&self, i: usize) -> ArrayView1<'_, C64>
Stream i as an ndarray view.
Sourcepub fn stream_mut(&mut self, i: usize) -> &mut [C64] ⓘ
pub fn stream_mut(&mut self, i: usize) -> &mut [C64] ⓘ
Stream i as a mutable slice.
Sourcepub fn to_streams(&self) -> Vec<Vec<C64>>
pub fn to_streams(&self) -> Vec<Vec<C64>>
The streams, one vector each (the inverse of Signal::from_streams).
§Examples
use comnum_core::signal::Signal;
use comnum_core::units::Hertz;
use num_complex::Complex64 as C64;
let streams = vec![vec![C64::new(1.0, 0.0); 3], vec![C64::new(0.0, 1.0); 3]];
let x = Signal::from_streams(&streams, Hertz(1.0))?;
assert_eq!(x.to_streams(), streams);Sourcepub fn as_array_mut(&mut self) -> &mut Array2<C64>
pub fn as_array_mut(&mut self) -> &mut Array2<C64>
The samples, mutable.
Sourcepub fn resize(&mut self, n_streams: usize, n_samples: usize, sample_rate: Hertz)
pub fn resize(&mut self, n_streams: usize, n_samples: usize, sample_rate: Hertz)
Resize to n_streams × n_samples at sample_rate (the content is reset when the
shape changes), for the _into methods.
Sourcepub fn set_stream(&mut self, x: &[C64], sample_rate: Hertz)
pub fn set_stream(&mut self, x: &[C64], sample_rate: Hertz)
Make self the single stream x at sample_rate, reusing its storage when the
length is unchanged (ADR 0011).
Sourcepub fn assign(&mut self, other: &Signal)
pub fn assign(&mut self, other: &Signal)
Make self a copy of other, reusing its storage when the shape is unchanged
(ADR 0011).
Sourcepub fn set_streams(
&mut self,
streams: &[Vec<C64>],
sample_rate: Hertz,
) -> Result<()>
pub fn set_streams( &mut self, streams: &[Vec<C64>], sample_rate: Hertz, ) -> Result<()>
Make self the streams streams at sample_rate, reusing its storage when the
shape is unchanged (ADR 0011).
§Errors
Error::Shape if the streams have different lengths (self is then left
unchanged).
Sourcepub fn try_map_streams(
&self,
out: &mut Signal,
f: impl FnMut(usize, &[C64]) -> Result<Vec<C64>>,
) -> Result<()>
pub fn try_map_streams( &self, out: &mut Signal, f: impl FnMut(usize, &[C64]) -> Result<Vec<C64>>, ) -> Result<()>
Fill out with f(stream index, stream) for every stream of self, at the
same shape and sampling rate: the body of the _into methods of the blocks that
process the streams independently.
§Errors
The first error of f; Error::Shape if f returns another number of
samples than it was given, rather than leaving the samples of an earlier use of
out in place.
Sourcepub fn map_streams(
&self,
out: &mut Signal,
f: impl FnMut(usize, &[C64]) -> Vec<C64>,
)
pub fn map_streams( &self, out: &mut Signal, f: impl FnMut(usize, &[C64]) -> Vec<C64>, )
Signal::try_map_streams for an f that cannot fail.
§Panics
If f returns another number of samples than it was given (a broken invariant
of the calling block).
Sourcepub fn map_streams_in_place(
&self,
out: &mut Signal,
f: impl FnMut(usize, &mut [C64]),
)
pub fn map_streams_in_place( &self, out: &mut Signal, f: impl FnMut(usize, &mut [C64]), )
Make out a copy of self (its storage reused when the shape is unchanged, ADR
0011), then apply f(stream index, stream) to every stream of out in place: the
body of the _into methods of the blocks that transform the samples where they
are, with no allocation per stream.
§Examples
use comnum_core::signal::Signal;
use comnum_core::units::Hertz;
use num_complex::Complex64 as C64;
let x = Signal::from_streams(&[vec![C64::new(1.0, 0.0); 3], vec![C64::new(0.0, 1.0); 3]], Hertz(1.0))?;
let mut out = Signal::zeros(0, 0, Hertz(1.0));
x.map_streams_in_place(&mut out, |s, y| y.iter_mut().for_each(|v| *v *= (s + 1) as f64));
assert_eq!(out.stream(1), &[C64::new(0.0, 2.0); 3]);