
from mathutils import *

"""
a = [1,2,3,4,7,10,50,4,3,2,1,2,1,1,2,3,4,7,10,50,4,3,2,1,1,2,1,2,3,4,7,10,50,4,3,2,1,2,1,2,3,4,7,10,50,4,3,2,1,1,2,1,2,3,4,7,10,50,4,3,2,1,2]
ft = fft(a)
it = ffti(ft)
plot([a,get_real(ft),get_imag(ft) ,get_real(it),get_imag(it)],("a", "real","img", "i real","i img"))



data = [2,3,4,10,20,5,3,2,1]
print estimate_peak_indexes(data)
print estimate_peak_indexes(data, threshold =21.0)
print estimate_peak_indexes(data, positive=False)
print estimate_peak_indexes(data, threshold =1.0, positive=False)
print estimate_peak_indexes(data, threshold = None, min_peak_distance = 10.0 , positive=False)
xdata = [1,3,7,23,34,45,56,58, 61]
print estimate_peak_indexes(data, xdata, threshold = None, min_peak_distance = 10.0 , positive=False)

"""




import random

#A sin wave  is:  A.sin(2.pi.f.t)
#The signal is composed by 3 sinusoids (100Hz, 200Hz, 400Hz) and a background noise
def signal_generator(t):
    return  math.sin(100*2*math.pi*t)  + 0.5 * math.sin(200*2*math.pi*t) + 0.25 * math.sin(400*2*math.pi*t) + 0.5* random.random()

sampling_frequency = 1024.0         
number_of_samples = 1024

time_vector = [x / sampling_frequency for x in frange(0, number_of_samples, 1)]
signal = [signal_generator(x) for x in time_vector]

tranform = fft(signal)
two_side_spectrum = [x / number_of_samples for x in get_modulus(tranform)]
spectrum = [two_side_spectrum[0],] + [x * 2 for x in two_side_spectrum[1:len(two_side_spectrum)/2 + 1] ]


number_of_samples = len(tranform)    # Signal may have been padded to next power of two
freq_vector = [x * sampling_frequency / float(number_of_samples) for x in frange(0, len(spectrum) , 1)]

#print f

plot([signal,spectrum], ["signal", "spectrum"],[time_vector, freq_vector])
#plot(p1, xdata = f)