#!/usr/bin/python
"""
	Daubechies D4 Wavelet transformation
	This downsamples a set of data by smooth(low-pass) and non-smooth(high-pass) filters.
"""
import argparse
import numpy as np
from matplotlib import pyplot as plt

def setup_data():
	s = np.zeros(2**12)
	s[4] = 1
	s[30] = 1
	return s, np.arange(len(s))

def main(args):
	s, x = setup_data()
	M = np.zeros((len(s), len(s)))

	#Define DB4 Coefficients
	c0 = (1+np.sqrt(3))/(4*np.sqrt(2))
	c1 = (3+np.sqrt(3))/(4*np.sqrt(2))
	c2 = (3-np.sqrt(3))/(4*np.sqrt(2))
	c3 = (1-np.sqrt(3))/(4*np.sqrt(2))

	b0 = c3
	b1 = -c2
	b2 = c1
	b3 = -c0

	#build coefficient matrix
	for i in xrange(0, len(M[:,0])-1, 2):
		for j in xrange(0, len(M[0,:])-4, 2):

			M[i,j+0] = c0
			M[i,j+1] = c1
			M[i,j+2] = c2
			M[i,j+3] = c3

			M[i+1,j+0] = b0
			M[i+1,j+1] = b1
			M[i+1,j+2] = b2
			M[i+1,j+3] = b3
	#NOTE: still need to deal with wrap around at end of matrix
	#bruteforce it, bottom left of matrix
	M[len(M[0,:])-2,0] = c2
	M[len(M[0,:])-1,0] = b2
	M[len(M[0,:])-2,1] = c3
	M[len(M[0,:])-1,1] = b3
	#bottom right of matrix
	M[len(M[0,:])-2, len(M[:,0])-2] = c0
	M[len(M[0,:])-1, len(M[:,0])-1] = c1
	M[len(M[0,:])-2, len(M[:,0])-2] = b0
	M[len(M[0,:])-1, len(M[:,0])-1] = b1


	#new values
	new = np.dot(M, s)
	newlow = np.zeros(len(new/2))
	newhigh = newlow
	inew = 0
	for i in xrange(0, len(new)-1, 2):
		newlow[inew] = new[i]
		newhigh[inew] = new[i+1]
		inew = inew +1



	fig = plt.figure()

	plt.plot(x, s, x, newlow, x, newhigh)
	plt.show()




if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument("-n", "--n", type=int, default=100, help="number of points")
	parser.add_argument("-s", "--s", type=int, default=0.5, help="standard deviation")
	args = parser.parse_args()
	main(args)