How can I add two fft functions?
10 views (last 30 days)
Show older comments
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;
subplot(211);
plot(d);
0 Comments
Accepted Answer
Paul
on 18 Jan 2024
Edited: Paul
on 19 Jan 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;
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.
More Answers (1)
Star Strider
on 18 Jan 2024
Edited: Star Strider
on 18 Jan 2024
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.
.
0 Comments
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!