# Program to identify the freqs amps and phases of a function
# that is a single sine wave that oscillates an intral number
# of times for N points
N = 1024 # FFT works best with powers of 2
t = seq(from=0,by=2*pi/N,length=N) # N evenly spaced pts 0 -- 2*pi
f = 100 # number of cycles per N pts
ph = 1 # the phase at f
amp = 4 # the amplitude at f
y = amp*cos(f*t+ph); # our "signal"
plot(y[1:100],type='l')
Y = fft(y) # take the fft
#plot(Mod(Y[1:(N/2)]) / (N/2)) # plot of Modulus = amplitudes
# Note how only one (at f) is non-zero
print("amp at f is ")
print(Mod(Y[f+1])/(N/2)) # remember sine oscillating f times
# appears at f+1
# also must divide by N/2 to get correct amp
print("phase at f is")
print(Arg(Y[f+1])) # the phase in (-pi/2,+pi/2)