forked from jaidevd/pyhht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyhht.py
122 lines (99 loc) · 2.67 KB
/
pyhht.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
#!/usr/bin/env python
from numpy import zeros, vstack, array, sin, linspace,hstack, real, imag,diff
from numpy.random import normal
from scipy.interpolate import splrep,splev
from scipy.io.wavfile import read
from scipy.signal import hilbert
from scipy import angle, unwrap
from math import pi
from matplotlib.pyplot import plot,show,imshow,colorbar,subplot,grid
def envlps(f):
x=zeros((len(f),),dtype=int)
y=x.copy()
for i in range(1,len(f)-1):
if (f[i]>f[i-1])&(f[i]>f[i+1]):
x[i]=1
if (f[i]<f[i-1])&(f[i]<f[i+1]):
y[i]=1
x=(x>0).nonzero()
y=(y>0).nonzero()
y=y[0]
x=x[0]
x=hstack((0,x,len(f)-1))
y=hstack((0,y,len(f)-1))
t=splrep(x,f[x])
# numpy.arange
top=splev(array(range(len(f))),t)
t=splrep(y,f[y])
bot=splev(array(range(len(f))),t)
return top,bot
def sift(t):
top,bot=envlps(t)
c=t-(top+bot)/2
return c
def localmean(t):
top,bot=envlps(t)
return (top+bot)/2
def checkimf(t):
xtrm=zeros((len(t),),dtype=int)
zcross=xtrm.copy()
for i in range(1,len(t)-1):
if (t[i]>t[i-1])&(t[i]>t[i+1]):
xtrm[i]=1
if (t[i]<t[i-1])&(t[i]<t[i+1]):
xtrm[i]=1
if t[i-1]*t[i+1]<0:
zcross[i]=1
a=(xtrm>0).nonzero()
b=(zcross>0).nonzero()
return abs(len(a[0])-len(b[0]))
def siftrun(t,n):
d=checkimf(t)
iters=0
while d>1:
t=sift(t)
iters=iters+1
d=checkimf(t)
if iters==n:
break
return t
def emd(f,n):
imfs=zeros((1,len(f)),dtype=float)
for i in range(n):
t=f-sum(imfs)
t=siftrun(t,100)
print i
imfs=vstack((imfs,t))
imfs = imfs[1:n+1,:]
imfs=vstack((imfs,f-sum(imfs)))
return imfs
def demoinit():
Fs,f=read('sare.wav')
f=f[30000:31001]
return Fs,f
def plothilbert(imfs):
for i in range(imfs.shape[0]):
h=hilbert(imfs[i,:])
plot(real(h),imag(h))
show()
def symmetrydemo():
a=sin(linspace(-5*pi,5*pi,10000))
b=a+2
c=a-0.5
ah,bh,ch=hilbert(a),hilbert(b),hilbert(c)
ph_a,ph_b,ph_c=unwrap(angle(ah)),unwrap(angle(bh)),unwrap(angle(ch))
omega_a=diff(ph_a)
omega_b=diff(ph_b)
omega_c=diff(ph_c)
subplot(211),plot(ph_a),plot(ph_b),plot(ph_c)
subplot(212),plot(omega_a),plot(omega_b),plot(omega_c)
grid()
show()
return a,b,c
def getinstfreq(imfs):
omega=zeros((imfs.shape[0],imfs.shape[1]-1),dtype=float)
for i in range(imfs.shape[0]):
h=hilbert(imfs[i,:])
theta=unwrap(angle(h))
omega[i,:]=diff(theta)
return omega