#!/usr/bin/env python3

"""

	Wavelet Base 1/2

	Miss-signed :

	on THE DISCRETE HAAR WAVELET TRANSFORMATION - Patrick J. Van Fleet
											approx        detail
	Sample: (6,12,15,15,14,12,120,116) → (9,15,13,118 | 3,0,−1,−2)
	Patrick algo is (120+116)/2 = 118 - 120 = -2
	                                M - A   = D
	Patrick say "There are a couple of choices for directed distances" (2 formulas)

	on https://fr.wikibooks.org/wiki/Compression_de_donn%C3%A9es/Le_format_JPEG_2000
	Sample: (2, 4, 15, 11) → (3, 13 | -1, +2) → (8 | -5, -1, +2)
	Here, algo is (15+11)/2 = 13 - 11 = +2
	                           M - B  = D
	pywt uses this algo

	----------

	Another method :
	(a + b) / 2 = A (low-pass)
	(a - b) / 2 = H (high-pass)		(method used by pywt)
		or		
	(b - a) / 2 = H (high-pass)		(method Patrick)

	----------
	A = approx
	D = detail
	----------
"""
print("Using Wavelet 1/2")

input = [31, 29, 23, 17, -6, -8, -2, -4]
input = [6, 12, 15, 15, 14, 12, 120, 116]
input = [128, 255, 50, 100, 200, 50, 150, 255]  # (148.5 | -15.25 | 58.25, -38.75 | -63.5, -25.0, 75.0, -52.5)
# input = [56, 40, 8, 24, 48, 48, 40, 16]  # ( 35 | −3  16  10  8  −8  0  12 )  # from whitepaper
# input = [31,29,23,17, -6,-8,-2,-4]  # ( 10 | 15 | 5, -2 | 1, 3, 1, 1 ) # from thesis
# input = [5, 7, 8, 8, 4, 2, -0.5, -1.5]  # ( 4.0, 3.0, -1.0, 2.0, -1.0, 0.0, 1.0, 0.5 )  # from video 'how to draw'
# input = [56, 40, 8, 24, 48, 48, 40, 16]  # ([35 -3 16 10 8 -8 0 12]) # from lab3
print(f"input={input}")

print("\n================= 1st decomposition ==================================")

# approximative coeff
A1_1 = (input[0] + input[1]) / 2	# A = (a+b)/2
A1_2 = (input[2] + input[3]) / 2
A1_3 = (input[4] + input[5]) / 2
A1_4 = (input[6] + input[7]) / 2

# detail coeff
D1_1 = (input[0] - A1_1)			# D = (a-A)		or		(input[0] - input[1]) / 2
D1_2 = (input[2] - A1_2)			#				or		(input[2] - input[3]) / 2
D1_3 = (input[4] - A1_3)			#				or		(input[4] - input[5]) / 2
D1_4 = (input[6] - A1_4)			#				or		(input[6] - input[7]) / 2

print(f"{A1_1}, {A1_2}, {A1_3}, {A1_4}\t\t{D1_1}, {D1_2}, {D1_3}, {D1_4}")
print(f"+------ approx -------+\t\t+---- detail ----->")

# print((input[0] - input[1]) / 2, (input[2] - input[3]) / 2, (input[4] - input[5]) / 2, (input[6] - input[7]) / 2)

print("\n================= 2nd decomposition ==================================")

# approx
A2_1 = (A1_1 + A1_2) / 2
A2_2 = (A1_3 + A1_4) / 2

# detail
D2_1 = (A1_1 - A2_1)
D2_2 = (A1_3 - A2_2)

print(f"{A2_1}, {A2_2}\t\t{D2_1}, {D2_2}, {D1_1}, {D1_2}, {D1_3}, {D1_4}")
print(f"+- approx -+\t\t+--------- detail ---------->")


print("\n================= 3rd decomposition ==================================")

# approx
A3 = (A2_1 + A2_2 ) / 2
# detail
D3 = (A2_1 - A3)
print(f"{A3}\t\t{D3}, {D2_1}, {D2_2}, {D1_1}, {D1_2}, {D1_3}, {D1_4}")
print(f"+-A-+\t\t+-------------- detail ------------>")
