Handling DWT Coefficients¶
Convenience routines are available for converting the outputs of the multilevel
dwt functions (wavedec
, wavedec2
and wavedecn
) to and from a
single, concatenated coefficient array.
Concatenating all coefficients into a single array¶
-
pywt.
coeffs_to_array
(coeffs, padding=0, axes=None)¶ Arrange a wavelet coefficient list from wavedecn into a single array.
Parameters: coeffs: array-like
dictionary of wavelet coefficients as returned by pywt.wavedecn
padding : float or None, optional
If None, raise an error if the coefficients cannot be tightly packed.
axes : sequence of ints, optional
Axes over which the DWT that created
coeffs
was performed. The default value ofNone
corresponds to all axes.Returns: coeff_arr : array-like
Wavelet transform coefficient array.
coeff_slices : list
List of slices corresponding to each coefficient. As a 2D example, coeff_arr[coeff_slices[1][‘dd’]] would extract the first level detail coefficients from coeff_arr.
See also
array_to_coeffs
- the inverse of coeffs_to_array
Notes
Assume a 2D coefficient dictionary, c, from a two-level transform.
Then all 2D coefficients will be stacked into a single larger 2D array as follows:
+---------------+---------------+-------------------------------+ | | | | | c[0] | c[1]['da'] | | | | | | +---------------+---------------+ c[2]['da'] | | | | | | c[1]['ad'] | c[1]['dd'] | | | | | | +---------------+---------------+ ------------------------------+ | | | | | | | | | | c[2]['ad'] | c[2]['dd'] | | | | | | | | | | +-------------------------------+-------------------------------+
Examples
>>> import pywt >>> cam = pywt.data.camera() >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3) >>> arr, coeff_slices = pywt.coeffs_to_array(coeffs)
Splitting concatenated coefficient array back into its components¶
-
pywt.
array_to_coeffs
(arr, coeff_slices, output_format='wavedecn')¶ Convert a combined array of coefficients back to a list compatible with waverecn.
Parameters: arr: array-like
An array containing all wavelet coefficients. This should have been generated via coeffs_to_array.
coeff_slices : list of tuples
List of slices corresponding to each coefficient as obtained from array_to_coeffs.
output_format : {‘wavedec’, ‘wavedec2’, ‘wavedecn’}
Make the form of the coefficients compatible with this type of multilevel transform.
Returns: coeffs: array-like
Wavelet transform coefficient array.
See also
coeffs_to_array
- the inverse of array_to_coeffs
Notes
A single large array containing all coefficients will have subsets stored, into a waverecn list, c, as indicated below:
+---------------+---------------+-------------------------------+ | | | | | c[0] | c[1]['da'] | | | | | | +---------------+---------------+ c[2]['da'] | | | | | | c[1]['ad'] | c[1]['dd'] | | | | | | +---------------+---------------+ ------------------------------+ | | | | | | | | | | c[2]['ad'] | c[2]['dd'] | | | | | | | | | | +-------------------------------+-------------------------------+
Examples
>>> import pywt >>> from numpy.testing import assert_array_almost_equal >>> cam = pywt.data.camera() >>> coeffs = pywt.wavedecn(cam, wavelet='db2', level=3) >>> arr, coeff_slices = pywt.coeffs_to_array(coeffs) >>> coeffs_from_arr = pywt.array_to_coeffs(arr, coeff_slices) >>> cam_recon = pywt.waverecn(coeffs_from_arr, wavelet='db2') >>> assert_array_almost_equal(cam, cam_recon)