-
Notifications
You must be signed in to change notification settings - Fork 17
/
filter.py
140 lines (109 loc) · 4.04 KB
/
filter.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import numpy as np
import matplotlib.pyplot as plt
import sys
from pylab import *
from nptdms import TdmsFile
from scipy.signal import butter, lfilter, filtfilt
def depth_stack(data,nstack=1):
nsamples,nchannels=data.shape
nchout=int(nchannels/nstack)
dataout=np.ndarray((nsamples,nchout))
for i in range(nchout):
dataout[:,i]=np.sum(data[:,i:i+nstack],axis=1)/nstack
return dataout
def apply_moveout(datain,moveout,dt=0.001):
shift=-int(moveout[-1]/dt)
dataout=np.zeros_like(datain)
# ((datain.shape[0]-shift,len(moveout)))
print(dataout.shape)
for i in range(len(moveout)):
dataout[shift::,i]=datain[shift+int(moveout[i]/dt):int(moveout[i]/dt)+datain.shape[0],i]
return dataout
def get_meanvel(depth):
velmodel=np.loadtxt('/mnt/d/Research/OpticSensing/ALJ.2017/ALJ.2017/velocity_vertical_Ichihara.txt')
res1p=np.polyfit(velmodel[:,2], velmodel[:,3], 1)
res2p=np.polyfit(velmodel[:,2], velmodel[:,3], 2)
res3p=np.polyfit(velmodel[:,2], velmodel[:,3], 3)
res3s=np.polyfit(velmodel[:,2], velmodel[:,4], 1)
res3s=np.polyfit(velmodel[:,2], velmodel[:,4], 2)
res3s=np.polyfit(velmodel[:,2], velmodel[:,4], 3)
dep=np.arange(0,depth,1)
vels=np.poly1d(res3s)(dep)
velp=np.poly1d(res3p)(dep)
return np.mean(velp),np.mean(vels)
def get_traveltime(meanvel,distance):
return distance/meanvel
# def butter_bandpass(lowcut, highcut, fs, order=5):
# nyq = 0.5 * fs
# low = lowcut / nyq
# high = highcut / nyq
# b, a = butter(order, [low, high], btype='band')
# return b, a
# def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
# b, a = butter_bandpass(lowcut, highcut, fs, order=order)
# y = lfilter(b, a, data)
# return y
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = filtfilt(b, a, data)
return y
def butter_highpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='high', analog=False)
return b, a
def butter_highpass_filter(data, cutoff, fs, order=5):
b, a = butter_highpass(cutoff, fs, order=order)
y = filtfilt(b, a, data)
return y
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = filtfilt(b, a, data)
return y
def rms(data):
nsample=data.shape[0]
rms=np.sqrt(np.sum(data**2,axis=0)/nsample)
return rms
def get_snr(data,time,ch,rmswindow=2):
index1=np.argmax(data.tt>time)
index2=np.argmax(data.tt>time-rmswindow)
index3=np.argmax(data.tt>time+rmswindow)
rmsnoise=rms(data.data[index2:index1,ch])
rmssignal=rms(data.data[index1:index3,ch])
snr=rmssignal/rmsnoise
return snr
def get_snr_single(data,time,rmswindow=2):
index1=np.argmax(data.tt>time)
index2=np.argmax(data.tt>time-rmswindow)
index3=np.argmax(data.tt>time+rmswindow)
rmsnoise=rms(data.data[index2:index1])
rmssignal=rms(data.data[index1:index3])
snr=rmssignal/rmsnoise
return snr
def rms_all(data):
nsample=data.shape[0]
rms=np.square(np.sum(data**2,axis=0)/nsample)
return rms
def get_snr_all(data,time,rmswindow=2,depini=2834,depend=3664):
index1=np.argmax(data.tt>time)
index2=np.argmax(data.tt>time-rmswindow)
index3=np.argmax(data.tt>time+rmswindow)
rmsnoise=rms_all(data.data[index2:index1,depini:depend])
rmssignal=rms_all(data.data[index1:index3,depini:depend])
snr=rmssignal/rmsnoise
return snr
def moving_average(data,nave):
outdata=np.convolve(data,np.ones(nave)/float(nave),'same')
return outdata