Faster Fourier - Bessel Coefficient Calculations on Python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












So I'm trying to get the fourier-bessel coefficients of a very large array of numbers that are around 1 million points in size, but I'm coming across some speed issues with calculating $J_o(x)$ for a large array.



I have my put code below (Python)



import numpy as np
import scipy.integrate as integrate
from scipy.special import j0, j1, jn_zeros

def fourier_bessel_coeffs(x, num_coeffs=None):
if num_coeffs is None:
num_coeffs = len(x)

# Roots of the Zeroth-Order Bessel Function
zeroth_roots = jn_zeros(0, num_coeffs)

# Define domain
t = np.arange(0, len(x))

# Define end point of domain, which is required as paramter
a = len(x)

# Calculate Coefficients
coeffs = np.array([2*integrate.trapz(t*x*j0(i*t/a)) / (a**2 * j1(i)**2)
for i in zeroth_roots])

return coeffs


The code is based on the following:
Any signal $y(t)$ can be represented as a fourier-bessel expansion:
beginequation
y(t) = sum_n=1^N D_nJ_0 left( fraclambda_nat right)
endequation
Where $J_0(cdot)$ are zero-order Bessel functions and the $lambda_n; n = 1,2,..., N$ is the ascending order positive roots of $J_0(lambda) = 0$



Using the orthogonality of the set $ left J_0 left( fraclambda_nat right) right$ , the FB coefficients $D_n$ are computed by the following:
beginequation
D_n = frac2 int_0^a ty(t) J_0 left( fraclambda_nat right) dta^2
endequation
with $1 leq n leq N$ and $J_1(lambda_n)$ are the first order bessel functions.



From what I've been trying out, is that it gets hung up when trying to compute $J_0(fraclambda_na t)$



Edit: Note x = $y(t)$ in the code







share|cite|improve this question




















  • Why is the number of coefficients you want the same as the length of $y(t)$?
    – Winther
    Aug 28 at 15:25










  • @Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
    – Sam
    Aug 28 at 15:26










  • It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
    – Winther
    Aug 28 at 15:28










  • Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
    – Winther
    Aug 28 at 15:51










  • @Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
    – Sam
    Aug 28 at 16:00














up vote
0
down vote

favorite












So I'm trying to get the fourier-bessel coefficients of a very large array of numbers that are around 1 million points in size, but I'm coming across some speed issues with calculating $J_o(x)$ for a large array.



I have my put code below (Python)



import numpy as np
import scipy.integrate as integrate
from scipy.special import j0, j1, jn_zeros

def fourier_bessel_coeffs(x, num_coeffs=None):
if num_coeffs is None:
num_coeffs = len(x)

# Roots of the Zeroth-Order Bessel Function
zeroth_roots = jn_zeros(0, num_coeffs)

# Define domain
t = np.arange(0, len(x))

# Define end point of domain, which is required as paramter
a = len(x)

# Calculate Coefficients
coeffs = np.array([2*integrate.trapz(t*x*j0(i*t/a)) / (a**2 * j1(i)**2)
for i in zeroth_roots])

return coeffs


The code is based on the following:
Any signal $y(t)$ can be represented as a fourier-bessel expansion:
beginequation
y(t) = sum_n=1^N D_nJ_0 left( fraclambda_nat right)
endequation
Where $J_0(cdot)$ are zero-order Bessel functions and the $lambda_n; n = 1,2,..., N$ is the ascending order positive roots of $J_0(lambda) = 0$



Using the orthogonality of the set $ left J_0 left( fraclambda_nat right) right$ , the FB coefficients $D_n$ are computed by the following:
beginequation
D_n = frac2 int_0^a ty(t) J_0 left( fraclambda_nat right) dta^2
endequation
with $1 leq n leq N$ and $J_1(lambda_n)$ are the first order bessel functions.



From what I've been trying out, is that it gets hung up when trying to compute $J_0(fraclambda_na t)$



Edit: Note x = $y(t)$ in the code







share|cite|improve this question




















  • Why is the number of coefficients you want the same as the length of $y(t)$?
    – Winther
    Aug 28 at 15:25










  • @Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
    – Sam
    Aug 28 at 15:26










  • It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
    – Winther
    Aug 28 at 15:28










  • Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
    – Winther
    Aug 28 at 15:51










  • @Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
    – Sam
    Aug 28 at 16:00












up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I'm trying to get the fourier-bessel coefficients of a very large array of numbers that are around 1 million points in size, but I'm coming across some speed issues with calculating $J_o(x)$ for a large array.



I have my put code below (Python)



import numpy as np
import scipy.integrate as integrate
from scipy.special import j0, j1, jn_zeros

def fourier_bessel_coeffs(x, num_coeffs=None):
if num_coeffs is None:
num_coeffs = len(x)

# Roots of the Zeroth-Order Bessel Function
zeroth_roots = jn_zeros(0, num_coeffs)

# Define domain
t = np.arange(0, len(x))

# Define end point of domain, which is required as paramter
a = len(x)

# Calculate Coefficients
coeffs = np.array([2*integrate.trapz(t*x*j0(i*t/a)) / (a**2 * j1(i)**2)
for i in zeroth_roots])

return coeffs


The code is based on the following:
Any signal $y(t)$ can be represented as a fourier-bessel expansion:
beginequation
y(t) = sum_n=1^N D_nJ_0 left( fraclambda_nat right)
endequation
Where $J_0(cdot)$ are zero-order Bessel functions and the $lambda_n; n = 1,2,..., N$ is the ascending order positive roots of $J_0(lambda) = 0$



Using the orthogonality of the set $ left J_0 left( fraclambda_nat right) right$ , the FB coefficients $D_n$ are computed by the following:
beginequation
D_n = frac2 int_0^a ty(t) J_0 left( fraclambda_nat right) dta^2
endequation
with $1 leq n leq N$ and $J_1(lambda_n)$ are the first order bessel functions.



From what I've been trying out, is that it gets hung up when trying to compute $J_0(fraclambda_na t)$



Edit: Note x = $y(t)$ in the code







share|cite|improve this question












So I'm trying to get the fourier-bessel coefficients of a very large array of numbers that are around 1 million points in size, but I'm coming across some speed issues with calculating $J_o(x)$ for a large array.



I have my put code below (Python)



import numpy as np
import scipy.integrate as integrate
from scipy.special import j0, j1, jn_zeros

def fourier_bessel_coeffs(x, num_coeffs=None):
if num_coeffs is None:
num_coeffs = len(x)

# Roots of the Zeroth-Order Bessel Function
zeroth_roots = jn_zeros(0, num_coeffs)

# Define domain
t = np.arange(0, len(x))

# Define end point of domain, which is required as paramter
a = len(x)

# Calculate Coefficients
coeffs = np.array([2*integrate.trapz(t*x*j0(i*t/a)) / (a**2 * j1(i)**2)
for i in zeroth_roots])

return coeffs


The code is based on the following:
Any signal $y(t)$ can be represented as a fourier-bessel expansion:
beginequation
y(t) = sum_n=1^N D_nJ_0 left( fraclambda_nat right)
endequation
Where $J_0(cdot)$ are zero-order Bessel functions and the $lambda_n; n = 1,2,..., N$ is the ascending order positive roots of $J_0(lambda) = 0$



Using the orthogonality of the set $ left J_0 left( fraclambda_nat right) right$ , the FB coefficients $D_n$ are computed by the following:
beginequation
D_n = frac2 int_0^a ty(t) J_0 left( fraclambda_nat right) dta^2
endequation
with $1 leq n leq N$ and $J_1(lambda_n)$ are the first order bessel functions.



From what I've been trying out, is that it gets hung up when trying to compute $J_0(fraclambda_na t)$



Edit: Note x = $y(t)$ in the code









share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Aug 28 at 15:01









Sam

164




164











  • Why is the number of coefficients you want the same as the length of $y(t)$?
    – Winther
    Aug 28 at 15:25










  • @Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
    – Sam
    Aug 28 at 15:26










  • It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
    – Winther
    Aug 28 at 15:28










  • Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
    – Winther
    Aug 28 at 15:51










  • @Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
    – Sam
    Aug 28 at 16:00
















  • Why is the number of coefficients you want the same as the length of $y(t)$?
    – Winther
    Aug 28 at 15:25










  • @Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
    – Sam
    Aug 28 at 15:26










  • It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
    – Winther
    Aug 28 at 15:28










  • Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
    – Winther
    Aug 28 at 15:51










  • @Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
    – Sam
    Aug 28 at 16:00















Why is the number of coefficients you want the same as the length of $y(t)$?
– Winther
Aug 28 at 15:25




Why is the number of coefficients you want the same as the length of $y(t)$?
– Winther
Aug 28 at 15:25












@Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
– Sam
Aug 28 at 15:26




@Winther wouldn't I need as many coefficients as I have points of $y(t)$ to accurately represent $y(t)$
– Sam
Aug 28 at 15:26












It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
– Winther
Aug 28 at 15:28




It depends on the function how many you need, but generally I would think you would need significantly less. Just test it. Reducing this could speed it up significantly.
– Winther
Aug 28 at 15:28












Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
– Winther
Aug 28 at 15:51




Another issue with your code is that you only sample the function at integer $t$'s. So if $a=2$ then you only have $2$ points to represent it. This seems strange to me.
– Winther
Aug 28 at 15:51












@Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
– Sam
Aug 28 at 16:00




@Winther fair point , though doesn't $a$ in this case represent the region of integration of the the function $y(t)$ so wouldn't $a$ always be the length of $y(t)$
– Sam
Aug 28 at 16:00















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "69"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2897367%2ffaster-fourier-bessel-coefficient-calculations-on-python%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2897367%2ffaster-fourier-bessel-coefficient-calculations-on-python%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Carbon dioxide

Why am i infinitely getting the same tweet with the Twitter Search API?