我需要使用Python重现一篇论文中使用了MODWT的结果。目前我正在使用pywt,但它只包含平稳小波变换(SWT)。我进行了一些研究,似乎目前没有专门用于MODWT的Python软件包,并且许多人认为SWT和MODWT是相同的。但是,使用MATLAB的MODWT结果与使用Python的SWT结果不同。是否有Python软件包可以直接使用MODWT?或者我可以使用SWT实现MODWT的结果吗?
最近,我读了一本有关小波的书:
Percival, D. B. 和 A. T. Walden 所著的《小波时间序列分析方法》(Wavelet Methods for Time Series Analysis),剑桥大学出版社,2000年
在阅读后,我使用Python实现了modwt和多尺度分析算法。你可以在GitHub这里找到Python代码。希望这对你有所帮助。
import matlab.engine
future = matlab.engine.start_matlab(background=True) # Starts Matlab on the background, meanwhile Python can execute more commands
import numpy as np
x = np.sin(2 * np.pi * np.arange(100) / 20) + np.random.randn(100)
# Transfiere la señal desde Python a MATLAB
eng.workspace['x'] = x
# Realizar la MODWT en MATLAB
wavelet = 'db4' # Nombre de la wavelet (puedes cambiarlo)
level = 5 # Nivel de descomposición
# Llama a la función modwt de MATLAB
eng.eval(f"wcoefs = modwt(x, '{wavelet}', {level})", nargout=0)
# Recupera los coeficientes de detalle y aproximación desde MATLAB
eng.eval("detail_coefficients = wcoefs(end, :)", nargout=0)
eng.eval("approximation_coefficients = wcoefs(1, :)", nargout=0)
# Transfiere los resultados desde MATLAB a Python
detail_coefficients = eng.workspace['detail_coefficients']
approximation_coefficients = eng.workspace['approximation_coefficients']
# Puedes imprimir o graficar los coeficientes según tus necesidades
print("Coeficientes de detalle en el nivel", level, ":", detail_coefficients)
print("Coeficientes de aproximación:", approximation_coefficients)
eng.quit()