Using Inverse DFT to reconstruct a sampled sine wave is not perfect?

Start with a 2 Hz signal. The signal is sampled at a 4.167 Hz sample rate. The intent is to reconstruct a sampled signal (top right side) to be identical to the original signal (top left side).

Insert 200 zeros between the max frequency in the middle of the frequency spectrum (middle right side image). Then use Inverse DFT to attempt to reconstruct the original signal. The reconstructed signal (bottom right side) is similar, but not identical to the original signal (top left side).

Increasing the number of sine wave cycles being sampled, and changing the sample delta X from 0.01 sec to 0.001 sec, does not eliminate the problem. It should be possible to reconstruct the original, smooth sine wave (according to Nyquist).

Why does this not work perfectly?

Perhaps there are minor errors in the DFT code equations.

DFT:

{
Complex[] acplxFrequencies = new Complex[iNumOfValues];
for (int ii = 0; ii < iNumOfValues; ii++) {
acplxFrequencies[ii] = 0;
for (int iii = 0; iii < iNumOfValues; iii++) {
Complex.Exp(-Complex.ImaginaryOne *
2 * Math.PI * (ii * iii) /
Convert.ToDouble(iNumOfValues));
}
acplxFrequencies[ii] = acplxFrequencies[ii] / iNumOfValues;
}
return acplxFrequencies;
}//acplxConvertSignalToDft

Inverse DFT:

{
//---Number of spectrum elements
int iNumOfValues = acplxFrequencies.Length;
for (int ii = 0; ii < iNumOfValues; ii++) {
Complex cplxSum = 0;
for (int iii = 0; iii < iNumOfValues; iii++) {
cplxSum += acplxFrequencies[iii] *
Complex.Exp(Complex.ImaginaryOne *
2 * Math.PI * (iii * ii) /
Convert.ToDouble(iNumOfValues));
}
//---As a result we expect only real values (if our calculations
//    are correct, imaginary values should be equal or close to zero).
}

This DFT code comes from Jakub Szymanowski => https://www.codeproject.com/Articles/1077529/Fourier-Transform-in-Digital-Signal-Processing.

One key comment in the Inverse DFT code: "As a result we expect only real values (if our calculations are correct, imaginary values should be equal or close to zero)." Maybe the imaginary values cannot be ignored.

In addition, the frequency spectrum of the sine wave (middle left image) shows side bands next to the 2 Hz frequency point (easier to see in the first image at the top of this post). Perhaps there is a minor error in the DFT code equations, as only the 2 Hz point should appear in a perfect case.

• iiuc you are interpolating the time-domain signal by zero-padding in the FFT domain. Have a look at this dspguru.com/dsp/howtos/… Nov 12 '21 at 7:29
• The point that you've added is the DC (zero frequency point). It definitely should NOT be replicated at the top of the frequency range. The original is perfectly symmetrical about the DC point.
– Peter K.
Nov 12 '21 at 13:26
• @ttom: I've re-worded your question in a manner that I think gets at the heart of the question you're asking. Please feel free to revert it if I've missed the mark.
– Peter K.
Nov 13 '21 at 0:29
• Thanks Peter K. for clarifying my original post!
– ttom
Nov 14 '21 at 13:23

import matplotlib.pyplot as plt
import numpy as np

T = 201
fs = 100
t = np.arange(T)/fs

x = np.sin(2*np.pi*2*t)
x_fft = np.fft.fft(x)

is not bandlimited.

So when you decimate it by a factor of 24

decimation_factor = 24
x_decimated = x[1:T:decimation_factor]
t_decimated = t[1:T:decimation_factor]

you're going to get aliasing.  The FFT of the original 201 point signal on a log scale is below. As you can see, the values in between the peaks are non-zero.

To look at this even more closely: the red line shows the new Nyquist frequency (fs/2). As you can see, the frequency content of the original signal is non-zero above this frequency. So subsampling at this new sampling frequency will result in aliasing.

I'm still working on this to find a signal that can be perfectly reconstructed after subsampling to this level. Watch this space for another update.

• Is that because the original sine wave is not perfect? A delta X of 0.001 is better than a delta X of 0.01? For 0.001, there are 2001 points.
– ttom
Nov 18 '21 at 20:01
• After thinking more about this, the sampled x and y values are precise, not an approximation, although the accuracy of the computer will never result in complete precision. Per Nyquist, it should be possible to identify the frequencies => create the original sine wave, regardless of the delta X used. Is there a slight error in the DFT equations?
– ttom
Nov 18 '21 at 21:12
• @ttom Here's a minor update. Still doesn't have an example of something that could be reconstructed perfectly, but explains why your example can't be.
– Peter K.
Nov 24 '21 at 17:39