Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

EMD.py 4.2 KB

You have to be logged in to leave a comment. Sign In
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # # 经验模态分解
  4. # ## 基本想法
  5. # ### 简单介绍
  6. # 经验模态分解是一种新的时频分析方法,而且是一种自适应的时频局部化分析方法
  7. #
  8. # - IMF与采样频率相关
  9. # - 基于数据本身变化,优于傅里叶方法,摆脱了傅里叶变换的局限性
  10. # - 缺点是模态混叠
  11. #
  12. # 一种非平稳信号分析方法,但是它不同于FFT,EMD适合事宜数据,基于数据本身来分解,不需要基函数
  13. #
  14. # 基于这样的假设:
  15. #
  16. # - 认为信号由不同的IMF组合而成
  17. # - IMF同时具备线性和非线性的特点
  18. #
  19. # IMF:本征模态分量,Intrinsic Mode Function
  20. # ### 经验模态EMD分解方法的原理及特性
  21. # **本征模态分量**
  22. #
  23. # Norden E.Huang 为了得到瞬时频率,提出本征模态分量(Intrinsic Mode Function),其必须满足下面两个条件:
  24. #
  25. # 1. 在整个数据序列内,极值点的个数$N_e$和过零点的个数$N_z$必须满足以下关系:
  26. #
  27. # $$
  28. # (N_z-1)\leq N_e \leq (N_z+1)
  29. # $$
  30. #
  31. # 2. 在任一时间点,信号由局部极大值确定的上包络线和由局部极小值确定的下包络线的均值必须满足:
  32. #
  33. # $$
  34. # \frac{f_{max}(t)+f_{min}(t)}{2}=0
  35. # $$
  36. # ## PyEMD
  37. #
  38. # - https://github.com/laszukdawid/PyEMD
  39. # In[1]:
  40. get_ipython().run_line_magic('reload_ext', 'autoreload')
  41. get_ipython().run_line_magic('autoreload', '2')
  42. # In[2]:
  43. from PyEMD import EMD
  44. # In[3]:
  45. get_ipython().run_line_magic('pinfo', 'EMD')
  46. # In[4]:
  47. import numpy as np
  48. # In most cases, default setting are enough. Simply import `EMD` and pass your signal to `emd` method.
  49. # In[5]:
  50. s = np.random.random(100)
  51. emd = EMD()
  52. IMFs = emd.emd(s)
  53. # In[6]:
  54. IMFs.shape
  55. # ## EMD
  56. #
  57. # Here is a complete script on how to create and plot results.
  58. # In[7]:
  59. from PyEMD import EMD
  60. import numpy as np
  61. import pylab as plt
  62. # In[8]:
  63. # Define signal
  64. t = np.linspace(0,1,200)
  65. s = np.cos(11*2*np.pi*t*t) + 6*t*t
  66. # In[9]:
  67. get_ipython().run_line_magic('pinfo', 'EMD.emd')
  68. # In[10]:
  69. # Execute EMD on signal
  70. IMF = EMD().emd(s,t)
  71. N = IMF.shape[0] + 1
  72. # In[11]:
  73. IMF.shape
  74. # In[12]:
  75. # Plot results
  76. plt.subplot(N,1,1)
  77. plt.plot(t,s,'r')
  78. plt.title("Input signal : $S(t)=cos(22\pi t^2) + 6t^2$")
  79. plt.xlabel("Time [s]")
  80. for n, imf in enumerate(IMF):
  81. plt.subplot(N,1,n+2)
  82. plt.plot(t,imf,'g')
  83. plt.title("IMF" + str(n+1))
  84. plt.xlabel("Time [s]")
  85. plt.tight_layout()
  86. plt.show()
  87. # 可以看出该方法将原始时间序列的两个模态完全分离了出来
  88. # ## EEMD
  89. #
  90. # Simplest case of using Esnembled EMD (EEMD) is by importing `EEMD` and passing your signal to `eemd` method.
  91. # In[13]:
  92. from PyEMD import EEMD
  93. import numpy as np
  94. import matplotlib.pyplot as plt
  95. # In[14]:
  96. # Define signal
  97. t = np.linspace(0,1,200)
  98. sin = lambda x,p: np.sin(2*np.pi*x*t+p)
  99. S = 3*sin(18,0.2)*(t-0.2)**2
  100. S += 5*sin(11,2.7)
  101. S += 3*sin(14,1.6)
  102. S += 1*np.sin(4*2*np.pi*(t-0.8)**2)
  103. S += t**2.1 - t
  104. # In[15]:
  105. # Assign EEMD to `eemd` variable
  106. eemd = EEMD()
  107. # In[16]:
  108. # Say we want to detect extrema using parabolic method
  109. emd = eemd.EMD
  110. emd.extrema_detection = 'parabol'
  111. # In[17]:
  112. # execute EEMD on S
  113. eIMFs = eemd.eemd(S,t)
  114. nIMFs = eIMFs.shape[0]
  115. # In[18]:
  116. # plot results
  117. plt.figure(figsize=(12,9))
  118. plt.subplot(nIMFs+1,1,1)
  119. plt.plot(t,S,'r')
  120. for n in range(nIMFs):
  121. plt.subplot(nIMFs+1,1,n+2)
  122. plt.plot(t,eIMFs[n],'g')
  123. plt.ylabel("eIMF %i" %(n+1))
  124. plt.locator_params(axis='y',nbins=5)
  125. plt.xlabel("Time [s]")
  126. plt.tight_layout()
  127. plt.show()
  128. # 原始信号被分出了6个IMF,而原始信号刚好是由6个不同部分组成的,可以比较一下他们的结果
  129. # In[19]:
  130. def get_part(t,num=0):
  131. if num==0:
  132. return 3*sin(18,0.2)*(t-0.2)**2
  133. elif num==1:
  134. return 5*sin(11,2.7)
  135. elif num==2:
  136. return 3*sin(14,1.6)
  137. elif num==3:
  138. return 1*np.sin(4*2*np.pi*(t-0.8)**2)
  139. elif num==4:
  140. return t**2.1
  141. elif num==5:
  142. return t
  143. # In[20]:
  144. part = [get_part(t,num) for num in range(6)]
  145. # In[21]:
  146. plt.plot(part[0],label='part')
  147. plt.plot(eIMFs[0],label='IMF')
  148. plt.legend()
  149. # 似乎并没有完全对应上,关于本征模态分量的含义还需要进一步地进行理解
  150. # In[ ]:
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...