-
Notifications
You must be signed in to change notification settings - Fork 0
/
libraries.py
107 lines (93 loc) · 3.08 KB
/
libraries.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
from scipy.signal import hilbert,find_peaks,correlate,blackman
import numpy as np
from statsmodels.tsa.stattools import acf,ccf
from scipy.fftpack import fft,ifft
def sigmoid(len):
x = np.linspace(-6,+6,len)
y = 1/(1 + np.exp(-x))
return -y + 1
def extract_peaks(data):
amps = np.abs(hilbert(data))
peaks_times = find_peaks(amps)
peaks_times = peaks_times[0]
peaks = []
for i in peaks_times:
peaks.append(amps[i])
peaks = peaks[:-1]
return peaks
def time_diffrences(data):
amps = np.abs(hilbert(data))
peaks_times = find_peaks(amps)
peaks_times = peaks_times[0]
diff_list = np.diff(peaks_times)
diff_list = np.array(diff_list)
return diff_list
# A function to extract peaks
def amps_detection(data):
amps_data = np.abs(hilbert(data))
amps_times = find_peaks(amps_data)
amps_times = list(amps_times[0])
amps_peak_values = []
for l in amps_times:
amps_peak_values.append(amps_data[l])
amps_peak_values = amps_peak_values[:-1]
amps_peak_values = np.array(amps_peak_values)
return amps_peak_values
def shuff_corr(data1, data2):
np.random.shuffle(data1)
np.random.shuffle(data2)
shuff_corr = ccf(data1,data2,unbiased=False)
return shuff_corr
# A function to shuffle the data and then finding the auto correlation
def shuff_acf(data):
np.random.shuffle(data)
shuff_corr = acf(data,fft=True)
return shuff_corr
def smooth(y, box_pts=3):
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
return y_smooth
def manualFFT(signal, type='one'):
if (type == 'one'):
N = signal.size
# sampling rate
T = 1.0 / 1000.0
time_domain = np.linspace(0, 1 / (2*T), N - 1)
fft = np.fft.fft(signal)
fft = 1/N * np.abs(fft[1:])
#fft = np.abs(fft[0:N//2])
if (type == 'two'):
N = signal.size
# sampling rate
T = 1.0 / 1000.0
time_domain = np.linspace(0, 1 / (2*T), N - 1)
fft = np.fft.fft(signal)
fft = 1/N * np.abs(fft[1:])
return fft, time_domain
def filtering(signal, range=20, type = 'gaussian'):
N = signal.size
# sampling rate
T = 1.0 / 1000.0
time_domain = np.linspace(0, 1 / (2*T), N-1)
signal_fft = fft(signal)
signal_fft = signal_fft[1:N]
signal_fft_abs = np.abs(signal_fft[:N//2])
fft_peak = np.argmax(signal_fft_abs)
#fft_peak = 400
# for bandpass filter
if (type == 'gaussian'):
blackman_ = blackman(2*range)
sameSizeKernel = np.zeros(len(signal_fft))
sameSizeKernel[fft_peak-range:fft_peak+range] = blackman_
# for lowpass filtering
if ( type == 'lowpass'):
sigmoid_ = sigmoid(2*range)
sameSizeKernel = np.ones(len(signal_fft))
sameSizeKernel[fft_peak:fft_peak+len(sigmoid_)] = sigmoid_
sameSizeKernel[fft_peak+len(sigmoid_):] = 0
filtered = np.multiply(sameSizeKernel,signal_fft.real)
#filtered[:fft_peak-range] = 0
#filtered[fft_peak+range:] = 0
inverse_signal = ifft(filtered)
#fft = np.abs(fft[0:N//2])
return inverse_signal.real