[英]Estimate Power spectral density using Continuous wavelet transform form pycwt
I want to estimate the Power spectral density using Continuous wavelet transform and a Morlet Wavelet.我想使用连续小波变换和 Morlet 小波来估计功率谱密度。 Bellow you can find the function I am using.
您可以在下面找到我正在使用的 function。 Any comments or suggestions on whether or not the following code is correct?
关于以下代码是否正确的任何意见或建议?
import pycwt as wavelet
mother_wave_dict = {
'gaussian': wavelet.DOG(),
'paul': wavelet.Paul(),
'mexican_hat': wavelet.MexicanHat()
}
def trace_PSD_wavelet(x, dt, dj, mother_wave='morlet'):
"""
Method to calculate the power spectral density using wavelet method.
Parameters
----------
x : array-like
the components of the field to apply wavelet tranform
dt: int
the sampling time of the timeseries
dj: determines how many scales are used to estimate wavelet coeff
(e.g., for dj=1 -> 2**numb_scales
mother_wave: str
Returns
-------
db_x,db_y,db_zz: array-like
component coeficients of th wavelet tranform
freq : list
Frequency of the corresponding psd points.
psd : list
Power Spectral Density of the signal.
psd : list
The scales at which wavelet was estimated
"""
if mother_wave in mother_wave_dict.keys():
mother_morlet = mother_wave_dict[mother_wave]
else:
mother_morlet = wavelet.Morlet()
N = len(x)
db_x, _, freqs, _, _, _ = wavelet.cwt(x, dt, dj, wavelet=mother_morlet)
# Estimate trace powerspectral density
PSD = (np.nanmean(np.abs(db_x)**2, axis=1))*( 2*dt)
# Also estimate the scales to use later
scales = ((1/freqs)/dt)#.astype(int)
return db_x, freqs, PSD, scales
It is hard to answer this question without knowing what you mean by "correct".如果不知道“正确”是什么意思,很难回答这个问题。
As far as I understand, your code allows to:据我了解,您的代码允许:
I was able to run your code against an electrogram data example from the scipy
library and it ran as expected:我能够针对
scipy
库中的电图数据示例运行您的代码,并且它按预期运行:
from scipy.misc import electrocardiogram
ecg = electrocardiogram()
Since these data are sampled at 360Hz, I use dt=1/360:由于这些数据是以 360Hz 采样的,所以我使用 dt=1/360:
db_x, freqs, PSD, scales = trace_PSD_wavelet(ecg, 1/360, 1/24, 'morlet')
Plotting the output db_x
:绘制 output
db_x
:
fig = plt.imshow(np.abs(db_x), extent=[db_x.shape[1],db_x.shape[0],scales[-1],scales[]], aspect='auto')
plt.xlabel('time')
plt.ylabel('scale')
Plotting the corresponding "PSD":绘制相应的“PSD”:
What you call "PSD" measures the energy contained in the CWT-transformed data at each scale, averaged over the entire recording , 5 minutes of data in this example.你所谓的“PSD”测量每个尺度的 CWT 转换数据中包含的能量,在整个记录中平均,在这个例子中是 5 分钟的数据。 I am not sure how you plan to use this information but be careful that this is not the PSD of the original input time domain data.
我不确定您打算如何使用此信息,但请注意这不是原始输入时域数据的 PSD。
Finally, concerning the Python implementation, you may simplify the way you call the default wavelet.最后,关于 Python 实现,您可以简化调用默认小波的方式。 Simply add the Morlet wavelet to your dictionary:
只需将 Morlet 小波添加到您的字典中:
mother_wave_dict = {
'gaussian': wavelet.DOG(),
'paul': wavelet.Paul(),
'mexican_hat': wavelet.MexicanHat()
'morlet': wavelet.Morlet()
}
Then you can avoid the if
statement in your function and simply call:然后,您可以避免 function 中的
if
语句,只需调用:
mother_morlet = mother_wave_dict[mother_wave]
. mother_morlet = mother_wave_dict[mother_wave]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.