## L409 Ondrej Velisek
'''
Haar wavelets algorithm: first steps
'''
from cdi import *
import math

def trend (f) :
    r2 = 1.4142135623730951
    N = len(f)
    J = range(N//2)
    return [(f[2*j] + f[2*j + 1]) / r2 for j in J]



f = [ (x/500-1)**2 * (math.sin(x/10)) for x in range(500)]
a = round(trend(f),2)

print(f)
print(a)

def fluct (f) :
    r2 = 1.4142135623730951
    N = len(f)
    J = range(N//2)
    return [(f[2*j] - f[2*j + 1]) / r2 for j in J]
    
d = round(fluct(f),2)

print(d)


def haar (f) :
    a = trend (f)
    d = fluct (f)
    return a + d 

print(round(haar(f),2))


def energy(f) :
    return sum(x**2 for x in f)

E = energy(f)
print (E)

A = energy(trend(f))
D = energy(fluct(f))
print (A)
print (D)
print (A + D)




# draw

import numpy as np
import matplotlib.pyplot as plt



    
t1 = np.arange(0.0, 1.0, 0.002)
t2 = np.arange(0.0, 1.0, 0.002)

plt.close()

plt.figure("Haar")

plt.subplot(2,1,1)
plt.plot(t1, f, '-')

plt.subplot(2,1,2)
plt.plot(t2, haar(f), '-')

plt.show()





