本文整理汇总了Python中pywt.Wavelet方法的典型用法代码示例。如果您正苦于以下问题:Python pywt.Wavelet方法的具体用法?Python pywt.Wavelet怎么用?Python pywt.Wavelet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pywt
的用法示例。
在下文中一共展示了pywt.Wavelet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _wavelet_coefs
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def _wavelet_coefs(data, wavelet_name='db4'):
"""Compute Discrete Wavelet Transform coefficients.
Parameters
----------
data : ndarray, shape (n_channels, n_times)
wavelet_name : str (default: db4)
Wavelet name (to be used with ``pywt.Wavelet``). The full list of
Wavelet names are given by: ``[name for family in pywt.families() for
name in pywt.wavelist(family)]``.
Returns
-------
coefs : list of ndarray
Coefficients of a DWT (Discrete Wavelet Transform). ``coefs[0]`` is
the array of approximation coefficient and ``coefs[1:]`` is the list
of detail coefficients.
"""
wavelet = pywt.Wavelet(wavelet_name)
levdec = min(pywt.dwt_max_level(data.shape[-1], wavelet.dec_len), 6)
coefs = pywt.wavedec(data, wavelet=wavelet, level=levdec)
return coefs
示例2: compute_wavelet_feature_vector
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def compute_wavelet_feature_vector(image, wavelet='db6'):
image_np = np.array(image)
rgb = [image_np[:, :, i] for i in (0, 1, 2)]
if isinstance(wavelet, basestring):
wavelet = pywt.Wavelet(wavelet)
feature_vector = []
for c in rgb:
level = pywt.dwt_max_level(min(c.shape[0], c.shape[1]), wavelet.dec_len)
levels = pywt.wavedec2(c, wavelet, mode='sym', level=level)
for coeffs in levels:
if not isinstance(coeffs, tuple):
coeffs = (coeffs,)
for w in coeffs:
w_flat = w.flatten()
feature_vector += [float(np.mean(w_flat)), float(np.std(w_flat))]
return feature_vector
示例3: check_coefficients
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def check_coefficients(wavelet):
epsilon = 5e-11
level = 10
w = pywt.Wavelet(wavelet)
# Lowpass filter coefficients sum to sqrt2
res = np.sum(w.dec_lo)-np.sqrt(2)
msg = ('[RMS_REC > EPSILON] for Wavelet: %s, rms=%.3g' % (wavelet, res))
assert_(res < epsilon, msg=msg)
# sum even coef = sum odd coef = 1 / sqrt(2)
res = np.sum(w.dec_lo[::2])-1./np.sqrt(2)
msg = ('[RMS_REC > EPSILON] for Wavelet: %s, rms=%.3g' % (wavelet, res))
assert_(res < epsilon, msg=msg)
res = np.sum(w.dec_lo[1::2])-1./np.sqrt(2)
msg = ('[RMS_REC > EPSILON] for Wavelet: %s, rms=%.3g' % (wavelet, res))
assert_(res < epsilon, msg=msg)
# Highpass filter coefficients sum to zero
res = np.sum(w.dec_hi)
msg = ('[RMS_REC > EPSILON] for Wavelet: %s, rms=%.3g' % (wavelet, res))
assert_(res < epsilon, msg=msg)
示例4: test_custom_wavelet
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_custom_wavelet():
haar_custom1 = pywt.Wavelet('Custom Haar Wavelet',
filter_bank=_CustomHaarFilterBank())
haar_custom1.orthogonal = True
haar_custom1.biorthogonal = True
val = np.sqrt(2) / 2
filter_bank = ([val]*2, [-val, val], [val]*2, [val, -val])
haar_custom2 = pywt.Wavelet('Custom Haar Wavelet',
filter_bank=filter_bank)
# check expected default wavelet properties
assert_(~haar_custom2.orthogonal)
assert_(~haar_custom2.biorthogonal)
assert_(haar_custom2.symmetry == 'unknown')
assert_(haar_custom2.family_name == '')
assert_(haar_custom2.short_family_name == '')
assert_(haar_custom2.vanishing_moments_phi == 0)
assert_(haar_custom2.vanishing_moments_psi == 0)
# Some properties can be set by the user
haar_custom2.orthogonal = True
haar_custom2.biorthogonal = True
示例5: test_wavedecn_coeff_reshape_even
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_wavedecn_coeff_reshape_even():
# verify round trip is correct:
# wavedecn - >coeffs_to_array-> array_to_coeffs -> waverecn
# This is done for wavedec{1, 2, n}
rng = np.random.RandomState(1234)
params = {'wavedec': {'d': 1, 'dec': pywt.wavedec, 'rec': pywt.waverec},
'wavedec2': {'d': 2, 'dec': pywt.wavedec2, 'rec': pywt.waverec2},
'wavedecn': {'d': 3, 'dec': pywt.wavedecn, 'rec': pywt.waverecn}}
N = 28
for f in params:
x1 = rng.randn(*([N] * params[f]['d']))
for mode in pywt.Modes.modes:
for wave in wavelist:
w = pywt.Wavelet(wave)
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len)
if maxlevel == 0:
continue
coeffs = params[f]['dec'](x1, w, mode=mode)
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs)
coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices,
output_format=f)
x1r = params[f]['rec'](coeffs2, w, mode=mode)
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4)
示例6: test_waverecn_coeff_reshape_odd
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_waverecn_coeff_reshape_odd():
# verify round trip is correct:
# wavedecn - >coeffs_to_array-> array_to_coeffs -> waverecn
rng = np.random.RandomState(1234)
x1 = rng.randn(35, 33)
for mode in pywt.Modes.modes:
for wave in ['haar', ]:
w = pywt.Wavelet(wave)
maxlevel = pywt.dwt_max_level(np.min(x1.shape), w.dec_len)
if maxlevel == 0:
continue
coeffs = pywt.wavedecn(x1, w, mode=mode)
coeff_arr, coeff_slices = pywt.coeffs_to_array(coeffs)
coeffs2 = pywt.array_to_coeffs(coeff_arr, coeff_slices)
x1r = pywt.waverecn(coeffs2, w, mode=mode)
# truncate reconstructed values to original shape
x1r = x1r[[slice(s) for s in x1.shape]]
assert_allclose(x1, x1r, rtol=1e-4, atol=1e-4)
示例7: test_swt_dtypes
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_swt_dtypes():
wavelet = pywt.Wavelet('haar')
for dt_in, dt_out in zip(dtypes_in, dtypes_out):
errmsg = "wrong dtype returned for {0} input".format(dt_in)
# swt
x = np.ones(8, dtype=dt_in)
(cA2, cD2), (cA1, cD1) = pywt.swt(x, wavelet, level=2)
assert_(cA2.dtype == cD2.dtype == cA1.dtype == cD1.dtype == dt_out,
"swt: " + errmsg)
# swt2
with warnings.catch_warnings():
warnings.simplefilter('ignore', FutureWarning)
x = np.ones((8, 8), dtype=dt_in)
cA, (cH, cV, cD) = pywt.swt2(x, wavelet, level=1)[0]
assert_(cA.dtype == cH.dtype == cV.dtype == cD.dtype == dt_out,
"swt2: " + errmsg)
示例8: test_swt2_axes
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_swt2_axes():
atol = 1e-14
current_wavelet = pywt.Wavelet('db2')
input_length_power = int(np.ceil(np.log2(max(
current_wavelet.dec_len,
current_wavelet.rec_len))))
input_length = 2**(input_length_power)
X = np.arange(input_length**2).reshape(input_length, input_length)
with warnings.catch_warnings():
warnings.simplefilter('ignore', FutureWarning)
(cA1, (cH1, cV1, cD1)) = pywt.swt2(X, current_wavelet, level=1)[0]
# opposite order
(cA2, (cH2, cV2, cD2)) = pywt.swt2(X, current_wavelet, level=1,
axes=(1, 0))[0]
assert_allclose(cA1, cA2, atol=atol)
assert_allclose(cH1, cV2, atol=atol)
assert_allclose(cV1, cH2, atol=atol)
assert_allclose(cD1, cD2, atol=atol)
# duplicate axes not allowed
assert_raises(ValueError, pywt.swt2, X, current_wavelet, 1,
axes=(0, 0))
# too few axes
assert_raises(ValueError, pywt.swt2, X, current_wavelet, 1, axes=(0, ))
示例9: test_accuracy_pymatbridge
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_accuracy_pymatbridge():
rstate = np.random.RandomState(1234)
# max RMSE (was 1.0e-10, is reduced to 5.0e-5 due to different coefficents)
epsilon = 5.0e-5
epsilon_pywt_coeffs = 1.0e-10
mlab.start()
try:
for wavelet in wavelets:
w = pywt.Wavelet(wavelet)
mlab.set_variable('wavelet', wavelet)
for N in _get_data_sizes(w):
data = rstate.randn(N)
mlab.set_variable('data', data)
for pmode, mmode in modes:
ma, md = _compute_matlab_result(data, wavelet, mmode)
yield _check_accuracy, data, w, pmode, ma, md, wavelet, epsilon
ma, md = _load_matlab_result_pywt_coeffs(data, wavelet, mmode)
yield _check_accuracy, data, w, pmode, ma, md, wavelet, epsilon_pywt_coeffs
finally:
mlab.stop()
示例10: _compute_matlab_result
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def _compute_matlab_result(data, wavelet, mmode):
""" Compute the result using MATLAB.
This function assumes that the Matlab variables `wavelet` and `data` have
already been set externally.
"""
if np.any((wavelet == np.array(['coif6', 'coif7', 'coif8', 'coif9', 'coif10', 'coif11', 'coif12', 'coif13', 'coif14', 'coif15', 'coif16', 'coif17'])),axis=0):
w = pywt.Wavelet(wavelet)
mlab.set_variable('Lo_D', w.dec_lo)
mlab.set_variable('Hi_D', w.dec_hi)
mlab_code = ("[ma, md] = dwt(data, Lo_D, Hi_D, 'mode', '%s');" % mmode)
else:
mlab_code = "[ma, md] = dwt(data, wavelet, 'mode', '%s');" % mmode
res = mlab.run_code(mlab_code)
if not res['success']:
raise RuntimeError("Matlab failed to execute the provided code. "
"Check that the wavelet toolbox is installed.")
# need np.asarray because sometimes the output is a single float64
ma = np.asarray(mlab.get_variable('ma'))
md = np.asarray(mlab.get_variable('md'))
return ma, md
示例11: test_3D_reconstruct
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_3D_reconstruct():
data = np.array([
[[0, 4, 1, 5, 1, 4],
[0, 5, 26, 3, 2, 1],
[5, 8, 2, 33, 4, 9],
[2, 5, 19, 4, 19, 1]],
[[1, 5, 1, 2, 3, 4],
[7, 12, 6, 52, 7, 8],
[2, 12, 3, 52, 6, 8],
[5, 2, 6, 78, 12, 2]]])
wavelet = pywt.Wavelet('haar')
for mode in pywt.Modes.modes:
d = pywt.dwtn(data, wavelet, mode=mode)
assert_allclose(data, pywt.idwtn(d, wavelet, mode=mode),
rtol=1e-13, atol=1e-13)
示例12: test_stride
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_stride():
wavelet = pywt.Wavelet('haar')
for dtype in ('float32', 'float64'):
data = np.array([[0, 4, 1, 5, 1, 4],
[0, 5, 6, 3, 2, 1],
[2, 5, 19, 4, 19, 1]],
dtype=dtype)
for mode in pywt.Modes.modes:
expected = pywt.dwtn(data, wavelet)
strided = np.ones((3, 12), dtype=data.dtype)
strided[::-1, ::2] = data
strided_dwtn = pywt.dwtn(strided[::-1, ::2], wavelet)
for key in expected.keys():
assert_allclose(strided_dwtn[key], expected[key])
示例13: test_3D_reconstruct_complex
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_3D_reconstruct_complex():
# All dimensions even length so `take` does not need to be specified
data = np.array([
[[0, 4, 1, 5, 1, 4],
[0, 5, 26, 3, 2, 1],
[5, 8, 2, 33, 4, 9],
[2, 5, 19, 4, 19, 1]],
[[1, 5, 1, 2, 3, 4],
[7, 12, 6, 52, 7, 8],
[2, 12, 3, 52, 6, 8],
[5, 2, 6, 78, 12, 2]]])
data = data + 1j
wavelet = pywt.Wavelet('haar')
d = pywt.dwtn(data, wavelet)
# idwtn creates even-length shapes (2x dwtn size)
original_shape = [slice(None, s) for s in data.shape]
assert_allclose(data, pywt.idwtn(d, wavelet)[original_shape],
rtol=1e-13, atol=1e-13)
示例14: test_idwtn_missing
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_idwtn_missing():
# Test to confirm missing data behave as zeroes
data = np.array([
[0, 4, 1, 5, 1, 4],
[0, 5, 6, 3, 2, 1],
[2, 5, 19, 4, 19, 1]])
wavelet = pywt.Wavelet('haar')
coefs = pywt.dwtn(data, wavelet)
# No point removing zero, or all
for num_missing in range(1, len(coefs)):
for missing in combinations(coefs.keys(), num_missing):
missing_coefs = coefs.copy()
for key in missing:
del missing_coefs[key]
LL = missing_coefs.get('aa', None)
HL = missing_coefs.get('da', None)
LH = missing_coefs.get('ad', None)
HH = missing_coefs.get('dd', None)
assert_allclose(pywt.idwt2((LL, (HL, LH, HH)), wavelet),
pywt.idwtn(missing_coefs, 'haar'), atol=1e-15)
示例15: test_error_on_invalid_keys
# 需要导入模块: import pywt [as 别名]
# 或者: from pywt import Wavelet [as 别名]
def test_error_on_invalid_keys():
data = np.array([
[0, 4, 1, 5, 1, 4],
[0, 5, 6, 3, 2, 1],
[2, 5, 19, 4, 19, 1]])
wavelet = pywt.Wavelet('haar')
LL, (HL, LH, HH) = pywt.dwt2(data, wavelet)
# unexpected key
d = {'aa': LL, 'da': HL, 'ad': LH, 'dd': HH, 'ff': LH}
assert_raises(ValueError, pywt.idwtn, d, wavelet)
# mismatched key lengths
d = {'a': LL, 'da': HL, 'ad': LH, 'dd': HH}
assert_raises(ValueError, pywt.idwtn, d, wavelet)