Evaluate a function via fast fourier transform using Matlab

In summary: Instead, you should use the definition given at http://mathworld.wolfram.com/DiscreteFourierTransform.html which is
  • #1
wu_weidong
32
0
Hi all,
I'm new to Matlab, and I'm trying to evaluate a function via fast Fourier transform using Matlab, then compare the values at each gridpoint with the exact value.

The function is
y1 = cos(x)-20*sin(5*x)+6*sin(12*x)
on the interval [-pi, pi], using n = 9 gridpoints.

I first tried to find the Fourier coefficients F1:
n = 9;
x = -pi:(2*pi/(n-1)):pi;
y1 = cos(x)-20*sin(5*x)+6*sin(12*x);
F1 = fft(y1);

I checked the values of my coefficients with the formula given by my teacher:
F1_k = SUM[y1(x_j)*exp(pi*i*j*k/m)]
where m = n/2, k = 0, 1, ..., 2m-1, and SUM is from j = 0 to j = 2m-1.
The coefficients matched.

Then I tried to compute the function F(x) at each grid point x_j using the formula
F(x) = (1/m)*SUM(F1_k*exp(i*k*x))
where SUM is from k = 0 to 2m-1
(This is the formula given by my teacher.)

for j=1:1:n
sum=0;
for k=1:1:n
sum=sum+(F1(k)*(exp(i*(k-1)*x(j))));
end
F(j)=sum/(n/2);
end

However, the values for F(x) that I got were

-0.2222 -14.8231i
-15.3243 +36.4597i
22.2864 -22.5086i
-4.6765 + 1.8450i
-2.0000 + 0.0000i
-30.6032 -12.5842i
26.5710 +26.7933i
-7.6067 -17.8277i
-0.2222 -14.8231i

instead of the actual values

-1
-14.849242
20
-13.435029
1
14.849242
-20
13.435029
-1

What is wrong with my program?

Thank you.

Regards,
Rayne
 
Physics news on Phys.org
  • #2
For starters, you should avoid using j as a variable, since in Matlab, both i and j denote the imaginary unit.

I venture to guess that your exercise is to verify that the FFT operation is reversible where in the later part of your work, you had used the IFFT to recover the original signal. I do not see, however, the logic behind the given function...

wu_weidong said:
Then I tried to compute the function F(x) at each grid point x_j using the formula
F(x) = (1/m)*SUM(F1_k*exp(i*k*x))
where SUM is from k = 0 to 2m-1
(This is the formula given by my teacher.)

In particular, why the x in the exponential? I would advise you, instead, to stick to the conventional definition of the IFFT, e.g., http://mathworld.wolfram.com/DiscreteFourierTransform.html" [Broken].

The following code should do the trick:
Code:
for j=1:1:n
sum=0;
for k=1:1:n
sum=sum+(F1(k)*(exp(i*(k-1)*(j-1)*2*pi/n)));
end
F(j)=sum/n;
end
 
Last edited by a moderator:
  • #3
The goal of my program was simply to compute the value of the function y1 using FFT at each grid point x, where x = -pi:(2*pi/(n-1)):pi. For example, if n = 9, I would then get 9 values of F(x) for 9 grid points.

Thanks a lot for your help, the program works now!
 
  • #4
Well yes, but it is still incorrect to plug the x into the IFFT as is.
 

1. What is a fast Fourier transform (FFT)?

A fast Fourier transform (FFT) is an algorithm used to efficiently compute the discrete Fourier transform (DFT) of a sequence or signal. It is commonly used in signal processing and data analysis to convert a signal from its original domain (often time or space) to a representation in the frequency domain.

2. How does the FFT work?

The FFT works by breaking down a signal into a series of smaller sub-signals, calculating the DFT of each sub-signal, and then combining them to obtain the final result. This is achieved through a process known as "decimation in time," which involves splitting the signal into even and odd components and recursively applying the DFT algorithm until the final result is obtained.

3. Why is the FFT important?

The FFT is important because it significantly reduces the computational complexity of calculating the DFT. In fact, the FFT can compute the DFT in O(nlogn) time, compared to the O(n^2) time required by the traditional DFT algorithm. This makes it a powerful tool for analyzing large datasets and signals in real-time applications.

4. How is the FFT used in Matlab?

In Matlab, the FFT function is used to compute the DFT of a sequence or signal. It takes the signal as an input and returns the complex amplitudes of its frequency components. This result can then be used for further processing, such as filtering, spectral analysis, or inverse FFT to reconstruct the original signal.

5. What are some limitations of using the FFT?

While the FFT is a powerful tool, it does have some limitations. It is only applicable to signals that are periodic and have a finite length. Additionally, it assumes that the signal is uniformly sampled, and any non-uniform sampling can introduce errors in the results. Finally, the FFT is sensitive to noise and can produce inaccurate results if the signal contains a high level of noise.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Calculus and Beyond Homework Help
Replies
5
Views
221
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Differential Equations
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Replies
4
Views
210
  • Calculus and Beyond Homework Help
Replies
1
Views
95
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top