heuristic_min_samples
timecave.utils.heuristic_min_samples(fs, freq_limit)
Compute the minimum number of samples the series should have according to the 10 / 20 sampling heuristic.
This function computes the minimum and maximum lengths for capturing a given frequency,
assuming the time series was
sampled at a frequency of fs Hertz and the largest frequency
of interest for modelling purposes is freq_limit Hertz. The
interval in which the sampling frequency should lie for freq_limit
to be effectively captured is also derived. The 10 / 20 sampling
heuristic is used to derive both results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fs |
float | int
|
The time series' sampling frequency (Hz). |
required |
freq_limit |
float | int
|
Largest frequency of interest (Hz). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Minimum and maximum number of samples (Min_samples and Max_samples, respectively) required to capture freq_limit with a sampling frequency of fs, according to the 10 / 20 heuristic rule. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If either |
ValueError
|
If either |
Warning
|
If the choice of |
See also
Nyquist_min_samples: Performs the same computations using the Nyquist theorem.
Notes
Under certain circumstances, the conditions of the Nyquist theorem might not be enough to guarantee that the reconstruction of the signal is possible. To address this isssue, a heuristic has been developed in the field of control engineering, according to which the sampling frequency should be 10 to 20 times higher than the largest frequency of interest:
Theoretically, the higher the sampling frequency, the better (i.e. the easier it can be to reconstruct the original signal), though hardware limitations naturally come into play here.
Examples:
>>> from timecave.utils import heuristic_min_samples
>>> n_samples = heuristic_min_samples(150, 10);
10 / 20 sampling heuristic results
----------------------------------
Minimum sampling rate required to capture a frequency of 10 Hz : 100 Hz
Maximum sampling rate required to capture a frequency of 10 Hz : 200 Hz
----------------------------------------------------------------------------------------------
Capturing a frequency of 10 Hz with a sampling frequency of 150 Hz would require:
150 to 300 samples
>>> n_samples
{'Min_samples': 150, 'Max_samples': 300}
If the frequency of interest cannot be captured using the sampling frequency provided by the user according to the heuristic, an exception is thrown:
>>> samples = heuristic_min_samples(80, 10);
Traceback (most recent call last):
...
Warning: This choice of sampling frequency and frequency of interest is not compliant with the 10 / 20 sampling heuristic.
If negative frequencies are passed, or if their values are neither integers nor floats, exceptions are thrown as well:
>>> samples = heuristic_min_samples(-2, 1);
Traceback (most recent call last):
...
ValueError: Frequencies should be non-negative.
>>> samples = heuristic_min_samples(1, "a");
Traceback (most recent call last):
...
TypeError: Both 'fs' and 'freq_limit' should be either integers or floats.
Source code in timecave/utils.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |