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) tensor([ -8., -2., -2., -16., 8., 6., -2.])
- 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) tensor([ -2., -16., -2., 8., -8., 6., -2.])
- 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('cpu')) >>> y_torch = wh.fwalsh_G_torch(x_torch) >>> print(y_np) [ -8 -2 -2 -16 8 6 -2] >>> print(y_torch) tensor([ -8., -2., -2., -16., 8., 6., -2.])
- 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('cpu')) >>> t = timeit.timeit(lambda: wh.fwalsh_G_torch(x), number=10) >>> print(f"No indices as inputs (10x): {t:.3f} seconds") No indices as inputs (10x): ... 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") With indices as inputs (10x): ... seconds