How can I integrate sinusoids in python code using FFT?

In summary: This is bc the # algorithim that computes the FFT results in the order #being reversed. This is corrected whenever the IFFT is taken. position=position[::-1] return
  • #1
jameslat
28
0
Hello,
Thank you for taking time to read my post.

I have a discrete set of data points that represent an acceleration signal. I want to take the integral of this set of points twice so as to get a function which represents the position over time.
To accomplish this, I have taken the FFT of the acceleration signal . From there I can extract the complex harmonics and plot the signal with reasonable accuracy. This part I can do and it works great. However I keep running into problems whenever I try to integrate the sinusoids. Can you take a look at my below code and see where I might be going wrong?
Inputs:
accel, this is suppose to be the FFT of the signal we want to integrate twice
w, omega = 2 pi f
L, how many harmonics you want to use (example, L=10 would use from n=-10,-9...0...9,10)

Code:
#feed a set of FFT points.
#will alter the coefficients of the real and complex points so as to
#account for integrating each harmonic twice!
#ideally, you will be able to input the acceleration and the ouput
#will be the trajectory of the position
def int2(accel,w,L):
    sinusoids=[]
    L=int(L)
    period = (2*n.pi)/w
    xaxis = n.linspace(0,period,num=len(accel))
       
    position =[]
    tempMain = 0;
    for i in range(0,(len(accel)-1)):
        for j in range(-L,L):
            a_ = n.real(accel[j])
            b_ = n.imag(accel[j])
            n_ = len(accel)
            if j!=0:
                j2=j
                tempMain -= (1/(j2**2))*(a_*n.cos(j*w*xaxis[i])+b_*n.sin(j*w*xaxis[i]))/n_
            else:
                j2=1
                tempMain += ((xaxis[i]*xaxis[i])/(2))*(a_+b_)/n_
           
           
                       
        position.append(tempMain)
        tempMain = 0
    #we need to reverse the order of the list. This is bc the
    # algorithim that computes the FFT results in the order
    #being reversed. This is corrected whenever the IFFT is taken.
    position=position[::-1]   
       
    return position
 
Technology news on Phys.org
  • #2
jameslat said:
Hello,
Thank you for taking time to read my post.

I have a discrete set of data points that represent an acceleration signal. I want to take the integral of this set of points twice so as to get a function which represents the position over time.
To accomplish this, I have taken the FFT of the acceleration signal . From there I can extract the complex harmonics and plot the signal with reasonable accuracy. This part I can do and it works great. However I keep running into problems whenever I try to integrate the sinusoids.
What problems are you having? It would be helpful to know exactly what this code does as opposed to what you expect it to do.
jameslat said:
Can you take a look at my below code and see where I might be going wrong?
Inputs:
accel, this is suppose to be the FFT of the signal we want to integrate twice
w, omega = 2 pi f
L, how many harmonics you want to use (example, L=10 would use from n=-10,-9...0...9,10)

Code:
#feed a set of FFT points.
#will alter the coefficients of the real and complex points so as to
#account for integrating each harmonic twice!
#ideally, you will be able to input the acceleration and the ouput
#will be the trajectory of the position
def int2(accel,w,L):
    sinusoids=[]
    L=int(L)
    period = (2*n.pi)/w
    xaxis = n.linspace(0,period,num=len(accel))
      
    position =[]
    tempMain = 0;
    for i in range(0,(len(accel)-1)):
        for j in range(-L,L):
            a_ = n.real(accel[j])
            b_ = n.imag(accel[j])
            n_ = len(accel)
            if j!=0:
                j2=j
                tempMain -= (1/(j2**2))*(a_*n.cos(j*w*xaxis[i])+b_*n.sin(j*w*xaxis[i]))/n_
            else:
                j2=1
                tempMain += ((xaxis[i]*xaxis[i])/(2))*(a_+b_)/n_
          
          
                      
        position.append(tempMain)
        tempMain = 0
    #we need to reverse the order of the list. This is bc the
    # algorithim that computes the FFT results in the order
    #being reversed. This is corrected whenever the IFFT is taken.
    position=position[::-1]  
      
    return position
 
  • #3
Hi Mark,

So the goal of this function is to accept the FFT of a discrete set of data points which represent an acceleration signal a(t) and find the position over time x(t).

The input is fft(a(t))=a(w).
I know that for ever harmonic in a(w), that the corresponding time domain signal is An(t) = (real(a(n))*cos(w*n*t) + imag(a(n))*sin(w*n*t))/(total number of points)
I know that if you integrate cos(nt) you get cos(nt)/n and if you integrate sin(nt) you get sin(nt)/n
There is a special case, n=0, where this will be a constant(ie n=0 and the sine signal cancel and the cosine goes to 1)

So I want to integrate everything twice. In this way I will go from acceleration to velocity and then from velocity to position.
Does that explain things well? Please let me know how I can further elaborate.

Thank you so much for your feedback :)
 
  • #4
jameslat said:
I know that if you integrate cos(nt) you get cos(nt)/n and if you integrate sin(nt) you get sin(nt)/n
No to both. ##\int \cos(nt)dt = \frac 1 n \sin(nt)## and ##\int \sin(nt) dt = -\frac 1 n \cos(nt)##, omitting the constants of integration. Could this be why things aren't working for you?
If you integrate the answers above you get ##-\frac 1 {n^2} \cos(nt)## for cos(nt) integrated twice and ##-\frac 1 {n^2}\sin(nt)## for the second one.
 

1. What is FFT and how is it used in Python code?

FFT stands for Fast Fourier Transform, and it is a mathematical algorithm used for efficiently computing the discrete Fourier transform of a signal or sequence. In Python, FFT is commonly used for signal processing, time series analysis, and image processing.

2. How do I integrate FFT into my Python code?

To integrate FFT into your Python code, you can use the NumPy library, specifically the fft function. This function takes in an array of data points and returns the discrete Fourier transform of the data. You can then use this transformed data for further analysis or processing.

3. Can FFT be used for real-time data analysis?

Yes, FFT can be used for real-time data analysis in Python. However, the efficiency of the algorithm may be affected by the size of the data and the processing power of your machine. It is important to optimize your code and use efficient data structures to ensure real-time performance.

4. Are there any limitations to using FFT in Python?

One limitation of using FFT in Python is that it requires the input data to be in the form of an array. This may limit its use in certain applications where data is not readily available in array form. Additionally, the accuracy of the results may be affected by the size of the data and the resolution of the data points.

5. Can I visualize the results of the FFT in Python?

Yes, you can visualize the results of the FFT in Python using various plotting libraries such as Matplotlib or Seaborn. These libraries allow you to plot the transformed data in different forms, such as a frequency spectrum or a time-frequency plot, making it easier to analyze and interpret the results.

Similar threads

Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
21
Views
4K
Replies
5
Views
354
  • Programming and Computer Science
Replies
8
Views
872
Replies
6
Views
2K
Back
Top