# Express a wave as a sum of cosine

I started off with this:

$$x = 5 \cos(200 \times 2 \pi t ) + 10 \cos( 400 \times 2 \pi t)$$

by performing a 1024 point DFT on it. Then I performed IDFT on the complex values obtained through DFT. Now, I want to express the original wave in terms of it's harmonics, so when I combine the DC and first harmonic or upto n harmonics, I get the original wave back. But how do I obtain these harmonics?

This is what I actually want to do. I have some real valued data(144 points). I want to express this data as a sum of cosines. For this purpose, I perform a 144 point DFT operation on this data, and bring them to frequency domain. Does the Euler's identity, give me these cosine?

$$X[k] * e^{\tfrac{j2\pi kn}{144}} + X[144−k] * e^{\tfrac{−j2\pi kn}{144}}$$

• Your cosines are both of the same frequency. Are you sure you wrote down the question correctly? – Phonon Feb 21 '12 at 19:51
• I'm not sure I understand your question. What you want is just the harmonics that represent those two sinusoidal signals? And if I understand correctly, by harmonics you mean the frequency at which each cosine is centered? You want the amplitude and phase of the cosines? I'm not understanding your question :S – El Developer Feb 21 '12 at 22:39
• possible duplicate of Calculate harmonics using DFT from real points – Dilip Sarwate Feb 22 '12 at 0:33
• Discrete Cosine Transform?... – Spacey Feb 22 '12 at 6:48

The Fourier transform converts signals into coefficients of cosines and sines. This is why the coefficients are complex. Simply adding up those two coefficients won't give you sufficient information about the phase (someone can correct me on this statement, but I believe it's true). In other words, for signal of length N you're trying to find coefficients $a_k$ and $b_k$ such that

$$x[n] = \sum_{k=0}^{N/2} a_k \cos(\omega k)+\sum_{k=0}^{N/2} b_k \sin(\omega k)$$

I would suggest using Discrete Cosine Transform (DCT) instead. It literally breaks you signal up into a sum of cosines, it gives you back all real coefficients and it takes care of the phase problem completely.

If you must use Fourier Transform, you should realize that

$$e^{i \omega t} = \cos (\omega t) + i \sin(\omega t)$$

This tells us that we can recreate the even-symmetric component (cosine) from real coefficients of the Fourier Transform, and odd-symmetric component (sine) from imaginary coefficients of the Fourier Transform.

The formula then befomes:

For a given $k$,

$$a_k \cos\left( \frac{2 \pi kn}{N} \right) = \frac{1}{N}\left( \Re (X[k]) e^{ \tfrac{i 2 \pi kn}{N}} + \Re (X[N-k]) e^{ \tfrac{i 2 \pi (N-k)n}{N} } \right),$$

$$b_k \sin\left( \frac{2 \pi kn}{N} \right) = \frac{1}{Ni}\left( \Im (X[k]) e^{ \tfrac{i 2 \pi kn}{N}} + \Im (X[N-k]) e^{ \tfrac{i 2 \pi (N-k)n}{N} } \right),$$

where $\Re(\cdot)$ and $\Im(\cdot)$ represent taking real and imaginary part of a complex number respectively. Note that this is done for each $n \in \{0 \ldots N-1\}$

Note that because of circular symmetry of DFT, $X[N] = X[0]$, so you'll end up adding up the same thing twice according to the formula above. If you do that, then the actual $a_0$ is half of what you get according to the formula above. If you find this division by 2 confusing, this section of Wikipedia article on Fourier Series may provide a better explanation (look for the formula where $a_0$ is divided by 2).

• Beware of lots of typographical errors in the statement: "For each $k$, $$a_k \cos\left( \frac{2 \pi kn}{N} \right) = \Re (X[k]) e^{ \tfrac{i 2 \pi kn}{N}} + \Re (X[N-k]) e^{ \tfrac{i 2 \pi (N-k)n}{N} },$$ $$b_k \sin\left( \frac{2 \pi kn}{N} \right) = \frac{1}{i}\left( \Im (X[k]) e^{ \tfrac{i 2 \pi kn}{N}} + \Im (X[N-k]) e^{ \tfrac{i 2 \pi (N-k)n}{N} } \right),$$ where $\Re(\cdot)$ and $\Im(\cdot)$ represent taking real and imaginary part of a complex number respectively. Note that this is done for each $n \in \{0 \ldots N-1\}$" – Dilip Sarwate Feb 23 '12 at 3:39
• @DilipSarwate I have to be either blind or wrong in my math. Feel free to fix it or post your own answer. – Phonon Feb 23 '12 at 3:45
• @DilipSarwate ...or point out what's wrong. Is it the math or the style? I checked the formula with MATLAB. Recovered the signal fine. – Phonon Feb 23 '12 at 3:49
• I had voted to close this question as a duplicate of another one which had been answered and the (correct) answer there accepted by OP Rakesh. Your answer is wrong because it does not distinguish $X[0]$ must always be treated separately, and when $N$ is even, as it is in this case, $X[N/2]$ must also be must be treated separately. See Hilmar's answer to the other question or Wikipedia. The result is something that looks like your (continued) – Dilip Sarwate Feb 24 '12 at 2:29
• $$x[n] = \sum_{k=0}^{N/2} a_k \cos(\omega k)+\sum_{k=0}^{N/2} b_k \sin(\omega k)$$ which also has typos since the a_0 term is missing. Your assertion that "Note that this is done for each $n \in \{0…N−1\}$" is incorrect; you only need to do it for $1 \leq n \leq (N-1)/2$ and you need to account for $n=0$ and $n=N/2$ (even $N$) separately. – Dilip Sarwate Feb 24 '12 at 2:39