How can I add two fft functions?

조회 수: 10 (최근 30일)
Anagha
Anagha 2024년 1월 18일
편집: Paul 2024년 1월 19일
I want to convert 2 sine functions of time to functions of frequency using fourier transform and then I tried adding 2 of the fft functions I got and it is showing error. d=zabs+xabs is showing error.
A=1;
t=0:0.001:0.1;
x=A*sin(2*pi*1046*t);
xft=fft(x,1046);
xabs=abs(xft);
A=1;
t=0:0.0001:0.01;
z=A*sin(2*pi*523*t);
zft=fft(z,523);
zabs=abs(zft);
d=zabs+xabs;
Arrays have incompatible sizes for this operation.
subplot(211);
plot(d);

채택된 답변

Paul
Paul 2024년 1월 18일
편집: Paul 2024년 1월 19일
Hi Anagha
A=1;
t=0:0.001:0.1;
Here, the sampling frquency is 1000 Hz ... but the frequency of x is 1046 Hz. Assuming x should look like samples of 1046 Hz sine wave, the sampling frequency needs to be much higher, like maybe 5000 or 10000 Hz if you want to use round numbers.
x=A*sin(2*pi*1046*t);
Why zero pad the fft to 1046 samples?
xft=fft(x,1046);
xabs=abs(xft);
A=1;
t=0:0.0001:0.01;
Here, the sampling frequency of z is different than for x, so the samples of the fft of z can't be directly combined or compared to the samples of the fft of x, as pointed out by @Star Strider
z=A*sin(2*pi*523*t);
Here, the fft is zero padded to 523 samples. Why 523?
zft=fft(z,523);
zabs=abs(zft);
Here there is an error because zabs has 523 elements and xabs has 1046 elements. They each have to have the same number of elements in order to add them together
d=zabs+xabs;
Arrays have incompatible sizes for this operation.
Also, it's incorrect to take the sum of the magnitudes. Probably you meant to take the magnitude of the sum, i.e.,
d = abs(zft + xft)
Summary: Use a sampling frequency high enough for the signals of interest, use the same sampling frequency for both signals, and use the same length fft's for both, and add the fft outputs first.
  댓글 수: 1
Anagha
Anagha 2024년 1월 19일
Ok, I understand. Thank you so much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Star Strider
Star Strider 2024년 1월 18일
편집: Star Strider 2024년 1월 18일
It is obvious from looking at the code that the fft results have different lengths, specifically 523 and 1046 elements in each. In order to add them, they have to have the same numbers of elements.
The solution is to have the same fft lengths in both ‘zabs’ and ‘xabs’. Ideally, they should also have the same frequencies at the same locations in the frequency vector (that you will need to code separately). The fft documentation has examples that illustrate that process for one-sided and two-sided fft results.
EDIT — Corrected typographical errors.
.

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by