Skip to content

Commit 09091e3

Browse files
committed
Add docstrings for all of the defined functions.
1 parent c1960c0 commit 09091e3

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

labscript/functions.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,160 @@
1515
import numpy as np
1616

1717
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+
"""
1828
print('t = {0:.9f} s:'.format(t),description)
1929

2030
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+
"""
2143
m = (final - initial)/duration
2244
return lambda t: m*t + initial
2345

2446
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+
"""
2561
return lambda t: amplitude*sin(angfreq*(t) + phase) + dc_offset
2662

2763
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+
"""
2876
return lambda t: (final-initial)*(sin(pi*(t)/(2*duration)))**2 + initial
2977

3078
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+
"""
3191
return lambda t: (final-initial)*(sin(pi*(t)/(2*duration)))**4 + initial
3292

3393
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+
"""
34106
return lambda t: (final-initial)*(sin(pi/2+pi*(t)/(2*duration)))**4 + initial
35107

36108
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+
"""
37123
rate = 1/duration * log((initial-zero)/(final-zero))
38124
return lambda t: (initial-zero)*exp(-rate*(t)) + zero
39125

40126
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+
"""
41141
zero = (final-initial*exp(-duration/time_constant)) / (1-exp(-duration/time_constant))
42142
return lambda t: (initial-zero)*exp(-(t)/time_constant) + zero
43143

44144
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+
"""
45152
a = (final-initial)
46153
return lambda t: initial + a * (
47154
(9./2 * t**3/duration**3) * (t<duration/3)
48155
+ (-9*t**3/duration**3 + 27./2*t**2/duration**2 - 9./2*t/duration + 1./2) * (t<2*duration/3)*(t>=duration/3)
49156
+ (9./2*t**3/duration**3 - 27./2 * t**2/duration**2 + 27./2*t/duration - 7./2) * (t>= 2*duration/3))
50157

51158
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+
"""
52172
pulse_sequence = np.asarray(sorted(pulse_sequence, key=lambda x: x[0], reverse=True))
53173
pulse_sequence_times = pulse_sequence[:, 0]
54174
pulse_sequence_states = pulse_sequence[:, 1]

0 commit comments

Comments
 (0)