fft is not showing correct frequency or amplitude

49 views (last 30 days)
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')

Accepted Answer

VBBV
VBBV on 2 Oct 2021
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 Comments
VBBV
VBBV on 2 Oct 2021
clear all;
close all;
f1=1; f2=2; f3=3; f4=4; % freq of 4 signals, Hz
A1=1; A2=2; A3=3; A4=4;% 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
axis([0 6 0 4])
xlabel('Freq')
ylabel('Amplitude')
And if you use A1 = 1; A2 = 2; A3 = 3; A4 = 4; in your code you would get this
M Prabhu
M Prabhu on 2 Oct 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!