79164356

Date: 2024-11-06 21:34:48
Score: 0.5
Natty:
Report link

The answer given by @Bob only returns the imaginary part of the transform. If you'd like to return a complex number (more similar to the scipy hilbert function), you can return a complex tensor like so.

def hilbert_transform(data):
    # Allocates memory on GPU with size/dimensions of signal
    data = torch.from_numpy(data).to('cuda')
    transforms = -1j * torch.fft.rfft(data, axis=-1)
    transforms[0] = 0;
    imaginary = torch.fft.irfft(transforms, axis=-1)
    real = data
    return torch.complex(real, imaginary)

This will play nicely with functions like torch.angle to get signal phase and torch.abs to get the envelope using both real and imaginary components.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Bob
  • Low reputation (0.5):
Posted by: Chris