##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#fft_examply.py
#
#PURPOSE:
#  To show an example of how to use the fft along with frequencies

from numpy import fft
from numpy import *
import matplotlib.pylab as mplot
import scipy
from pylab import *

NumSamp  = 50000                               #Total number of points
Samp_ts  = 0.1                                 #Sampling interval - every Samp_ts seconds
tpts     = arange(NumSamp)*Samp_ts             #The sampling points
t_len    = Samp_ts*NumSamp                     #Total length of signal

k_max    = NumSamp/2                           #Maximum k value 
FSpac    = 1./(NumSamp*Samp_ts)                #Frequency Spacing

ks       = FSpac*(-NumSamp/2+arange(NumSamp))  #k values : f=2pi/k
ks_real  = FSpac*arange(NumSamp/2+1)           #ks_real  : real k values
tpts_real= tpts[0:NumSamp/2+1]
f1       = 0.2                                 #frequency 1
f2       = 4.5                                 #frequency 2
s        = cos(2*pi*f1*tpts)+cos(2*pi*f2*tpts) #Signal

ffts     = rfft(s)
mplot.figure(0)
mplot.clf()
mplot.plot(tpts, s)

mplot.figure(1)
mplot.clf()
mplot.plot(ks_real[1:], ffts[1:].real**2)
