STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
time_series.py
Go to the documentation of this file.
1from __future__ import annotations
2
3from typing import Tuple
4
5import numpy as np
6
7
9 profile_type: str,
10 start_value: float,
11 end_value: float,
12 duration: float,
13 n_steps: int,
14 **params,
15) -> Tuple[np.ndarray, np.ndarray]:
16 """Generate a pressure profile over time using analytic functions.
17
18 Parameters
19 ----------
20 profile_type : str
21 One of "linear", "exponential", or "power".
22 start_value : float
23 Initial pressure value (arbitrary units – caller handles unit conversion).
24 end_value : float
25 Final pressure value at t = duration.
26 duration : float
27 Total time span in seconds.
28 n_steps : int
29 Number of discrete samples (>= 2).
30 params : dict
31 Additional shape parameters. Supported keys:
32 - decay_constant (exponential): positive float controlling curvature (default 3.0)
33 - power (power): exponent > 0 (default 2.0)
34
35 Returns
36 -------
37 times : np.ndarray
38 Array of monotonically increasing time values in seconds (size n_steps).
39 values : np.ndarray
40 Pressure values corresponding to `times` (same units as inputs).
41 """
42
43 if n_steps < 2:
44 raise ValueError("n_steps must be at least 2 for time-series generation")
45
46 duration = max(float(duration), 1e-6)
47 times = np.linspace(0.0, duration, n_steps)
48 x = times / duration # Normalized progress 0 → 1
49
50 profile_type_lower = profile_type.lower().strip()
51
52 if profile_type_lower == "linear":
53 factor = x
54 elif profile_type_lower == "exponential":
55 decay_constant = float(params.get("decay_constant", 3.0))
56 decay_constant = max(decay_constant, 1e-6)
57 numerator = 1.0 - np.exp(-decay_constant * x)
58 denominator = 1.0 - np.exp(-decay_constant)
59 if denominator <= 1e-9:
60 factor = x # Nearly linear
61 else:
62 factor = numerator / denominator
63 elif profile_type_lower == "power":
64 power = max(float(params.get("power", 2.0)), 1e-6)
65 factor = x ** power
66 else:
67 raise ValueError(f"Unsupported profile type '{profile_type}'.")
68
69 values = start_value + (end_value - start_value) * factor
70 return times, values
#define max(a, b)
Definition Arduino.h:43
Tuple[np.ndarray, np.ndarray] generate_pressure_profile(str profile_type, float start_value, float end_value, float duration, int n_steps, **params)