Discrete Fourier Transform algorithm not working properly?

118 Views Asked by At

I'm making a program where a vectorial image (set of complex points) is converted into a sum of complex waves. I'm first doing the DFT of this set of points (the number may vary, usually it's around 100 to 1000) calculating the coefficients: $$c_n=\frac{1}{N}\sum_{t=0}^{N - 1}{f(t)e^{-2\pi n i\frac{t}{N}}}$$ I calculate $101$ coefficients ($-50\leq n\leq50$).

Now that I have the coefficients I try to redraw the original shape, by doing the inverse DFT. I get a 2D curve but most of the times it gets just chaotic and diverges. The formula I use to do this is: $$f(t)=\sum_{n=0}^{N_C}c_ne^{2\pi nti}$$

Where $N_C$ is the number of coefficients used. I tested my algorithms by using simple functions like the following as input instead of images: $$f(t)=0.5e^{2\pi t i}+e^{3\cdot2\pi t i}$$ And I get $c_1=0.5$ and $c_3=1$ as expected, while every other coefficient is just $0$. If i use this function as an input: $$f(t)=e^{0.5\cdot2\pi t i}$$ I get strange results: The coefficients between $-25$ and $25$ are:

Re Im
0.003816815838070725 0.012315218857076468
0.003813591027954187 0.012862565533832103
0.003810100370944189 0.01345614355367736
0.0038063083980105065 0.014102273703532226
0.0038021730430501544 0.014808483672193947
0.003797644033982658 0.015583810793825061
0.0037926607888532186 0.016439200799321756
0.003787149629662038 0.017388040107298792
0.003781020041042592 0.01844687696126995
0.0037741595686217886 0.019636414570122863
0.0037664267427997542 0.020982904114953127
0.0037576410748355353 0.022520139212974098
0.0037475686073907646 0.024292378770455193
0.0037359005304209957 0.02635874571475219
0.003722220642032876 0.02880005281548732
0.0037059542190082478 0.03172978029462135
0.0036862846016637754 0.0353124932746145
0.00366201091096426 0.0397963546618827
0.003631291968961577 0.045574226113904465
0.0035911539091093743 0.05330793171742221
0.0035364608248771904 0.06420851151140089
0.0034575135452857234 0.08076509107412773
0.003333534321451747 0.1090896628519892
0.0031105269675283423 0.16979990844168213
0.0025906927272201746 0.42547968993915825
1.951997591342902E-15 -2.2279944896489305E-16
0.007812325836175332 -0.41937986251549136
0.0051945065938289285 -0.1698637389622447
0.004672502264143089 -0.10914343193900801
0.004448897029335363 -0.08081604884385901
0.00432467176415203 -0.06425826521749266
0.004245599938926926 -0.053357054912080565
0.004190835222609711 -0.04562297679486064
0.004150652220332767 -0.0398448665762224
0.004119903242738353 -0.03536084283873617
0.0040956084918419115 -0.031778014396121726
0.004075923539739443 -0.028848201842546873
0.0040596456055803 -0.026406830236898944
0.004045956856629951 -0.024340413212598392
0.0040342818142545854 -0.022568133992855396
0.004024203772274635 -0.021030866945578087
0.004015413573581822 -0.019684351284790513
0.0040076770156440445 -0.018494792053751236
0.0040008134325139055 -0.01743593709573443
0.003994681223942561 -0.016487082477533156
0.003989167837524223 -0.015631679408706252
0.003984182683170614 -0.014856341051377994
0.00397965202512267 -0.014150121348875616
0.0039755152362089315 -0.013503982710787482
0.003971722008543331 -0.01291039724451122
0.003968230247372277 -0.012363043999386375

Whose resulting curve is seems to be right on the X coordinate but it is just wrong on the Y. The same exact thing happens with the vectorial image as an input.

Can you help me figure out what's the problem when there's an arbitrary set of points as an input?

1

There are 1 best solutions below

2
On

If you have a set of $N$ points $\{x_0,x_1,x_2,...,x_{N-1}\}$ then the DFT is $$\hat{x}_k=\sum_{n=0}^{N-1}x_ne^{-2\pi i \frac{nk}{N}}$$ Now it is key to understand what are the DFT coefficients and what is their range.

If $N$ is odd, the first $(N-1)/2$ terms are $\{\hat{x}_{-\frac{N-1}{2}},\hat{x}_{-\frac{N-1}{2}+1},\hat{x}_{-\frac{N-1}{2}+2},...,\hat{x}_{-2},\hat{x}_{-1}\}$, then the next term is $\hat{x}_0$ and the subsequent terms are $\{\hat{x}_1,\hat{x}_2,...,\hat{x}_{\frac{N-1}{2}-2},\hat{x}_{\frac{N-1}{2}-1},\hat{x}_{\frac{N-1}{2}}\}$. The inverse DFT is then $$x_n=\frac{1}{N}\sum_{k=-\frac{N-1}{2}}^{k=\frac{N-1}{2}}\hat{x}_ke^{2\pi i\frac{nk}{N}}$$

If $N$ is even the first $(N-2)/2$ terms are $\{\hat{x}_{-\frac{N-2}{2}},\hat{x}_{-\frac{N-2}{2}+1},\hat{x}_{-\frac{N-1}{2}+2},...,\hat{x}_{-2},\hat{x}_{-1}\}$, then the next term is $\hat{x}_0$ and the subsequent terms are $\{\hat{x}_1,\hat{x}_2,...,\hat{x}_{\frac{N-2}{2}-2},\hat{x}_{\frac{N-2}{2}-1},\hat{x}_{\frac{N-2}{2}}\}$ and then there is an additional term $\hat{x}_\frac{N}{2}$. The inverse DFT is then $$x_n=\frac{1}{N}\sum_{k=-\frac{N-2}{2}}^{k=\frac{N}{2}}\hat{x}_ke^{2\pi i\frac{nk}{N}}$$ Remember that, in general, you have to use exactly these ranges to reconstruct the data properly exactly as you had it before using the DFT. This is due to a number of phenomena related to this.


For partial reconstruction using the first $Q$ low frequency terms of the DFT, you have to compute

$$x_n^Q=\frac{1}{N}\sum_{k=-Q}^{Q}\hat{x}_ke^{2\pi i\frac{nk}{N}}$$