質問編集履歴
1
ソースコードとエラー文の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -29,3 +29,259 @@
|
|
29
29
|
のMorlet関数になります。
|
30
30
|
|
31
31
|
余りに初歩的な問題かもしれませんが、宜しくお願いします。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
質問がありましたので、ソースコード全体とエラー文を追記します。
|
36
|
+
|
37
|
+
ソースコードから
|
38
|
+
|
39
|
+
```python
|
40
|
+
|
41
|
+
#ライブラリ読み込み
|
42
|
+
|
43
|
+
from __future__ import division
|
44
|
+
|
45
|
+
import numpy as np
|
46
|
+
|
47
|
+
import matplotlib.pyplot as plt
|
48
|
+
|
49
|
+
import pycwt as wavelet
|
50
|
+
|
51
|
+
from pycwt.helpers import find
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#分析データ読み込み
|
56
|
+
|
57
|
+
url = "http://paos.colorado.edu/research/wavelets/wave_idl/nino3sst.txt"
|
58
|
+
|
59
|
+
dat = np.genfromtxt(url, skip_header=19)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
#メタデータ設定
|
64
|
+
|
65
|
+
title = "xion t"
|
66
|
+
|
67
|
+
label = "xion sst"
|
68
|
+
|
69
|
+
unit = "deg"
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
t0 = 1871.0
|
74
|
+
|
75
|
+
dt = 0.25
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
#時間データ生成
|
80
|
+
|
81
|
+
N = dat.size
|
82
|
+
|
83
|
+
t = np.arange(0,N) * dt + t0
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
#分析対象データの整形
|
88
|
+
|
89
|
+
p = np.polyfit(t - t0, dat, 1)
|
90
|
+
|
91
|
+
dat_notrend = dat - np.polyval(p, t - t0)
|
92
|
+
|
93
|
+
std = dat_notrend.std()#標準偏差
|
94
|
+
|
95
|
+
var = std ** 2#分散
|
96
|
+
|
97
|
+
dat_norm = dat_notrend / std#正規化
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
#ウェーブレット変換のパラメタ設定
|
102
|
+
|
103
|
+
mother = mothers.Morlet(6)
|
104
|
+
|
105
|
+
s0 = 2 * dt
|
106
|
+
|
107
|
+
dj = 1 / 12
|
108
|
+
|
109
|
+
J = 7 / dj
|
110
|
+
|
111
|
+
alpha, _, _ = wavelet.ar1(dat)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
#ウェーブレット変換と逆ウェーブレット変換
|
116
|
+
|
117
|
+
wave, scales, freqs, coi, fft, fftfreqs = wavelet.cwt(dat_norm, dt, dj, s0, J, mother)
|
118
|
+
|
119
|
+
iwave = wavelet.icwt(wave, scales, dt, dj, mother) * std
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
#ウェーブレットとフーリエの各スペクトル算出
|
124
|
+
|
125
|
+
power = (np.abs(wave)) ** 2
|
126
|
+
|
127
|
+
fft_power = np.abs(fft) ** 2
|
128
|
+
|
129
|
+
period = 1 / freqs
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
#規格化
|
134
|
+
|
135
|
+
power /= scales[:, None]
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
#パワースペクトル95%信頼区間での優位性検証
|
140
|
+
|
141
|
+
signif, fft_theor = wavelet.significance(1.0, dt, scales, 0, alpha, significance_lavel=0.95, wavelet=mother)
|
142
|
+
|
143
|
+
sig95 = np.ones([1, N]) * signif[:, None]
|
144
|
+
|
145
|
+
sig95 = power / sig95
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
#グローバルウェーブレットスペクトルとその優位性の算出
|
150
|
+
|
151
|
+
glbl_power = power.mean(axis=1)
|
152
|
+
|
153
|
+
dof = N - scales
|
154
|
+
|
155
|
+
glbl_signif, tmp = wavelet.signifcance(var, dt, scales, 1, alpha, significance_lavel=0.95, dof=dof, wavelet=mother)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
#スケールの平均値とその優位性の算出
|
160
|
+
|
161
|
+
sel = find((period >=2) & (period < 8))
|
162
|
+
|
163
|
+
Cdelta = mother.cdelta
|
164
|
+
|
165
|
+
scale_avg = (scales * np.ones((N, 1))).transpose()
|
166
|
+
|
167
|
+
scale_avg = power / scale_avg
|
168
|
+
|
169
|
+
scale_avg = var * dj * dt / Cdelta * scale_avg[sel, :].sum(axis=0)
|
170
|
+
|
171
|
+
scale_avg_signif, tmp = wavelet.significance(var, dt, scales, 2, alpha, significance_lavel=0.95, dof=[scales[sel[0]],scales[sel[-1]]], wavelet=mother)
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
#ウェーブレット解析結果の可視化
|
176
|
+
|
177
|
+
plt.close("all")
|
178
|
+
|
179
|
+
plt.ioff()
|
180
|
+
|
181
|
+
figprops = dict(figsize=(11, 8), dpi=72)
|
182
|
+
|
183
|
+
fig = plt.figure(**figprops)
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
ax = plt.axes([0.1, 0.75, 0.65, 0.2])
|
188
|
+
|
189
|
+
ax.plot(t, iwave, "-", linewidth=1, color=[0.5, 0.5, 0.5])
|
190
|
+
|
191
|
+
ax.plot(t, dat, "k", linewidth=1.5)
|
192
|
+
|
193
|
+
ax.set_title("a) {}".format(title))
|
194
|
+
|
195
|
+
ax.set_ylabel(r"{} [{}]".format(lavel, units))
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
bx = plt.axes([0.1, 0.37, 0.65, 0.28], sharex=ax)
|
200
|
+
|
201
|
+
levels = [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16]
|
202
|
+
|
203
|
+
bx.contourf(t, np.log2(period), np.log2(power), np.log2(levels), extend="both", cmap=plt.cm.viridis)
|
204
|
+
|
205
|
+
extent = [t.min(), t.max(), 0, max(period)]
|
206
|
+
|
207
|
+
bx.contour(t, np.log2(period), sig95, [-99, 1], colors="k", linewidths=2, extent=extent)
|
208
|
+
|
209
|
+
bx.fill(np.concatenate([t, t[-1:], +dt, t[-1:], + dt, t[:1] - dt, t[:1] - dt]),
|
210
|
+
|
211
|
+
np.concatenate(np.log2(coi), [1e-9], np.log2(period[-1:]), np.log2(period[-1:]), [1e-9]), "k", alpha=0.3, hatch="x")
|
212
|
+
|
213
|
+
bx.set_title("b){}Wavelet Power Spectrum ({})".format(label, mother.name))
|
214
|
+
|
215
|
+
bx.set_ylabel("Period (years)")
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
Yticks = 2 ** np.arange(np.ceil(np.log2(period.min())), np.ciel(period.max()))
|
220
|
+
|
221
|
+
bx.set_yticks(np.log2(Yticks))
|
222
|
+
|
223
|
+
bx.set_yticklabels(Yticks)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
cx = plt.axes([0.77, 0.37, 0.2, 0.28], sharey=bx)
|
228
|
+
|
229
|
+
cx.plot(glbl_signif, np.log2(period), "k--")
|
230
|
+
|
231
|
+
cx.plot(var * fft_theor, np.log2(period), "--", color="#ccccc")
|
232
|
+
|
233
|
+
cx.plot(var * fft_power, np.log2(1./fftfreqs), "-", color="#ccccc", linewidth=1.)
|
234
|
+
|
235
|
+
cx.plot(var * glbl_power, np.log2(period), "k-", linewidth=1.5)
|
236
|
+
|
237
|
+
cx.set_title("c) Global Wavelet Spectrum")
|
238
|
+
|
239
|
+
cx.set_xlabel(r"Power [({})^2]".format(units))
|
240
|
+
|
241
|
+
cx.set_xlim([0, glbl_power.max() +var])
|
242
|
+
|
243
|
+
cx.set_ylim(np.log2([period.min(), period.max()]))
|
244
|
+
|
245
|
+
cx.set_yticks(np.log2(Yticks))
|
246
|
+
|
247
|
+
cx.set_yticklabels(Yticks)
|
248
|
+
|
249
|
+
plt.setp(cx.get_yticklabels(), visible=False)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
dx = plt.axes([0.1, 0.07, 0.65, 0.2], sharex=ax)
|
254
|
+
|
255
|
+
dx.axhline(scale_avg_signif, color="k", linestyle="--", linewidth=1.)
|
256
|
+
|
257
|
+
dx.plot(t, scale_avg, "k-", linewidth=1.5)
|
258
|
+
|
259
|
+
dx.set_title("d) {}--{} year scale-averaged power".format(2, 8))
|
260
|
+
|
261
|
+
dx.set_xlabel("Time (year)")
|
262
|
+
|
263
|
+
dx.set_ylabel(r"Average variance [{}]".format(units))
|
264
|
+
|
265
|
+
ax.set_xlim([t.min(), i.max()])
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
plt.show()
|
270
|
+
|
271
|
+
```
|
272
|
+
|
273
|
+
エラー文
|
274
|
+
|
275
|
+
```python
|
276
|
+
|
277
|
+
Traceback (most recent call last):
|
278
|
+
|
279
|
+
File "Z:\03作業用フォルダ\学生\テキスト読み込みできるwavelet変換\wavelet_power_spectle.py", line 6, in <module>
|
280
|
+
|
281
|
+
from pycwt.helpers import find
|
282
|
+
|
283
|
+
ModuleNotFoundError: No module named 'pycwt.helpers'
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
以上です。よろしくお願いします。
|