DFT Matlab - fft and ifft commands

In summary, the conversation is about a person struggling with a periodic boundary 1D Poisson solver code on Matlab. They are trying to use the FFT and IFFT commands to solve the problem and have shared their code and procedure. However, there are a few issues with the code that may be causing it to not work as expected. The expert suggests checking for division by zero errors, ensuring that the frequencies used in the DFT and IFFT match, and using a larger number of points when plotting the results.
  • #1
SoajanII
1
0

Homework Statement



Hello people

For a few days I'm having trouble with periodic boundary 1D poisson solver code on matlab. I'm trying to figure out discritized Fourier transform (fft), and ifft commands.

To make things simpler I want to type a simple code. Aim on the code is integrating a function ( such as sin(x) ) two times on frequency domain, then convert back to time domain and check it with the analytical result.

My procedure is: first fft(y), then divide by (i*w)^2, finally invert back to time domain by using ifft.

2. The attempt at a solution

This is my code, but have no idea why it is not working...

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
clc

L=100; %length of the domain
J=1000; %number of grids
x=linspace(0,L,J);
y=sin(x); %original y(x) function
g=-sin(x); % if you integrate y(x) 2 times, you get this one.
%And we use this to check code

f=fft(y,J); %DFT of function y

freq=(2*pi)*[0:(J/2-1) (-J/2):-1]/J; %frequencies

for a=2:J
f(a)=-f(a)/freq(a)^2; %integrating 2 times
end

f(1)=0;

gf=ifft(f);

gf=real(gf);X=1:J; %Plot part, and visiul check of the result
plot(x(X),g(X));
hold on
plot(x(X),gf(X),'r');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Thank you
 
Last edited:
Physics news on Phys.org
  • #2
for sharing your code and explaining your procedure. I can see that you are trying to use the FFT and IFFT commands to solve a periodic boundary 1D Poisson equation. However, there are a few issues with your code that may be causing it to not work as expected.

Firstly, in your for loop, you are dividing by freq(a)^2, which can be problematic as some of the elements in freq may be zero. This can lead to division by zero errors and incorrect results. Instead, you can try using a small value (such as 0.0001) instead of freq(a)^2.

Secondly, the frequencies you are using for your DFT do not match the frequencies for your IFFT. In your DFT, you are using frequencies from 0 to J/2-1 and then from -J/2 to -1, while in your IFFT, you are using frequencies from 1 to J. This mismatch in frequencies can also lead to incorrect results.

Lastly, when plotting your results, it may be helpful to use a larger number of points (such as 10000) to get a smoother curve and better visualize the accuracy of your code.

I hope these suggestions help you troubleshoot your code and solve your problem. Best of luck!
 

Related to DFT Matlab - fft and ifft commands

1. What is the difference between DFT and FFT in Matlab?

The Discrete Fourier Transform (DFT) is a mathematical algorithm that converts a signal from its original domain (often time or space) to a representation in the frequency domain. The Fast Fourier Transform (FFT) is a more efficient implementation of the DFT algorithm. In Matlab, the fft command uses the FFT algorithm to compute the DFT of a signal.

2. How do I use the fft command in Matlab?

To use the fft command, you must first have your signal stored as a vector in Matlab. Then, simply use the command "fft(signal)" to compute the DFT of your signal. You can also specify the length of the DFT to be computed by using the command "fft(signal, n)".

3. What is the output of the fft command in Matlab?

The output of the fft command is an array of complex numbers, representing the magnitude and phase of each frequency component in the DFT of the input signal. To visualize the output, you can use the abs() function to compute the magnitude and angle() function to compute the phase.

4. How do I use the ifft command in Matlab?

The ifft command is used to compute the inverse DFT of a signal. Similar to the fft command, you must have your signal stored as a vector in Matlab. Then, use the command "ifft(signal)" to compute the inverse DFT. You can also specify the length of the inverse DFT by using the command "ifft(signal, n)".

5. Can I use the fft and ifft commands for signals with different lengths?

Yes, you can use the fft and ifft commands for signals with different lengths. However, it is important to note that the results may not be accurate if the signal length is not a power of 2. In these cases, you can use the "nextpow2" function in Matlab to find the nearest power of 2 and pad your signal with zeros.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
163
  • Engineering and Comp Sci Homework Help
Replies
1
Views
929
  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
972
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top