Explore different methods of signal padding for SWT

The Stationary Wavelet Transform (SWT) is a powerful signal processing technique used for various applications such as denoising, compression, and feature extraction. One critical aspect of performing a successful SWT is ensuring that the input signal is appropriately padded. Padding a signal involves adding extra data points to the signal to achieve desirable properties like length, periodicity, or symmetry, which can significantly impact the quality of the resulting wavelet transform. By adding extra data points to the signal, we ensure that the length of the transformed signal matches the original signal length, maintaining the integrity of the time-domain information. Moreover, appropriate padding can improve the frequency-domain representation, reduce boundary effects, and enhance the localization of wavelet coefficients.

In this tutorial, we will display an easy method of calculating the length that a signal should be extended to, and explore different methods of padding a signal for SWT to demonstrate their practical applications. Padding a signal is crucial for obtaining accurate and reliable results from the SWT.

### 1. Extended Length

When padding an even-length or odd-length signal for the Stationary Wavelet Transform (SWT), there are a few methods to calculate the length of padding to achieve desired properties. Here I introduce an easy method that I usually to calculate the extended length.

#### Step 1: Create a simple signal

S = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

#### Step 2: Calculate its length

SL = len(S)

#### Step 3: Calculate if it is divisible by 2^𝑛

import math

x = math.log2(SL)

print(x)

4.087462841250339

#### Step 4: Calculate the pad length

n = math.ceil(x)

PL = 2**n

print(PL)

32

#### Step 5: Calculate the numbers added

N = PL - SL

N

15

### 2. The ‘numpy.pad()’ method

The `numpy.pad()`

function provides several different methods to pad a signal up to an appropriate length. You can read more details about this method from online document of NumPy. In this tutorial, we only display three widely used methods: Zero Padding, Symmetric Padding, and Periodic Padding.

#### Example 1: Zero Padding

import numpy as np

padded_signal = np.pad(S, (0, 15), 'constant', constant_values=(0))

print(padded_signal)

print(len(padded_signal))

[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0]

32

In this example, we pad the signal with 15 zeros at the end, resulting in a total length of 32.

#### Example 2: Symmetric Padding

# Perform symmetric padding

padded_signal = np.pad(S, (7, 8), 'symmetric')

print(padded_signal)

print(len(padded_signal))

[ 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

17 16 15 14 13 12 11 10]

32

In this example, the signal is symmetrically padded with 7 samples on the left side and 8 samples on the right side.

#### Example 3: Periodic Padding

# Perform periodic padding

padded_signal = np.pad(S, (7, 8), 'wrap')

print(padded_signal)

print(len(padded_signal))

[11 12 13 14 15 16 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1 2 3 4 5 6 7 8]

32

In this example, the signal is periodically padded by wrapping it around, resulting in a total length of 32.

### 3. The ‘pywt.pad()’ method

PyWavelets provides convenient methods for padding signals. It offers different padding modes that can be used for various applications. In this article, there are some padding methods available in PyWavelets. We will review 3 examples as follows.

#### Example 1: Zero extension mode

import pywt

padded_signal = pywt.pad(S,(6,9),mode='zero')

print(padded_signal)

print(len(padded_signal))

[ 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0

0 0 0 0 0 0 0 0]

32

In this example, the signal is padded with 6 zero samples on the left side and 9 zero samples on the right side.

#### Example 2: Constant extension mode

import pywt

padded_signal = pywt.pad(S,(7,8),mode='constant')

print(padded_signal)

print(len(padded_signal))

[ 1 1 1 1 1 1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

17 17 17 17 17 17 17 17]

32

In this example, the signal is padded with 7 constant (i.e. 1) samples on the left side and 8 constant (i.e. 17) samples on the right side.

#### Example 3: Periodization extension mode

import pywt

padded_signal = pywt.pad(a,(7,7),'periodization')

print(padded_signal)

print(len(padded_signal))

[12 13 14 15 16 17 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

17 1 2 3 4 5 6 7]

32

In this example, the signal is periodically padded by wrapping it around, resulting in a total length of 32.

### Conclusion

In this tutorial, we explored different methods of padding a signal for the Stationary Wavelet Transform (SWT), which are provided by NumPy and PyWavelets. We discussed the importance of padding and its impact on the quality of the wavelet transform. First, an easy but practical method was introduced to calculate the extended length of a signal, and then few example were presented as viable example methods each with its advantages and suitability for specific types of signals. Real examples were provided to demonstrate the practical application of these padding techniques. By carefully selecting the appropriate padding method, researchers and practitioners can achieve more accurate and reliable results when applying the SWT to various signal processing tasks.

*Originally published at https://medium.com**/* * on July 1, 2023.*