I'm seeking some guidance/reassurance on my understanding so far of the DFT, as demonstrated by my MATLAB script below. I deliberately compute the DFT longhand.
My first question: Are my interpretations of the Nyquist-Shannon Sampling Theorum principles correct, as described here:
The DFT can provide perfect reconstruction of the original signal provided:
- The highest frequency component of the signal cannot exceed the Nyquist frequency = (sampling frequency)/2,
- the sampling frequency is at least twice the signal frequency. This lower limit is known as the Nyquist Rate = (signal frequency) * 2, and
- does not equal the Nyquist Rate exactly (it must exceed it)
My second question: what determines the (practical) lower limit for the number of samples (N)? Is there a formula? For example the script below correctly reconstructs the signal even for N reduced to 20, but further reduction results in a signal spread across numerous frequency bins.
My last question: is it possible to achieve perfect reconstruction if only one cycle of the signal is sampled?
Here's my code and plots:
f0 = 3; % signal fequency (Hz) w0 = 2*pi*f0; % rad/sec T0 = 1/f0; % signal period fs = 20; % sample frequency (Hz) N = 100; % sample count T = 1/fs; % sample period st = N*T; % total sample time f = transpose((0:N-1)*(fs/N)); % frequency sampling bins nyquistRate = f0*2; fNyquist = fs/2; fNyquistIdx = N/2; % frequency bin for the Nyquist frequency t = transpose(0 : T : st-T); % time vector, length N A = 4.5; % amplitude s = A*sin(w0*t); % generate N samples X = zeros(N,1); % perform DFT for k = 0:N-1 for n = 0:N-1 X(k+1,1) = X(k+1,1) + s(n+1)*exp(-1i*2*pi*k*n/N); end end
I do get the same results if I use the MATLAB inbuilt FFT in place of above
X = fft(s);
To convert the resulting spectrum to a single-sided reconstruction:
1) Discard all components above the Nyquist Frequency in the 2-sided plot, and
2) Double the magnitudes of those below the Nyquist Frequency, and
3) Divide the result by the number of samples (N)
XX = X; XX(1:fNyquistIdx) = XX(1:fNyquistIdx)*2/N; XX(fNyquistIdx+1:N) = 0 + 0i;