diff --git a/doc/source/ref/wavelet-packets.rst b/doc/source/ref/wavelet-packets.rst index fd4c96b5..782b8b54 100644 --- a/doc/source/ref/wavelet-packets.rst +++ b/doc/source/ref/wavelet-packets.rst @@ -304,6 +304,14 @@ WaveletPacket and Node from the reconstruction. + When initialized with data, the packet remembers the input shape and + trims the reconstruction to that shape. When initialized with + ``data=None``, the coefficients can correspond to signals of different + lengths, so the reconstruction may have extra samples at the end. To + have the packet trim the reconstruction automatically, initialize it + with a zero array of the original shape before assigning coefficients. + + WaveletPacket2D and Node2D ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/source/regression/wp.md b/doc/source/regression/wp.md index b3200a80..df0c4d69 100644 --- a/doc/source/regression/wp.md +++ b/doc/source/regression/wp.md @@ -282,6 +282,34 @@ print([n.path for n in new_wp.get_leaf_nodes(False)]) print([n.path for n in new_wp.get_leaf_nodes(True)]) ``` +### Preserving the original length + +A packet initialized with `data=None` relies on the coefficient lengths, +which can correspond to more than one input length. The five-sample signal +below reconstructs to six samples. The extension adds the extra sample at +the end. + +```{code-cell} +import numpy as np + +signal = np.arange(1.0, 6.0) +source = pywt.WaveletPacket(signal, 'haar', maxlevel=1) +from_coefficients = pywt.WaveletPacket(None, 'haar', maxlevel=1) +for path in ('a', 'd'): + from_coefficients[path] = source[path].data +print(format_array(from_coefficients.reconstruct())) +``` + +Initialize the packet with zeros of the original shape to trim the +reconstruction automatically. + +```{code-cell} +with_shape = pywt.WaveletPacket(np.zeros_like(signal), 'haar', maxlevel=1) +for path in ('a', 'd'): + with_shape[path] = source[path].data +print(format_array(with_shape.reconstruct())) +``` + ## Removing nodes from Wavelet Packet tree Let's create some sample data: