Wavelet: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
<pre> | <pre> | ||
; Create a tplot variable that contains a wave. | |||
t = FINDGEN(4000) | t = FINDGEN(4000) | ||
time = time_double('2010-01-01') + 10*t | time = time_double('2010-01-01') + 10*t | ||
Line 11: | Line 12: | ||
data2 = sin(2*!pi*t/64.) | data2 = sin(2*!pi*t/64.) | ||
data[1000:3000] = data2[1000:3000] | data[1000:3000] = data2[1000:3000] | ||
var = 'sin_wav' | var = 'sin_wav' | ||
store_data, var, data={x:time, y:data} | store_data, var, data={x:time, y:data} | ||
; Apply wavelet transformation. | |||
wav_data, var | |||
; Plot the wave and the result of the transformation. | |||
pvar = 'sin_wav_wv_pow' | pvar = 'sin_wav_wv_pow' | ||
tplot, [var, pvar] | tplot, [var, pvar] | ||
</pre> | </pre> | ||
Line 30: | Line 33: | ||
import numpy as np | import numpy as np | ||
import pytplot | import pytplot | ||
import | from pyspedas import time_float | ||
from pyspedas.analysis.wavelet import wavelet | from pyspedas.analysis.wavelet import wavelet | ||
# Create a tplot variable that contains a wave. | |||
t = np.arange(4000.) | t = np.arange(4000.) | ||
y = np.sin(2*np.pi*t/32.) | y = np.sin(2*np.pi*t/32.) | ||
y2 = np.sin(2*np.pi*t/64.) | y2 = np.sin(2*np.pi*t/64.) | ||
y[1000:3000] = y2[1000:3000] | y[1000:3000] = y2[1000:3000] | ||
var = 'sin_wav' | var = 'sin_wav' | ||
time = | time = time_float('2010-01-01') + 10*t | ||
pytplot.store_data(var, data={'x':time, 'y':y}) | pytplot.store_data(var, data={'x':time, 'y':y}) | ||
# Complex Morlet wavelets transformation | # Complex Morlet wavelets transformation. | ||
powervar = wavelet(var, wavename='cmorl0.5-1.0') | powervar = wavelet(var, wavename='cmorl0.5-1.0') | ||
pvar = powervar[0] | pvar = powervar[0] | ||
# Define plotting parameters and plot. | |||
pytplot.options(pvar, 'colormap', 'jet') | pytplot.options(pvar, 'colormap', 'jet') | ||
pytplot.ylim(pvar, 0.001, 0.1) | pytplot.ylim(pvar, 0.001, 0.1) | ||
Line 58: | Line 59: | ||
[[File:py_sin_wavelet.png|400px|||pyspedas spectrogram example]] | [[File:py_sin_wavelet.png|400px|||pyspedas spectrogram example]] | ||
With a small change | With a small change to the python code we can use a different wavelet, for example the Gaussian Derivative wavelet. | ||
<pre> | <pre> | ||
import numpy as np | import numpy as np | ||
import pytplot | import pytplot | ||
import | from pyspedas import time_float | ||
from pyspedas.analysis.wavelet import wavelet | from pyspedas.analysis.wavelet import wavelet | ||
# Create a tplot variable that contains a wave. | |||
t = np.arange(4000.) | t = np.arange(4000.) | ||
y = np.sin(2*np.pi*t/32.) | y = np.sin(2*np.pi*t/32.) | ||
y2 = np.sin(2*np.pi*t/64.) | y2 = np.sin(2*np.pi*t/64.) | ||
y[1000:3000] = y2[1000:3000] | y[1000:3000] = y2[1000:3000] | ||
var = 'sin_wav' | var = 'sin_wav' | ||
time = | time = time_float('2010-01-01') + 10*t | ||
pytplot.store_data(var, data={'x':time, 'y':y}) | pytplot.store_data(var, data={'x':time, 'y':y}) | ||
# Gaussian Derivative wavelets transformation | # Gaussian Derivative wavelets transformation. | ||
powervar = wavelet(var, wavename='gaus1') | powervar = wavelet(var, wavename='gaus1') | ||
pvar = powervar[0] | pvar = powervar[0] | ||
# Define plotting parameters and plot. | |||
pytplot.options(pvar, 'colormap', 'jet') | pytplot.options(pvar, 'colormap', 'jet') | ||
pytplot.ylim(pvar, 0.001, 0.1) | pytplot.ylim(pvar, 0.001, 0.1) | ||
Line 88: | Line 87: | ||
pytplot.tplot([var, pvar]) | pytplot.tplot([var, pvar]) | ||
</pre> | </pre> | ||
[[File:py_gaus1_wavelet.png|400px|||pyspedas spectrogram example]] | [[File:py_gaus1_wavelet.png|400px|||pyspedas spectrogram example]] |
Latest revision as of 18:00, 3 May 2020
Compare IDL code to python code for the wavelet transformation of a simple wave function.
IDL code: SPEDAS
The SPEDAS IDL code uses Complex Morlet wavelets internally, and computes the resulting power.
; Create a tplot variable that contains a wave. t = FINDGEN(4000) time = time_double('2010-01-01') + 10*t data = sin(2*!pi*t/32.) data2 = sin(2*!pi*t/64.) data[1000:3000] = data2[1000:3000] var = 'sin_wav' store_data, var, data={x:time, y:data} ; Apply wavelet transformation. wav_data, var ; Plot the wave and the result of the transformation. pvar = 'sin_wav_wv_pow' tplot, [var, pvar]
Python code: pySPEDAS
Similar code in python uses the pywavelets library. This example uses the Complex Morlet wavelets.
import numpy as np import pytplot from pyspedas import time_float from pyspedas.analysis.wavelet import wavelet # Create a tplot variable that contains a wave. t = np.arange(4000.) y = np.sin(2*np.pi*t/32.) y2 = np.sin(2*np.pi*t/64.) y[1000:3000] = y2[1000:3000] var = 'sin_wav' time = time_float('2010-01-01') + 10*t pytplot.store_data(var, data={'x':time, 'y':y}) # Complex Morlet wavelets transformation. powervar = wavelet(var, wavename='cmorl0.5-1.0') pvar = powervar[0] # Define plotting parameters and plot. pytplot.options(pvar, 'colormap', 'jet') pytplot.ylim(pvar, 0.001, 0.1) pytplot.options(pvar, 'ylog', True) pytplot.options(pvar, 'ytitle', pvar) pytplot.tplot([var, pvar])
With a small change to the python code we can use a different wavelet, for example the Gaussian Derivative wavelet.
import numpy as np import pytplot from pyspedas import time_float from pyspedas.analysis.wavelet import wavelet # Create a tplot variable that contains a wave. t = np.arange(4000.) y = np.sin(2*np.pi*t/32.) y2 = np.sin(2*np.pi*t/64.) y[1000:3000] = y2[1000:3000] var = 'sin_wav' time = time_float('2010-01-01') + 10*t pytplot.store_data(var, data={'x':time, 'y':y}) # Gaussian Derivative wavelets transformation. powervar = wavelet(var, wavename='gaus1') pvar = powervar[0] # Define plotting parameters and plot. pytplot.options(pvar, 'colormap', 'jet') pytplot.ylim(pvar, 0.001, 0.1) pytplot.options(pvar, 'ylog', True) pytplot.options(pvar, 'ytitle', pvar) pytplot.tplot([var, pvar])