|
15 | 15 | import numpy as np
|
16 | 16 |
|
17 | 17 | def print_time(t, description):
|
| 18 | + """Print time with a descriptive string. |
| 19 | +
|
| 20 | + Useful debug tool to print time at a specific point |
| 21 | + in the shot, during shot compilation. Helpful when |
| 22 | + the time is calculated. |
| 23 | +
|
| 24 | + Args: |
| 25 | + t (float): Time to print |
| 26 | + description (str): Descriptive label to print with it |
| 27 | + """ |
18 | 28 | print('t = {0:.9f} s:'.format(t),description)
|
19 | 29 |
|
20 | 30 | def ramp(duration, initial, final):
|
| 31 | + """Defines a linear ramp. |
| 32 | +
|
| 33 | + f(t) = (final - initial)*t/duration + initial |
| 34 | +
|
| 35 | + Args: |
| 36 | + duration (float): Duration of ramp |
| 37 | + initial (float): Starting value of ramp |
| 38 | + final (float): Ending value of ramp |
| 39 | +
|
| 40 | + Returns: |
| 41 | + func: Function that takes a single parameter `t`. |
| 42 | + """ |
21 | 43 | m = (final - initial)/duration
|
22 | 44 | return lambda t: m*t + initial
|
23 | 45 |
|
24 | 46 | def sine(duration, amplitude, angfreq, phase, dc_offset):
|
| 47 | + """Defines a sine wave. |
| 48 | +
|
| 49 | + f(t) = amplitude*sin(angfreq*t + phase) + dc_offset |
| 50 | +
|
| 51 | + Args: |
| 52 | + duration (float): Not used. |
| 53 | + amplitude (float): Amplitude of sine wave. |
| 54 | + angfreq (float): Angular frequency of sine wave. |
| 55 | + phase (float): Phase of sine wave. |
| 56 | + dc_offset (float): Verticle offset of sine wave. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + func: Function that takes a single parameter `t`. |
| 60 | + """ |
25 | 61 | return lambda t: amplitude*sin(angfreq*(t) + phase) + dc_offset
|
26 | 62 |
|
27 | 63 | def sine_ramp(duration, initial, final):
|
| 64 | + """Defines a square sinusoidally increasing ramp. |
| 65 | +
|
| 66 | + f(t) = (final-initial)*(sin(pi*t/(2*duration)))^2 + initial |
| 67 | +
|
| 68 | + Args: |
| 69 | + duration (float): Length of time for the ramp to complete. |
| 70 | + initial (float): Initial value of ramp. |
| 71 | + final (float): Final value of ramp. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + func: Function that takes a single parameter `t`. |
| 75 | + """ |
28 | 76 | return lambda t: (final-initial)*(sin(pi*(t)/(2*duration)))**2 + initial
|
29 | 77 |
|
30 | 78 | def sine4_ramp(duration, initial, final):
|
| 79 | + """Defines a quartic sinusoidally increasing ramp. |
| 80 | +
|
| 81 | + f(t) = (final-initial)*(sin(pi*t/(2*duration)))^4 + initial |
| 82 | +
|
| 83 | + Args: |
| 84 | + duration (float): Length of time for the ramp to complete. |
| 85 | + initial (float): Initial value of ramp. |
| 86 | + final (float): Final value of ramp. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + func: Function that takes a single parameter `t`. |
| 90 | + """ |
31 | 91 | return lambda t: (final-initial)*(sin(pi*(t)/(2*duration)))**4 + initial
|
32 | 92 |
|
33 | 93 | def sine4_reverse_ramp(duration, initial, final):
|
| 94 | + """Defines a quartic sinusoidally decreasing ramp. |
| 95 | +
|
| 96 | + f(t) = (final-initial)*(sin(pi/2+pi*t/(2*duration)))^4 + initial |
| 97 | +
|
| 98 | + Args: |
| 99 | + duration (float): Length of time for the ramp to complete. |
| 100 | + initial (float): Initial value of ramp. |
| 101 | + final (float): Final value of ramp. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + func: Function that takes a single parameter `t`. |
| 105 | + """ |
34 | 106 | return lambda t: (final-initial)*(sin(pi/2+pi*(t)/(2*duration)))**4 + initial
|
35 | 107 |
|
36 | 108 | def exp_ramp(duration,initial,final,zero):
|
| 109 | + """Defines an exponential ramp via offset value. |
| 110 | +
|
| 111 | + f(t) = (initial-zero)*e^(-rate*t) + zero |
| 112 | + rate = log((initial-zero)/(final-zero))/duration |
| 113 | +
|
| 114 | + Args: |
| 115 | + duration (float): Length of time for the ramp to complete |
| 116 | + initial (float): Initial value of ramp. |
| 117 | + final (float): Final value of ramp. |
| 118 | + zero (float): Zero offset of ramp. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + func: Function that takes a single parameter `t`. |
| 122 | + """ |
37 | 123 | rate = 1/duration * log((initial-zero)/(final-zero))
|
38 | 124 | return lambda t: (initial-zero)*exp(-rate*(t)) + zero
|
39 | 125 |
|
40 | 126 | def exp_ramp_t(duration,initial,final,time_constant):
|
| 127 | + """Defines an exponential ramp via time constant. |
| 128 | +
|
| 129 | + f(t) = (initial-zero)*e^(-t/time_constant) + zero |
| 130 | + zero = (final-initial*e^(-duration/time_constant))/(1-e^(-duration/time_constant)) |
| 131 | +
|
| 132 | + Args: |
| 133 | + duration (float): Length of time for the ramp to complete |
| 134 | + initial (float): Initial value of ramp. |
| 135 | + final (float): Final value of ramp. |
| 136 | + zero (float): Zero offset of ramp. |
| 137 | +
|
| 138 | + Returns: |
| 139 | + func: Function that takes a single parameter `t`. |
| 140 | + """ |
41 | 141 | zero = (final-initial*exp(-duration/time_constant)) / (1-exp(-duration/time_constant))
|
42 | 142 | return lambda t: (initial-zero)*exp(-(t)/time_constant) + zero
|
43 | 143 |
|
44 | 144 | def piecewise_accel(duration,initial,final):
|
| 145 | + """Defines a piecewise acceleration. |
| 146 | +
|
| 147 | + Args: |
| 148 | + duration (float): Length of time for the acceleration to complete. |
| 149 | + initial (float): Initial value. |
| 150 | + final (float): Final value. |
| 151 | + """ |
45 | 152 | a = (final-initial)
|
46 | 153 | return lambda t: initial + a * (
|
47 | 154 | (9./2 * t**3/duration**3) * (t<duration/3)
|
48 | 155 | + (-9*t**3/duration**3 + 27./2*t**2/duration**2 - 9./2*t/duration + 1./2) * (t<2*duration/3)*(t>=duration/3)
|
49 | 156 | + (9./2*t**3/duration**3 - 27./2 * t**2/duration**2 + 27./2*t/duration - 7./2) * (t>= 2*duration/3))
|
50 | 157 |
|
51 | 158 | def pulse_sequence(pulse_sequence,period):
|
| 159 | + """Returns a function that interpolates a pulse sequence. |
| 160 | +
|
| 161 | + Relies on :obj:`numpy.digitize` to perform the interpolation. |
| 162 | +
|
| 163 | + Args: |
| 164 | + pulse_sequence (:obj:`numpy:numpy.ndarray`): 2-D timeseries of |
| 165 | + change times and associated states. |
| 166 | + period (float): Period of each pulse. |
| 167 | +
|
| 168 | + Returns: |
| 169 | + func: Interpolating function that takes a single parameter `t`. |
| 170 | + Only well defined if `t` falls within the `pulse_sequence` change times. |
| 171 | + """ |
52 | 172 | pulse_sequence = np.asarray(sorted(pulse_sequence, key=lambda x: x[0], reverse=True))
|
53 | 173 | pulse_sequence_times = pulse_sequence[:, 0]
|
54 | 174 | pulse_sequence_states = pulse_sequence[:, 1]
|
|
0 commit comments