Resample

class baseband_tasks.sampling.Resample(ih, offset, whence='start', *, pad=64, samples_per_frame=None)[source] [edit on github]

Bases: baseband_tasks.sampling.ShiftAndResample

Resample a stream such that a sample occurs at the given offset.

The offset pointer is left at the requested time, so one can think of this task as a precise version of the seek() method.

Generally, the stream start time will change, by up to one sample, and the stream length reduced by one frame. The precision with which the resampling is done depends on pad.

Parameters
ihtask or baseband stream reader

Input data stream, with time as the first axis.

offsetfloat, Quantity, or Time

Offset to ensure the output stream includes. Can an absolute time, or a (float) number of samples or time offset relative to the start of the underlying stream.

whence{0, 1, 2, ‘start’, ‘current’, or ‘end’}, optional

Like regular seek, the offset is taken to be from the start if whence=0 (default), from the current position if 1, and from the end if 2. One can alternativey use ‘start’, ‘current’, or ‘end’ for 0, 1, or 2, respectively. Ignored if offset is a time.

padint, optional

Padding to apply on each side when shifting data. This sets the size of the sinc function which which the data is convolved (see above). The default of 64 ensures an accuracy of better than 0.1%.

samples_per_frameint, optional

Number of resampled samples which should be produced in one go. The number of input samples used will be larger by 2*pad+1. If not given, works on the larger of the samples per frame from If not given, the larger of the sampler per frame in the underlying stream or 14 times the padding (to ensure ~87.5% efficiency).

See also

ShiftAndResample

also shift a stream, possibly including phase delay

Examples

Suppose one wanted to read the 8 samples surrounding a precise time:

>>> from baseband_tasks.sampling import Resample
>>> from astropy.time import Time
>>> from baseband import data, vdif
>>> fh = vdif.open(data.SAMPLE_VDIF)
>>> texact = Time('2014-06-16T05:56:07.000123456')
>>> ((texact - fh.start_time) * fh.sample_rate).to(1)
... 
<Quantity 3950.59201992>
>>> rh = Resample(fh, texact)
>>> rh.time.isot
'2014-06-16T05:56:07.000123456'
>>> rh.seek(-4, 1)
3883
>>> data = rh.read(8)
>>> data[:, 4]  
array([ 3.8278387 ,  2.0259624 , -0.3738842 , -1.2480919 , -0.04606577,
        2.6100893 ,  3.4867156 ,  3.2312815 ], dtype=float32)

For comparison, if one uses the underlying filehandle directly, one gets the data only at the approximate time:

>>> fh.seek(texact)
3951
>>> fh.time.isot
'2014-06-16T05:56:07.000123469'
>>> fh.seek(-4, 1)
3947
>>> data = fh.read(8)
>>> data[:, 4]  
array([ 3.316505,  1.      , -1.      , -1.      ,  1.      ,  3.316505,
        3.316505,  3.316505], dtype=float32)
>>> fh.close()