Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vendored Hilbert function #61

Open
maedoc opened this issue Feb 23, 2024 · 0 comments
Open

Add vendored Hilbert function #61

maedoc opened this issue Feb 23, 2024 · 0 comments

Comments

@maedoc
Copy link
Member

maedoc commented Feb 23, 2024

The PR to merge hilbert transform into Jax is pending since a year with credit to @joglekara (thanks!), so we can vendor in the code from there in the meantime,

import scipy.signal as osp_signal
from jax._src.numpy.util import _wraps, check_arraylike
import jax.numpy as jnp

@_wraps(osp_signal.hilbert)
def hilbert(x, N=None, axis=-1):
  check_arraylike('hilbert', x)
  x = jnp.asarray(x)
  if x.ndim > 1:
    raise NotImplementedError("x must be 1D.")
  if jnp.iscomplexobj(x):
    raise ValueError("x must be real.")
  if N is None:
    N = x.shape[axis]
  if N <= 0:
    raise ValueError("N must be positive.")

  Xf = jnp.fft.fft(x, N, axis=axis)
  if N % 2 == 0:
    h = jnp.zeros(N, Xf.dtype).at[0].set(1).at[1:N // 2].set(2).at[N // 2].set(1)
  else:
    h = jnp.zeros(N, Xf.dtype).at[0].set(1).at[1:(N+1) // 2].set(2)

  x = jnp.fft.ifft(Xf * h, axis=axis)
  return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant