How i will write the code for fft

  • Thread starter cobraa_eyz
  • Start date
  • Tags
    Code Fft
In summary, the conversation is about writing a code for FFT and fixing an error that occurs when using two variable values. The error is due to MATLAB treating * as matrix multiplication. The solution is to use a scalar for A and use a for loop for different magnitudes. The conversation also discusses adding limits to the FFT, which can be done using element-wise multiplication.
  • #1
cobraa_eyz
3
0

Homework Statement




How i will write the code for fft

Homework Equations



ACos(ωt+φ)

The Attempt at a Solution


n = 100;
n = 0:n;
nodd = 2*n + 1;
T=n*pi/2w
w=2*pi*1.93*10^14;
phi= 0;
A=-6:6;
X= A*cos(w*T+pi)
%Here it generates error
" ? Error using ==> mtimes Inner matrix dimensions must agree."
Y=fft(X)
Plot(A,T)


When ever i use two variable values it gives me the same error, how can i fix that.
Regards
 
Physics news on Phys.org
  • #2


I think the error lies in that MATLAB treats * as matrix multiplication. In
> X = A*cos(w*T+pi)
the cos part is a 1x101 matrix (1 row, 101 columns) while the A part is a 1x13 matrix (1 row, 13 columns). Clearly, those can't multiply. You should probably just use a scalar for A. Then if you want to know the Fourier transforms of different magnitudes, just do a for loop but make sure the fft is within the for loop.
 
  • #3


Dear Spring
many thanks for your reply. Well i have solve that problem. Now am taking the FFT how i will add Limit to FFT i.e [0,T]
.
%E= Acos(wt+pi) [0,T]" Time domain-> Frequency domain
n=100;
n=0:n;
nodd=2*n+1;
%pi=0; %"0:360
w=2*pi*1.93*10^14;

T=n*pi/2*w;
A=-50:50;% size(A)=[ 1 101] " to adjust matrix mismatch
X=A.*cos(w*T+pi);% element wise mulitpication [size(X)=[1,101]
Y=real(fft(X))
plot(T,Y)


Regards
Shahzad
 

Related to How i will write the code for fft

1. What is the FFT algorithm and how does it work?

The FFT (Fast Fourier Transform) algorithm is a mathematical technique used to efficiently calculate the Discrete Fourier Transform (DFT) of a signal. It works by dividing the signal into smaller sub-signals and then combining the results to produce the overall DFT.

2. What programming language is typically used to write FFT code?

The FFT algorithm can be implemented in any programming language, but it is most commonly used in languages such as MATLAB, Python, and C++. These languages have built-in functions or libraries that make it easier to write FFT code.

3. How do I choose the correct FFT size for my data?

The FFT size should be a power of 2 for optimal performance. If your data size is not a power of 2, you can pad it with zeros to the nearest power of 2. Additionally, the FFT size should be larger than the length of your data to avoid aliasing and loss of information.

4. Can I use the FFT algorithm for any type of data?

The FFT algorithm is best suited for processing discrete, time-domain signals. However, it can also be applied to other types of data such as images, audio, and video by converting them into the frequency domain using techniques such as the 2D FFT.

5. Are there any common mistakes to avoid when writing FFT code?

One common mistake is not properly handling the output of the FFT, which is complex numbers. The real and imaginary parts of the output should be combined to get the magnitude and phase of the signal. Another mistake is not properly scaling the output, which can result in incorrect amplitude values.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
400
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
334
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
982
  • Engineering and Comp Sci Homework Help
Replies
2
Views
944
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
Back
Top