Applinalgsolutionspython PDF
Applinalgsolutionspython PDF
Øyvind Ryan
Feb 8, 2017
Contents
2
Chapter 1
Solution. Setting pref =0.00002 Pa and p=100 000 Pa in the decibel expression
we get
105
p 100000
20 log10 = 20 log10 = 20 log10
pref 0.00002 2 × 10−5
10
10
= 20 log10 = 20 (10 − log10 2) ≈ 194db.
2
Solution. sin(2πν1 t) has period 1/ν1 , while sin(2πν2 t) has period 1/ν2 . The
period is not unique, however. The first one also has period n/ν1 , and the
second also n/ν2 , for any n. The sum is periodic if there exist n1 , n2 so that
n1 /ν1 = n2 /ν2 , i.e. so that there exists a common period between the two. This
common period will also be a period of f . This amounts to that ν1 /ν2 = n1 /n2 ,
i.e. that ν1 /ν2 is a rational number.
3
CHAPTER 1. SOUND AND FOURIER SERIES 4
Solution. The important thing to note here is that there are two oscillations
present in the right part of Figure 1.1 in the book: One slow oscillation with
a higher amplitude, and one faster oscillation, with a lower amplitude. We see
that there are 10 periods of the smaller oscillation within one period of the
larger oscillation, so that we should be able to reconstruct the figure by using
frequencies where one is 10 times the other, such as 440Hz and 4400Hz. Also,
we see from the figure that the amplitude of the larger oscillation is close to 1,
and close to 0.3 for the smaller oscillation. A good choice therefore seems to be
a = 1, b = 0.3. The code can look this:
t = arange(0,3,1/float(fs))
x = sin(2*pi*440*t) + 0.3*sin(2*pi*4400*t)
x /= abs(x).max()
play(x, fs)
play(x, fs)
play(x, 2*fs)
play(x, fs/2)
The sample file castanets.wav played at double sampling rate can be found in
the file castanetsdouble.wav, and in castanetshalf.wav for half the sampling rate.
z = x + c*(2*random.random(shape(x))-1)
This adds noise to all channels. The function for returning random numbers
returns numbers between 0 and 1, and above we have adjusted these so that
CHAPTER 1. SOUND AND FOURIER SERIES 5
they are between −1 and 1 instead, as for other sound which can be played by
the computer. c is a constant (usually smaller than 1) that dampens the noise.
Write code which adds noise to the audio sample file, and listen to the result
for damping constants c=0.4 and c=0.1. Remember to scale the sound values
after you have added noise, since they may be outside [−1, 1] then.
z = x + c*(2*random.random(shape(x))-1)
z /= abs(z).max()
play(z, fs)
With c = 0.4 the resulting sound can be found in the file castanetsnoisehigh.wav,
while with c = 0.1 it can be found in the file castanetsnoiselow.wav.
totransl=1-4*abs(t-T/2)/T
plt.figure()
plt.plot(concatenate([t, t+T, t+2*T, t+3*T, t+4*T]),tile(totransl,5), ’k-’)
Then we repeat one period to obtain a sound with the desired length, and play
it as follows.
x = tile(oneperiod, antsec*f)
play(x, fs)
t = linspace(0, T, 100)
x = zeros(len(t))
for k in range(1, 20, 2):
x -= (8/(k*pi)**2)*cos(2*pi*k*t/T)
plt.figure()
plt.plot(t, x, ’k-’)
b) Write code so that you can listen to the Fourier series of the triangle wave.
How high must you choose N for the Fourier series to be indistuingishable from
the square/triangle waves themselves?
x = tile(x, antsec/T)
play(x, fs)
Z T Z T h iT
f (t)dt = lim t−1/2 dt = lim 2t1/2
0 x→0+ x x→0+ x
1/2 1/2 1/2
= lim (2T − 2x ) = 2T
x→0+
Z T Z T
T
f (t)2 dt = lim t−1 dt = lim [ln t]x
0 x→0+ x x→0+
= ln T − lim ln x = ∞.
x→0+
Solution. The space VN1 ,T1 is spanned by pure tones with frequencies 1/T1 , . . . , N1 /T1 ,
while VN2 ,T2 is spanned by pure tones with frequencies 1/T2 , . . . , N2 /T2 . We
must have that the first set of frequencies is contained in the second. This is
achieved if and only if 1/T1 = k/T2 for some integer k, and also N1 /T1 ≤ N2 /T2 .
In other words, T2 /T1 must be an integer, and T2 /T1 ≤ N2 /N1 .
N
fN (t) + fN (−t) X
= a0 + an cos(2πnt/T )
2 n=1
N
fN (t) − fN (−t) X
= bn sin(2πnt/T ).
2 n=1
1
RT T
Solution. For f (t) = t we get that a0 = T 0
tdt = 2 . We also get
CHAPTER 1. SOUND AND FOURIER SERIES 8
Z T
2
an = t cos(2πnt/T )dt
T 0
T Z T !
2 T T
= t sin(2πnt/T ) − sin(2πnt/T )dt = 0
T 2πn 0 2πn 0
Z T
2
bn = t sin(2πnt/T )dt
T 0
T Z T !
2 T T T
= − t cos(2πnt/T ) + cos(2πnt/T )dt = − .
T 2πn 0 2πn 0 πn
Note that this is almost a sine series, since it has a constant term, but no other
cosine terms. If we had subtracted T /2 we would have obtained a function which
is antisymmetric, and thus a pure sine series.
RT 2
For f (t) = t2 we get that a0 = T1 0 t2 dt = T3 . We also get
Z T
2
an = t2 cos(2πnt/T )dt
T 0
T Z T !
2 T 2 T
= t sin(2πnt/T ) − t sin(2πnt/T )dt
T 2πn 0 πn 0
T2
T T
= − − = 2 2
πn πn π n
Z T
2
bn = t2 sin(2πnt/T )dt
T 0
T Z T !
2 T 2 T
= − t cos(2πnt/T ) + t cos(2πnt/T )dt
T 2πn 0 πn 0
T2
=− .
πn
Here we see that we could use the expressions for the Fourier coefficients of
f (t) = t to save some work. The Fourier series is thus
T2 X
2
T2
T
+ cos(2πnt/T ) − sin(2πnt/T ) .
3 π 2 n2 πn
n≥1
1
RT T3
For f (t) = t3 we get that a0 = T 0
t3 dt = 4 . We also get
CHAPTER 1. SOUND AND FOURIER SERIES 9
Z T
2
an = t3 cos(2πnt/T )dt
T 0
T Z T !
2 T 3 3T 2
= t sin(2πnt/T ) − t sin(2πnt/T )dt
T 2πn 0 2πn 0
T2 3T 3
3T
= − − =
2πn πn 2π 2 n2
Z T
2
bn = t3 sin(2πnt/T )dt
T 0
T Z T !
2 T 3 3T 2
= − t cos(2πnt/T ) + t cos(2πnt/T )dt
T 2πn 0 2πn 0
T3 3T T 2 T3 3T 3
=− + = − + .
πn 2πn π 2 n2 πn 2π 3 n3
Also here we saved some work, by reusing the expressions for the Fourier
coefficients of f (t) = t2 . The Fourier series is thus
T3 X 3T 3 T3 3T 3
+ cos(2πnt/T ) + − + sin(2πnt/T ) .
4 2π 2 n2 πn 2π 3 n3
n≥1
We see that all three Fourier series converge slowly. This is connected to the
fact that none of the functions are continuous at the borders of the periods.
Solution. Let us define an,k , bn,k as the Fourier coefficients of tk . When k > 0
and n > 0, integration by parts gives us the following difference equations:
CHAPTER 1. SOUND AND FOURIER SERIES 10
Z T
2
an,k = tk cos(2πnt/T )dt
T 0
T !
Z T
2 T k kT k−1
= t sin(2πnt/T ) − t sin(2πnt/T )dt
T 2πn 0 2πn 0
kT
=− bn,k−1
2πn
Z T
2
bn,k = tk sin(2πnt/T )dt
T 0
T Z T !
2 T k kT k−1
= − t cos(2πnt/T ) + t cos(2πnt/T )dt
T 2πn 0 2πn 0
Tk kT
=− + an,k−1 .
πn 2πn
When n > 0, these can be used to express an,k , bn,k in terms of an,0 , bn,0 , for
Tk
which we clearly have an,0 = bn,0 = 0. For n = 0 we have that a0,k = k+1 for all
k.
The following program computes an,k , bn,k recursively when n > 0.
Z T Z T
1 1
he2πin1 t/T , e2πin2 t/T i = e2πin1 t/T e−2πin2 t/T dt = e2πi(n1 −n2 )t/T dt
T 0 T 0
T
1 T
= e2πi(n1 −n2 )t/T
T 2πi(n1 − n2 ) 0
1 1
= − = 0.
2πi(n1 − n2 ) 2πi(n1 − n2 )
2
1 2πit/T
f (t) = sin2 (2πt/T ) = (e − e−2πit/T
2i
1 1 1 1
= − (e2πi2t/T − 2 + e−2πi2t/T ) = − e2πi2t/T + − e−2πi2t/T .
4 4 2 4
This gives the Fourier series of the function (with y2 = y−2 = −1/4, y0 = 1/2).
This could also have been shown by using the trigonometric identity sin2 x =
1 1 T −2πint/T
R
2 (1 − cos(2x)) first, or by computing the integral T 0 f (t)e dt (but this
is rather cumbersome).
n
n 1 it −it
cos (t) = (e + e )
2
n
1 it
sinn (t) = (e − e−it )
2i
CHAPTER 1. SOUND AND FOURIER SERIES 12
If we multiply out here, we get a sum of terms of the form eikt , where −n ≤ k ≤ n.
As long as n ≤ N it is clear that this is in VN,2π .
b) Write down the N ’th order complex Fourier series for f1 (t) = cos t, f2 (t) =
cos2 t, og f3 (t) = cos3 t.
1 it
cos(t) = (e + e−it )
2
1 1 1 1
cos2 (t) = (eit + e−it )2 = e2it + + e−2it
4 4 2 4
1 1 3 3 1
cos3 (t) = (eit + e−it )3 = e3it + eit + e−it + e−3it .
8 8 8 8 8
Therefore, for the first function the nonzero Fourier coefficients are y−1 = 1/2,
y1 = 1/2, for the second function y−2 = 1/4, y0 = 1/2, y2 = 1/4, for the third
function y−3 = 1/8, y−1 = 3/8, y1 = 3/8, y3 = 1/8.
c) In b) you should be able to see a connection between the Fourier coefficients
and the three first rows in Pascal’s triangle. Formulate and prove a general
relationship between row n in Pascal’s triangle and the Fourier coefficients of
fn (t) = cosn t.
Solution. In order to find the Fourier coefficients of cosn (t) we have to multiply
out the expression 21n (eit + e−it )n . The coefficients we get after this can alos be
obtained from Pascal’s triangle.
Z T /2 Z T
1 1
yn = e−2πint/T dt − e−2πint/T dt
T 0 T T /2
T /2 T
1 T −2πint/T T −2πint/T 1
=− e e +
T 2πin 0 2πin T T /2
1
−e−πin + 1 + 1 − e−πin +
=
2πin (
1 −πin
0, if n is even;
= 1−e = .
πin 2/(πin), if n is odd.
CHAPTER 1. SOUND AND FOURIER SERIES 13
Instead using Theorem 1.15 in the book together with the coefficients bn =
2(1−cos(nπ)
nπ we computed in Example 1.12 in the book, we obtain
( (
1 1 0, if n is even; 0, if n is even;
yn = (an − ibn ) = − i =
2 2 4/(nπ), if n is odd. 2/(πin), if n is odd.
T Z T !
Z T
1 −2πint/T 1 T −2πint/T T −2πint/T
yn = te dt = − te + e dt
T 0 T 2πin 0 0 2πin
T T
=− = i.
2πin 2πn
T
From Exercise 1 we had bn = − πn , for which Theorem 1.15 in the book gives
T
yn = 2πn i for n > 0, which coincides with the expression we obtained. The case
n < 0 follows similarly.
For f (t) = t2 we get
T !
Z T Z T
1 2 −2πint/T 1 T 2 −2πint/T T −2πint/T
yn = t e dt = − t e +2 te dt
T 0 T 2πin 0 0 2πin
T2 T2 T2 T2
=− + 2 2 = + i.
2πin 2π n 2π 2 n2 2πn
2 2
T T
From Exercise 1 we had an2 = π2 n22 and bn = − πn , for which Theorem 1.15 in
the book gives yn = 12 πT2 n2 + i πn
T
for n > 0, which also is seen to coincide
with what we obtained. The case n < 0 follows similarly.
For f (t) = t3 we get
CHAPTER 1. SOUND AND FOURIER SERIES 14
T !
Z T Z T
1 3 −2πint/T T 3 −2πint/T
1 T 2 −2πint/T
yn = t e −
dt = t e +3 t e dt
T 0 T
2πin 0 0 2πin
T3 T2 T2 T3
3
T3
T T
=− +3 ( + i) = 3 2 2 + −3 3 3 i=
2πin 2πin 2π 2 n2 2πn 4π n 2πn 4π n
3T 3 T 3 3
From Exercise 1 we had an = 2π 2 n2 and bn = − πn + 2π3T3 n3 for which Theorem 1.15
in the book gives
3T 3 T3 3T 3 3T 3 T3 3T 3
1
yn = +i − 3 3 = + − 3 3 i
2 2π 2 n2 πn 2π n 4π 2 n2 2πn 4π n
for n > 0, which also is seen to coincide with what we obtained. The case n < 0
follows similarly.
PN
d) Show that n=−N yn e2πint/T is antisymmetric when y0 = 0 and y−n = −yn
for all n, and rewrite it as a sine-series.
T /4
1 −T /4 −2πint/T 1 T /2 −2πint/T
Z Z Z
1 −2πint/T
yn = e dt − e dt − e dt
T −T /4 T −T /2 T T /4
T /4 −T /4 T /2
1 −2πint/T 1 −2πint/T 1 −2πint/T
=− e + e + e
2πin −T /4 2πin −T /2 2πin T /4
1 −πin/2
= −e + eπin/2 + eπin/2 − eπin + e−πin − e−πin/2
2πin
1 2
= (2 sin(πn/2) − sin(πn)) = sin(πn/2).
πn πn
The square wave defined in this exercise can be obtained by delaying our original
square wave with −T /4. Using Property 3 in Theorem 1.17 in the book with
d = −T /4 on the complex Fourier coefficients
(
0, if n is even;
yn =
2/(πin), if n is odd,
which we obtained for the square wave in Exercise 1, we obtain the Fourier
coefficients
CHAPTER 1. SOUND AND FOURIER SERIES 16
( (
2πin(T /4)/T 0, if n is even; 0, if n is even;
e = 2i sin(πn/2)
2/(πin), if n is odd. πin , if n is odd.
(
0, if n is even;
= 2
.
πn sin(πn/2), if n is odd.
This verifies the result.
Hint. Attempt to use one of the properties in Theorem 1.17 in the book on
the Fourier series of the square wave.
Theorem 1.15 in the book gives us that the complex Fourier coefficients are
yn = − 12 i πn
4 2i
= − πn , and y−n = 12 i πn
4 2i
= πn for n > 0. This means that
2i
yn = − πn for all n, so that the complex Fourier series of the square wave is
X 2i
− e2πint/T .
πn
n odd
Using Property 4 in Theorem 1.17 in the book we get that the e−2πi4t/T (i.e. set
2i
d = −4) times the square wave has its n’th Fourier coefficient equal to − π(n+4) .
Using linearity, this means that 2ie−2πi4t/T times the square wave has its n’th
4
Fourier coefficient equal to π(n+4) . We thus have that the function
(
2ie−2πi4t/T , 0 ≤ t < T /2
f (t) =
−2ie−2πi4t/T , T /2 ≤ t < T
has the desired Fourier series.
Z 2T
1
f˘(t)e−2πi2nt/(2T ) dt
2T 0
Z T Z 2T
1 −2πint/T 1
= f (t)e dt + f (2T − t)e−2πint/T dt.
2T 0 2T T
Z T Z 0
1 1
= f (t)e−2πint/T dt − f (u)e2πinu/T du
2T 0 2T T
Z T Z T
1 1
= f (t)e−2πint/T dt + f (t)e2πint/T dt
2T 0 2T 0
1 1
= yn + y−n .
2 2
Therefore we have a2n = yn − y−n .
1 2πint/T −2πint/T
s(cos(2πnt/T )) = s (e +e )
2
1 1
= λs (n/T )e2πint/T + λs (−n/T )e−2πint/T
2 2
1 2πint/T −2πint/T
= λs (n/T ) (e +e ) = λs (n/T ) cos(2πnt/T ),
2
so that s is symmetric.
CHAPTER 1. SOUND AND FOURIER SERIES 18
We saw that the symmetric extension of a function took the form of a cosine-
series, and that this converged faster to the symmetric extension than the Fourier
series did to the function. If a filter preserves cosine-series it will also preserve
symmetric extensions, and therefore also map fast-converging Fourier series to
fast-converging Fourier series.
N
1 T
X Z
fN (t) = f (s)e−2πins/T ds e2πint/T
T 0
n=−N
Z T N
1 X
= f (s) e−2πins/T e2πint/T ds
T 0 n=−N
Z T N Z T
1 X 1
= f (s) e2πin(t−s)/T ds = f (s)DN (t − s)ds.
T 0 T 0
n=−N
c) Prove that DN (t) satisfies only two of the properties of a summability kernel.
d) Write a function which takes N and T as arguments, and plots DN (t) over
[−T /2, T /2].
CHAPTER 1. SOUND AND FOURIER SERIES 19
2
Hint. Use that π |u| ≤ | sin u| when u ∈ [−π/2, π/2].
b) Show that FN (t) satisfies the three properties of a summability kernel.
RT PN
c) Show that T1 0 f (t − u)FN (u)du = N1+1 n=0 fn .
1
PN
Hint. Show that FN (t) = N +1 n=0 Dn (t), and use Exercis 1 b).
d) Write a function which takes N and T as arguments, and plots FN (t) over
[−T /2, T /2].
Chapter 2
2 1 1 1 1 2
3 1 1 −i −1 i 3
F4
4 = 2 1 −1 1 −1 4
5 1 i −1 −i 5
2+3+4+5 7
1 2 − 3i − 4 + 5i
= −1 + i .
=
2 2−3+4−5 −1
2 + 3i − 4 − 5i −1 − i
20
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS21
1 1√ 1 √ 1 1 √ 1√
1
1/2 − i √3/2 −1/2 − i√3/2 −1 −1/2 + i√3/2 1/2 + i √2/2
1 1 −1/2 − i 3/2 −1/2 + i 3/2 1 −1/2 − i 3/2 +1/2 − i 3/2
F6 = √
6 1
−1 √ 1 √ −1 1 √ −1 √
1 −1/2 + √i 3/2 −1/2 − i√3/2 1 −1/2 + i√3/2 −1/2 − √i 3/2
1 1/2 + i 2/2 −1/2 + i 3/2 −1 −1/2 − i 3/2 1/2 − i 3/2
The cases N = 8 and N = 12 follow similarly, but are even more tedious. For
N = 8 the entries are √18 eπink/4 , which can be expressed exactly since we can
express exactly any sines and cosines of a multiple of π/4. For N = 12 we get
the base angle π/6, for which we also have exact values for sines and cosines for
all multiples.
Solution. By Theorem 2.7 in the book we know that (FN (x))N −n = (FN (x))n
when x is a real vector. If we set N = 8 and n = 2 we get that (F8 (x))6 =
(F8 (x))2 = 2 − i = 2 + i.
1 2πik/N 2
2 −2πikn/N
cos (2πk/N ) = e +e
2
1 1 1 1 1 1
= e2πi2k/N + + e−2πi2k/N = e2πi2k/N + + e2πi(N −2)k/N
4 2 4 4 2 4
√
1 1 1
= N φ2 + φ0 + φN −2 .
4 2 4
We here used the periodicity of e2πikn/N , i.e. that e−2πi2k/N = e2πi(N −2)k/N .
Since FN is linear and FN (φn ) = en , we have that
√ √
1 1 1
FN (x) = N e2 + e0 + eN −2 = N (1/2, 0, 1/4, 0, . . . , 0, 1/4, 0) .
4 2 4
1 − cN
yn =
1 − ce−2πin/N
for n = 0, . . . , N − 1.
Solution. We get
N
X −1 N
X −1
k −2πink/N
yn = c e = (ce−2πin/N )k
k=0 k=0
1 − (ce−2πin/N )N 1 − cN
= = .
1 − ce−2πin/N 1 − ce−2πin/N
Hint. Split into real and imaginary parts, and use linearity of the DFT.
• The function also takes a second parameter called forward. If this is true
the DFT is applied. If it is false, the IDFT is applied. If this parameter is
not present, then the forward transform should be assumed.
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS23
N/2−1 N −1
1 X X
yn = √ xk e−2πikn/N + xk e−2πikn/N
N k=0 k=N/2
N/2−1 N/2−1
1 X X
=√ xk e−2πikn/N + xk e−2πi(k+N/2)n/N
N k=0 k=0
N/2−1
1 X
=√ xk (e−2πikn/N + (−1)n e−2πikn/N )
N k=0
N/2−1
1 X
= (1 + (−1)n ) √ xk e−2πikn/N
N k=0
If n is odd, we see that yn = 0.
b) Show that, if xk+N/2 = −xk for all 0 ≤ k < N/2, then yn = 0 when n is
even.
Solution. The proof is the same as in a), except for a sign change.
c) Show also the converse statements in a) and b).
Solution. Clearly the set of vectors which satisfies xk+N/2 = ±xk is a vector
space V of dimension N/2. The set of vectors where every second component is
zero is also a vector space of dimension N/2, let us denote this by W . We have
shown that FN (V ) ⊂ W , but since FN is unitary, FN (V ) also has dimension
N/2, so that FN (V ) = W . This shows that when every second yn is 0, we must
have that xk+N/2 = ±xk , and the proof is done.
d) Also show the following:
• xn = 0 for all odd n if and only if yk+N/2 = yk for all 0 ≤ k < N/2.
• xn = 0 for all even n if and only if yk+N/2 = −yk for all 0 ≤ k < N/2.
1
(FN (x1 ))k = (FN (x))k + (FN (x))N −k
2
1
(FN (x2 ))k = (FN (x))k − (FN (x))N −k
2i
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS25
This shows that we can compute two DFT’s on real data from one DFT on
complex data, and 2N extra additions.
(FN (x))k = (FN (x1 + ix2 ))k = (FN (x1 ))k + i(FN (x2 ))k
(FN (x))N −k = (FN (x1 ))N −k + i(FN (x2 ))N −k = (FN (x1 ))k + i(FN (x2 ))k ,
where we have used Property 1 of Theorem 2.7 in the book. If we take the
complex conjugate in the last equation, we are left with the two equations
x = x[0:2**17]
y = fft.fft(x, axis=0)
y[(2**17/4):(3*2**17/4)] = 0
newx = abs(fft.ifft(y))
newx /= abs(newx).max()
play(newx, fs)
Comment in particular why we adjust the sound samples by dividing with the
maximum value of the sound samples. What changes in the sound do you expect
to hear?
Solution. First a sound file is read. We then restrict to the first 212 sound
samples, perform a DFT, zero out the frequencies which correspond to DFT-
indices between 210 and 212 − 210 − 1, and perform an IDFT. Finally we scale
the sound samples so that these lie between −1 and 1, which is the range we
demand for the sound samples, and play the new sound.
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS26
well. This function calls a kernel for computing the FFT. The
kernel assumes that the input has been bit-reversed, and contains
only one axis. This function is where the actual bit reversal and
the splitting of the axes take place.
x: a vector
FFTKernel: can be any of FFTKernelStandard, FFTKernelNonrec, and
FFTKernelSplitradix. The kernel assumes that the input has been
bit-reversed, and contains only one axis.
forward: Whether the FFT or the IFFT is applied
"""
if ndim(x) == 1:
bitreverse(x)
FFTKernel(x, forward)
else:
bitreversearr(x)
for s2 in xrange(shape(x)[1]):
FFTKernel(x[:, s2], forward)
if not forward:
x /= len(x)
b) A problem for large N is that there is such a big difference in the execution
times between the two implementations. We can address this by using a loglog-
plot instead. Plot N against execution times using the function loglog. How
should the fact that the number of arithmetic operations are 8N 2 and 5N log2 N
be reflected in the plot?
Solution. The two different curves you see should have a derivative approxi-
mately equal to one and two, respectively.
c) It seems that the built-in FFT is much faster than our own FFT implemen-
tation, even though they may use similar algorithms. Try to explain what can
be the cause of this.
Solution. There may be several reasons for this. One is that Python code
runs slowly when compared to native code, which is much used in the built-in
FFT. Also, the built-in fft has been subject to much more optimization than
we have covered here.
x0, fs = audioread(’sounds/castanets.wav’)
kvals = arange(3,16)
slowtime = zeros(len(kvals))
fasttime = zeros(len(kvals))
fastesttime = zeros(len(kvals))
N = 2**kvals
for k in kvals:
x = x0[0:2**k].astype(complex)
start = time()
DFTImpl(x)
slowtime[k - kvals[0]] = time() - start
start = time()
FFTImpl(x, FFTKernelStandard)
fasttime[k - kvals[0]] = time() - start
start = time()
fft.fft(x, axis=0)
fastesttime[k - kvals[0]] = time() - start
# a.
plt.plot(kvals, slowtime, ’ro-’, \
kvals, fasttime, ’go-’, \
kvals, fastesttime, ’bo-’)
plt.grid(’on’)
plt.title(’time usage of the DFT methods’)
plt.legend([’DFT’, ’Standard FFT’, ’Built-in FFT’])
plt.xlabel(’log_2 N’)
plt.ylabel(’time used [s]’)
plt.show()
plt.figure()
# b.
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS30
Solution. We get
1 1 1 1 1 16
1 −i −1 i 3 = −4 + 4i
DFT4 x1 =
1 −1 1 −1 5 −4
1 i −1 −i 7 −4 − 4i
1 1 1 1 2 20
1 −i −1 i 4 = −4 + 4i
DFT4 x2 =
1 −1 1 −1 6 −4
1 i −1 −i 8 −4 − 4i
In the FFT-algorithm we split the computation of DFT4 (x) into the computation
of DFT2 (x(e) ) and DFT2 (x(o) ), where x(e) and x(o) are vectors of length 4 with
even-indexed and odd-indexed components, respectively. In this case we have
x(e) = (1, 3, 5, 7) and x(o) = (2, 4, 6, 8). In other words, the FFT-algorithm uses
the FFT-computations we first made, so that we can save computation. The
benefit of using the FFT-algorithm is that we save computations, so that we end
up with O(5N log2 N ) real arithmetic operations.
We see here that, if we already have computed DFTN/2 x(e) and DFTN/2 x(o) ,
we need one additional complex multiplication for each yn with 0 ≤ n < N/4
(since e−2πin/N and (DFTN/2 x(o) )n are complex). No further multiplications
are needed in order to compute yN/2−n , since we simply conjugate terms before
adding them. Again yN/2 must be handled explicitly with this approach. For
this we can use the formula
Solution. The formulas at the top show that we need only compute the
e−2πin/N (DFTN/2 x(o) )n for 0 ≤ n < N/4, i.e. N/4 complex additions. Since
N/2 formulas are listed, N/2 complex additions are needed. This translates to
N real multiplications, and N/2 + N = 3N/2 real additions. These are exactly
half the numbers we obtained for the complex version of the algorithm, for
which we obtained MN = O(2N log2 (N )) and AN = O(3N log2 (N )). It is then
not difficult to see that the real version will require MN = O(N log2 N ) real
multiplications and AN = O 32 N log2 N real additions.
b) Find an IFFT algorithm adapted to vectors with conjugate symmetry, which
has the same operation count as this FFT algorithm adapted to real data.
showing that the vectors z and w also have conjugate symmetry. Note also that,
if we add and subtract the equations at the top, we obtain
so that
1 1
x(e) = IDFTN/2 z x(o) = IDFTN/2 w.
2 2
The point is that the number of arithmetic operations in computing the vectors
z and w is the same as the FFT in a), so that the overall complexity is the same.
Solution. The algorithm for the non-recursive FFT can look as follows
If you add the non-recursive algorithm to the code from Exercise 2, you will see
that the non-recursive algorithm performs much better. There may be several
reasons for this. First of all, there are no recursive function calls. Secondly,
the values in the matrices DN/2 are constructed once and for all with the non-
recursive algorithm. Code which compares execution times for the original FFT
algorithm, our non-recursive implementation, and the split-radix algorithm of
the next exercise, can look as follows:
x0, fs = audioread(’sounds/castanets.wav’)
kvals = arange(3,16)
slowtime = zeros(len(kvals))
fasttime = zeros(len(kvals))
fastesttime = zeros(len(kvals))
N = 2**kvals
for k in kvals:
x = x0[0:2**k].astype(complex)
start = time()
FFTImpl(x, FFTKernelStandard)
slowtime[k - kvals[0]] = time() - start
start = time()
FFTImpl(x, FFTKernelNonrec)
fasttime[k - kvals[0]] = time() - start
start = time()
FFTImpl(x, FFTKernelSplitradix)
fastesttime[k - kvals[0]] = time() - start
plt.plot(kvals, slowtime, ’ro-’, \
kvals, fasttime, ’bo-’, \
kvals, fastesttime, ’go-’)
plt.grid(’on’)
plt.title(’time usage of the DFT methods’)
plt.legend([’Standard FFT algorithm’, \
’Non-recursive FFT’, \
’Split radix FFT’])
plt.xlabel(’log_2 N’)
plt.ylabel(’time used [s]’)
plt.show()
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS34
DFTN/4 DN/4 DFTN/4 (e)
DFTN/2 DN/2 x
DFT N/4 −D N/4 DFT N/4 (oe)
DFTN x = x .
DFTN/4 DN/4 DFTN/4
DFTN/2 −DN/2 x(oo)
DFTN/4 −DN/4 DFTN/4
(2.1)
The term radix describes how an FFT is split into FFT’s of smaller sizes, i.e. how
the sum in an FFT is split into smaller sums. The FFT algorithm we started
this section with is called a radix 2 algorithm, since it splits an FFT of length
N into FFT’s of length N/2. If an algorithm instead splits into FFT’s of length
N/4, it is called a radix 4 FFT algorithm. The algorithm we go through here is
called the split radix algorithm, since it uses FFT’s of both length N/2 and N/4.
a) Let GN/4 be the
(N/4)×(N/4) diagonal
matrix with e−2πin/N on the diagonal.
GN/4 0
Show that DN/2 = .
0 −iGN/4
b) Let HN/4 be the (N/4) × (N/4) diagonal matrix GD/4 DN/4 . Verify the
following rewriting of Equation (2.1):
GN/4 DFTN/4 HN/4 DFTN/4 (e)
DFTN/2 x
−iG N/4 DFT N/4 iH N/4 DFT N/4 (oe)
DFTN x = x
−GN/4 DFTN/4 −HN/4 DFTN/4
DFTN/2 x(oo)
iGN/4 DFTN/4 −iHN/4 DFTN/4
I 0 GN/4 HN/4 (e)
0 I −iGN/4 iHN/4 DFTN/2 0 0 x
= 0 DFT N/4 0 x(oe)
I 0 −GN/4 −HN/4
0 0 DFTN/4 x(oo)
0 I iGN/4 −iHN/4
GN/4 HN/4
DFTN/2 x(e)
I −iG iH
N/4 N/4
= DFTN/4 x(oe)
GN/4 HN/4
I − DFTN/4 x(oo)
−iGN/4 iHN/4
GN/4 DFTN/4 x(oe) + HN/4 DFTN/4 x(oo)
(e)
DFTN/2 x + −i GN/4 DFTN/4 x(oe) − HN/4 DFTN/4 x(oo)
=
GN/4 DFTN/4 x(oe) + HN/4 DFTN/4 x(oo)
(e)
DFTN/2 x −
−i GN/4 DFTN/4 x(oe) − HN/4 DFTN/4 x(oo)
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS35
c) Explain from the above expression why, once the three FFT’s above have
been computed, the rest can be computed with N/2 complex multiplications,
and 2 × N/4 + N = 3N/2 complex additions. This is equivalent to 2N real
multiplications and N + 3N = 4N real additions.
Hint. It is important that GN/4 DFTN/4 x(oe) and HN/4 DFTN/4 x(oo) are com-
puted first, and the sum and difference of these two afterwards.
d) Due to what we just showed, our new algorithm leads to real multiplication
and addition counts which satisfy
Solution. If you add the split-radix FFT algorithm also to the code from
Exercise 2, you will see that it performs better than the FFT algorithm, but
worse than the non-recursive algorithm. That it performs better than the FFT
algorithm is as expected, since it has a reduced number of arithmetic operations,
and also a smaller number of recursive calls. It is not surprising that the non-
recursive function performs better, since only that function omits recursive calls,
and computes the values in the diagonal matrices once and for all.
By carefully examining the algorithm we have developed, one can reduce the
operation count to 4N log2 N − 6N + 8. This does not reduce the order of the
algorithm, but for small N (which often is the case in applications) this reduces
the number of operations considerably, since 6N is large compared to 4N log2 N
for small N . In addition to having a lower number of operations than the FFT
algorithm of Theorem 2.15 in the book, a bigger percentage of the operations
are additions for our new algorithm: there are now twice as many additions
than multiplications. Since multiplications may be more time-consuming than
additions (depending on how the CPU computes floating-point arithmetic), this
can be a big advantage.
Solution. The code for the split-radix algorithm can look as follows
CHAPTER 2. DIGITAL SOUND AND DISCRETE FOURIER ANALYSIS36
j = 0
for i in range(N-1):
print j
m = N/2
while (m >= 1 and j >= m):
j -= m
m /= 2
j += m
Explain that the code prints all numbers in [0, N − 1] in bit-reversed order (i.e. j).
Verify this by running the program, and writing down the bits for all numbers
for, say N = 16. In particular explain the decrements and increments made to
the variable j. The code above thus produces pairs of numbers (i, j), where j is
the bit-reverse of i. As can be seen, bitreverse applies similar code, and then
swaps the values xi and xj in x, as it should.
Solution. If i < N/2 is odd, then the least significant bit is 1. This means
that the most significant bit of j is 1, so that j ≥ N/2, so that j > i.
In bitreversal, you can see that the bit-reversal of 2r and 2r +1 are handled
together (i.e. i is increased with 2 in the for-loop). The effect of this is that the
number of if-tests can be reduced, due to the observations from b) and c).
Chapter 3
Solution. Here we have that t−1 = 1/4, t0 = 1/4, t1 = 1/4, and t2 = 1/4. We
now get that s0 = t0 = 1/4, s1 = t1 = 1/4, and s2 = t2 = 1/4 (first formula),
and sN −1 = s7 = t−1 = 1/4 (second formula). This means that the matrix of S
is
1 1 0 0 0 0 1 1
1 1 1 0 0 0 0 1
1 1 1 1 0 0 0 0
1 0 1 1 1 1 0 0 0
S= .
4 0 0 1 1 1 1 0 0
0 0 0 1 1 1 1 0
0 0 0 0 1 1 1 1
1 0 0 0 0 1 1 1
38
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS39
Solution. The eigenvalues of S are 1, 5, 9,√ and are found by computing a DFT
of the first column (and multiplying by N = 2). The eigenvectors are the
Fourier basis vectors. 1 has multiplicity 2.
b) Verify the result from a) by computing the eigenvectors and eigenvalues the
way you taught in your first course in linear algebra. This should be a much
more tedious task.
c) Use a computer to compute the eigenvectors and eigenvalues of S also. For
some reason some of the eigenvectors seem to be different from the Fourier basis
vectors, which you would expect from the theory in this section. Try to find an
explanation for this.
Solution. The computer uses some numeric algorithm to find the eigenvectors.
However, eigenvectors may not be unique, so you have no control on which
eigenvectors it actually selects. In particular, here the eigenspace for λ = 1 has
dimension 2, so that any linear combination of the two eigenvectors from this
eigenspace also is an eigenvector. Here it seems that a linear combination is
chosen which is different from a Fourier basis vector.
This means that the eigenvalues of S1 + S2 are the sum of the eigenvalues of S1
and S2 . The actual eigenvalues which are added are dictated by the index of the
frequency response, i.e. λS1 +S2 ,n = λS1 ,n + λS2 ,n .
b) How can you express the eigenvalues of S1 S2 in terms of the eigenvalues of
S1 and S2 ?
Solution. The matrix for the operation which keeps every second component
is
1 0 ··· 0 0
0 0 · · · 0 0
.. .. .. .. .. ,
. . . . .
0 0 · · · 1 0
0 0 ··· 0 0
where 1 and 0 are repeated in alternating order along the main diagonal. Since
the matrix is not constant on the main diagonal, it is not a circulant Toeplitz
matrix, and hence not a filter.
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS43
X 1
λS (ω) = tk e−ikω = (1 + 3e−iω + 3e−2iω + e−3iω )
3
k
2 1
= e−3iω/2 (e3iω/2 + 3eiω/2 + 3e−iω/2 + e−3iω/2 )
3 2
2 −3iω/2
= e (cos(3ω/2) + 3 cos(ω/2)).
3
From this expression it is easy to plot the frequency response, but since this is
complex, we have to plot the magnitude, i.e. |λS (ω)| = 23 | cos(3ω/2)+3 cos(ω/2)|.
We also see that λS (0) = 23 , and that λS (π) = 0, so that the filter is a low-pass
filter.
b) Find an expression for the vector frequency response λS,2 . What is Sx when
x is the vector of length N with components e2πi2k/N ?
Solution. If we use the connection between the vector frequency response and
the continuous frequency response we get
2 −6πi/N
λS,2 = λS (2π2/N ) = e (cos(6π/N ) + 3 cos(2π/N )).
3
Alternatively you can here compute that the first column in the circulant Toeplitz
matrix for S is given by s0 = t1 , s2 = t2 , s3 = t3 , and s4 = t4 , and insert this in
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS44
PN −1
the definition of the vector frequency response, λS,2 = k=0 sk e−2πi2k/N . We
know that e2πi2k/N is an eigenvector for S since S is a filter, and that λS,2 is
the corresponding eigenvalue. We therefore get that
2 −6πi/N
Sx = λS,2 x = e (cos(6π/N ) + 3 cos(2π/N ))x.
3
4
1 2iω 1 iω/2
λS1 (ω) = (e +4eiω +6+4e−iω +e−2iω ) = (e + e−iω/2 ) = cos4 (ω/2),
16 2
2
1 iω/2
λS2 (ω) = (eiω + 2 + e−iω )/4 = (e + e−iω/2 ) = cos2 (ω/2).
2
We then get that
λS1 S2 (ω) = λS1 (ω)λS2 (ω) = cos4 (ω/2) cos2 (ω/2) = cos6 (ω/2)
6
1 iω/2
= (e + e−iω/2 )
2
1 3iω
= (e + 6e2iω + 15eiω + 20 + 15e−iω + 6e−2iω + e−3iω ),
64
where we have used that, since we have a sixth power, the values can be obtained
from fra a row in Pascal’s triangle also here. It is now clear that
1
S1 S2 = {1, 6, 15, 20, 15, 6, 1}.
64
1
You could also argue here by taking the convolution of 16 (1, 4, 6, 4, 1) with
1
4 (1, 2, 1).
Solution. We have that λS (ω) = 12 (1 + cos ω). This clearly has the maximum
point (0, 1), and the minimum point (π, 0).
Solution. We have that |λT (ω)| = 12 (1 − cos ω). This clearly has the maximum
point (π, 1), and the minimum point (0, 0). The connection between the frequency
responses is that λT (ω) = λS (ω + π).
Solution. The sum of two digital filters is again a digital filter, and the first
column in the sum can be obtained by summing the first columns in the two
matrices. This means that the filter coefficients in 12 (S1 + S2 ) can be obtained
by summing the filter coefficients of S1 and S2 , and we obtain
1
({1, 0, . . . , 0, c} + {1, 0, . . . , 0, −c}) = {1}.
2
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS48
This means that 12 (S1 + S2 ) = I, since I is the unique filter with e0 as first
column. The interpretation in terms of echos is that the echo from S2 cancels
that from S1 .
Solution. The code can look like this, when we reduce the treble. We have
used the convolve function:
t = [1.]
for kval in range(k):
t = convolve(t, [1/2., 1/2.])
If we reduce the bass instead, the code can look like this.
t = [1.]
for kval in range(k):
t = convolve(t, [1/2., -1/2.])
Hint. Use Theorem 3.18 in the book to compute the frequency response of
S1 S2 first.
d) Write down the matrix of the filter S1 S2 for N = 8.
• First a time delay filter with delay d1 = 2, due to internal transfer of data
in the system,
• then the treble-reducing filter T = {1/4, 1/2, 1/4},
• finally a time delay filter with delay d2 = 4 due to internal transfer of the
filtered data.
Hint. Use the expressions (Ed1 x)n = xn−d1 , (T x)n = 14 xn+1 + 12 xn + 14 xn−1 ,
(Ed2 x)n = xn−d2 , and compute first (Ed1 x)n , then (T Ed1 x)n , and finally
(T2 x)n = (Ed2 T Ed1 x)n . From the last expression you should be able to read
out the filter coefficients.
c) Assume that N = 8. Write down the 8 × 8-circulant Toeplitz matrix for the
filter T2 .
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS50
X 1 X 2πikn/N
λS2 (ω) = cos(2πkn/N )tk e−ikω = (e + e−2πikn/N )tk e−ikω
2
k k
!
1 X
−ik(ω−2πn/N )
X
−ik(ω+2πn/N )
= tk e + tk e
2
k k
1
= (λS1 (ω − 2πn/N ) + λS1 (ω + 2πn/N )).
2
x, fs = audioread(’sounds/castanets.wav’)
N, nchannels = shape(x)
z = zeros((N, nchannels))
for n in range(1,N-1):
z[n] = 2*x[n+1] + 4*x[n] + 2*x[n-1]
z[0] = 2*x[1] + 4*x[0] + 2*x[N-1]
z[N-1] = 2*x[0] + 4*x[N-1] + 2*x[N-2]
z = z/abs(z).max()
play(z, fs)
Comment in particular on what happens in the three lines directly after the
for-loop, and why we do this. What kind of changes in the sound do you expect
to hear?
Solution. In the code a filter is run on the sound samples from the file
castanets.wav. Finally the new sound is played. In the first two lines after the
for-loop, the first and the last sound samples in the filtered sound are computed,
under the assumption that the sound has been extended to a periodic sound
CHAPTER 3. OPERATIONS ON DIGITAL SOUND: DIGITAL FILTERS51
with period N. After this, the sound is normalized so that the sound samples lie
in the range between −1 and 1. In this case the filter is a low-pass-filter (as we
show in b)), so that we can expect that that the treble in the sound is reduced.
b) Write down the compact filter notation for the filter which is used in the
code, and write down a 5 × 5 circulant Toeplitz matrix which corresponds to
this filter. Plot the (continuous) frequency response. Is the filter a low-pass- or
a high-pass filter?
Solution. Compact filter notation for the filter which is run is {2, 4, 2}. A
5 × 5 circulant Toeplitz matrix becomes
4 2 0 0 2
2 4 2 0 0
0 2 4 2 0
.
0 0 2 4 2
2 0 0 2 4
The frequency response is λS (ω) = 2eiω + 4 + 2e−iω = 4 + 4 cos ω. It is clear
that this gives a low-pass filter.
c) Another filter is given by the circulant Toeplitz matrix
4 −2 0 0 −2
−2 4 −2 0 0
0 −2 4 −2 0
.
0 0 −2 4 −2
−2 0 0 −2 4
Express a connection between the frequency responses of this filter and the filter
from b). Is the new filter a low-pass- or a high-pass filter?
where S is the filter from the first part of the exercise. The new filter therefore
becomes a high-pass filter, since to add π to ω corresponds to swapping the
frequencies 0 and π. We could also here refered to Observation 3.22 in the book,
where we stated that adding an alternating sign in the filter coefficients turns a
low-pass filter into a high-pass filter and vice versa.
53
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 54
1 1 1
2 4 0 0 0 0 0 4
1 1 1
4 2 4 0 0 0 0 0
1 1 1
0 4 2 4 0 0 0 0
1 1 1
0 0 4 2 4 0 0 0
1 1 1
0 0 0 4 2 4 0 0
1 1 1
0 0 0 0 4 2 4 0
1 1 1
0 0 0 0 0 4 2 4
1 1 1
4 0 0 0 0 0 4 2
1 1
1
1
2 4 0 0 0 0 0 4 4 0 0 0
1 1 1
0 0 0 0 0 0 0 0 0
4
S1 = 2
1
4
1 1
S2 =
(S2 )f = .
0 4 2 4 0 0 0 0 0 0 0 0
1 1 1 1
0 0 4 2 4 0 0 0 0 0 0 4
q q q
1 1 1
q 3 3 3
π1
q
π3
q
π5
DCT3 = 23 cos 2 2
cos cos
3 2 3 3 2 3 3 2
q q q
2 2π 1 2 2π 3 2 2π 5
3 cos 3 2 cos
3 3 2 3cos 3 2
q q q
1 1 1
q 3 q q 3 3
= 23 cos(π/6) 2
cos(π/2) 2
cos(5π/6)
q3 q3
q
2 2 2
3 cos(π/3) 3 cos(π) 3 cos(5π/3)
q q q
1 1 1
q √ 3 3 q √
3
2 2
= 3 ( 3/2 + i/2) 0 (− 3/2 + i/2)
q √ q q3 √
2
3 (1/2 + 3i/2) − 23 2
3 (1/2 − 3i/2)
n + 21
1 2n + 1 1
cos 2π k+ = cos 2π k+ .
2N 2 4N 2
If we consider these as vectors of length 2N , we recognize these as thep
unit vectors
d
p 2n+1 in the 2N -dimensional DCT, divided
√ by the factor dn = 2/(2N ) =
1/N , so that these vectors have length N . To see that these vectors are
orthogonal when we restrict to the first N elements we use Equation (6.36) in
the book as follows:
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 56
2N −1
n1 + 21 n2 + 12
X 1 1
N δn1 ,n2 = cos 2π k+ cos 2π k+
2N 2 2N 2
k=0
N −1
n1 + 12 n2 + 21
X 1 1
= cos 2π k+ cos 2π k+
2N 2 2N 2
k=0
2N −1
n1 + 21 n2 + 12
X 1 1
+ cos 2π k+ cos 2π k+
2N 2 2N 2
k=N
N −1
n1 + 12 n2 + 21
X 1 1
= cos 2π k+ cos 2π k+
2N 2 2N 2
k=0
N −1
n1 + 12 n2 + 12
X 1 1
+ cos 2π k+ cos 2π k+
2N 2 2N 2
k=0
N −1
n1 + 21 n2 + 12
X 1 1
=2 cos 2π k+ cos 2π k+ .
2N 2 2N 2
k=0
N −1 1 1
X n1 + 2 1 n2 + 2 1 N
cos 2π k+ cos 2π k+ = δn1 ,n2 ,
2N 2 2N 2 2
k=0
p
so that the vectors are orthogonal with lengths N/2.
r
N (IV) 0 A
M= DCTN
2 B 0
··· ··· 0 −1 −1 0 ··· ···
.. .. .. .. .. .. .. ..
f
A= . . . . . . . .
= −IN/2 −IN/2
0 −1 ··· ··· · · · · · · −1 0
−1 0 ··· ··· · · · · · · 0 −1
1 0 ··· ··· · · · · · · 0 −1
0 1 ··· ··· · · · · · · −1 0
f
B= . .. = IN/2 −IN/2 .
.. .. .. .. .. ..
.. . . . . . . .
··· ··· 0 1 −1 0 · · · · · ·
Due to this expression, any algorihtm for the DCT-IV can be used to compute
the MDCT.
are easy to verify. From the first property it follows that columns N/2, . . . , 3N/2−
1 of the MDCT are columns N − 1, N − 2, . . . , 0 of the DCT-IV, with a sign
change (they occur in opposite order). From the second property, it follows
that columns 3N/2, . . . , 2N − 1 of the MDCT are columns 0, . . . , N/2 − 1 of the
DCT-IV, with a sign change. This means that, if y is a vector of length 2N , the
MDCT of y can be written as
−y3N/2 − y3N/2−1
−y3N/2+1 − y3N/2−2
..
r
.
N (IV) −y2N −1 − yN
.
DCTN
2
y0 − yN −1
y1 − yN/2+1
..
.
yN/2−1 − yN/2
q q
N
The factor 2 was added since N2 was added in front of the cosine-matrix
(IV)
in order to make
DCT N orthogonal. The result now follows by noting that
0 A
we can write y for the vector on the right hand side, with A and B as
B 0
defined in the text of the exercise.
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 58
b) The MDCT is not invertible, since it is not a square matrix. We will show
here that it still can be used in connection with invertible transformations. We
first define the IMDCT as the matrix M T /N . Transposing the matrix expression
we obtained in a) gives
BT
1 0 (IV)
√ DCTN
2N AT 0
for the IMDCT, which thus also has an efficient implementation. Show that if
x0 x1
y0,1 =M y1,2 = M
x1 x2
(i.e. we compute two MDCT’s where half of the data overlap), then
−1 N −1
x1 = {IMDCT(y0,1 )}2N
k=N + {IMDCT(y1,2 )}k=0 .
Even though the MDCT itself is not invertible, the input can still be recovered
from overlapping MDCT’s.
Solution. Applying the MDCT first, and then the IMDCT, gives us the matrix
BT
T
1 0 0 A 1 B B 0
=
2 AT 0 B 0 2 0 AT A
Note that
f
! f
!
T
IN/2 IN/2 T
IN/2 −IN/2
A A= f B B= f .
IN/2 IN/2 −IN/2 IN/2
x0 − xrev 1 x1 − xrev
1 0 1
and
2 x1 + xrev
1 2 x2 + xrev
2
Adding the second component of the first and the first component of the second
gives x1 , which proves the result.
n
X L
X
zn = t0 xn + tk (xn+k + xn−k ) + tk (xn+k + xn−k+N ). (4.1)
k=1 k=n+1
b) L ≤ n < N − L:
L
X
zn = t0 xn + tk (xn+k + xn−k ). (4.2)
k=1
c) N − L ≤ n < N :
−1−n
NX L
X
zn = t 0 xn + tk (xn+k + xn−k ) + tk (xn+k−N + xn−k ). (4.3)
k=1 k=N −1−n+1
The convolve function may not pick up this reduction in the number of
multiplications, since it does not assume that the filter is symmetric. We will
still use the convolve function in implementations, however, due to its heavy
optimization.
a) Assume that x is a real signal. Equation (4.12) in the book, which said that
n n
yn = cos π <((DFTN x(1) )n ) + sin π =((DFTN x(1) )n )
2N
n 2N
n
yN −n = sin π <((DFTN x(1) )n ) − cos π =((DFTN x(1) )n )
2N 2N
for the n’th and N − n’th coefficient of the DCT. This can also be rewritten as
n
yn = <((DFTN x(1) )n ) + =((DFTN x(1) )n ) cos π
n n 2N
(1)
− =((DFTN x )n )(cos π − sin π )
2N 2N n
yN −n = − <((DFTN x(1) )n ) + =((DFTN x(1) )n ) cos π
n n 2N
(1)
+ <((DFTN x )n )(sin π + cos π ).
2N 2N
Explain that the first two equations require 4 multiplications to compute yn and
yN −n , and that the last two equations require 3 multiplications to compute yn
and yN −n .
b) Explain why the trick in a) reduces the number of additional multiplications
in a DCT, from 2N to 3N/2.
c) Explain why the trick in a) can be used to reduce the number of additional
multiplications in an IDCT with the same number.
Hint. match the expression eπin/(2N ) (yn − iyN −n ) you encountered in the
IDCT with the rewriting you did in b).
d) Show that the penalty of the trick we here have used to reduce the number
of multiplications, is an increase in the number of additional additions from N
to 3N/2. Why can this trick still be useful?
a) Let y = DFTN x be the N -point DFT of the real vector x. Show that
<((DFTN/2 x(e) )n ) + (CN/4 z)n 0 ≤ n ≤ N/4 − 1
<(yn ) = <((DFTN/2 x(e) )n ) n = N/4
<((DFTN/2 x(e) )n ) − (CN/4 z)N/2−n N/4 + 1 ≤ n ≤ N/2 − 1
(4.4)
=((DFTN/2 x(e) )n ) n=0
=(yn ) = =((DFTN/2 x(e) )n ) + (CN/4 w)N/4−n 1 ≤ n ≤ N/4 − 1 (4.5)
=((DFTN/2 x(e) )n ) + (CN/4 w)n−N/4 N/4 ≤ n ≤ N/2 − 1
where x(e) is as defined in Theorem 2.15 in the book, where z, w ∈ RN/4 defined
by
Explain from this how you can make an algorithm which reduces an FFT of
length N to an FFT of length N/2 (on x(e) ), and two DCT’s of length N/4 (on
z and w). We will call this algorithm the revised FFT algorithm.
Solution. Taking real and imaginary parts in Equation (2.13) in the book for
the FFT algorithm we obtain
<(yn ) = < (DFTN/2 x(e) )n + <((DN/2 DFTN/2 x(o) )n
=(yn ) = = (DFTN/2 x(e) )n + =((DN/2 DFTN/2 x(o) )n ,
These equations explain the first terms <((DFTN/2 x(e) )n ) and =((DFTN/2 x(e) )n )
on the right hand sides in equations (4.4) and (4.5). It remains to rewrite
< (DN/2 DFTN/2 x(o) )n and = (DN/2 DFTN/2 x(o) )n so that the remaining
terms on the right hand sides can be seen. Let us first consider the equation for
the real part with 0 ≤ n ≤ N/4 − 1. In this case we can write
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 62
where we have used that cos is periodic with period 2π, that cos is symmetric, and
where z is the vector defined in the text of the theorem. When 0 ≤ n ≤ N/4 − 1
this can also be written as
N/4−1
X
(CN/4 )n,k zk = (CN/4 z)n ,
k=0
we arrive at −(CN/4 z)N/2−n instead, and this proves the third formula in
Equation (4.4). For the case n = N4 all the cosine entries in the sum are zero,
and this completes the proof of Equation (4.4).
For the imaginary part, using that sin is periodic with period 2π, and that
sin is anti-symmetric, analogous calculations as above give
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 63
N/4−1 !
(o)
X
(o) (o) n k + 21
=((DN/2 DFTN/2 x )n ) = ((x )N/2−1−k − (x )k ) sin 2π .
N/2
k=0
(4.6)
Using that
N/4−1
(N/4 − n)(k + 21 )
X
((x(o) )N/2−1−k − (x(o) )k )(−1)k cos 2π
N/2
k=0
N/4−1
(N/4 − n)(k + 21 )
X
= wk cos 2π ,
N/2
k=0
where w is the vector defined as in the text of the theorem. When n = 0 this is 0
since all the cosines entries are zero. When 1 ≤ n ≤ N/4 this is (CN/4 w)N/4−n ,
since cos(2π(N/4 − n)(k + 1/2)/(N/2)) are entries in the matrix CN/4 . This
proves the second formula in Equation (4.5).
For N/4 ≤ n ≤ N/2 − 1 we can use that cos(2π(N/4 − n)(k + 1/2)/(N/2)) =
cos(2π(n − N/4)(k + 1/2)/(N/2)), which is an entry in the matrix DCTN/4
as well, so that we get (CN/4 z)n−N/4 . This also proves the third formula in
Equation (4.5), and the proof is done.
a) says nothing about the coefficients yn for n > N2 . These are obtained in
the same way as before through symmetry. a) also says nothing about yN/2 .
This can be obtained with the same formula as in Theorem 2.15 in the book.
Let us now compute the number of arithmetic operations our revised algorithm
needs. Denote by the number of real multiplications needed by the revised N -
point FFT algorithm
b) Explain from the algorithm in a) that
Hint. 3N/8 should come from the extra additions/multiplications (see Exer-
cise 4) you need to compute when you run the algorithm from Theorem 4.22 in
the book for CN/4 . Note also that the equations in a) require no extra multipli-
cations, but that there are xix equations involved, each needing N/4 additions,
so that we need 6N/4 = 3N/2 extra additions.
CHAPTER 4. SYMMETRIC FILTERS AND THE DCT 64
the revised DCT. The total number of operations is thus O(2N log2 N ), i.e. half
the operation count of the split-radix algorithm. The orders of these algorithms
are thus the same, since we here have adapted to read data.
e) Explain that, if you had not employed the trick from Exercise 4, we would
instead have obtained MN = O 23 log2 N , and AN = O 43 log2 N , which
equal the orders for the number of multiplications/additions for the split-radix
algorithm. In particular, the order of the operation count remains the same, but
the trick from Exercise 4 turned a bigger percentage of the arithmetic operations
into additions.
The algorithm we here have developed thus is constructed from the beginning
to apply for real data only. Another advantage of the new algorithm is that it
can be used to compute both the DCT and the DFT.
for 1 ≤ n ≤ N/4 − 1. Explain how one can compute z and w from this using
two IDCT’s of length N/4.
b) Using equations (4.4)-(4.5), show that
and explain how one can compute x(e) from this using an IFFT of length N/2.
Chapter 5
Solution. Since f is constant and equal to f (n) on [n, n + 1/2), and constant
and equal to f (n + 1/2) on [n + 1/2, n + 1), we get that
65
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES66
Z N Z n+1
hf, φ0,n i = f (t)φ0,n (t)dt = f (t)dt
0 n
Z n+1/2 Z n+1
= f (t)dt + f (t)dt
n n+1/2
Z n+1/2 Z n+1
= f (n)dt + f (n + 1/2)dt
n n+1/2
√
projV0 (φ1,2n ) = φ0,n / 2
√
projV0 (φ1,2n+1 ) = φ0,n / 2
√
[projV0 (φ1,2n )]φ0 = en / 2
√
[projV0 (φ1,2n+1 )]φ0 = en / 2.
These are the columns in the matrix for projV0 relative to the bases φ1 and φ0 .
This matrix is thus
1 1 0 0 0 ··· 0 0 0
1 0 0 1 1 0 · · ·
0 0 0
√ . . . . . .. .
.. .. ..
2 .. .. .. .. .. . . . .
0 0 0 0 0 ··· 0 1 1
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES67
b) Similarly, use Lemma 5.11 in the book to write down the matrix for projW0 :
V1 → W0 relative to the bases φ1 and ψ0 .
√
projW0 (φ1,2n ) = ψ0,n / 2
√
projW0 (φ1,2n+1 ) = −ψ0,n / 2
√
[projW0 (φ1,2n )]ψ0 = en / 2
√
[projW0 (φ1,2n+1 )]ψ0 = −en / 2.
These are the columns in the matrix for projW0 relative to the bases φ1 and ψ0 .
This matrix is thus
1 −1 0 0 0 ··· 0 0 0
1 0 0 1 −1
0 ··· 0 0 0
√ . .
.. .. .. .. .. .. .. ..
2 .. . . . . . . . .
0 0 0 0 0 ··· 0 1 −1
Solution. The first columns in the matrix of projV0 relative to (φ0 , ψ0 ) are
It follows that the matrix of projV0 relative to (φ0 , ψ0 ) is given by the diagonal
matrix where the first half of the entries on the diagonal are 1, the second half 0.
c) Show in a similar way that the projection of V1 onto W0 has a matrix relative
to (φ0 , ψ0 ) given by the diagonal matrix where the first half of the entries on
the diagonal are 0, the second half 1.
for any f . Show also that the first part of Proposition 5.12 in the book follows
from this.
−1 −1 Z n+1
N
! N
X Z N X
projV0 (f ) = f (t)φ0,n (t)dt φ0,n = f (t)dt φ0,n ,
n=0 0 n=0 n
where we have used the orthogonal decomposition formula. Note also that, if
f (t) ∈ V1 , and fn,1 is the value f attains on [n, n + 1/2), and fn,2 is the value f
attains on [n + 1/2, n + 1), we have that
N
X −1 Z n+1
projV0 (f ) = f (t)dt φ0,n (t)
n=0 n
N −1 N −1
X 1 1 X fn,1 + fn,2
= fn,1 + fn,2 φ0,n (t) = φ0,n (t),
n=0
2 2 n=0
2
which is the function which is (fn,1 + fn,2 )/2 on [n, n + 1). This proves the first
part of Proposition 5.12 in the book.
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES69
X Z n+1 X Z n+1 2
k f (t)dt φ0,n (t) − f k2 = hf, f i − f (t)dt .
n n n n
This, together with the previous exercise, gives us an expression for the least-
squares error for f from V0 (at least after taking square roots). 2DO: Generalize
to m
from what we just showed in Exercise 5 (use that the φ0,n are orthonormal).
This means that the above can be written
N
X −1 Z N Z n+1 N
X −1 Z n+1 2
= hf, f i − 2 f (s)ds φ0,n (t)f (t)dt + f (t)dt
n=0 0 n n=0 n
N
X −1 Z n+1 Z n+1 N
X −1 Z n+1 2
= hf, f i − 2 f (s)ds f (t)dt + f (t)dt
n=0 n n n=0 n
N
X −1 Z n+1 2 NX
−1 Z n+1 2
= hf, f i − 2 f (t)dt + f (t)dt
n=0 n n=0 n
N
X −1 Z n+1 2
= hf, f i − f (t)dt .
n=0 n
for any f . Show also that the second part of Proposition 5.12 in the book follows
from this.
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES70
−1 −1
N N
!
X X Z N
projW0 (f ) = hf, ψ0,n iψ0,n (t) = f (t)ψ0,n (t)dt ψ0,n (t)
n=0 n=0 0
N
X −1 Z n+1
= f (t)ψ0,n (t)dt ψ0,n (t)
n=0 n
−1
N
!
X Z n+1/2 Z n+1
= f (t)dt − f (t)dt ψ0,n (t),
n=0 n n+1/2
where we used that ψ0,n is nonzero only on [n, n + 1), and is 1 on [n, n + 1/2),
and −1 on [n + 1/2, n + 1). Note also that, if f (t) ∈ V1 , and fn,1 is the value f
attains on [n, n + 1/2), and fn,2 is the value f attains on [n + 1/2, n + 1), we
have that
−1
N
!
X Z n+1/2 Z n+1
projW0 (f ) = f (t)dt − f (t)dt ψ0,n (t)
n=0 n n+1/2
N −1 N −1
X 1 1 X fn,1 − fn,2
= fn,1 − fn,2 ψ0,n (t) = ψ0,n (t),
n=0
2 2 n=0
2
which is the function which is (fn,1 − fn,2 )/2 on [n, n + 1/2), and −(fn,1 − fn,2 )/2
on [n + 1/2, n + 1). This proves the second part of Proposition 5.12 in the book.
(φ0,0 , ψ0,0 , φ0,1 , ψ0,1 , . . . , φ0,(N −1)/2 , ψ(N −1)/2 , φ0,(N +1)/2 ).
Since all functions are assumed to have period N , we have that
1 1
φ0,(N +1)/2 = √ (φ1,N −1 + φ1,N ) = √ (φ1,0 + φ1,N −1 ).
2 2
From this relation one can find the last column in the change of coordinate
matrix from φ0 to (φ1 , ψ1 ), i.e. the IDWT matrix. In particular, when N is
odd, we see that the last column in the IDWT matrix circulates to the upper
right corner. In terms of coordinates, we thus have that
1 1
c1,0 = √ (c0,0 + w0,0 + c0,(N +1)/2 ) c1,N −1 = √ c0,(N +1)/2 . (5.3)
2 2
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES71
1
c0,0 = √ (c1,0+c1,1 −c1,N −1 )
2
1
w0,0 = √ (c1,0−c1,1 −c1,N −1 )
2
1
c0,(N +1)/2 = √ 2c1,N −1 . (5.4)
2
b) Explain that the DWT matrix is orthogonal if and only if N is even. Also
explain that it is only the last column which spoils the orthogonality.
Solution. Since φm,n ∈ Vm we must have that T (φm,n ) = φm,n . Since ψm,n
is in the orthogonal complement of Vm in Vm+1 we must have that T (ψm,n ) = 0.
The first half of the columns in the matrix of projVm relative to (φm , ψm ) are
P2m N −1
Solution. If f ∈ Vm we can write f (t) = n=0 cm,n φm,n (t). We now get
2mX
N −1 2mX
N −1
g(t) = f (2t) = cm,n φm,n (2t) = cm,n 2m/2 φ(2m 2t − n)
n=0 n=0
2mX
N −1 2mX
N −1
−1/2 (m+1)/2
= cm,n 2 2 φ(2 m+1
t − n) = cm,n 2−1/2 φm+1,n (t).
n=0 n=0
This shows that g ∈ Vm+1 . To prove the other way, assume that g(t) = f (2t) ∈
P2m+1 N −1
Vm+1 . This means that we can write g(t) = n=0 cm+1,n φm+1,n (t). We
now have
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES73
2m+1
X N −1 2m+1
X N −1
f (t) = g(t/2) = cm+1,n φm+1,n (t/2) = cm+1,n 2(m+1)/2 φ(2m t − n)
n=0 n=0
2mX
N −1 2m+1
X N −1
= cm+1,n 2(m+1)/2 φ(2m t − n) + cm+1,n 2(m+1)/2 φ(2m t − n)
n=0 n=2m N
2mX
N −1 2mX
N −1
= cm+1,n 2(m+1)/2 φ(2m t − n) + cm+1,n+2m N 2(m+1)/2 φ(2m t − n − 2m N )
n=0 n=0
2mX
N −1 2mX
N −1
= cm+1,n 2(m+1)/2 φ(2m t − n) + cm+1,n+2m N 2(m+1)/2 φ(2m t − n)
n=0 n=0
2mX
N −1
= (cm+1,n + cm+1,n+2m N )21/2 2m/2 φ(2m t − n)
n=0
2mX
N −1
= (cm+1,n + cm+1,n+2m N )21/2 φm,n (t) ∈ Vm
n=0
The thing which made this a bit difficult was that the range of the n-indices
here was outside [0, 2m N − 1] (which describe the legal indices in the basis Vm ),
so that we had to use the periodicity of φ.
[T1 ⊕ T2 ⊕ . . . ⊕ Tn ](B1 ,B2 ,...,Bn ) = [T1 ]B1 ⊕ [T2 ]B2 ⊕ · · · ⊕ [Tn ]Bn ,
Here two new concepts are used: a direct sum of matrices, and a direct sum of
linear transformations.
Solution. By definition, [T1 ]B1 ⊕ [T2 ]B2 ⊕ · · · ⊕ [Tn ]Bn is a block matrix where
the blocks on the diagonal are the matrices [T1 ]B1 , [T2 ]B2 , and so on. If bi are
the basis vectors in Bi , the columns in [Ti ]Bi are [T (bj )]Bi . This means that
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES74
[T1 ]B1 ⊕ [T2 ]B2 ⊕ · · · ⊕ [Tn ]Bn has [T (bj )]Bi in the j’th block, and 0 elsewhere.
This means that we can write it as
0 ⊕ · · · 0 ⊕ [T (bj )]Bi ⊕ 0 · · · 0.
On the other hand, [T1 ⊕ T2 ⊕ . . . ⊕ Tn ](B1 ,B2 ,...,Bn ) is a matrix of the same
size, and the corresponding column to that of the above is
Here bj occurs as the i’th summand. This is clearly the same as what we
computed for the right hand side above.
T1 0 v1
(T1 ⊕ T2 )(v1 ⊕ v2 ) =
0 T2 v2
T1 v1 λv1
= =
T2 v2 λv2
v
= λ 1 = λ(v1 ⊕ v2 ).
v2
−1
A 0 A 0
(A ⊕ B)(A−1 ⊕ B −1 ) =
0 B 0 B −1
AA−1
0 I 0
= = =I
0 BB −1 0 I
A 0 C 0 AC 0
(A ⊕ B)(C ⊕ D) = = = (AC) ⊕ (BD)
0 B 0 D 0 BD
1.2 1.2
1.0 1.0
0.8 0.8
0.6 0.6
0.4 0.4
0.2 0.2
0.0 50 100 150 200 250 0.0 50 100 150 200 250
Figure 5.1: 2 vectors x1 and x2 which seem equal, but where the DWT’s are
very different.
(n+1/2)2−m (n+1)2−m
!
Z N Z Z
m/2
f (t)ψm,n (t)dt = 2 f (t)dt − f (t)dt .
0 n2−m (n+1/2)2−m
With f (t) = 1 − 2|1/2 − t/N | we have two possibilities: when n < N 2m−1
we have that [n2−m , (n + 1)2−m ) ⊂ [0, N/2], so that f (t) = 2t/N , and we get
(n+1/2)2−m (n+1)2−m
Z Z !
m/2
wm,n = 2 2t/N dt − 2t/N dt
n2−m (n+1/2)2−m
(n+1/2)2−m (n+1)2−m
= 2m/2 [t2 /N ]n2−m − 2m/2 [t2 /N ](n+1/2)2−m
2−3m/2 2−3m/2−1
2(n + 1/2)2 − n2 − (n + 1)2 = −
= .
N N
RN
When n ≥ N 2m−1 we have that f (t) = 2−2t/N , and using that 0 ψm,n (t)dt = 0
−3m/2−1
we must get that wm,n = 2 N .
For f (t) = 1/2 + cos(2πt/N )/2, note first that this has the same coefficients
RN
as cos(2πt/N )/2, since 0 ψm,n (t)dt = 0. We now get
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES77
(n+1/2)2−m (n+1)2−m
Z Z !
m/2
wm,n = 2 cos(2πt/N )/2dt − cos(2πt/N )/2dt
n2−m (n+1/2)2−m
(n+1/2)2−m (n+1)2−m
= 2m/2 [N sin(2πt/N )/(4π)]n2−m − 2m/2 [N sin(2πt/N )/(4π)](n+1/2)2−m
2m/2−2 N
2 sin(2π(n + 1/2)2−m /N ) − sin(2πn2−m /N ) − sin(2π(n + 1)2−m /N ) .
=
π
There seems to be no more possibilities for simplification here.
Solution. We get
(n+1/2)2−m (n+1)2−m
Z Z !
m/2 k k
wm,n = 2 (t/N ) dt − (t/N ) dt
n2−m (n+1/2)2−m
(n+1/2)2−m (n+1)2−m
= 2m/2 [tk+1 /((k + 1)N k )]n2−m − 2m/2 [tk+1 /((k + 1)N k )](n+1/2)2−m
2−m(k+1/2)
2(n + 1/2)k+1 − nk+1 − (n + 1)k+1 .
= k
(k + 1)N
The leading term nk+1 will here cancel, but the others will not, so there is no
room for further simplification here.
(φ0 , ψ0 , ψ1 , ψ2 , ψ3 , ψ4 , ψ5 , ψ6 , ψ7 , ψ8 , ψ9 ),
(the DWT is nothing√but the change of coordinates from φ10 to this basis), and
P1023
since f (t) √= n=0 2ψ9,n , it is clear that the coordinate vector of f in this
basis has 2 in the second part (the ψ9 -coordinates), and 0 elsewhere. The
10-level DWT of x therefore
√ gives the vector of length 1024 which is 0 on the
first half, and equal to 2 on the second half. m = 10 is here arbitrarily chosen:
The result would have been the same for m = 1, m = 2, and so on. The following
code verifies the result:
x = tile([1.,-1.], 512)
DWTImpl(x, 10, ’Haar’)
print x
Clearly the only integer for which φ(s) 6= 0 is s = 0 (since φ(0) = 1), so that the
only n which contributes in the sum is n = k. This means that f (k) = ck , so
that [f ]φ0 = (f (0), f (1), . . . , f (N − 1)).
Z n+1 Z n+1 Z 1
f (t)2 dt = (xn + (xn+1 − xn )(t − n))2 dt = (xn + (xn+1 − xn )t)2 dt
n n 0
Z 1
= (x2n + 2xn (xn+1 − xn )t + (xn+1 − xn )2 t2 )dt
0
1
= x2n t + xn (xn+1 − xn )t2 + (xn+1 − xn )2 t3 /3 0
1
= x2n + xn (xn+1 − xn ) + (xn+1 − xn )2 /3 = (x2n + xn xn+1 + x2n+1 ).
3
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES80
b) Show that
1/2 √
Z
1 1
(x0 + (x1 − x0 )t)φ1,1 (t)dt = 2 2 x0 + x1
0 12 24
1 √
Z
1 1
(x0 + (x1 − x0 )t)φ1,1 (t)dt = 2 2 x0 + x1 .
1/2 24 12
Z 1/2
(x0 + (x1 − x0 )t)φ1,1 (t)dt
0
Z 1/2 √ √ Z 1/2
= (x0 + (x1 − x0 )t)2 2tdt = 2 2 (x0 t + (x1 − x0 )t2 )dt
0 0
1/2
√ 1 √
2 1 3 1 1
= 2 2 x0 t + (x1 − x0 )t =2 2 x0 + (x1 − x0 ))
2 3 0 8 24
√
1 1
=2 2 x0 + x1 .
12 24
In the same way
Z 1
(x0 + (x1 − x0 )t)φ1,1 (t)dt
1/2
Z 1 √√ Z 1
= (x0 + (x1 − x0 )t)2 2(1 − t)tdt = 2 2 (x0 + (x1 − 2x0 )t − (x1 − x0 )t2 )tdt
1/2 1/2
1
√ √
1 2 1 3 1 3 7
= 2 2 x0 t + (x1 − 2x0 )t − (x1 − x0 )t =2 2 x0 + (x1 − 2x0 ) − (x1 − x0 )
2 3 1/2 2 8 24
√
1 1
=2 2 x0 + x1 .
24 12
c) Use the fact that
Z N N
X −1
(φ1,1 (t) − xn φ0,n (t))2 dt
0 n=0
Z 1 Z 1/2 Z 1
= φ1,1 (t)2 dt − 2 (x0 + (x1 − x0 )t)φ1,1 (t)dt − 2 (x0 + (x1 − x0 )t)φ1,1 (t)dt
0 0 1/2
N
X −1 Z n+1
+ (xn + (xn−1 − xn )t)2 dt
n=0 n
PN −1
and a) and b) to find an expression for kφ1,1 (t) − n=0 xn φ0,n (t)k2 .
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES81
N −1
√ √
2 X 1 2 1 1 1 1
+ (xn + xn xn+1 + x2n+1 ) − 2 2 2 x0 + x1 − 2 2 x0 + x1
3 n=0 3 12 24 24 12
N −1 N −1 √
2 2 X 2 1 X 2
= + xn + xn xn+1 − (x0 + x1 ).
3 3 n=0 3 n=0 2
d) To find the minimum least squares error, we can set the gradient of the
expression in c) to zero, and thus find the expression for the projection of φ1,1
−1
onto V0 . Show that the values {xn }Nn=0 can be found by solving the equation
1
Sx = b, where S = 3 {1, 4, 1} is an N × N symmetric filter, and b is the vector
√
with components b0 = b1 = 2/2, and bk = 0 for k ≥ 2.
√
∂f 1 4 1 2
= xN −1 + x0 + x1 −
∂x0 3 3 3 2
√
∂f 1 4 1 2
= x0 + x1 + x1 −
∂x1 3 3 3 2
∂f 1 4 1
= xi−1 + xi + xi+1 2 ≤ i < N − 1
∂xi 3 3 3
∂f 1 4 1
= xN −2 + xN −1 + x0 .
∂xN −1 3 3 3
√
Moving the two terms 22 over to the right hand side, setting the gradient equal
to zero is the same as solving the system Sx = b which we stated.
e) Solve the system in d. for some values of N to verify that the projection of
φ1,1 onto V0 is nonzero, and that its support covers the entire [0, N ].
2 1
hφ0,n , φ0,n i = hφ0,n , φ0,n±1 i = hφ0,n , φ0,n±k i = 0 for k > 1.
3 6
As a consequence, the {φ0,n }n are neither orthogonal, nor have norm 1.
Z n+1
hφ0,n , φ0,n i = (1 − |t − n|)2 dt
n−1
Z n+1
1 − 2|t − n| + (t − n)2 dt
=
n−1
n+1
1 2
=2−2+ (t − n)3 = .
3 n−1 3
We also have
Z n+1 Z 1
hφ0,n , φ0,n+1 = (1 − (t − n))(1 + (t − n − 1))dt = (1 − u)(1 + u − 1)du
n 0
Z 1
1 1 1
= (t − t2 )dt = − = .
0 2 3 6
Finally, the supports of φ0,n and φ0,n±k are disjoint for k > 1, so that we must
have hφ0,n , φ0,n±k i = 0 in that case.
if mod(len(x), 2)==0:
if bd_mode.lower() == ’symm’:
x[-1] += 2*lmbda*x[-2] # With symmetric extension
else:
x[-1] += lmbda*(x[0]+x[-2])
Since λ appears twice in each row, note that we have added entries before
multiplication, to avoid extra multiplication.
Show that we can obtain the piecewise linear φ we have defined as φ = χ[−1/2,1/2) ∗
χ[−1/2,1/2) (recall that χ[−1/2,1/2) is the function which is 1 on [−1/2, 1/2) and
0 elsewhere). This gives us a nice connection between the piecewise constant
scaling function (which is similar to χ[−1/2,1/2) ) and the piecewise linear scaling
function in terms of convolution.
Z ∞
χ[−1/2,1/2) ∗ χ[−1/2,1/2) (x) = χ[−1/2,1/2) (t)χ[−1/2,1/2) (x − t)dt.
−∞
The integrand here is 1 when −1/2 < t < 1/2 and −1/2 < x − t < 1/2, or
in other words when max(−1/2, −1/2 + x) < t < min(1/2, 1/2 + x) (else it is
0). When x > 0 this happens when −1/2 + x < t < 1/2, and when x < 0 this
happens when −1/2 < t < 1/2 + x. This means that
(R 1/2
dt = 1 − x , x > 0
χ[−1/2,1/2) ∗ χ[−1/2,1/2) (x) = R−1/2+x
1/2+x
−1/2
dt = 1 + x , x < 0.
But this is by definition φ.
Since also here λ appears twice in each row, we have added entries before
multiplication, to avoid extra multiplication.
2t
0 ≤ t < 1/2
φ1,1 (t) = 2 − 2t 1/2 ≤ t < 1
0 elsewhere
1 + t
−1 ≤ t < 0
φ0,0 (t) = 1 − t 0≤t<1
0 elsewhere
t
0≤t<1
φ0,1 (t) = 2 − t 1≤t<2 .
0 elsewhere
It follows that ψ̂(t) = φ1,1 (t) − αφ(t) − βφ1,1 is piecewise linear, and linear on
the segments [−1, 0], [0, 1/2], [1/2, 1], [1, 2].
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES85
On the segment [−1, 0] only the function φ is seen to be nonzero, and since
φ(t) = 1 + t here, we have that ψ̂(t) = −α(1 + t) = −α − αt here.
On the segment [0, 1/2] all three functions are nonzero, and
φ1,1 (t) = 2t
φ0,0 (t) = 1 − t
φ0,1 (t) = t
φ1,1 (t) = 2 − 2t
φ0,0 (t) = 1 − t
φ0,1 (t) = t
Z N Z N
1 1
ψ̂(t)dt = − α − β, tψ̂(t)dt = − β.
0 2 0 4
Z N Z 2 Z 0 Z 1/2 Z 1 Z 2
ψ̂(t)dt = ψ̂(t)dt = ψ̂(t)dt + ψ̂(t)dt + ψ̂(t)dt + ψ̂(t)dt
0 −1 −1 0 1/2 1
Z 0 Z 1/2
= (−α − αt)dt + (2 + α − β)t − α)dt
−1 0
Z 1 Z 2
+ ((α − β − 2)t − α + 2)dt + (βt − 2β)dt
1/2 1
0 1/2
1 2 1 2
= −αt − αt + (2 + α − β)t − αt
2 −1 2 0
1 2
1 2 1 2
+ (α − β − 2)t + (2 − α)t + βt − 2βt
2 1/2 2 1
1 1 1 3 1 3
= −α + α + (2 + α − β) − α + (α − β − 2) + (2 − α) + β − 2β
2 8 2 8 2 2
1
= − α − β,
2
RN 1
0
tψ̂(t)dt is computed similarly, so that we in the end arrive at 4 − β.
c) Explain why there is a unique function on the form given by Equation (5.38)
in the book which has two vanishing moments, and that this function is given
by Equation (5.40) in the book.
1
−α−β =0
2
1
−β =0
4
has the unique solution α = β = 41 , which we already have found.
Z 1 Z 2 Z 1
k k
ak = t (1 − |t|)dt, bk = t (1 − |t − 1|)dt, ek = tk (1 − 2|t − 1/2|)dt,
−1 0 0
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES87
Hint. you can integrate functions in Python with the function quad in the
package scipy.integrate . As an example, the function φ(t), which is nonzero
only on [−1, 1], can be integrated as follows:
Solution.
R RIn order for ψ to have vanishing moments we must have that
ψ̂(t)dt = tψ̂(t)dt = 0 Substituting ψ̂ = ψ − αφ0,0 − βφ0,1 we see that,
for k = 0, 1,
Z Z
t (αφ0,0 + βφ0,1 ) dt = tk ψ(t)dt.
k
Z Z Z
k k
t (αφ0,0 + βφ0,1 ) dt = α t φ0,0 dt + β tk φ0,1 (t)dt
Z 1 Z 2
k
=α t (1 − |t|)dt + β tk (1 − |t − 1|)dt = αak + βbk .
−1 0
A = zeros((2, 2))
b = zeros((2, 1))
for k in range(2):
res1, err1 = quad(lambda t: t**k*(1-abs(t)), -1, 1)
res2, err2 = quad(lambda t: t**k*(1-abs(t-1)), 0, 2)
res3, err3 = quad(lambda t: t**k*(1-2*abs(t-1/2.)), 0, 1)
A[k,:] = [res1, res2]
b[k] = res3
linalg.solve(A,b)
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES88
Z 0 Z 3
gk = tk (1 − |t + 1|)dt, dk = tk (1 − |t − 2|)dt
−2 1
A=zeros((4, 4))
b=zeros((4, 1))
for k in range(4):
res1, err1 = quad(lambda t: t**k*(1-abs(t)), -1, 1)
res2, err2 = quad(lambda t: t**k*(1-abs(t-1)), 0, 2)
res3, err3 = quad(lambda t: t**k*(1-abs(t+1)), -2, 0)
res4, err4 = quad(lambda t: t**k*(1-abs(t-2)), 1, 3)
res5, err5 = quad(lambda t: t**k*(1-2*abs(t-1/2.)), 0, 1)
A[k,:] = [res1, res2, res3, res4]
b[k] = res5
coeffs = linalg.solve(A,b)
Solution. The function ψ̂ now is supported on [−2, 3], and can be plotted as
follows:
t=linspace(-2,3,100)
plt.plot(t, (t >= 0)*(t <= 1)*(1-2*abs(t - 0.5)) \
-coeffs[0]*(t >= -1)*(t <= 1)*(1 - abs(t)) \
-coeffs[1]*(t >= 0)*(t <= 2)*(1 - abs(t - 1)) \
-coeffs[2]*(t >= -2)*(t <= 0)*(1 - abs(t + 1)) \
-coeffs[3]*(t >= 1)*(t <= 3)*(1 - abs(t - 2)))
Hint. The placement of −γ may seem a bit strange here, and has to with
−1
that φ0,−1 is not one of the basis functions {φ0,n }N
n=0 . However, we have that
φ0,−1 = φ0,N −1 , i.e. φ(t + 1) = φ(t − N + 1), since we always assume that the
functions we work with have period N .
e) Sketch a more general procedure than the one you found in b)., which can
be used to find wavelet bases where we have even more vanishing moments.
Solution. If we define
K
X
ψ̂ = ψ0,0 − (αk φ0,−k − βk φ0,k+1 ) ,
k=0
b) Find constants α, β so that ψ̂(t) = ψ(t)−αφ0,0 (t)−βφ0,1 (t) has two vanishing
moments, i.e. so that hψ̂, 1i = 0, and hψ̂, ti = 0. Plot also the function ψ̂.
R R R R
Hint.
R Start with R computing the integrals ψ(t)dt, tψ(t)dt, φ0,0 (t)dt, φ0,1 (t)dt,
and tφ0,0 (t)dt, tφ0,1 (t)dt.
c) Express φ and ψ̂ with the help of functions from φ1 , and use this to write
down the change of coordinate matrix from (φ0 , ψ̂0 ) to φ1 .
CHAPTER 5. MOTIVATION FOR WAVELETS AND SOME SIMPLE EXAMPLES90
a) Show that ψ̂ has k vanishing moments if and only if a0 , . . . , ak−1 solves the
equation
c0,0 c0,1 ··· c0,k−1 a0 e0
c1,0 c1,1 ··· c1,k−1
a1 e1
= (5.6)
.. .. .. .. . .
.. ..
. . . .
ck−1,0 ck−1,1 · · · ck−1,k−1 ak−1 ek−1
b) Write a function vanishingmomshaar which takes k as input, solves Equation
(5.6), and returns the vector a = (a0 , a1 , . . . , ak−1 ).
m = 1
x, fs = forw_comp_rev_DWT(m, ’Haar’)
play(x, fs)
m = 1
x, fs = forw_comp_rev_DWT(m, ’pwl0’)
play(x, fs)
m = 1
x, fs = forw_comp_rev_DWT(m, ’pwl2’)
play(x, fs)
Chapter 6
91
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 92
Solution. Assume that the kernel transformations of the DWT and the IDWT
are H and G, respectively. For the transpose of the DWT the point is that, while
its kernel transformation is H T , we compose the kernel with a permutation matrix
when we compute the DWT. When we transpose, the order of the kernel and
the permutation changes, so the transpose must use an IDWT implementation
instead. The remaining code follows similarly, since the kernel of the transpose
of the dual DWT is, G that of the transpose of the IDWT is GT , and that of
the transpose of the dual IDWT is H.
1 2λ 0 0 ··· 0 0 0
0 1 0 0
··· 0 0 0
0 λ 1 λ ··· 0 0 0
(Aλ )r = . (6.1)
.. .. .. .. .. .. ..
.. . . . . . . .
0 0 0 0 · · · λ 1 λ
0 0 0 0 ··· 0 0 1
1 0 0 0 ··· 0 0 0
λ 1 λ 0
· · · 0 0 0
0 0 1 0 · · · 0 0 0
(Bλ )r = . . . . .. . (6.2)
.. .. ..
.. .. .. .. . . . .
0 0 0 0 · · · 0 1 0
0 0 0 0 · · · 0 2λ 1
S1 S2
S= ∈ R2N −2 × R2N −2
S3 S4
a symmetric filter, with S1 ∈ RN × RN , S2 ∈ RN × RN −2 , we have that
(S2 )f
Sr = S1 + 0 0 .
Use the proof of Theorem 4.9 in the book as a guide.
Solution. We compute
x0
..
.
x0 xN −2 x0 x1
xN −2
. . . f .
Sr x = S1 S2 xN −1 = S1 .. + S2 .. = S1 .. + (S2 ) ..
xN −2
xN −1 x1 xN −1 xN −2
.
..
x1
x0 x0
= S1 ... + 0 (S2 )f 0 ... = (S1 + 0 (S2 )f 0 )x,
xN −1 xN −1
so that
(S2 )f
Sr = S1 + 0 0 .
b) Show that
( N −2 )
1
e0 , √ (ei + e2N −2−i ) , eN −1 (6.3)
2 n=1
b with x ∈ R2N −2 a
is an orthonormal basis for the vectors on the form x
symmetric extension.
Solution. Clearly these vectors are an orthonormal basis for the set of vectors
where zn = z2N −2−n . The vectors from a) are obtained by multiplying these
with e−πin . But the orthonormality of these vectors are not affected when we
multiply with e−πin , so we may skip this.
c) Show that
1 0
√ cos 2π k
2N − 2 2N − 2
N −2
1 n
√ cos 2π k
N −1 2N − 2 n=1
1 N −1
√ cos 2π k (6.4)
2N − 2 2N − 2
Solution. We compute the IDFT for all vectors in b). Since the IDFT is
unitary, this will give us an orthonormal basis for the symmetric vectors in
R2N −2 . Since (FN )H φn = en we get that
H 1 0
(FN ) e0 = φ0 = √ cos 2π k
2N − 2 2N − 2
1 1
(FN )H √ (en + e2N −2−n ) = √ (φn + φ2N −2−n )
2 2
1 1
=√ √ e2πikn/(2N −2) + e−2πikn/(2N −2)
2 2N − 2
1 n
=√ cos 2π k
N −1 2N − 2
1 N −1
(FN )H eN −1 = φN −1 = √ cos 2π k .
2N − 2 2N − 2
These coincide with the vectors listed in the exercise.
d) Assume that S is symmetric. Show that the vectors listed in (6.4) are
eigenvectors for Sr , when the vectors are viewed as vectors in RN , and that they
are linearly independent. This shows that Sr is diagonalizable.
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 95
N −1
n
cos 2π k (6.5)
2N − 2 n=0
2N −3 2
if n1 = n2 ∈ {0, N − 1}
X n1 n2
cos 2π k cos 2π k = (N − 1) × 1 if n1 = n2 6∈ {0, N − 1} .
2N − 2 2N − 2
k=0
0 if n1 6= n2
(6.6)
a) Show that
1
if n1 = n2 ∈ {0, N − 1}
1
(N − 1) × if n1 = n2 6∈ {0, N − 1}
2
0 if n1 6= n2
1 n1 1 n2
= √ cos 2π · 0 √ cos 2π ·0
2 2N − 2 2 2N − 2
N −2
X n1 n2
+ cos 2π k cos 2π k
2N − 2 2N − 2
k=1
1 n1 1 n2
+ √ cos 2π (N − 1) √ cos 2π (N − 1) .
2 2N − 2 2 2N − 2
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 96
Solution. Using that cos x = cos(2π − x) we can here pair the summands k
and 2N − 2 − k to obtain
2N −3
X n1 n2
cos 2π k cos 2π k
2N − 2 2N − 2
k=0
n1 n2
= cos 2π · 0 cos 2π ·0
2N − 2 2N − 2
N −2
X n1 n2
+2 cos 2π k cos 2π k
2N − 2 2N − 2
k=1
n1 n2
+ cos 2π (N − 1) cos 2π (N − 1) .
2N − 2 2N − 2
N −2 !
1 n n 1 n
dn,N √ cos 2π · 0 , cos 2π k , √ cos 2π (N − 1) ,
2 2N − 2 2N − 2 k=1 2 2N − 2
√ √
1/ 2 0 ··· 0 0 1/ 2 0 ··· 0 0
r 0 1 ··· 0 0 0
1 ··· 0 0
2
.. .. .. ..
..
n . .. .. ..
..
cos 2π 2N −2 k ..
N −1 . . . . . . . . .
0 0 ··· 1 0√ 0 0 ··· 1 0√
0 0 ··· 0 1/ 2 0 0 ··· 0 1/ 2
is orthogonal.
−1
c) Explain from b) that cos 2π 2Nn−2 k can be written as
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 97
1/2 0 ··· 0 0 1/2 0 ··· 0 0
0 1 ··· 0 0 0 1 ··· 0 0
2
.. .. .. .. ..
. .. .. .. ..
cos 2π 2Nn−2 k ..
N −1 . . . . . . . . .
0 0 ··· 1 0 0 0 ··· 1 0
0 0 ··· 0 1/2 0 0 ··· 0 1/2
Solution. You can set for instance H0 = {1/4, 1/2, 1/4}, and H1 = {1} (when
you write down the corresponding matrix you will see that A0,1 = 1/2, A1,0 = 0,
so that the matrix is not symmetric)
1/5 1/5 1/5 0 0 0 ··· 0 1/5 1/5
−1/3 1/3 −1/3 0 0 0 ··· 0 0 0
H = 1/5
1/5 1/5 1/5 1/5 0 ··· 0 0 0
0 0 −1/3 1/3 −1/3 0 ···0 0 0
.. .. .. .. .. .. .. .. .. ..
. . . . . . . . . .
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 98
Write down the compact form for the corresponding filters H0 , H1 , and compute
and plot the frequency responses. Are the filters symmetric?
1 1
Solution. We have that H0 = 5 {1, 1, 1, 1, 1}, and H1 = 3 {−1, 1, −1}. The
frequency responses are
Solution. We have that G0 = {1/4, 1/2, 1/4}, and G1 = {1/16, −1/4, 3/8, −1/4, 1/16}.
The frequency responses are
1 iω 1 1 −iω
λG0 (ω) = e + + e
4 2 4
1 1
= cos(ω) +
2 2
1 2iω 1 iω 3 1 −iω 1 −2iω
λG1 (ω) = e − e + − e e
16 4 8 4 16
1 1 3
= cos(2ω) − cos ω + .
8 2 8
Both filters are symmetric.
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 99
1 iω 1 1 −iω 2 1
λG0 (ω) = e + + e = cos ω +
3 3 3 3 3
1 2iω 1 iω 1 1 −iω 1 −2iω
λG1 (ω) = e − e + − e e
5 5 5 5 5
2 2 1
= cos(2ω) − cos ω +
5 5 5
The two first columns in Pφ1 ←C1 are
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 100
1/3 −1/5
1/3 1/5
0
−1/5
0 1/5
0 0
.. ..
. .
0 0
1/3 1/5
The remaining columns are obtained by translating these in alternating order.
c = (x[0::2] + x[1::2])/sqrt(2)
w = (x[0::2] - x[1::2])/sqrt(2)
newx = concatenate([c, w])
newx /= abs(newx).max()
play(newx,44100)
a) Comment the code and explain what happens. Which wavelet is used? What
do the vectors c and w represent? Describe the sound you believe you will hear.
b) Assume that we add lines in the code above which sets the elements in the
vector w to 0 before we compute the inverse operation. What will you hear if
you play the new sound you then get?
g1 = array([-coeffs[0],-coeffs[1],-coeffs[3],0,0,0,0,-coeffs[2],\
1, 0, 0, 0, 0, 0, 0, 0])
IDWTImpl(g1, 1, ’pwl0’, ’per’)
g1 = hstack([g1[13:16],g1[0:6]]) # Compact filter notation
Note that we have used a kernel which does not make symmetric extensions.
b) Plot the frequency response of G1 .
omega = linspace(0,2*pi,100)
plt.plot(omega, g1[4] + g1[5]*2*cos(omega) + g1[6]*2*cos(2*omega) \
+ g1[7]*2*cos(3*omega) + g1[8]*2*cos(4*omega))
x0 = x.copy(); x0[1::2] = 0
x1 = x.copy(); x1[::2] = 0
filterS(f0, x0, bd_mode)
filterS(f1, x1, bd_mode)
x[:] = x0 + x1
With the functions defined in b) you can now define standard DWT and
IDWT kernels in the following way, once the filters are known.
def mp3forwardfbt(x):
N = len(x)
z = mat(zeros((N,1)))
C = mp3ctable() # The analysis window;
x = x[(N-1)::(-1)]
x = concatenate([x, zeros(512 - 32)])
# The 32x64 matrix M
yvec = arange(0, 32, 1, float)
yvec = yvec.reshape((32, 1))
xvec = arange(-16, 48, 1, float)
xvec = xvec.reshape((1, 64))
M = cos((2*yvec+1)*xvec*pi/64)
start = len(x) - 512;
for n in range(1, N/32 + 1):
X = x[start:(start + 512)]
Z = C*X # Pointwise multiplication
Y = zeros(64)
for j in range(8):
Y += Z[(64*j):(64*(j+1))]
Y = Y.reshape((64, 1))
z[((n-1)*32):(n*32), 0] = mat(M)*mat(Y)
start -= 32
z = array(z).flatten()
return z
def mp3reversefbt(z):
N = len(z)
z = z. reshape((N,1))
z = mat(z)
Ns = N/32
x = zeros(32*Ns)
D = mp3dtable() # The reconstruction window.
V = mat(zeros((1024,1)))
# The 64x32 matrix N
yvec = arange(16, 80, 1, float)
yvec = yvec.reshape((64, 1))
xvec = arange(0, 32, 1, float)
xvec = xvec.reshape((1, 32))
Nmatr = mat(cos(yvec*(2*xvec + 1)*pi/64))
U = zeros(512)
for n in range(1, Ns+1):
V[64:1024, 0] = V[0:(1024-64), 0]
V[0:64, 0] = Nmatr*z[((n-1)*32):(n*32), 0]
for i in range(8):
U[(i*64):(i*64 + 32)] = \
array(V[(i*128):(i*128 + 32), 0]).flatten()
U[(i*64 + 32):((i + 1)*64)] = \
array(V[(i*128 + 96):((i+1)*128), 0]).flatten()
CHAPTER 6. THE FILTER REPRESENTATION OF WAVELETS 105
W = U*D
for i in range(16):
x[((n-1)*32):(n*32)] += W[32*i:(32*(i + 1))]
return x
Chapter 7
Constructing interesting
wavelets
Solution. Assuming that the vector coeffs has been set as in Exercise 6, the
code can look as follows
m = 10
t = linspace(-2, 6, 8*2**m)
coordsvm = hstack([[-coeffs[0], -coeffs[1], -coeffs[3], 0, 0, 0, 0,\
-coeffs[2], 1, 0, 0, 0, 0, 0, 0, 0], \
zeros(8*2**m-16)])
IDWTImpl(coordsvm, m, ’pwl0’, ’per’)
coordsvm *= 2**(m/2.)
plt.plot(t,hstack([coordsvm[(6*2**m):(8*2**m+1)], coordsvm[0:(6*2**m)]]))
106
CHAPTER 7. CONSTRUCTING INTERESTING WAVELETS 107
N1 /2 2
1 1 1 1
λH0 (ω) = (1 + cos ω) (1 − cos ω) =
Q1 (1 + cos ω) Q(4) (1 − cos ω)
2 2 2 2
N2 /2 2
1 1 1
λG0 (ω) = (1 + cos ω) Q2 (1 − cos ω) = (1 + cos ω)
2 2 2
2
1 1 1 1 2iω
= 1 + eiω + e−iω = (e + 4eiω + 6 + 4e−iω + e−2iω ).
4 2 2 16
1
Therefore G0 = 16 {1, 4, 6, 4, 1}. We do not recommend to compute H0 by hand.
With the package sympy in Python you can do as follows to compute H0 .
x = Symbol(’x’)
z = expand( ((1+x/2+1/(2*x))/2)**2* \
(2+8*((1-x/2-1/(2*x))/2)+20*((1-x/2-1/(2*x))/2)**2\
+40*((1-x/2-1/(2*x))/2)**3) )
Here we have substituted x for eiω , 1/x for e−iω . The first part represents
1
2 (4) 2 3
2 (1 + cos ω) , the second part represents
Q (u) = 2 + 8u + 20u + 40u with
1 1 1 iω 1 −iω
u = 2 (1 − cos ω) = 2 1 − 2 e − 2 e . This gives
1
H0 = {−5, 20, −1, −96, 70, 280, 70, −96, −1, 20, −5}.
128
Using Theorem 6.15 in the book with α = 1, d = 0, we get
1
H1 = {1, −4, 6, −4, 1}
16
1
G1 = {5, 20, 1, −96, −70, 280, −70, −96, 1, 20, 5}
128
The polyphase
representation and wavelets
109
CHAPTER 8. THE POLYPHASE REPRESENTATION AND WAVELETS110
Solution. The first column in the matrix on the left hand side gives the filter
G0 . On the right hand side, a) states that the even-indexed columns are taken
from the filter with frequency response
This shows that λG0 (ω) = αe2idω λH1 (ω + π). We obtain Equation (8.2) in the
book easily from this. Now, the second column in the matrix on the left hand
side gives the filter coefficients of G1 . On the right hand side, a) states that the
odd-indexed columns are taken from the filter with frequency response
This shows that λG1 (ω) = αe2idω λH0 (ω + π), which is Equation (8.3) in the
book.
a)
I 0
G
S I
is an MRA matrix with filters G̃0 , G1 , where
b)
I S
G
0 I
is an MRA matrix with filters G0 , G̃1 , where
c)
I 0
H
S I
is an MRA-matrix with filters H0 , H̃1 , where
I ST
HT
0 I
Since H T has filters (H0 )T and (H1 )T in the columns, from b) it follows that
I ST
HT
0 I
has columns given by (H0 )T and the filter with frequency response
λ(H1 )T (ω) + λS T (2ω)eiω λ(H0 )T (ω) = λH1 (ω) + λS (2ω)e−iω λH0 (ω),
so that
I 0
H
S I
has row filters H0 and a filter H̃1 with frequency response
λ(H0 )T (ω) + λS T (2ω)e−iω λ(H1 )T (ω) = λH0 (ω) + λS (2ω)eiω λH1 (ω),
A −B
H=G=
B A
CHAPTER 8. THE POLYPHASE REPRESENTATION AND WAVELETS114
T
AT BT −B T AT BT
A
H= G= = .
−B A B AT −B A
T T
BT BT AT BT
A A
H= G= = .
B −A B −AT B −A
It is straightforward to check that also these satisfy the alias cancellation con-
dition, and that the perfect reconstruction condition also here takes the form
|λH0 (ω)|2 + |λH0 (ω + π)|2 = 2.
a) Write the DWT and IDWT kernel transformations for the Spline 5/3 wavelet,
using the lifting factorization obtained in Example 8.2.2 in the book. Call your
functions dwt_kernel_53 and idwt_kernel_53.
b) Write the DWT and IDWT kernel transformations for the CDF 9/7 wavelet,
using the lifting factorization obtained in Example 8.2.3 in the book. Call your
functions DWTKernel97 and IDWTKernel97.
Solution. The following code can be used for listening to the low-resolution
approximations for a given value of m.
m = 1
x, fs = forw_comp_rev_DWT(m, ’cdf53’)
play(x, fs)
m = 1
x, fs = forw_comp_rev_DWT(m, ’cdf97’)
play(x, fs)
d) Plot all scaling functions and mother wavelets for the Spline 5/3 and the CDF
9/7 wavelets, using the cascade algorithm and the kernels you have implemented.
plotwaveletfunctions(IDWTKernel53, -4, 4)
plotwaveletfunctions(IDWTKernel97, -4, 4)
In the plot for the CDF 9/7 wavelet, it is seen that the functions and their dual
counterparts are close to being equal. This reflects the fact that this wavelet is
close to being orthogonal.
Solution. The following code can be used for listening to the low-resolution
approximations for a given value of m.
x, fs = forw_comp_rev_DWT(m, ’db2’)
play(x, fs)
CHAPTER 8. THE POLYPHASE REPRESENTATION AND WAVELETS118
x, fs = forw_comp_rev_DWT(m, ’db3’)
play(x, fs)
x, fs = forw_comp_rev_DWT(m, ’db4’)
play(x, fs)
d) Plot all scaling functions and mother wavelets for the orthonormal wavelets for
N = 1, 2, 3, 4, using the cascade algorithm. Since the wavelets are orthonormal,
we should have that φ = φ̃, and ψ = ψ̃. In other words, you should see that the
bottom plots equal the upper plots.
liftingfactortho(2)
plotwaveletfunctions(IDWTKernelOrtho, -4, 4)
liftingfactortho(3)
plotwaveletfunctions(IDWTKernelOrtho, -4, 4)
liftingfactortho(4)
plotwaveletfunctions(IDWTKernelOrtho, -4, 4)
PD1 ←(φ1 ,ψ̂1 ) = PD1 ←(φ1 ,ψ1 ) P(φ1 ,ψ1 )←(φ1 ,ψ̂1 ) .
By inversion, find also a lifting factorization of H.
where S2 = {−γ, −α, −β, −δ} This gives as before the lifting factorization
1 I 0 I {−γ, −α, −β, −δ}
PD1 ←(φ1 ,ψ̂1 ) = √ . (8.2)
2 S1 I 0 I
where S1 = {1/2, 1/2} as before.
1
H0 = {−5, 20, −1, −96, 70, 280, 70, −96, −1, 20, −5}
128
1
H1 = {1, −4, 6, −4, 1}
16
1
G0 = {1, 4, 6, 4, 1}
16
1
G1 = {5, 20, 1, −96, −70, 280, −70, −96, 1, 20, 5}.
128
a) Show that
1
I − 14 {1, 1}
1
I − 128 {5, −29, −29, 5} I 0 0
G= 4 .
0 I −{1, 1} I 0 I 0 4
From this we can easily derive the lifting factorization of G.
1 1 1 1
I − 14 {1, 1}
16 {1, 6, 1} 128 {5, 1, −70, −70, 1, 5} = 16 {4} 128 {20, −116, −116, 20} .
1 1 1 1
0 I 16 {4, 4} 128 {20, −96, 280, −96, 20} 16 {4, 4} 128 {20, −96, 280, −96, 20}
1 1
1 1
I 0 16 {4} 128 {20, −116, −116, 20} = 4 128 {20, −116, −116, 20}
1 1
−{1, 1} I 16 {4, 4} 128 {20, −96, 280, −96, 20} 0 4
Since
1
1 1
1
I − 512 {20, −116, −116, 20} 4 128 {20, −116, −116, 20} 4 0
= ,
0 I 0 4 0 4
CHAPTER 8. THE POLYPHASE REPRESENTATION AND WAVELETS120
it follows that
1
I − 14 {1, 1}
1
I − 128 {5, −29, −29, 5} I 0 0
G= 4 .
0 I −{1, 1} I 0 I 0 4
b) Implement the kernels of the wavelet of this exercise using what you did in
Exercise 6.
H0 = array([-5, 20, -1, -96, 70, 280, 70, -96, -1, 20, -5])/128.
H1 = array([1, -4, 6, -4, 1])/16.
G0 = array([1, 4, 6, 4, 1])/16.
G1 = array([5, 20, 1, -96, -70, 280, -70, -96, 1, 20, 5])/128.
f =lambda x, symm, dual: DWTKernelFilters(H0,H1,G0,G1,x,symm,dual)
invf=lambda x, symm, dual: IDWTKernelFilters(H0,H1,G0,G1,x,symm,dual)
Solution.
c) Listen to the low-resolution approximations and detail components in sound
for this wavelet.
Solution. The following code can be used for listening to the low-resolution
approximations for a given value of m.
d) Plot all scaling functions and mother wavelets for this wavelet, using the
cascade algorithm.
plotwaveletfunctions(invf, -4, 4)
x = arange(1,8193)
mp3reversefbt(mp3forwardfbt(x))
plt.plot(x)
There are some small errors from the original vector in the resulting vector, when
one compensates for the delay of 481 elements.
CHAPTER 8. THE POLYPHASE REPRESENTATION AND WAVELETS121
T
V (1) V (2) (W (1) )T (W (2) )T
(1)
−W (3)
(1)
V (2)
W V
VW = =
−V (3) V (4) W (2) W (4) −V (3) V (4) −(W (3) )T (W (4) )T
T
V (1) V (2) E15 (W (1) )T E15 (W (2) )T
E15 0
= = I.
−V (3) V (4) −E15 (W (3) )T E15 (W (4) )T 0 E15
Now, the matrices U (i) = E15 (W (i) )T are on the form stated in Equation (8.20)
in the book, and we have that
(1)
V (2)
(1)
U (2)
V U E−15 0
=
−V (3) V (4) −U (3) U (4) 0 E−15
We can now conclude from Theorem 8.13 in the book that if we define the
synthesis prototype filter as therein, and set c = 1, d = −15, we have that
GH = 16E481−32·15 = 16E1 .
Chapter 9
Digital images
def contrastadjust0(X,n):
"""
Assumes that the values are in [0,255]
"""
X /= 255.
X -= 1/2.
X *= n
arctan(X, X)
X /= (2*arctan(n/2.))
X += 1/2.0
X *= 255 # Maps the values back to [0,255]
b) Generate the left and right images in Figure 9.9 in the book on your own by
writing code which uses the two functions contrastadjust0 and contrastadjust.
123
CHAPTER 9. DIGITAL IMAGES 124
X1 = img.copy()
contrastadjust0(X1, 10)
imshow(uint8(X1))
X2 = img.copy()
contrastadjust(X2, 0.01)
imshow(uint8(X2))
fn (x) = xn ,
for all n maps the interval [0, 1] → [0, 1], and that f 0 (1) → ∞ as n → ∞.
b) The color image secret.jpg,shown in Figure 9.1, contains some information
that is nearly invisible to the naked eye on most computer monitors. Use the
function f (x), to reveal the secret message.
Hint. You will first need to convert the image to a greyscale image. You can
then use the function contrastadjust as a starting point for your own program.
CHAPTER 9. DIGITAL IMAGES 125
n = 100
X = double(imread(’images/secret.jpg’, ’jpg’))
X = (X[:,:,0] + X[:,:,1]+ X[:,:,2])/3.
mapto01(X)
X **= n
X *= 255
X = uint8(X)
imshow(X)
X3 = excerpt.copy()
def longmolecule(x):
filterS(S2, x, True)
tensor_impl(X3, longmolecule, longmolecule)
imshow(uint8(X3))
X3 = excerpt.copy()
tensor_impl(X3, donothing, diffxmolecule)
mapto01(X3)
X3 *= 255
contrastadjust0(X3,50)
imshow(uint8(X3))
excerpt=createexcerpt()
X1 = excerpt.copy()
tensor_impl(X1, donothing, diffxmolecule)
X2 = excerpt.copy()
tensor_impl(X2, diffymolecule, donothing)
X1 = X1**2 + X2**2
imshow(uint8(X1))
X2[:] = X1
mapto01(X2)
X2 *= 255
imshow(uint8(X2))
X3[:] = X2
contrastadjust0(X3, 50)
imshow(uint8(X3))
excerpt=createexcerpt()
X1 = excerpt.copy()
tensor_impl(X1, donothing, diffxmolecule)
mapto01(X1)
X1 *= 255
contrastadjust0(X1, 50)
imshow(uint8(X1))
X2 = excerpt.copy()
tensor_impl(X2, diffymolecule, donothing)
mapto01(X2)
X2 *= 255
contrastadjust0(X2, 50)
imshow(uint8(X2))
excerpt=createexcerpt()
X1 = excerpt.copy()
tensor_impl(X1, donothing, diffxmolecule)
tensor_impl(X1, donothing, diffxmolecule)
mapto01(X1)
X1 *= 255
contrastadjust0(X1, 100)
imshow(uint8(X1))
X2 = excerpt.copy()
tensor_impl(X2, donothing, diffxmolecule)
tensor_impl(X2, diffymolecule, donothing)
mapto01(X2)
X2 *= 255
contrastadjust0(X2, 100)
imshow(uint8(X2))
X3 = excerpt.copy()
tensor_impl(X3, diffymolecule, donothing)
tensor_impl(X3, diffymolecule, donothing)
mapto01(X3)
X3 *= 255
contrastadjust0(X3, 100)
imshow(uint8(X3))
This means that also ((S ⊗ I)X)i,j = XM −1−i,j for all X, so that S ⊗ I reverses
rows, and thus is a solution to a).
b) The mapping which reverses the order of the columns in a matrix.
1 1 2 1
(1, 2, 1) ⊗ (1, 2, 1) = (1, 2, 1) ⊗ (1, 2, 1) = 2 1 2 1 = 2 4 2 .
1 1 2 1
b) Let us define x = (1, 2, 3), y = (3, 2, 1), z = (2, 2, 2), and w = (1, 4, 2).
Compute the matrix A = x ⊗ y + z ⊗ w.
1 2
A = 2 3 2 1 + 2 1 4 2
3 2
3 2 1 2 8 4 5 10 5
= 6 4 2 + 2 8 4 = 8 12 6 .
9 6 3 2 8 4 11 14 7
c) Compute (S ⊗ S)A by applying the filter S to every row and column in the
matrix the way we have learned. If the matrix A was more generally an image,
what can you say about how the new image will look?
CHAPTER 9. DIGITAL IMAGES 130
Solution. Note forst that the filter is a smoothing filter (a low-pass filter). We
know that S ⊗ I corresponds to applying S to the columns of the matrix, so that
we get the result by applying the smoothing filter to the columns of the matrix.
The result of this is that horizontal edges are smoothed. Similarly, the tensor
product I ⊗ S corresponds to applying S to the rows of the matrix, so that
vertical edges are smoothed. Finally, S ⊗ S corresponds to applying S first to the
columns of the matrix, then to the rows. The result is that both horizontal and
vertical edges are smoothed. You could also have computed the computational
molecules for S ⊗ I, I ⊗ S, and S ⊗ S, by taking the tensor product of the filter
coefficients 14 {1, 2, 1} with itself. From these molecules it is also clear that they
either work on the columns, the rows, or on both rows and columns.
b) Compute (S ⊗ S)(x ⊗ y), where x = (4, 8, 8, 4), y = (8, 4, 8, 4) (i.e. both x
and y are column vectors).
2 1 0 1 4 2+2+1 5
1 1 2 1 0 8 4 + 2 + 1 7
Sx = = =
4 0 1 2 1 8 4 + 2 + 1 7
1 0 1 2 4 2+2+1 5
2 1 0 1 8 4+1+1 6
1 1 2 1 0 4 2 + 2 + 2 6
Sy = = = .
4 0 1 2 1 8 4 + 1 + 1 6
1 0 1 2 4 2+2+2 6
From this it is clear that
5 30 30 30 30
7 42 42 42 42
(S ⊗ S)(x ⊗ y) = (Sx)(Sy)T =
7 6 6 6 6 =
.
42 42 42 42
5 30 30 30 30
for n in range(N):
X[0, n] = 0.25*X[N-1, n] + 0.5*X[0, n] + 0.25*X[1, n]
X[1:(N-1), n] = 0.25*X[0:(N-2), n] + 0.5*X[1:(N-1), n] \
+ 0.25*X[2:N, n]
X[N-1, n] = 0.25*X[N-2, n] + 0.5*X[N-1, n] + 0.25*X[0, n]
for m in range(m):
X[m, 0] = 0.25*X[m, M-1] + 0.5*X[m, 0] + 0.25*X[m, 1]
X[m, 1:(M-1)] = 0.25*X[m, 0:(M-2)] + 0.5*X[m, 1:(M-1)] \
+ 0.25*X[m, 2:M]
X[m, M-1] = 0.25*X[m, M-2] + 0.5*X[m, M-1] + 0.25*X[m, 0]
Which tensor product is applied to the image? Comment what the code does, in
particular the first and third line in the inner for-loop. What effect does the
code have on the image?
Solution. In the code the filter S = {1/4, 1/2, 1/4} is applied to the clumns
and the rows in the image. We have learned that this corresponds to applying
the tensor product S ⊗ S to the image. k=1 in the outer for-loop corresponds
to applying S on the columns, k=2 corresponds to applying S on the rows. The
first and last lines in the inner for-loop are necessary since we apply S to the
periodic extension of the image. Since S is a smoothing filter, the effect will be
that the image is smoothed vertically and horizontally.
x ⊗ y ∈ RM,N → x ⊗k y ∈ RM N
thus stacks the rows of the input matrix into one large row vector, and transposes
the result.
b) Show that (A ⊗k B)(x ⊗k y) = (Ax) ⊗k (By). We can thus use any of
the defined tensor products ⊗, ⊗k to produce the same result, i.e. we have the
commutative diagram shown in Figure 9.3, where the vertical arrows represent
stacking the rows in the matrix, and transposing, and the horizontal arrows
represent the two tensor product linear transformations we have defined. In
particular, we can compute the tensor product in terms of vectors, or in terms
of matrices, and it is clear that the Kronecker tensor product gives the matrix
of tensor product operations.
A⊗B
x⊗y / (Ax) ⊗ (By)
A⊗k B
x ⊗k y / (Ax) ⊗k (By),
(A ⊗k B)(x ⊗k y)
a11 B a12 B · · · a1M B x1 y (a11 x1 + . . . + a1M xm )By
a21 B a22 B · · · a2M B x2 y (a21 x1 + . . . + a2M xm )By
= . .. .. =
.. .. ..
..
. . . . .
ap1 B ap2 B · · · apM B xM y (ap1 x1 + . . . + apM xm )By
(Ax)1 By
(Ax)2 By
= = (Ax) ⊗k (By).
..
.
(Ax)p By
hx1 ⊗ y1 , x2 ⊗ y2 i = hx1 ⊗k y1 , x2 ⊗k y2 i.
Show that
* (x1 )0 y1 (x2 )0 y2 + M −1
hx1 ⊗ y1 , x2 ⊗ y2 i = .. .
..
X
(x1 )i (x2 )i hy1 , y2 i
, =
.
(x1 )M −1 y1 (x2 )M −1 y2 i=0
M
X −1
= hy1 , y2 i (x1 )i (x2 )i = hx1 , x2 ihy1 , y2 i.
i=0
def DFTImpl8(x):
N = shape(x)[0]
for n in range(0,N,8):
x[n:(n+8), :] = fft.fft(x[n:(n+8), :], axis=0)
def IDFTImpl8(x):
N = shape(x)[0]
for n in range(0, N, 8):
x[n:(n+8), :] = fft.ifft(x[n:(n+8), :], axis=0)
def DCTImpl8(x):
N = shape(x)[0]
for n in range(0,N,8):
x[n:(n+8), :] = dct(x[n:(n+8), :], norm=’ortho’, axis=0)
def IDCTImpl8(x):
N = shape(x)[0]
for n in range(0, N, 8):
x[n:(n+8), :] = idct(x[n:(n+8), :], norm=’ortho’, axis=0)
def DFTImplFull(x):
x[:] = fft.fft(x, axis=0)
def IDFTImplFull(x):
x[:] = fft.ifft(x, axis=0)
def DCTImplFull(x):
x[:] = dct(x, norm=’ortho’, axis=0)
def IDCTImplFull(x):
x[:] = idct(x, norm=’ortho’, axis=0)
CHAPTER 9. DIGITAL IMAGES 135
Solution. Once
threshold = 30
[M, N] = shape(X)[0:2]
for n in range(N):
FFTImpl(X[:, n], FFTKernelStandard)
for m in range(M):
FFTImpl(X[m, :], FFTKernelStandard)
X = X.*(abs(X) >= threshold)
for n in range(N):
FFTImpl(X[:, n], FFTKernelStandard, 0)
for m in range(M):
FFTImpl((X[m, :], FFTKernelStandard, 0)
Comment what the code does. Comment in particular on the meaning of the
parameter threshold, and what effect this has on the image.
Solution. In the first part of the code one makes a change of coordinates with
the DFT. More precisely, this is a change of coordinates on a tensor product,
as we have defined it. In the last part the change of coordinates is performed
the opposite way. Both these change of coordinates is performed is performed
the way we have described them, first on the rows in the matrix, then on the
columns. The parameter threshold is used to neglect DFT-coefficients which
are below a certain value. We have seen that this can give various visual artefacts
in the image, even though the main contents of the image still may be visible. If
we increase threshold, these artefacts will be more dominating since we then
neglect many DFT-coefficients.
Chapter 10
137
CHAPTER 10. USING TENSOR PRODUCTS TO APPLY WAVELETS TO IMAGES138
for n in range(N):
c = (X[0:M:2, n] + X[1:M:2, n])/sqrt(2)
w = (X[0:M:2, n] - X[1:M:2, n])/sqrt(2)
X[:, n] = concatenate([c, w])
for m in range(M):
c = (X[m, 0:N:2] + X[m, 1:N:2])/sqrt(2)
w = (X[m, 0:N:2] - X[m, 1:N:2])/sqrt(2)
X[m, :] = concatenate([c,w])
a) Comment what the code does, and explain what you will see if you display X
as an image after the code has run.
Solution. The code runs a DWT over one level, and the Haar wavelet is
used. Inside the for-loops the DWT is applied to every row and column in the
image. k=1 i the for-loop corresponds to applying the DWT to the columns,
k=2 corresponds to applying the DWT to the rows. In the upper left corner we
will see a low-resolution version of the image. In the other three corners you will
see different tyoes of detail: In the upper right corner you will see detail which
corresponds to quick vertical changes, in the lower left corner you will see detail
which corresponds to quick horizontal changes, and in the lower right corner
you will see points where quick changes both vertically and horizontally occur
simultaneously.
b) The code above has an inverse transformation, which reproduce the original
image from the transformed values which we obtained. Assume that you zero
out the values in the lower left and the upper right corner of the matrix X after
the code above has run, and that you then reproduce the image by applying this
inverse transformation. What changes can you then expect in the image?
Solution. By zeroing out the two corners you remove detail which correpond
to quick horizontal and vertical changes. But since we keep the lower right
corner, we keep detail which corresponds to simultaneous changes vertically
and horizontally. The result after the inverse transformation is that most edges
have been smoothed, but we see no smoothing effect in points where quick
changes occur both horizontally and vertically. In Example 10.3 in the book,
this corresponds to that we emphasize the gridpoints in the chess pattern, mut
that we smooth out the horizontal and vertical edges in the chess pattern.
Comment the code. Describe what will be shown in the upper left corner of
X after the code has run. Do the same for the lower left corner of the matrix.
What is the connection with the images (G0 ⊗ G0 )X, (G0 ⊗ G1 )X, (G1 ⊗ G0 )X,
and (G1 ⊗ G1 )X?
Figure 10.1: A simple image before and after one level of the DWT2. The Haar
wavelet was used.
In Figure 10.1 we have applied the DWT2 with the Haar wavelet to an image
very similar to the one you see in Figure 10.6 in the book. You see here, however,
that there seems to be no detail components, which is very different from what
you saw in Example 10.3 in the book, even though the images are very similar.
Attempt to explain what causes this to happen.
CHAPTER 10. USING TENSOR PRODUCTS TO APPLY WAVELETS TO IMAGES140
Solution. In Example 10.3 in the book, the borders in the chess pattern was
chosen so that they occur at odd numbers. This means that the image can not
be represented exactly in Vm−1 ⊗ Vm−1 , so that there is detail present in the
image at all the borders in the chess pattern. In the new image, the borders in
the chess pattern was chosen so that they occur at even numbers. This means
that the image can be represented exactly in Vm−1 ⊗ Vm−1 , so that there is no
detail components present in the image.
Solution. The following code generates the different parts of Figure 10.3 in
the book.
img=double(imread(’images/fingerprint_500x500.png’))
imgnew=255*ones((512,512,3))
imgnew[0:500,0:500,:]=img
img=imgnew
l1, l2, l3 = 512, 512, 3
X0 = img.copy()
DWT2Impl(X0, 1, ’cdf97’)
X1 = X0.copy()
mapto01(X0)
X0 *= 255
imshow(uint8(X0))
DWT2Impl(Z[:32,:32,:],1,’cdf97’)
Y0 = Z.copy()
mapto01(Z)
Z *= 255
Z = uint8(Z)
imshow(Z)
The following code generates the parts of Figure 10.23 in the book.
Y0[16:, :, :] = 0
Y0[:16, 16:, :] = 0
IDWT2Impl(Y0, 5, ’cdf97’)
Y1 = Y0.copy()
mapto01(Y0)
Y0 *= 255
imshow(uint8(Y0))
Y1 = img.copy()
DWT2Impl(Y1, 5, ’cdf97’)
Y1[:16, :16, :] = 0
IDWT2Impl(Y1, 5, ’cdf97’)
mapto01(Y1)
Y1 *= 255
imshow(uint8(Y1))
Appendix A
142
Appendix B
143