-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolsFunctions.py
More file actions
118 lines (80 loc) · 1.98 KB
/
ToolsFunctions.py
File metadata and controls
118 lines (80 loc) · 1.98 KB
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
#!/home/sandra/anaconda3/bin/ipython
from math import *
from SharedGlobals import *
from scipy.fftpack import rfft, irfft, rfftfreq
import numpy as np
import scipy.signal
def UnixSecs2Date(unixtime):
return 0
#############
def Convert2Sph(x,y,z):
#convention phi=0 on y, phi=90 on -x
x=x-REFWE
y=y-REFSN
z=z-REFALT
r=sqrt(x**2+y**2+z**2)
theta=acos(z/r)
sinphi=-x/(r*sin(theta))
if sinphi<0:
phi=asin(sinphi)+2*pi
else:
phi=asin(sinphi)
theta=theta*180/pi
phi=phi*180/pi
return r,theta,phi
#############
def PassBand(vs,ts,fmin,fmax):
tstep=np.mean(np.diff(ts))
F=rfftfreq(len(vs))/tstep #len(vs) points between 0 and 1/2tstep=0.5e10Hz
VS=rfft(vs)
VS[F<fmin]=0
VS[F>fmax]=0
vs=irfft(VS)
return(vs)
#############
def Agglomerate(x,g):
g1=g
k=g+1
k1=g1+1
#filter isolated 1
k2=2*k1+1
if g1>1:
xx=np.zeros(len(x)+k2)
xx[0:len(x)]=x
inter=np.zeros(k2)+1
xx=scipy.signal.lfilter(inter,1,xx)
xx=xx[k1:len(xx)-k1-1]
boolx=np.zeros(len(x),bool)
for i in range(0,len(x)):
boolx[i]=(x[i]==1 and xx[i]>1)
x[:]=0
x[boolx]=1
#filter holes
mask=np.arange(k-1,-1,-1)
mask=2**mask
yb=scipy.signal.lfilter(mask,1,x) #left
K=yb>0
J=yb==0
yb[K]=k-1-np.floor(np.log2(yb[K]))
yb[J]=k+1
ya=scipy.signal.lfilter(mask,1,x[::-1]) #right
ya=ya[::-1]
K=ya>0
J=ya==0
ya[K]=k-1-np.floor(np.log2(ya[K]))
ya[J]=k+1
y=np.zeros(len(x))
booly=np.zeros(len(y),bool)
for i in range(0,len(y)):
booly[i]= (x[i]==1 or (ya[i]+yb[i])<=k)
y[booly]=1
return(y)
#############
if __name__ == "__main__": #si le module nest pas importe mais execute seul
#test
x=[1,1,1,1,0,0,0,0,0,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0]
x=np.asarray(x,float)
print(x)
g=3
y=Agglomerate(x,g) #we can input here as many arguments as needed
print(y)