def PlotSpillageVsNoiseFrequency():
	frequencies = [x/100.0 for x in range(5980, 6020)]
	allData = [[] for _ in frequencies]
	sps = 768
	xs  =[]
		
	for i, noiseFreq in enumerate(frequencies):
		data = dataImport.generateSignal(rawSps, [(noiseFreq, 1.0)])
		startOffset = 7
		for offset in range(0, 2500, 100):
		
			downSampledData = fftDataExtraction.downSample(data, rawSps, sps)[offset+startOffset:offset+128+startOffset]
			
			#pylab.plot([x/float(sps) for x in range(len(downSampledData))], downSampledData); pylab.show()
			
			#print sps, offset, len(downSampledData)
			#frequencies = [float(sps) * x / len(downSampledData) for x in range(len(downSampledData)/2+1)]
			
			leakage = getToneLeakage(downSampledData)
			#print 'leakage: %f' % leakage
			#plot(fData, frequencies)
			#print '%d - %f' % (sps, leakage)
			allData[i].append(leakage)
	
			
	pylab.subplot(211)
	for ys in zip(*allData):
		pylab.plot(frequencies, ys)
	pylab.grid(True)
	means = map(stats.mean, allData)
	pylab.subplot(212)
	pylab.grid(True)
	pylab.plot(frequencies, means, '-o')
	
	pylab.show()
	for f in frequencies:
		fStops.append(f - fRange)
		fStops.append(f + fRange)
		
	filter = signal.firwin(taps, fStops, nyq = sps/2)
	
	result = signal.convolve(filter, data)
	diff = len(result) - len(data)
	result = result[diff/2:-(diff-diff/2)]
	return result

if __name__ == "__main__":

	sampleFreq = 8000.0
	
	data = dataImport.generateSignal(sampleFreq, [(60.0, 1.0)], seconds = 2.0)
	
	#data = dataImport.readADSFile("Data/Mark/32kSPS_160kS_FlexorRadialis_Transitions.xls")
	#data = fftDataExtraction.downSample(data, 32000, sampleFreq, interpolate = False)

	times = [float(x) / sampleFreq for x in range(len(data))]
	
	fs = [60.0, 120.0, 180.0]
	data2 = bandStopFilterData(data, sampleFreq, frequencies = fs, fRange = 2.0, log2Taps = 13)
	
	pylab.subplot(211)
	pylab.plot(times, data)
	
	pylab.subplot(212)
	pylab.plot(times, data2)
	
				
				bestLeakage = getToneLeakage(bestBuffer)
				bestLeakages.append(bestLeakage)
				
				sampleRate = rawSps / sampleInterval
				sampleRates.append(sampleRate)
				
				bestRates.append(rawSps / bestInterval)
				
		lastSample = sample
	
	totalError = sum(leakages)
	print 'Total error through all time: %d' % totalError
	
	
	fig, ax = pylab.subplots()
	#pylab.subplot(211)
	ax.plot(range(len(sampleRates)), sampleRates, 'Blue')
	ax.plot(range(len(sampleRates)), bestRates, 'Red')
	ax.plot([0, 0], [767, 769])
	#pylab.grid(True); pylab.subplot(212)
	ax.twinx().plot(range(len(leakages)), leakages, color = 'Blue')
	ax.twinx().plot(range(len(bestLeakages)), leakages, color = 'Red')
	pylab.grid(True); pylab.show()
	
if __name__ == "__main__":
	#data = dataImport.readADSFile(filename)
	data = dataImport.generateSignal(rawSps, [(60.0, 1.0)], seconds = 10.0)#, (120.0, 0.5), (180.0, 0.8)])
	
	ConvergeOnCoherentSampling(data, rawSps)
	
	
	for i in range(n):
		for j in range(n):
			result[i] += timeData[j] * complex(cos(compression * 2.0 * pi * i / n*j), sin(compression * 2.0 * pi * i / n*j))
			
			
	result = map(lambda x: x/n, result)
	
	return result
	
if __name__ == "__main__":
	pylab.subplot(211)
	length = 1.0 / 6
	sps = 768.0
	compression = 0.2
	data = dataImport.generateSignal(sps, [(60.0, 1.0)], length)
	f = map(absolute, dft(data, compression))
	f = f[:len(f)/2]
	freqs = [compression / length * x for x in range(len(f))]
	
	pylab.plot(freqs, f)
	pylab.grid(True)
	
	pylab.subplot(212)
	length = 1.0 / 6
	sps = 768.0
	compression = 0.2
	data = dataImport.generateSignal(sps, [(60.2, 1.0)], length)
	f = map(absolute, dft(data, compression))
	f = f[:len(f)/2]
	freqs = [compression / length * x for x in range(len(f))]
				else:
					print 'no update'
					pass
				
		lastSample = data
				
if __name__ == "__main__":
	
	filename = "Data/Mark/32kSPS_160kS_FlexorRadialis_0%.xls"; rawSps = 32000
	#filename = "Data/Mark/32kSPS_160kS_ExtensorRadialis_0%.xls"; rawSps = 32000
	#filename = "NoiseData/6 Loops 8000SPS Volts.xls"; rawSps = 8000
	#rawData = dataImport.readADSFile(filename)
	f = 60.0
	sps = 8000
	#rawData = fftDataExtraction.downSample(rawData, rawSps, sps, interpolate = False)
	
	rawData = dataImport.generateSignal(sps, [(f, 1.0)], seconds = 30.0)
	TestConvergence(rawData, sps)
	
	x, y = zip(*sorted(allErrors))
	print len(x), len(y)
	pylab.plot(x, y, marker = 'o')
	#coefs = polyfit(x, y, 2)
	#x2 = [min(x) + i * (max(x) - min(x)) / len(x) for i in range(len(x))]
	#y2 = [sum([xi**(2.0-n) * c for n, c in enumerate(coefs)]) for xi in x2]
	#print len(x), len(y2)
	#pylab.plot(x2, y2, '-o')
	
	pylab.grid(True); pylab.show()
	#print stats.mean(centers)
	#print stats.stdDev(centers)