TimeSamples#
- class acoular.sources.TimeSamples
Bases:
SamplesGeneratorContainer for processing time data in
*.h5or NumPy array format.The
TimeSamplesclass provides functionality for loading, managing, and accessing time-domain data stored in HDF5 files or directly provided as a NumPy array. This data can be accessed iteratively through theresult()method, which returns chunks of the time data for further processing.See also
MaskedTimeSamplesExtends the functionality of class
TimeSamplesby enabling the definition of start and stop samples as well as the specification of invalid channels.
Notes
Examples
Data can be loaded from a HDF5 file as follows:
>>> from acoular import TimeSamples >>> file = <some_h5_file.h5> >>> ts = TimeSamples(file=file) >>> print(f'number of channels: {ts.num_channels}') number of channels: 56
Alternatively, the time data can be specified directly as a NumPy array. In this case, the
dataandsample_freqattributes must be set manually.>>> import numpy as np >>> data = np.random.rand(1000, 4) >>> ts = TimeSamples(data=data, sample_freq=51200)
Chunks of the time data can be accessed iteratively via the
result()generator. The last block will be shorter than the block size if the number of samples is not a multiple of the block size.>>> blocksize = 512 >>> generator = ts.result(num=blocksize) >>> for block in generator: ... print(block.shape) (512, 4) (488, 4)
- file = Union(None, File(filter=['*.h5'], exists=True), desc='name of data file')
Full path to the
.h5file containing time-domain data.
- basename = Property(depends_on=['file'], desc='basename of data file')
Basename of the
.h5file, set automatically from thefileattribute.
- num_channels = CInt(0, desc='number of input channels')
Number of input channels in the time data, set automatically based on the
loaded dataorspecified array.
- num_samples = CInt(0, desc='number of samples')
Total number of time-domain samples, set automatically based on the
loaded dataorspecified array.
- data = Any(transient=True, desc='the actual time data array')
A 2D NumPy array containing the time-domain data, shape (
num_samples,num_channels).
- h5f = Instance(H5FileBase, transient=True)
HDF5 file object.
- metadata = Dict(desc='metadata contained in .h5 file')
Metadata loaded from the HDF5 file, if available.
- digest = Property(depends_on=['basename', '_datachecksum', 'sample_freq', 'num_channels', 'num_samples'])
A unique identifier for the samples, based on its properties. (read-only)
- result(num=128)
Generate blocks of time-domain data iteratively.
The
result()method is a Python generator that yields blocks of time-domain data of the specified size. Data is either read from an HDF5 file (iffileis set) or from a NumPy array (ifdatais directly provided).- Parameters:
- num
int, optional The size of each block to be yielded, representing the number of time-domain samples per block.
- num
- Yields:
numpy.ndarrayA 2D array of shape (
num,num_channels) representing a block of time-domain data. The last block may have fewer thannumsamples if the total number of samples is not a multiple ofnum.
- Raises:
OSErrorIf no samples are available (i.e.,
num_samplesis0).
Examples
Create a generator and access blocks of data:
>>> import numpy as np >>> from acoular.sources import TimeSamples >>> ts = TimeSamples(data=np.random.rand(1000, 4), sample_freq=51200) >>> generator = ts.result(num=256) >>> for block in generator: ... print(block.shape) (256, 4) (256, 4) (256, 4) (232, 4)
Note that the last block may have fewer that
numsamples.