Time and frequency grids¶
This module provides helper classes to handle time and frequency sampling attributes in Mojito L1 files.
- class mojito.sampling.UniformTimeSampling(*, t0, dt, size)[source]¶
Helper class to handle sampling attributes in Mojito L1 files.
- dt: float¶
Time step of the sampling [s].
- property duration: float¶
Duration of the time series [s].
This is defined as
size * dt. Note that the last timestamp is nott0 + duration, butt0 + (size - 1) * dt.
- classmethod from_h5_group(group)[source]¶
Create an instance from an HDF5 group.
- Parameters:
group (
Group) – HDF5 group containing the sampling attributes.- Returns:
Instance populated with the group’s attributes.
- Return type:
TimeSampling
- property fs: float¶
Sampling frequency [Hz].
- size: int¶
Number of samples in the time series.
- slice_between(tmin, tmax)[source]¶
Return a slice that contains the interval [tmin, tmax].
We guarantee that any time
tsuch thattmin <= t <= tmaxis included in the returned slice.sampling = UniformTimeSampling(t0=0.0, dt=0.1, size=100) time_array = sampling.t() # Pick a random time between 2.3s and 5.7s tmin = 2.3 tmax = 5.7 random_t = np.random.uniform(tmin, tmax) # Slice the time array time_slice = sampling.slice_between(tmin, tmax) selected_times = time_array[time_slice] # We guarantee that random_t is in selected_times assert random_t in selected_times
Warning
Because floating-point arithmetics is not exact, we extend the slice so that it will always contain
tminandtmax. As a result, the returned slice can be larger than(tmax - tmin) / dt. Note that it is at most2 * self.dtlarger.- Parameters:
tmin (
float|None) – Lower bound of the time range [s]. If None, the slice starts at the beginning of the time series.tmax (
float|None) – Upper bound of the time range [s]. If None, the slice ends at the end of the time series.
- Return type:
slice- Returns:
A slice instance such that all times
twithtmin <= t <= tmaxare included inself.t()[slice].- Raises:
ValueError – If
tmin < self.t0, or iftmax > self.tmax().
- t(idx=None, *, elapsed=False)[source]¶
Return the uniform array of times.
The time grid is defined as
t0 + i * dtforiinrange(size).Use the
sliceparameter to return a sliced version of the time grid. This is useful for long datasets, as this will only allocate the selected timestamps in memory. For example:>>> sampling = UniformTimeSampling(t0=1.5, dt=0.5, size=10) >>> sampling.t(slice(2, 7, 2)) array([2.5, 3.5, 4.5])
If
elapsedis True, the time grid is defined asi * dtforiinrange(size). This can be useful to preserve numerical precision.- Parameters:
idx (
slice|None, default:None) – Slice selecting which timestamps to return.elapsed (
bool, default:False) – If True, return elapsed time grid starting at 0.
- Returns:
Array of times [s]. Shape is
(size,)ifidxis None, otherwise it matches the selected slice length.- Return type:
NDArray
- t0: float¶
Start time of the sampling [s].
- tmax(elapsed=False)[source]¶
Return the last timestamp.
The last timestamp is defined as the last element of the array returned by
t(). Note that this is nott0 + duration.If
elapsedis False, it corresponds tot0 + (size - 1) * dt. Ifelapsedis True, the time origin is ignored and the last timestamp is(size - 1) * dt.- Parameters:
elapsed (
bool, default:False) – If True, ignoret0.- Return type:
float- Returns:
Last timestamp [s].
- class mojito.sampling.LogUniformFrequencySampling(*, fmin, fmax, size)[source]¶
Helper class to handle logarithmic frequency sampling in Mojito L1 files.
Frequencies must be positive here.
- f(idx=None)[source]¶
Return the log-uniform array of frequencies.
The frequency grid is defined as \(f_i = 10^{F_i}\) with \(F_i = \log_{10}(f_\mathrm{min}) + i \Delta \log_{10}(f)\), for \(i\) ranging from
0tosize - 1and \(\Delta \log_{10}(f) = (\log_{10}(f_\mathrm{max}) - \log_{10}(f_\mathrm{min})) / (\text{size} - 1)\).Use the
idxparameter to return a sliced version of the frequency grid. This is useful for long datasets, as this will only allocate the selected frequencies in memory. For example:>>> sampling = LogUniformFrequencySampling(fmin=1.0, fmax=100.0, size=10) >>> sampling.f(slice(2, 7, 2)) array([ 2.7825594 , 7.74263683, 21.5443469 ])
- Parameters:
idx (
slice|None, default:None) – Slice selecting which frequencies to return.- Returns:
Array of frequencies [Hz]. Shape is
(size,)ifidxis None, otherwise it matches the selected slice length.- Return type:
NDArray
- fmax: float¶
Maximum frequency [Hz].
- fmin: float¶
Minimum frequency [Hz].
- classmethod from_h5_group(group)[source]¶
Create an instance from an HDF5 group.
- Parameters:
group (
Group) – HDF5 group containing the frequency sampling attributes.- Returns:
Instance populated with the group’s attributes.
- Return type:
- size: int¶
Number of frequencies.