# fft is not showing correct frequency or amplitude

조회 수: 19(최근 30일)
M Prabhu 2021년 10월 1일
댓글: M Prabhu 2021년 10월 2일
I generated a sample signal using sum of three sine waves. When I plot the signal using fft command to view the individual frequency components, I always get an error in the second (i.e., second in ascending order) frequency component. It either shows up as half the correct frequency or some multiple (twice/ four times, etc). If the frequency is correct, the amplitude is incorrect. The other two frequencies and amplitudes are always correct.
I tried adding a fourth signal, but the same problem repeats with the second signal only. All others are correct. I tried with two signals only and the result is the same. Second signal is wrong.
.
Tried restarting Matlab, but it repeats. I tried different values of frequencies and amplitudes, but it is always the second in ascending order which misbehaves. Did not try more than 4 frequencies. I am totally flabbergasted! I am not very familiar with fft, Am trying to learn. Attaching the code and output figure. Any help would be welcome please.
clear all;
close all;
f1=1; f2=2; f3=3; f4=4; % freq of 4 signals, Hz
A1=1; A2=0; A3=0; A4=0;% Amplitudes of 4 signals
f_max=max([f1 f2 f3 f4]); % max of all signal frequencies
dt=1/(f_max*10); % time step
t=0:dt:2000*dt; % time vector 1
x = A1*cos(2*pi*f1*t)+1*cos(A2*pi*f2*t)+A3*cos(2*pi*f3*t)+A4*cos(2*pi*f4*t); % final signal
fs=1/dt; % sampling rate
tm=0:1/fs:(length(x)-1)/fs; % time vector 2
X=fft(x,length(x))*(2/length(x)); %fft of final signal
mag_X = abs(X); % magnitude
freq=0:1/tm(end):fs/2-(1/tm(end)); % compute frequency
plot(freq,mag_X(1:length(freq))); % fft plot
xlabel('Freq')
ylabel('Amplitude')

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

### 채택된 답변

VBBV 2021년 10월 2일
clear all;
close all;
f1=1; f2=2; f3=3; f4=4; % freq of 4 signals, Hz
A1=1; A2=0; A3=0; A4=0;% Amplitudes of 4 signals
f_max=max([f1 f2 f3 f4]); % max of all signal frequencies
dt=1/(f_max*10); % time step
t=0:dt:2000*dt; % time vector 1
x = A1*cos(2*pi*f1*t)+A2*cos(2*pi*f2*t)+A3*cos(2*pi*f3*t)+A4*cos(2*pi*f4*t); % final signal
fs=1/dt; % sampling rate
tm=0:1/fs:(length(x)-1)/fs; % time vector 2
X=fft(x,length(x))*(2/length(x)); %fft of final signal
mag_X = abs(X); % magnitude
freq=0:1/tm(end):fs/2-(1/tm(end)); % compute frequency
plot(freq,mag_X(1:length(freq))); % fft plot
xlabel('Freq')
ylabel('Amplitude')
Can you check the difference now ? whether you get same error?
I think you have a typo for the amplitude in second term of
x = A1*cos(2*pi*f1*t)+A2*cos(2*pi*f2*t)+A3*cos(2*pi*f3*t)+A4*cos(2*pi*f4*t); % final signal
It should be A2*cos(2*pi*f2*t) and not 1*(cos(A2*pi*f2*t))
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
M Prabhu 2021년 10월 2일
Thank you. How silly of me! Didn't see that typo...and spent hours trying to fix with all kinds of data. Thank you very much, Start Strider and VBBV, for your effort and time.

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

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by