f =

This calculation of the frequency vector for the displayed Fourier transform is not the easiest to understand:

f = Fs*(0:(L/2))/L;

The frequency vector generally extends from 0 Hz (the units are actually cycles/(independent variable unit)) to ½ the sampling frequency, called the Nyquist frequency. In this instance, the fft l;ength is the same as the signal length ‘L’ so that is used to calculate the frequency vector. It extends from 0 to ‘L/2’ appropriately.

Going further:

syms L Fs

f = Fs*[0 1 2 3 (L/2)]/L

so the ‘f’ vector goes from 0 to the Nyquist frequency.in steps of ‘Fs/L’ (the default increment for the colon, : operator being 1).

The Nyquist frequency is the highest frequency at which a sampled signal can uniquely be described.

This then gets into sampling theory and details that are best left to texttbooks on digital signal processing. That quickly gets complicated, so I will stop here and encourage you to pursue that on your own.

Fs = 1000; % Sampling frequency

T = 1/Fs; % Sampling period

L = 1500; % Length of signal

t = (0:L-1)*T; % Time vector

S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);

X = S + 2*randn(size(t));

plot(1000*t(1:50),X(1:50))

title('Signal Corrupted with Zero-Mean Random Noise')

xlabel('t (milliseconds)')

ylabel('X(t)')

Y = fft(X);

P2 = abs(Y/L);

P1 = P2(1:L/2+1);

P1(2:end-1) = 2*P1(2:end-1);

f = Fs*(0:(L/2))/L

plot(f,P1)

title('Single-Sided Amplitude Spectrum of X(t)')

xlabel('f (Hz)')

ylabel('|P1(f)|')

EDIT — (18 Apr 2024 at 20:19)

Clarification added.

.