forked from EmmaW-0731/Spline-EMD-vs.-IRMF-Test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpline EMD Visualization.py
More file actions
372 lines (295 loc) · 11.2 KB
/
Copy pathSpline EMD Visualization.py
File metadata and controls
372 lines (295 loc) · 11.2 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/python
# coding: UTF-8
import logging
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import argrelextrema
from PyEMD import EMD
# ============================================================
# Plot settings
# ============================================================
plt.rcParams["figure.facecolor"] = "none"
plt.rcParams["axes.facecolor"] = "none"
def save_current_plot(title):
plt.show()
# ============================================================
# Colors
# ============================================================
color_data = "#1f77b4"
color_maxima = "#ff0000"
color_minima = "#0000ff"
color_upper = "#ff7f0e"
color_lower = "#2ca02c"
color_mean = "#d62728"
color_imf = "#00cfd5"
color_res = "#444444"
LINE_WIDTH = 2.0
trim = slice(2, -3)
# ============================================================
# Helper functions for visualization
# ============================================================
def count_zero_crossings(signal):
return int(np.sum(signal[:-1] * signal[1:] < 0))
def plot_sifting_iteration(
T,
original_residual,
current_signal,
proto_imf,
upper,
lower,
mean,
imf_no,
iteration,
):
current_residual = original_residual - proto_imf
max_peaks = argrelextrema(current_signal, np.greater)[0]
min_peaks = argrelextrema(current_signal, np.less)[0]
fig, axes = plt.subplots(2, 1, figsize=(18, 9), facecolor="none")
ax = axes[0]
ax.plot(T[trim], current_signal[trim], color=color_data, linewidth=LINE_WIDTH)
ax.plot(T[trim], upper[trim], color=color_upper, linewidth=LINE_WIDTH)
ax.plot(T[trim], lower[trim], color=color_lower, linewidth=LINE_WIDTH)
ax.plot(T[trim], mean[trim], color=color_mean, linewidth=LINE_WIDTH)
ax.plot(T[trim], proto_imf[trim], color=color_imf, linewidth=LINE_WIDTH)
trim_start = 2
trim_end = len(current_signal) - 3
max_peaks_trimmed = [p for p in max_peaks if trim_start <= p < trim_end]
min_peaks_trimmed = [p for p in min_peaks if trim_start <= p < trim_end]
ax.scatter(
T[max_peaks_trimmed],
current_signal[max_peaks_trimmed],
c=color_maxima,
s=25,
zorder=5,
)
ax.scatter(
T[min_peaks_trimmed],
current_signal[min_peaks_trimmed],
c=color_minima,
s=25,
zorder=5,
)
ax.set_title(f"IMF {imf_no}; sifting iteration {iteration}")
ax.set_facecolor("none")
ax = axes[1]
ax.plot(T[trim], current_residual[trim], color=color_res, linewidth=LINE_WIDTH)
ax.set_title("Current residual")
ax.set_facecolor("none")
plt.tight_layout()
save_current_plot(f"04 IMF {imf_no} sifting iteration {iteration:02d}")
def generate_sifting_process_plots(S, T, emd, max_imf=-1):
"""
Visualization-only PyEMD-style sifting loop.
The final decomposition is still computed by emd.emd().
"""
T_index = np.arange(len(S), dtype=S.dtype)
IMF = np.empty((0, len(S)))
imf_no = 0
finished = False
extNo = -1
while not finished:
residue = S - np.sum(IMF[:imf_no], axis=0)
imf = residue.copy()
n = 0
n_h = 0
while True:
n += 1
if n >= emd.MAX_ITERATION:
print(f"IMF {imf_no + 1}: max iterations reached.")
break
ext_res = emd.find_extrema(T_index, imf)
max_pos, min_pos, indzer = ext_res[0], ext_res[2], ext_res[4]
extNo = len(max_pos) + len(min_pos)
nzm = len(indzer)
if extNo > 2:
max_env, min_env, eMax, eMin = emd.extract_max_min_spline(T_index, imf)
mean = 0.5 * (max_env + min_env)
imf_old = imf.copy()
imf = imf - mean
plot_sifting_iteration(
T=T,
original_residual=residue,
current_signal=imf_old,
proto_imf=imf,
upper=max_env,
lower=min_env,
mean=mean,
imf_no=imf_no + 1,
iteration=n,
)
if emd.FIXE:
if n >= emd.FIXE:
break
elif emd.FIXE_H:
tmp_residue = emd.find_extrema(T_index, imf)
max_pos_new, min_pos_new, ind_zer_new = (
tmp_residue[0],
tmp_residue[2],
tmp_residue[4],
)
extNo = len(max_pos_new) + len(min_pos_new)
nzm = len(ind_zer_new)
if n == 1:
continue
n_h = n_h + 1 if abs(extNo - nzm) < 2 else 0
if n_h >= emd.FIXE_H:
print(
f"IMF {imf_no + 1} stopped by FIXE_H={emd.FIXE_H} "
f"at iteration {n}"
)
break
else:
break
else:
finished = True
break
IMF = np.vstack((IMF, imf.copy()))
imf_no += 1
if emd.end_condition(S, IMF) or imf_no == max_imf:
finished = True
break
print("Finished generating double-panel sifting plots.")
# ============================================================
# Main
# ============================================================
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
max_imf = -1
DTYPE = np.float64
# --------------------------------------------------------
# Your signal
# --------------------------------------------------------
np.random.seed(100)
data = np.random.random(100) - 0.5
data = data.astype(DTYPE)
N = len(data)
T = np.arange(N, dtype=DTYPE)
tMin, tMax = 0, N - 1
S = data.copy()
print("Input S.dtype:", S.dtype)
print("Signal length:", N)
# --------------------------------------------------------
# PyEMD settings
# --------------------------------------------------------
emd = EMD()
emd.FIXE_H = 5
emd.nbsym = 2
emd.spline_kind = "cubic"
emd.extrema_detection = "simple"
emd.DTYPE = DTYPE
# ========================================================
# Generate double-panel sifting process plots
# ========================================================
generate_sifting_process_plots(
S=S,
T=T,
emd=emd,
max_imf=max_imf,
)
# ========================================================
# Run official PyEMD decomposition
# ========================================================
imfs_with_residue = emd.emd(S, T, max_imf)
imfs, residue = emd.get_imfs_and_residue()
imfNo = imfs.shape[0]
print("Number of IMFs:", imfNo)
print("nbsym:", emd.nbsym)
print("FIXE_H:", emd.FIXE_H)
print("spline_kind:", emd.spline_kind)
# ========================================================
# 1. Original signal
# ========================================================
plt.figure(figsize=(18, 6), facecolor="none")
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.title("Original random signal")
plt.xlabel("index")
plt.ylabel("value")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("01 original random signal")
# ========================================================
# 2. Local extrema of original signal
# ========================================================
max_peaks = argrelextrema(S, np.greater)[0]
min_peaks = argrelextrema(S, np.less)[0]
plt.figure(figsize=(18, 6), facecolor="none")
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.scatter(T[max_peaks], S[max_peaks], c=color_maxima, s=30)
plt.scatter(T[min_peaks], S[min_peaks], c=color_minima, s=30)
plt.title("Find local extrema")
plt.xlabel("index")
plt.ylabel("value")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("02 local extrema original signal")
# ========================================================
# 3. First envelope construction
# ========================================================
try:
T_index = np.arange(N, dtype=DTYPE)
max_env, min_env, eMax, eMin = emd.extract_max_min_spline(T_index, S)
mean_env = 0.5 * (max_env + min_env)
plt.figure(figsize=(18, 6), facecolor="none")
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.plot(T, max_env, color=color_upper, linewidth=LINE_WIDTH)
plt.plot(T, min_env, color=color_lower, linewidth=LINE_WIDTH)
plt.plot(T, mean_env, color=color_mean, linewidth=LINE_WIDTH)
plt.title("Cubic spline envelopes with PyEMD mirror extension")
plt.xlabel("index")
plt.ylabel("value")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("03 first envelope construction")
imf1_one_step = S - mean_env
plt.figure(figsize=(18, 6), facecolor="none")
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.plot(T, imf1_one_step, color=color_imf, linewidth=LINE_WIDTH)
plt.plot(T, mean_env, color=color_mean, linewidth=LINE_WIDTH)
plt.title("IMF1: one sifting step")
plt.xlabel("index")
plt.ylabel("value")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("03b IMF1 one sifting step")
except Exception as exc:
print("Envelope plot skipped:", exc)
# ========================================================
# 5. Decomposition summary
# ========================================================
rows = imfNo + 2
plt.figure(figsize=(18, 2.8 * rows), facecolor="none")
plt.subplot(rows, 1, 1)
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.xlim((tMin, tMax))
plt.title("Original random signal")
plt.ylabel("signal")
plt.gca().set_facecolor("none")
for k in range(imfNo):
plt.subplot(rows, 1, k + 2)
plt.plot(T, imfs[k], color=color_imf, linewidth=LINE_WIDTH)
plt.xlim((tMin, tMax))
plt.ylabel(f"IMF {k + 1}")
plt.gca().set_facecolor("none")
plt.subplot(rows, 1, rows)
plt.plot(T, residue, color=color_res, linewidth=LINE_WIDTH)
plt.xlim((tMin, tMax))
plt.ylabel("residue")
plt.xlabel("index")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("05 Spline EMD decomposition random signal")
# ========================================================
# 6. Reconstruction check
# ========================================================
reconstruction = np.sum(imfs, axis=0) + residue
recon_error = np.max(np.abs(S - reconstruction))
print("Max reconstruction error:", recon_error)
plt.figure(figsize=(18, 6), facecolor="none")
plt.plot(T, S, color=color_data, linewidth=LINE_WIDTH)
plt.plot(T, reconstruction, "--", color=color_res, linewidth=LINE_WIDTH)
plt.title("Reconstruction check")
plt.xlabel("index")
plt.ylabel("value")
plt.gca().set_facecolor("none")
plt.tight_layout()
save_current_plot("06 reconstruction check")