How can I add two fft functions?

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);

 Respuesta aceptada

Paul
Paul el 18 de En. de 2024
Editada: Paul el 19 de En. de 2024
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 comentario

Anagha
Anagha el 19 de En. de 2024
Ok, I understand. Thank you so much for your help!

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 18 de En. de 2024
Editada: Star Strider el 18 de En. de 2024

1 voto

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.
.

Categorías

Más información sobre Fourier Analysis and Filtering en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de En. de 2024

Editada:

el 19 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by