## L414_Functions
'''
Haar wavelets algorithms: continued
'''
from math import sqrt

def trend(f):
    r2 = sqrt(2)
    N = len(f)
    J = range(N//2)
    return [(f[2*j] + f[2*j + 1])/r2 for j in J]
    
def fluct(f):
    r2 = sqrt(2)
    N = len(f)
    J = range(N//2)
    return [(f[2*j] - f[2*j + 1])/r2 for j in J]
    
def haar(f, r = 1):
    if r == 0: return f
    if r == 1: return (trend(f) + fluct(f))
    N = len(f); m = N // 2**(r - 1)
    h = haar(f, r - 1); a = h[:m]
    return trend(a) + fluct(a) + h[m:]
    
def energy(f):
    return sum(t**2 for t in f)
    
## Assignment

from math import *
from numpy import * 
from matplotlib.pyplot import *

X = arange(0, 10, 0.001)
def f(x): return (np.cos(3*x+5))*(np.sin(4*x))

close('all')
plt.figure("Assignment 414")
plt.grid(b=None, which='major', axis='both')

for r in range(5) :
    h_r = haar(f(X),r) 
    offset = 5*r
    plt.plot(list(map(lambda y:y+offset, h_r))) 
show()