How can I add two fft functions?

10 次查看(过去 30 天)
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 个)

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