# Discrete Fourier Transform by longhand using MATLAB - what is the lower limit for N?

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:

1. The highest frequency component of the signal cannot exceed the Nyquist frequency = (sampling frequency)/2,
2. the sampling frequency is at least twice the signal frequency. This lower limit is known as the Nyquist Rate = (signal frequency) * 2, and
3. 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)
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;


Here are the resulting spectrums for X and XX:

• The Nyquist theorem is related to the sampling of an analogue signal: the sampling frequency must be higher than twice the maximum signal frequency, for a real signal. Nothing to do with the DFT, which is a way to estimate the spectrum of a digital signal. If you start with a digital signal and perform DFT then IDFT, you will always get the original signal – Damien Jun 4 at 20:35
• "Nothing to do with the DFT" ? I'm not sure what you mean. The Nyquist-Shannon Sampling Theorum on Wikipedia starts with " the sampling theorem is a fundamental bridge between continuous-time signals and discrete-time signals". I read this as between analog and digital. It goes on to define Nyquist rate and frequency. Sorry but your response has confused. – Chris Jun 5 at 9:44
• It was a comment, not an answer :) The Wikipedia is compatible if what I wrote. The Nyquist theorem states if an analogue signal can be sampled or not at a given sampling frequency. DFT(Digital Fourier Transform) is applied to a digital signal. DFT is a non perfect way to check if a signal was sampled correctly. It can also be used to check if a further downsampling can be applied. But I may having misunderstood your question – Damien Jun 5 at 10:18
• Thanks for your ideas, but we would need a much longer discussion for me to get on your wavelength (no pun intended :). Although it's interesting you call it the Digital Fourier Transform, whereas it's usually the Discrete Fourier Transform ... unless these are different concepts? – Chris Jun 6 at 22:46
• Sorry, it was a typo. Discrete Fourier Transform of course – Damien Jun 7 at 5:56