def testPlotFrequencyDomain():
	filename = baseFilename % 0
	
	rawData = dataImport.readADSFile(filename)
	rawSps = 32000
	downSampled = _downSample(rawData, rawSps)
	downSampledLinear = _downSampleLinearInterpolate(rawData, rawSps)
	rawTimes = range(len(rawData))
	times = [float(x) * rawSps / samplesPerSecond for x in range(len(downSampled))]
	
	#pylab.plot(times, downSampled)
	#pylab.plot(rawTimes, rawData)
	#pylab.plot(times, downSampledLinear)
	pylab.show()
	
	index = 0
	fdat = applyTransformsToWindows(getFFTWindows(downSampled), True)[index]
	fdatLin = applyTransformsToWindows(getFFTWindows(downSampledLinear), True)[index]
	#print [str(x) for x in zip(fdat, fdatLin)]
	
	frequencies = [i * samplesPerSecond / windowSize for i in range(len(fdat))]
	
	pylab.semilogy(frequencies, fdat)
	pylab.semilogy(frequencies, fdatLin)
	pylab.grid(True)
	pylab.show()
def createChangingTimeDomainDataPhaseMatch(baseFilename, low = 0, high = 10):
	dat0 = dataImport.readADSFile(baseFilename % low)
	dat1 = dataImport.readADSFile(baseFilename % high)
	sps = dataImport.getSPS(baseFilename % low)
	if dataImport.getSPS(baseFilename % high) != sps:
		raise Exception("samples per second do not match - FAIL")
	
	dataSources = [dat0, dat1]
	indeces = [0, 0]
	
	if dataLength < float(constants.windowSize) / constants.samplesPerSecond * 1.5:
		raise Exception("Data length is too short - not getting full ffts of a single data source")
	
	numSamples = int(dataLength * sps)
	result = []
	output = []
	
	for i in range(numSignalChunks * 2):
		newIndex = i % 2
			
		if i == 0:
			dataToAppend = centerAroundZero(dataSources[newIndex][indeces[newIndex] : indeces[newIndex] + numSamples])
			indeces[newIndex] += numSamples
		else:
			#gotta phase match
			newData = centerAroundZero(dataSources[newIndex][indeces[newIndex] : int(indeces[newIndex] + numSamples + sps * startIndexRange*2)])
			startOffset = getBestStartIndex(result, newData, sps)
			#print startOffset
			dataToAppend = newData[startOffset: startOffset + numSamples]
			indeces[newIndex] += numSamples + startOffset
			
		if len(dataToAppend) != numSamples:
			raise Exception("Data to be appended is not the correct length")
		
		oldIndex = len(result)
		result += dataToAppend
		output += [newIndex] * numSamples
		
		
	#down sample result and output
	result = fftDataExtraction.downSample(result, sps, interpolate = True)
	output = fftDataExtraction.downSample(output, sps,)
	return result, output
def getDownSampledData(filename):
	rawData = dataImport.readADSFile(filename)
	rawSps = dataImport.getSPS(filename)
	
	result = downSample(rawData, rawSps)
	return result
import dataImport
from matplotlib import pylab
import fftDataExtraction

if __name__ == "__main__":
	filename = "Data/Mark/32kSPS_160kS_FlexorRadialis_Transitions.xls"
	
	data = dataImport.readADSFile(filename)
	data = data[40000:60000]
	
	data = fftDataExtraction.subtractPolynomialFit(data, 3)
	pylab.plot(range(len(data)), data)
	pylab.show()
	numOutputs = len(trainingData[0][1])
	for outputIndex in range(numOutputs):
		inputs = [input for (input, output) in trainingData]
		outputs = [output[outputIndex] for (input, output) in trainingData]
		
		prob = svmutil.svm_problem(outputs, inputs)
		param = svmAccuracy.getSvmParam(cross_validation_only = True)
		
		model = svmutil.svm_train(prob, param)
		print 'output index: %d - %s\n' % (outputIndex, {0 : "Index", 1: "Middle"}[outputIndex])
			
if __name__ == "__main__":
	datas = []
	
	for f in files:
		output = files[f]
		filename = baseFilename % f
		rawData = dataImport.readADSFile(filename, channels = len(output))
		if filterData:
			channels = zip(*rawData)
			filteredChannels = []
			for channel in channels:
				filteredChannels.append(FirFilter.bandStopFilterData(channel, rawSps, [60.0, 120.0, 180.0], log2Taps = 13))
				print 'filtered'
			rawData = zip(*filteredChannels)
		
		datas.append((rawData, output))
		
	#plotTimeDomainSignals(datas)
	trainingData = getSVMTrainingData(datas)
	getSVMAccuracy(trainingData)