# Rebuilding a signal using the fast fourier transform [closed]

I've created a signal by adding a sine wave and a cosine wave using R:

# number of samples
N <- 1000

# time period
TT <- 2

# sampling frequency (number of samples by unit of time)
fs <- N / TT

# vector of sampling time points
stp <- seq(from=0, to=TT - 1 / fs, by=1 / fs)

# 2 Hz sine wave with peak amplitude 2
sn1 <- 2 * sin(2 * pi * 2 * stp)

# 5 Hz cosine wave with peak amplitude 3
sn2 <- 3 * cos(2 * pi * 5 * stp)

# input signal
sn <- sn1 + sn2 Using the Fourier transform, I can see that the signal is made up of two waves with amplitudes 2 and 3 and frequencies 2 and 5, respectively.

fou <- fft(sn)
N <- length(fou)
fou <- fou[2:((N / 2) + 1)]
freq <- (1:(N / 2)) / TT
ampl <- Mod(fou) / length(fou)
plot(freq, ampl) Question: How do I know that the wave with frequency 2 is a sine wave and that the wave with frequency 5 is a cosine wave?

## closed as off-topic by Dilip Sarwate, jbowman, Xi'an, COOLSerdash, JohnFeb 3 '15 at 22:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

• You've plotted the amplitude: ampl <- Mod(fou) / length(fou), if you want to distinguish between a sine and a cosine you also need to look at the phase plot. Regarding the 'reconstruction', have you tried just using an inverse FFT? e.g. artax.karlin.mff.cuni.cz/r-help/library/signal/html/ifft.html – Dan Feb 3 '15 at 15:22

Sine and Cosine are the same wave shifted by $\pi/2$ in phase $$\cos(x)=\sin(x+\pi/2)$$. Fourier transform returns you a complex number for each frequency. This number has the amplitude and the angle (phase). It's basically a set of Sine waves with amplitudes and phases. Equivalently, you can re-write them as a sum of Sine and Cosine waves of different amplitudes.

Example:

phase <- Arg(fou)
sx=ampl*cos(2*pi*freq*stp+phase)+ampl*cos(2*pi*freq*stp+phase)
plot(sx)

• Thanks for the answer. Could you further help me to rebuild the signal above using the output of fft()? – user7064 Feb 3 '15 at 15:19
• You got the frequency already. All you need is a phase, which is an angle of the complex number. – Aksakal Feb 3 '15 at 15:22
• Thanks :-) Last thing: why do you use cosines in 'sx' rather than sines? – user7064 Feb 3 '15 at 15:50
• Try sines, but you'll have to add $\pi/2$ to the phase. It's just the way sinces and cosines are add up in FFT. – Aksakal Feb 3 '15 at 15:53
• Perfect :-) (+1) – user7064 Feb 3 '15 at 15:53