## A428_sanchez
'''
Draw the graph of D4(f) for your favorite signal f. Compare the result 
with D2(f).  
'''

import sympy as s   #Symbolic python
import matplotlib.pyplot as plt
from cdi_haar import *

simplify = s.simplify
Eq = s.Eq
r2 = s.sqrt(2)
r3 = s.sqrt(3)
d = 4*r2

a1 = (1+r3)/d; a2 = (3+r3)/d
a3 = (3-r3)/d; a4 = (1-r3)/d

def D4trend(f):
    N = len(f)
    if N%2: return 'D4trend: %d is not divisible by 2'%N
    return [a1*f[2*j]+a2*f[2*j+1]+a3*f[(2*j+2)%N]+a4*f[(2*j+3)%N] \
     for j in range(N//2)]
    
def D4fluct(f):
    N = len(f)
    if N%2: return 'D4fluct: %d is not divisible by 2'%N
    return [a4*f[2*j]-a3*f[2*j+1]+a2*f[(2*j+2)%N]-a1*f[(2*j+3)%N] \
     for j in range(N//2)]
    
#D4 transform
def D4(f):
    N = len(f)
    if N%2: return 'D4: %d is not divisible by 2'%N
    return D4trend(f) + D4fluct(f)

    
#Testing
F = lambda x: cos(10*pi*x)*sin(10*pi/(x+1))
n=9
N = 2**n
f = sample(F,N-1)
plt.close('all')

fig = plt.figure("Daubechies and Haar transforms")
fig.suptitle("Daubechies and Haar transforms")
ax = fig.add_subplot(111)
ax.set_title("Black: $f:\/cos(10 \pi x)sin(10 \pi /(x+1))$ | Blue: $Daubechies$ | Red: $Haar$",fontsize=11)
plt.xlim(0,N-1)
plt.plot(f,'k-')
plt.plot([x-3 for x in D4(f)],'b-')
plt.plot([x-6 for x in D2(f)],'r-')
