Search code examples
matlabfftsampling

Matlab fft function


The code below is from the Matlab 2011a help about fft function. I think there is a problem here : why do they multiply t(1:50) by Fs, and then say it's time in millisecond ? Certainly, it happens to be true in this very particular case, but change the value of Fs to, say, 2000, and it won't work anymore, obviously because of this factor of 2. Right ? Quite misleading, isn't it ? What do I miss ?

Fs = 1000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
L = 1000;                     % Length of signal
t = (0:L-1)*T;                % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); 
y = x + 2*randn(size(t));     % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')

Clearer with this :

fs = 2000;          % Sampling frequency
T = 1 / fs;         % Sample time
L = 1000;           % Length of signal
t2 = (0:L-1)*T;     % Time vector
f = 50;             % signal frequency
s2 = sin(2*pi*f*t2);
figure, plot(fs*t2(1:50),s2(1:50)); % NOT good
figure, plot(t2(1:50),s2(1:50));    % good

Solution

  • You don't miss anything. This is a typical bad practice of not having units as comments for each line. For example,

     Fs=1000;          % in [Hz]
     T=1/Fs;           % [sec]
     L = 1000;         % Length of signal
     t = (0:L-1)*T;    % Time vector
    

    then, the time vector has units of 1/1000 seconds, or milliseconds... (each element is 1 ms). In the other case Fs=2000 %[Hz], which makes the time vector to have units of 1/2000 seconds...