MATLAB: DFT linearity – “melodic” tones spectrum problem

fft

Hello,
My question is simple:
When I take the FFT of the signal consisting of three subsequent sinusoidal tones should I get the same resulting spectrum as when I take spectra of each tone separately and sum them up?
My script below suggest that this might not be true. Why?
With regards, Micholeodon
%%"MELODIC" vs "HARMONIC" tones analysis
clear;
close all;
clc;
% prepare custom signal: contains threee harmonics: f1 f2 f3 Hz. Each
% harmonic lasts for t_dur s. Harmonics onsets at: t1, t2, t3.
f1 = 2;
f2 = 10;
f3 = 20;
t1 = 0;
t2 = 1;
t3 = 2;
a1 = 1;
a2 = 1;
a3 = 1;
t_dur = 1;
srate = 500;
t_i = 0;
t_e = t_i + 3*t_dur - 1/srate; % nescessary to delete last sample in order to srate/length(timeline) be integer
timeline = t_i:(1/srate):t_e;
n = @(x) (x-t_i)*srate + 1; % function to get sample number from time
s1 = a1*sin(2*pi*f1*timeline);
s1(n(t_i):n(t1)) = 0;
s1(n(t1 + t_dur):n(t_e)) = 0;
s2 = a2*sin(2*pi*f2*timeline);
s2(n(t_i):n(t2)) = 0;
s2(n(t2 + t_dur):n(t_e)) = 0;
s3 = a3*sin(2*pi*f3*timeline);
s3(n(t_i):n(t3)) = 0;
s3(n(t3 + t_dur):n(t_e)) = 0;
sig_mel = s1 + s2 + s3;
h1 = a1*sin(2*pi*f1*timeline);
h2 = a2*sin(2*pi*f2*timeline);
h3 = a3*sin(2*pi*f3*timeline);
sig_harm = h1 + h2 + h3;
X_mel = fft(sig_mel);
X_mel_mag = abs(X_mel);
X_harm = fft(sig_harm);
X_harm_mag = abs(X_harm);
m = 0:length(timeline)-1;
figure(1)
subplot(4,1,1)
plot(timeline, sig_mel)
title('tones in sequence')
subplot(4,1,2)
plot(timeline, sig_harm)
title('tones harmonicly')
subplot(4,1,3)
plot(m, X_mel_mag)
title('DFT of sequenced tones')
subplot(4,1,4)
plot(m, X_harm_mag)
title('DFT of harmonic tones')
% check linearity of the spectrum: analyse each tone separately, obtain
% their spectra and sum them together and finally compare with melodic
% signal spectrum.
figure(2)
subplot(3,1,1)
plot(timeline,s1)
title('first tone')
subplot(3,1,2)
plot(timeline,s2)
title('second tone')
subplot(3,1,3)
plot(timeline,s3)
title('third tone')
figure(3)
s1_dft = fft(s1);
s2_dft = fft(s2);
s3_dft = fft(s3);
subplot(4,1,1)
plot(m,abs(s1_dft))
title('first tone DFT')
subplot(4,1,2)
plot(m,abs(s2_dft))
title('second tone DFT')
subplot(4,1,3)
plot(m,abs(s3_dft))
title('third tone DFT')
subplot(4,1,4)
sum_dft = abs(s1_dft)+abs(s2_dft)+abs(s3_dft);
plot(m,sum_dft)
title('sum of DFTs')
% compasrison
figure(4)
subplot(3,1,1)
plot(m, X_mel_mag)
title('DFT of sequenced tones')
subplot(3,1,2)
plot(m,sum_dft)
title('sum of DFTs')
subplot(3,1,3)
plot(m, X_mel_mag - sum_dft)
title('Difference')

Best Answer

You need to define the sum_dft as
sum_dft = s1_dft+s2_dft+s3_dft;
instead and then plot and compare using abs(sum_dft). Otherwise you are not doing a fair comparison as you throwing away all the phase information.
HTH