spyrit.misc.walsh_hadamard.fwalsh_G_torch

spyrit.misc.walsh_hadamard.fwalsh_G_torch(x, ind=True)[source]

Fast Walsh G-transform of x

Args:

x (torch.tensor): input signal with shape \((*, n)\). :math:`n`+1 should be a power of two.

ind (bool, optional): True for sequency (default)

ind (list, optional): permutation indices. This is faster than True when repeating the sequency-ordered transform multilple times.

Returns:

torch.tensor: S-transformed signal with shape \((*, n)\).

Example 1:

Walsh-ordered G-transform of a signal of length 7

>>> import spyrit.misc.walsh_hadamard as wh
>>> import torch
>>> x = torch.tensor([1, 3, 0, -1, 7, 5, 1])
>>> s = wh.fwalsh_G_torch(x)
>>> print(s)
Example 2:

Permuted fast G-transform

>>> import spyrit.misc.walsh_hadamard as wh
>>> import torch
>>> x = torch.tensor([1, 3, 0, -1, 7, 5, 1])
>>> ind = [0, 1, 3, 2, 7, 4, 5, 6]
>>> y = wh.fwalsh_G_torch(x, ind)
>>> print(y)
Example 3:

Comparison with the numpy transform

>>> import numpy as np
>>> import spyrit.misc.walsh_hadamard as wh
>>> x = np.array([1, 3, 0, -1, 7, 5, 1])
>>> y_np = wh.fwalsh_G(x)
>>> x_torch = torch.from_numpy(x).to(torch.device('cuda:0'))
>>> y_torch = wh.fwalsh_G_torch(x_torch)
>>> print(y_np)
>>> print(y_torch)
Example 3:

Repeating the Walsh-ordered G-transform using input indices is faster

>>> import timeit
>>> x = torch.rand(512,2**12-1, device=torch.device('cuda:0'))
>>> t = timeit.timeit(lambda: wh.fwalsh_G_torch(x), number=10)
>>> print(f"No indices as inputs (10x): {t:.3f} seconds")
>>> ind = wh.sequency_perm_ind(x.shape[-1]+1)
>>> t = timeit.timeit(lambda: wh.fwalsh_G_torch(x,ind), number=10)
>>> print(f"With indices as inputs (10x): {t:.3f} seconds")