-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.py
86 lines (66 loc) · 2.19 KB
/
algorithms.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
"""
Module for utility functions for the simulated GMD IOC.
"""
import numpy as np
def calc_attn_signal(signal, attn1, attn2):
"""
Calculate output signal from raw based on attenuator settings.
signal : list
List of waveform values.
attn1 : int
Attnuator 1 setting as an interger multiple of -3dB. Ranges from
0 dB (attn1=0) to -45 dB (attn1=15).
attn2 : int
Attnuator 2 setting as an interger multiple of -3dB. Ranges from
0 dB (attn2=0) to -45 dB (attn2=15).
"""
assert 0 <= attn1 < 16
assert 0 <= attn2 < 16
total_db = -3*(attn1+attn2)
out = [float(val)*10**(total_db/20) for val in signal]
return out
def ThreshDetect(high_val, low_val, signal):
if max(signal) > high_val:
return -1
elif max(signal) < low_val:
return 1
else:
return 0
def UpDownByOne(peak_status, current_att):
if peak_status == -1: # Too high
if current_att == 15:
return 15
else:
return current_att + 1
elif peak_status == 1: # Too low
if current_att == 0:
return 0
else:
return current_att - 1
else: # Juuuusssst right (or something bad happened)
return current_att
def EvenAttenuation(att_val):
preatt = int(att_val%2 + att_val/2)
posatt = int(att_val/2)
return preatt, posatt
#def PeakSharpen(signal, k2, k4):
def PeakSharpen(signal, k2):
"""
Algorithm to sharpen a peak based on its even derivatives.
Parameters
----------
signal : list of numeric
The signal to be differentiated
k2 : 2nd derivative scaling parameter.
k4 : 4th derivative scaling parameter.
"""
d1sig = np.diff(signal)
d2sig = np.diff(d1sig)
# Each derivative returns an array of n-1 length. We want the output array
# to maintain its original length, so pad the back side of the derivative
# with copies of the last value (probably ~zero anyway since the original
# signal is fairly flat in that region.
last = d2sig[-1]
d2sig = np.concatenate((d2sig, np.array([last, last])))
retsig = [signal[i]-k2*d2sig[i] for i in range(len(signal))]
return retsig