I am trying to write a MATLAB code for cpsd ,but I could not figure out what does nfft mean....could you please assist me to understand how could get nfft for the data signal?
Hi, the previous answer is fairly comprehensive, I would only add that you should choose NFFT depending on your problem. Since the FFT frequency resolution Δf = Fs/NFFT, you should choose a NFFT value that is a power of a prime number in order to speed up your operation for big arrays, or you can choose a value that simply best suits your analysis needs.
Suppose you are monitoring the ac line voltage and you want to measure its amplitude, you will expect a single pulse at the frequency of 50 Hz with a height equal to the sine amplitude. Unfortunately you will get different results depending on the value of NFFT. If you use nextpow2(L), you will experience a power leakage between contiguous FFT bins (e.g. 48.83 Hz, 49.8 Hz, 50.78 Hz) that results in an amplitude error of almost 6% if you measure the maximum. Conversely if you use NFFT = L, you will have a frequency bin in 50 Hz that will contain all the power. Run the following code to evaluate yourself
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
y = sin(2*pi*50*t); % Normalized 50 Hz Line voltage
NFFT2 = 2^nextpow2(L); % Next power of 2 from length of y
Y2 = fft(y,NFFT2)/L;
f2 = Fs/2*linspace(0,1,NFFT2/2+1);
NFFT1 = L; % Same length of y
Y1 = fft(y,NFFT1)/L;
f1 = Fs/2*linspace(0,1,NFFT1/2+1);
% Plot single-sided amplitude spectrum.
H = figure('Name','Single-Sided Amplitude Spectrum of y(t)')