## A519_Velisek
'''
By analogy with L519 and L521, work out the following points:
Chose a discrete signal f of length 210 by sampling your favourite function, and compute a = D4trend(f,3).
Repeat L521 to find the decompression of a using D2, D4 and D6.
Produce apropriate graphic representations of the results.
Write some relevant conclusions in a few sentences (can be in the form of a long comment).
'''

from cdi_wavelets import *

n = 10
N = 2**n
r = 3

F = lambda x: sin(14*x-5)*np.sin(26*x*cos(x*7))*sin(x*pi)
f = sample(F, N-1)

a6 = D6trend(f, r)
a4 = D4trend(f, r)
a2 = D2trend(f, r)




VD6 = D6VW(N)[0][r]
VD4 = D4VW(N)[0][r]
D2VW = HaarVWA
VD2 = D2VW(N)[0][r]


def lc(A, V) :
    sm = zeros(len(V[0]))
    for a, v in zip(A, V) :
        sm = sm + a*v
    return sm


f6 = lc(a6, VD6)
f4 = lc(a4, VD4)
f2 = lc(a2, VD2)


def diff(f, r, fluctFunc) :
    d = []
    for i in range(r,0,-1) :
        d += fluctFunc(f, i)
    return d

d6 = diff(f, r, D6fluct)
d4 = diff(f, r, D4fluct)
d2 = diff(f, r, D2fluct)

e = energy(f)
ed6 = energy(d6)
ed4 = energy(d4)
ed2 = energy(d2)

e6prec = 100-(ed6/e)*100
e4prec = 100-(ed4/e)*100
e2prec = 100-(ed2/e)*100

ratio = 2**r

canvas = plt.figure
axis=plt.axis
show = plt.show
view = plt.imshow
close = plt.close
plot = plt.plot
xlim = plt.xlim
ylim = plt.ylim

def write(X,T,ha='center', va = 'center', fs='16', rot=0.0, color='#000000'):
    return plt.text(X[0],X[1], T, horizontalalignment=ha,verticalalignment=va, fontsize=fs, rotation=rot, color=color)


close('all')
canvas("1. D6, D4, D2 Decompression Comparasion")

# becasue of same resolution of each subplot
lim = 1.1 * max(max(max(f),-min(f)), max(max(f2),-min(f2)), max(max(f4),-min(f4)), max(max(f6),-min(f6)))

def commonWrite(funcName, comment="") :
    b = 0.99
    write((N*b-100, -lim*b), "$f\_orig$", color="#bb0000", va="bottom", ha="right")
    write((N*b, -lim*b), funcName, color="#0033FF", va="bottom", ha="right")
    write((N*(1-b), -lim*b), comment, color="#000000", va="bottom", ha="left")

plt.subplot(3,1,1)
axis([0, N, -lim, lim])
plot(f , color="#BB0000")
plot(f6, color="#0033FF")
commonWrite("$f_6$", "compression 1:%d    precision%.2f%%"%(ratio, e6prec))

plt.subplot(3,1,2)
axis([0, N, -lim, lim])
plot(f , color="#BB0000")
plot(f4, color="#0033FF")
commonWrite("$f_4$", "compression 1:%d    precision %.2f%%"%(ratio, e4prec))

plt.subplot(3,1,3)
axis([0, N, -lim, lim])
plot(f , color="#BB0000")
plot(f2, color="#0033FF")
commonWrite("$f_2$", "compression 1:%d    precision %.2f%%"%(ratio, e2prec))



canvas("2. a6, a4, a2 trend difference (only part of it)")

xmin = len(a6)//2
xmax = (len(a6)*7)//8
xlim(xmin, xmax)
plot(a6, color="#BB0000")
plot(a4, color="#00BB00")
plot(a2, color="#0033FF")

write((xmax-12, 2.5), "$a6$", color="#BB0000", va="top", ha="right")
write((xmax-8 , 2.5), "$a4$", color="#00BB00", va="top", ha="right")
write((xmax-4 , 2.5), "$a2$", color="#0033FF", va="top", ha="right")


'''
Conclusion:
 
I have decompressed a singnal compressed with D6trend. I have done it 
by three different systems (Haar alias Daubechies2, Daubechies4 and Daubechies6)

I have change my favourite function because I wanted some function with slight part 
and also wilder parts because I wanted to see behaviour of compression systems 
on both extremes. By some experiments it is sin(14*x-5)*np.sin(26*x*cos(x*7))*sin(x*pi).

After that I've generated apropriate graphics which help me to see what happend.
I could clearly see that D6 is more precise than D4 and it is more precise than D2
as I expected.

We can clearly see that all decompressed functions are very good at the begining flat part
in the middle part where function is curve in D2 we can see differences. The other
two Daubechies are still acceptable. In the wild part (around 2/3) of fuction only 
D4 is enough close to function. So behaviour is absolutely expectable.

However I should make some proove with numbers. So I've computed 
energy what signal have lost by compression and I've compute percentage in relation to
energy of original signal. It gave me something like precision overview of problem.
Interesting is to change  r  and watch how precision is changing. 

In comparation to L521 I've used proper computation of trend (compress function) for
each system (D6, D4, D2). I think it is proper way. Functions looks similar but they aren't. 
Only for illustration I've plotted them and zoom it to wild part where differences are biggest.
So you can clearly see that I'm right.
Other thing is that for energy equal we need proper trend andbecause my precision computation 
is dependent on it (actually is independent on result) so I had to do it.


I'm not sure if I'm right but today I was thinking that all Daubechies system is using 
some kind of lossless knowledge from previouse chapters. Because compression works better
if signal is smooth => it means that next value is close to previous => so probability
that appears value closer to previous is bigger => enthropy is lower => compression is better.
'''


