Why Does My FFT Integration Method Produce Nonzero Imaginary Parts?

In summary, the student is struggling to find a formula for integrating sin(x) using a discrete Fourier transform. He tries to do the integration using a formula from a paper, but it does not work. He tests the code he wrote by calculating the coefficients of a Fourier series and it does not work. He finds an error in the formula and is able to fix it.
  • #1
SchroedingersLion
215
57
TL;DR Summary
I am posting this in the EE forum since electrical engineers are typically skilled with Fourier transforms.
Hi guys,

for a project I had to get involved with discrete Fourier transforms to solve PDEs.
However, the code that I implemented according to a pseudo-code from a paper did not work - it seems like I calculated integrals incorrectly.

To search the error, I tried to integrate the sin(x) function with my method, and it did not work.
I already posted the formula I used for this sine integration, as well as the code, here:
https://scicomp.stackexchange.com/questions/33886/calculate-integrals-using-numpy-fft
Does anyone know what I am doing wrong? The error does not depend on N, and after my inverse FT my signal contains nonzero imaginary parts which should not happen! Is there something wrong with my formula?

I am a bit desperate, since my deadlines are getting closer...
SL
 
Last edited:
Engineering news on Phys.org
  • #2
I don't think you're going to get much help with Thanksgiving coming up tomorrow.

I saw on Stack you got a couple of responses already.

These are the kinds of problems where you really need to dig in and gain some insight into what is actually going on. Sadly, unless someone has direct experience here its unlikely you'll get the help you need.

As a programmer, I can relate to using a function to do something not really knowing what's going on and trusting that it works as advertised. Most notably are the various integrators that folks like to use like Runge-Kutta RK4 and I have no idea how it does its magic but hey it works so I've use it.

Jedi
 
  • #3
Don't know about Thanksgiving, I am sitting in Europe ^^

I only got two comments, and they imply that something seems to be wrong with the formula.
Isn't this a complete basic problem? I thought that was a trivial question for people who know their way around FTs.

edit: I edited the question, maybe it was not clear that I am talking about integration of sin(x) using fft in this example.
 
  • #4
I can't help you directly but people use the FFT method for calculating integrals of acceleration to get velocity and position from IMUs (Inertial Measuring Units). The students here seem to find plenty of resources for doing this calculation in the IMU literature. Perhaps looking at some of this stuff may help.

Cheers
 
  • Like
Likes jedishrfu
  • #5
Isn't it just a question of normalization of the FFT?
 
  • #6
I would not know how. The deviation I am seeing is independent of number of data points N. Moreover, my final vector after the inverse transform has nonzero imaginary parts which should be impossible when I take the FFT of a purely real signal. To me, it looks like there is an error in my theoretical formula.

I expand the function in a Fourier series under the integral and truncate it for ##|k|>m##. In my code, I only know the function on ##2m+1## discrete data points and I try to calculate the ##A_k##, i.e. the coefficients of the Fourier series, via a FFT. Maybe there is something wrong with this approach?
 
  • #7
You might have forgotten to multiply by the last ##e^{2piky}## term in your loop.
 
  • #8
But why would I need these? They are simply the usual exponentials of the inverse transform, they do not belong to the amplitudes that are given by ifft.
 
  • #9
I think I found the mistake.
If I know the discrete Fourier transform, i.e. $$ A_k =\sum_{i=-N}^{N} f(x_i)\cdot e^{-i2\pi x_i k}$$ and I write $$ f(x_i)=\sum_{k=-N}^{N} A_k\cdot e^{i2\pi x_i k},$$ then it does not follow that
$$f(x_i+y)=\sum_{k=-N}^{N} A_k\cdot e^{i2\pi (x_i+y) k}.$$

I tested this with a small code:

error.PNG


This would explain why I have not found this kind of integration online :D

What do you think?
 

Attachments

  • error.PNG
    error.PNG
    15.8 KB · Views: 145
  • error.PNG
    error.PNG
    15.1 KB · Views: 147
Last edited:

1. How does the Fast Fourier Transform (FFT) help with solving integrals?

The FFT is a mathematical algorithm that allows for efficient computation of the discrete Fourier transform, which is used in solving integrals. It reduces the amount of computation needed to solve an integral, making it faster and more efficient.

2. Can the FFT be used for any type of integral?

Yes, the FFT can be used for any type of integral that can be represented as a sum of trigonometric functions. This includes both real and complex integrals.

3. What are the benefits of using the FFT for solving integrals?

Using the FFT for solving integrals can greatly reduce the computational time and resources needed. It also allows for more accurate results, as it reduces the chances of rounding errors.

4. Are there any limitations to using the FFT for solving integrals?

While the FFT is a powerful tool for solving integrals, it does have some limitations. It is most effective for integrals with a large number of data points, and may not be as efficient for smaller integrals. Additionally, it may not be suitable for integrals with highly oscillatory or discontinuous functions.

5. How can I implement the FFT for solving integrals?

Implementing the FFT for solving integrals involves converting the integral into a sum of trigonometric functions, applying the FFT algorithm, and then converting the result back into the integral form. There are many resources and tutorials available online to help with the implementation process.

Similar threads

Replies
8
Views
185
Replies
5
Views
365
Replies
3
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Electrical Engineering
Replies
1
Views
3K
  • Calculus and Beyond Homework Help
Replies
12
Views
990
  • Electrical Engineering
Replies
24
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
647
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top