to_samples()¶
- auglib.utils.to_samples(value, *, sampling_rate=None, length=None, unit='seconds', allow_negative=False)[source]¶
Express timestamp or timespan in samples.
If
unitis set to'samples', no argument must be given. In case of'relative', a value forlengthhas to be provided. In any other case, a value forsampling_rateis required andunitmust be supported bypandas.to_timedelta().Examples for a
sampling_rateof 8000, highlighting the influence ofunit:value
unit
length
result (in samples)
1.0
‘seconds’
8000
8000
‘samples’
8000
2/3600
‘hour’
16000
500
‘ms’
4000
0.5
‘s’
4000
0.25
‘relative’
8000
2000
- Parameters
value (
typing.Union[int,float,auglib.core.observe.Base,auglib.core.time.Time]) – timestamp or timespansampling_rate (
typing.Optional[int]) – sampling rate in Hzlength (
typing.Optional[int]) – reference point if unit isrelative(in number of samples)unit (
str) – literal specifying the format (ignored ifvaluehas typeauglib.Time)allow_negative (
bool) – allow negative values
- Return type
- Returns
number of samples
- Raises
ValueError – if
allow_negativeisFalseand computed value is negativeValueError – if
unitis not supportedValueError – if
lengthis not provided, butunitis'samples'ValueError – if
sampling_rateis not provided, butunitis not'samples'or'relative'
Examples
>>> to_samples(0.5, sampling_rate=10) 5 >>> to_samples(0.5, length=20, unit="relative") 10 >>> to_samples(time.Time(1500, unit="ms"), sampling_rate=10) 15