-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsavetemplate.py
61 lines (48 loc) · 1.56 KB
/
savetemplate.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
"""
Make a signal template from an LNGS wav and save it. Usage:
savetemplate.py [filenames...]
"""
import os
from matplotlib import pyplot as plt
import numpy as np
import template
import readwav
import templateplot
import firstbelowthreshold
def templatebasepath(source):
assert source.endswith('.wav'), source
return 'templates/' + os.path.split(source)[1][:-4] + '-template'
def templatepath(source):
return templatebasepath(source) + '.npz'
def defaulttrigger(source):
return 8969
def savetemplate(source, plot='show'):
dest = templatepath(source)
destbase = templatebasepath(source)
data = readwav.readwav(source)
print(f'computing template...')
if data.shape[1] == 1:
trigger = defaulttrigger(source)
else:
trigger = None
fig1 = plt.figure(num='savetemplate1', clear=True)
templ = template.Template.from_lngs(data, 7 * 512, trigger=trigger, fig=fig1)
print(f'write {dest}...')
templ.save(dest)
fig2 = plt.figure(num='savetemplate2', clear=True, figsize=[10, 7])
templateplot.templateplot(dest, fig=fig2)
for i, fig in enumerate([fig1, fig2]):
fig.tight_layout()
if plot == 'show':
fig.show()
elif plot == 'save':
destplot = f'{destbase}-{i + 1}.png'
print(f'write {destplot}...')
fig.savefig(destplot)
else:
raise KeyError(plot)
if __name__ == '__main__':
import sys
files = sys.argv[1:]
for file in files:
savetemplate(file, plot='show' if len(files) == 1 else 'save')