Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Fast Hankel Transform for 1d array

I'm trying to find any existing implementation for Hankel Transform in Python (actually i'm more into symmetric fourier transform of two 2d radially symmetric functions but it can be easily reduced to hankel transform).

I do know about hankel python module, but it requires lambda function for input whereas I have only 1d-array.

Any thoughts?

like image 848
Anton Savostyanov Avatar asked May 03 '26 09:05

Anton Savostyanov


2 Answers

I'm the author of hankel. While I would not recommend to use my code in this case (since as you mention, it requires a callable input function, and its purpose is to accurately compute integrals, not to do a DHT), I will say that it is possible.

All you need to do is interpolate your input 1D array. How to do this is up to you, but typically something like the following works pretty well:

from scipy.interpolate import InterpolatedUnivariateSpline as Spline
import numpy as np

x, y = # Import/create data vectors

# Do this if y is both negative and positive
fnc = Spline(x,y, k=1) #I usually choose k=1 in case anything gets extrapolated.

# Otherwise do this
spl = Spline(np.log(x), np.log(y), k=1)
fnc = lambda x : np.exp(spl(np.log(x)))

# Continue as normal with hankel.transform(fnc, kvec)

The big issue with doing this is in choosing the parameters N and h such that the transform is well approximated for all values of k in kvec. If kvec spans a wide dynamic range, then hankel is very inefficient as it uses the same underlying arrays (of length N) for each k in a transform, meaning the most difficult k sets the performance level.

So again, in short I wouldn't recommend hankel, but if you can't find anything else, it will still work ;-)

like image 187
StevenMurray Avatar answered May 04 '26 21:05

StevenMurray


A friend of mine was looking for a Hankel transform in Python and after deciding there was nothing available - partly on the basis of the answers to this question - approached me to share mine.

I have now tidied, documented and released the code as PyHank (docs).

This package performs Quasi-discrete Hankel transforms, as requested by the original question (if a few years too late!) but hopefully this answer will direct future users who find this question (like my friend) to the right place.

like image 20
EdR Avatar answered May 04 '26 23:05

EdR