# Problem of Discrete Fourier?

I tried Fourier Transform for a range of figures.

Firstly, I tried in this way.

data1=Table[1, {i,1,2,1}];
data2=Table[0, {i,1,1,1}];
data3=Join[data1, data2];
data=Flatten[Table[data3, {i,1,3}]];
ListPlot[data]


I tried Fourier transform according to its definition as follow:

ft1=Table[Sum[data[[m]] Exp[-i*m*k], {m,1,Length[data]}], {k,0,10 Pi,0.1}];
ListLinePlot[Abs[(ft1)^2], PlotRange -> All]


Secondly, I tried fourier transform by another way:

fdata[x_]:= 1 /; (x>0) && (x<=2)
fdata[x_]:= 0 /; (x>2) && (x<=3)
fdata[x_]:= 1 /; (x>3) && (x<=5)
fdata[x_]:= 0 /; (x>5) && (x<=6)
fdata[x_]:= 1 /; (x>6) && (x<=8)
fdata[x_]:= 0 /; (x>8) && (x<=9)
Plot[fdata[x], {x,0,9}]


ft2[k_]:=NIntegrate[fdata[x]Exp[-i*x*k],{x,0,9}];
Plot[Abs[(ft2[k])^2],{k,0,5Pi},PlotRange->All]


Then I got the result as follow:

The second result is the one should be. But what is wrong with the first method?Why the difference appears? Any suggestions R welcomed.

If it is the result of discrete data?How to avoid?

Such as, I plan to get the Fourier Transform of a large amount data as follow. (And I do not want to fit the curve by any functions because the difference will affect the result.)

data={1., 0.494297, 0.0554558, -0.00119684, 0.0347557, 0.0274166,0.0844178, 0.140589, 0.100378, 0.0305703, 0.00906179, 0.0165403,0.0333436, 0.0559314, 0.0647997, 0.0487751, 0.0278078, 0.0198066, 0.022007, 0.0273021, 0.0328905, 0.0344371, 0.0283518, 0.0199067,0.0149784, 0.0139971, 0.017503, 0.0188609, 0.0190994, 0.0164812, 0.0121582, 0.00943724, 0.00991142, 0.0110141, 0.0114948, 0.0092613, 0.00763611, 0.00498833, 0.0051873, 0.00499402, 0.00549799,0.00380389, 0.00191446, 0.00156621, 0.0015737, 0.00183529, 0.00192207, 0.00272739, 0.00171912, 0.00133939, 0.000664813, -0.000731862, 0.000542469, 0.00210733, 0.00157748, 0.000378449,-0.000413625, -0.000241379, -0.000524688, 0.00125707, 0.000893426, -0.000960053, -0.00141913, -0.000102416, -0.000507345, -0.00153032,-0.000878255, -0.00153944, -0.00212547, -0.00216173, -0.00205534, -0.00268722, -0.00284011, -0.00307404, -0.00385506, -0.00360069, -0.00226998, -0.00166825, -0.0022364, -0.0032332, -0.00312994,-0.00264376, -0.00222135, -0.00129998, -0.00118719, -0.00209173, -0.00368347, -0.00310659, -0.00122648, -0.000768231, -0.00177089,-0.0023162, -0.00259222, -0.00185519, 0.000111307, 0.000415605, 0.0000449796, -0.000621294, -0.00165163, -0.00163402, -0.000711393, -0.00110962, -0.00229118, -0.00360164, -0.00602596, -0.00820126, -0.00657783, -0.00431852, -0.00486986, -0.0060202, -0.00708399, -0.00858061, -0.00780665, -0.00629949, -0.00510292, -0.00752368, -0.00787104, -0.0106296, -0.0113897, -0.0107749, -0.0125735,-0.0143625, -0.0161466, -0.0163181, -0.0187493, -0.0200038, -0.0189971, -0.0182773, -0.0193625, -0.0191536, -0.0189072, -0.0165243, -0.0165929, -0.0167107, -0.0209827, -0.0244816, -0.0200228, -0.0224443, -0.0217907, -0.0182094, -0.0173637,-0.0290431};

ListLinePlot[data,PlotRange->All]


Then I did

ftEx=Table[Sum[data[[i]] Exp[-I*i*k], {i, 1,Length[data]}], {k, 0, 20, 0.01}];
ListLinePlot[Abs[ftEx], PlotRange -> All,Frame -> True]


I do not want all the peaks after the 1 on x-axis, shows as in the red circles.

This is the problem of discrete Fourier transform? Then how to avoid?

• There is nothing wrong with the results, you are just calculating different things. Have a look on discrete Fourier transform (DFT) of discrete-time (sampled) signals to understand what is going on. Jun 8, 2015 at 14:35
• If I have a large amount of discrete 1D data from an image and it is not easy to give an exact function to the sample, how can I avoid the problems brought from discrete samples?
– Cici
Jun 9, 2015 at 4:28

I'm pretty sure if you do ListLinePlot[data] you'll see your first data is actually this plot and not the square wave you expect:

Generate your second plot with a Piecewise:

Plot[Piecewise[{{1, 0 < x < 2}, {0, 2 < x < 3}, {1, 3 < x < 5}, {0,
5 < x < 6}, {1, 6 < x < 8}, {0, 8 < x < 9}}], {x, 0, 9}, Exclusions -> None]


You can use that Piecewise function to generate a better sampling of the square wave:

dt=1/100; (* Your sample rate *)
Table[Piecewise[{{1, 0 < x < 2}, {0, 2 < x < 3}, {1, 3 < x < 5},
{0, 5 < x < 6}, {1, 6 < x < 8}, {0, 8 < x < 9}}], {x, 0, 9, dt}]]]


Now remember Fourier is a discrete transform, so you need to sample the function. Because you can do it analytically with a FourierTransform, I recommend avoiding the problems of discrete sampling altogether. If you want a continuous FT in terms of w, then try :

FourierTransform[
Piecewise[{{1, 0 < x < 2}, {0, 2 < x < 3}, {1, 3 < x < 5},
{0, 5 < x < 6}, {1, 6 < x < 8}, {0, 8 < x < 9}}], x, w]

(*Result: ((E^(I w) + E^(4 I w) + E^(7 I w)) Sqrt[2/\[Pi]] Sin[w])/w *)


To complicate things further, you may need to explore the FourierParameters option of Fourier and FourierTransform

We can now plot the spectrum square magnitude:

Plot[Abs[(((E^(I w) + E^(4 I w) + E^(7 I w)) Sqrt[2/\[Pi]] Sin[w])/w)]^2,
{w, 0, 20},PlotRange -> Full]


• If I have a large amount of discrete 1D data from an image and it is not easy to give an exact function to the sample, how can I avoid the problems brought from discrete samples?
– Cici
Jun 9, 2015 at 2:22
• @Cici for enough data the problem starts to go away for low frequencies, just remember that Fourier will not capture higher frequencies especially above the Nyquist rate. You can look into Upsample, TimeSeriesResample and Interpolate to oversample your data. Fourier assumes a periodic signal. If your data is localized, only a tradeoff between locality and frequency is possible due to an uncertainty principle. I recommend other transforms, like the FourierDCT and the more localized DiscreteWaveletTransform Jun 9, 2015 at 4:12