-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeZap.py
113 lines (78 loc) · 3 KB
/
MakeZap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3
usageStr = \
"""usage: MakeZAP.py fileName t dt maxF [amplitude]
fileName should end in .atf
times in seconds, frequencies in Hz"""
import sys, math
################################################################################
def calcDeltaPhase(t, duration, dt, f0, slope):
tLeft = duration - t
if t < tLeft:
freq = f0 * math.exp(slope * t)
else:
freq = f0 * math.exp(slope * tLeft)
deltaPhase = 2 * math.pi * freq * dt
return deltaPhase
################################################################################
def generateInjection(duration, dt, numSamples, fMax, amp):
f0 = 0.25 #Hz
# the factor of two is because we have an increasing ramp followed
# by a decreasing ramp:
slope = 2.0 * math.log(fMax / f0) / duration
tStart = 0.0 #s
t = [tStart + dt * n for n in range(numSamples)]
deltaPhase = [calcDeltaPhase(t_n, duration, dt, f0, slope) for t_n in t]
signal = []
phase = 0
for n in range(len(deltaPhase)):
signal.append(amp * math.sin(phase))
phase += deltaPhase[n]
return signal
################################################################################
def bufferSignal(signal, dt, frontBuffer, numTotal):
frontLen = int(round(frontBuffer / dt))
backLen = numTotal - frontLen - len(signal)
buffSig = [0] * frontLen + signal + [0] * backLen
return buffSig
################################################################################
def outputSignal(signal, dt, fileName):
with open(fileName, 'w') as fHandle:
fHandle.write('ATF\t1.0\r\n')
fHandle.write('0\t2\r\n')
fHandle.write('"Time (s)"\t"Trace #1 (nA)"\r\n')
t = 0.0
for n in range(len(signal)):
lineStr = '%.6f\t%.6f\r\n' % (t, signal[n])
fHandle.write(lineStr)
t = t + dt
################################################################################
def makeZap(duration, dt, fMax, amplitude, frontBuffer=1.0, backBuffer=1.0):
numTotal = int(duration / dt)
duration = duration - (frontBuffer + backBuffer)
numSamples = int(duration / dt)
signal = generateInjection(duration, dt, numSamples, fMax, amplitude)
signal = bufferSignal(signal, dt, frontBuffer, numTotal)
return signal
################################################################################
def _parseArguments():
arguments = sys.argv
if len(arguments) not in (5, 6):
print(usageStr)
raise TypeError('Incorrect number of arguments.')
fileName = arguments[1]
duration = float(arguments[2])
dt = float(arguments[3])
fMax = float(arguments[4])
if len(arguments) < 6:
amplitude = 1.0
else:
amplitude = float(arguments[5])
return (fileName, duration, dt, fMax, amplitude)
################################################################################
if __name__ == "__main__":
(fileName, duration, dt, fMax, amplitude) = _parseArguments()
# make the signal
signal = makeZap(duration, dt, fMax, amplitude)
# output the signal
outputSignal(signal, dt, fileName)
sys.exit(0)