Python pywt.dwt_max_level() Examples
The following are 10
code examples of pywt.dwt_max_level().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pywt
, or try the search function
.
Example #1
Source File: utils.py From mne-features with BSD 3-Clause "New" or "Revised" License | 7 votes |
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
Example #2
Source File: wavelets.py From opensurfaces with MIT License | 6 votes |
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
Example #3
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
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)
Example #4
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 6 votes |
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)
Example #5
Source File: test_multilevel.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_wavedec(): x = [3, 7, 1, 1, -2, 5, 4, 6] db1 = pywt.Wavelet('db1') cA3, cD3, cD2, cD1 = pywt.wavedec(x, db1) assert_almost_equal(cA3, [8.83883476]) assert_almost_equal(cD3, [-0.35355339]) assert_allclose(cD2, [4., -3.5]) assert_allclose(cD1, [-2.82842712, 0, -4.94974747, -1.41421356]) assert_(pywt.dwt_max_level(len(x), db1) == 3)
Example #6
Source File: test__pywt.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_compare_downcoef_coeffs(): rstate = np.random.RandomState(1234) r = rstate.randn(16) # compare downcoef against wavedec outputs for nlevels in [1, 2, 3]: for wavelet in pywt.wavelist(): wavelet = pywt.DiscreteContinuousWavelet(wavelet) if isinstance(wavelet, pywt.Wavelet): max_level = pywt.dwt_max_level(r.size, wavelet.dec_len) if nlevels <= max_level: a = pywt.downcoef('a', r, wavelet, level=nlevels) d = pywt.downcoef('d', r, wavelet, level=nlevels) coeffs = pywt.wavedec(r, wavelet, level=nlevels) assert_allclose(a, coeffs[0]) assert_allclose(d, coeffs[1])
Example #7
Source File: test__pywt.py From DeepLearning_Wavelet-LSTM with MIT License | 5 votes |
def test_dwt_max_level(): assert_(pywt.dwt_max_level(16, 2) == 4) assert_(pywt.dwt_max_level(16, 8) == 1) assert_(pywt.dwt_max_level(16, 9) == 1) assert_(pywt.dwt_max_level(16, 10) == 0) assert_(pywt.dwt_max_level(16, 18) == 0)
Example #8
Source File: dtcwt.py From scikit-ued with MIT License | 5 votes |
def dt_max_level(data, first_stage, wavelet, axis=-1): """ Returns the maximum decomposition level possible from the dual-tree complex wavelet transform. Parameters ---------- data : ndarray Input data. Can be of any dimension. first_stage : str Wavelet to use for the first stage. See :func:`skued.available_first_stage_filters` for a list of suitable arguments wavelet : str Wavelet to use in stages > 1. Must be appropriate for the dual-tree complex wavelet transform. See :func:`skued.available_dt_filters` for possible values. axis : int, optional Axis over which to compute the transform. Default is -1 Returns ------- max_level : int Maximum decomposition level. """ real_wavelet, imag_wavelet = dualtree_wavelet(wavelet) return dwt_max_level( data_len=data.shape[axis], filter_len=max([real_wavelet.dec_len, imag_wavelet.dec_len]), )
Example #9
Source File: wavelet.py From sigpy with BSD 3-Clause "New" or "Revised" License | 4 votes |
def fwt(input, wave_name='db4', axes=None, level=None, apply_zpad=True): """Forward wavelet transform. Args: input (array): Input array. wave_name (str): Wavelet name. axes (None or tuple of int): Axes to perform wavelet transform. level (None or int): Number of wavelet levels. apply_zpad (bool): If true, zero-pad for linear convolution. """ device = backend.get_device(input) xp = device.xp if axes is None: axes = tuple([k for k in range(len(input.shape)) if input.shape[k] > 1]) if (type(axes) == int): axes = (axes,) wavdct = pywt.Wavelet(wave_name) dec_lo = xp.array(wavdct.dec_lo) dec_hi = xp.array(wavdct.dec_hi) if level is None: level = pywt.dwt_max_level( xp.min(xp.array([input.shape[ax] for ax in axes])), dec_lo.size) if level <= 0: return input assert level > 0 y = apply_dec_along_axis(input, axes, dec_lo, dec_hi, level, apply_zpad) approx_idx = tuple([slice(0, y.shape[k]//2) if k in axes else slice(0, None) for k in range(len(input.shape))]) y[approx_idx] = fwt(y[approx_idx], wave_name=wave_name, axes=axes, level=level-1, apply_zpad=False) return y
Example #10
Source File: wavelet.py From sigpy with BSD 3-Clause "New" or "Revised" License | 4 votes |
def iwt(input, oshape, wave_name='db4', axes=None, level=None, inplace=False): """Inverse wavelet transform. Args: input (array): Input array. oshape (tuple): Output shape. wave_name (str): Wavelet name. axes (None or tuple of int): Axes to perform wavelet transform. level (None or int): Number of wavelet levels. inplace (bool): Modify input array in place. """ device = backend.get_device(input) xp = device.xp if axes is None: axes = tuple([k for k in range(len(input.shape)) if input.shape[k] > 1]) if (type(axes) == int): axes = (axes,) wavdct = pywt.Wavelet(wave_name) rec_lo = xp.array(wavdct.rec_lo) rec_hi = xp.array(wavdct.rec_hi) if level is None: level = pywt.dwt_max_level( xp.min(xp.array([input.shape[ax] for ax in axes])), rec_lo.size) if level <= 0: return input assert level > 0 for ax in axes: assert input.shape[ax] % 2 == 0 x = input if inplace else input.astype(xp.complex64).copy() approx_idx = tuple([slice(0, input.shape[k]//2) if k in axes else slice(0, None) for k in range(len(input.shape))]) x[approx_idx] = iwt(x[approx_idx], input[approx_idx].shape, wave_name=wave_name, axes=axes, level=level-1, inplace=True) y = apply_rec_along_axis(x, axes, rec_lo, rec_hi) crop_idx = tuple([slice(0, oshape[k]) if k in axes else slice(0, None) for k in range(len(input.shape))]) return y[crop_idx]