From 516bab714cd7d5882a91d8427167f5d50f971fed Mon Sep 17 00:00:00 2001 From: Josh <39366189+M-Colley@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:16:34 +0000 Subject: [PATCH] DOC: explain wavelet packet reconstruction length Fixes #561. --- doc/source/ref/wavelet-packets.rst | 8 ++++++++ doc/source/regression/wp.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/doc/source/ref/wavelet-packets.rst b/doc/source/ref/wavelet-packets.rst index fd4c96b5a..782b8b54c 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 b3200a80a..df0c4d690 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: