Anda di halaman 1dari 52

Num.

Meth.
Phys.

6 Trigonometric Interpolation
Is there something else than polynomials and Taylor approximation in the world?

Idea (J. Fourier 1822): Approximation of a function not by usual polynomials but by
trigonometrical polynomials = partial sum of a Fourier series
Gradinaru

D-MATH

We call trigonometrical polynomial of degree 2m the function


Xm
T2m(t) := t 7 j e2ijt , j C, t R .
j=m

6.0

p. 306
Remark 6.0.1. T2m : R C is periodic of period 1. Moreover,
Num.
Meth.
if j = j for all j = 0, . . . , 2m, then T2m(t) takes only real values and may be written as Phys.

a0 X2m 
T2m(t) = + a cos(2jt) + bj sin(2jt)
2 j=1 j
with a0 = 20 and aj = 2 Re j , bj = 2 Im j for all j = 1, . . . , 2m.

Remark 6.0.2.
The functions wj (t) = e2ijt are orthogonal with respect to the L2(]0, 1[) scalar product.

Let us take as granted (or known from lectures in Analysis, Mathematcial Methods of Physics, etc.):
' $

Gradinaru
Theorem 6.0.1 (L2-convergence of the Fourier series). Every squared integrable function f D-MATH

L2(]0, 1[) := {f :]0, 1[7 C: kf kL2(]0,1[) < } is the L2(]0, 1[)-limit of its Fourier series
X
f (t) = fb(k) e2ikt in L2(]0, 1[) ,
k=
with Fourier coefficients defined by
Z 1
fb(k) = f (t) e2ikt dt , k Z .
0
& %

Remark 6.0.3. In view of this theorem, we may think of one function in two ways: once in the time (or 6.0

space) domain t f (t) and once in the frequency domain k fb(k). p. 307
' $

Num.
X Meth.

|fb(k)|2 = kf k2L2(]0,1[)
Phys.
Isometry property (Parseval): (6.0.1)
k=
& %

Remark 6.0.4. A real function f may be equally represented as


a 0 X 
+ a cos(2jt) + bj sin(2jt) in L2(]0, 1[) ,
2 j=1 j
with
Z 1
aj = 2 f (t) cos(2jt) dt , j 0 ,
Z0 1
bj = 2 f (t) sin(2jt) dt , j 1 . Gradinaru
0 D-MATH

' $

Lemma 6.0.2 (Derivative and Fourier coefficients).


f L1(]0, 1[) & f L1(]0, 1[) fb(k) = 2ik f(k)
b , k Z.
& %
Remark 6.0.5.
X
(n) 2
f 2 = (2)2n k 2n|fb(k)|2 . (6.0.2) 6.0
L ([0,1]) k=
p. 308
Z 1
d
If |f (n)(k)| |f (n)(t)| dt < fb(k) = O(k n) for |k| . Num.
Meth.
Phys.
0

The smoothness of a function directly reflects in the quick decay of its Fourier coefficients.

Example 6.0.6. The Fourier series associated with the characteristic function of an interval [a, b]
]0.1[ may be computed analytically as

1 X ikc sin kd i2kt
ba+ e e , t [0, 1] ,
k
|k|=1
with c = (a + b) and d = (b a). Gradinaru

Note the slow decay of the Fourier coefficients, and hence expect slow convergence of the series. D-MATH

Moreover, observe in the pictures below the Gibbs phenomenon: the ripples move closer to the
discontinuities and increase with larger n. Explanation: we have L2-convergence but no uniforme
convergence of the series!

6.0

p. 309
n = 10 n = 70 Num.
Meth.


2.0 10

Phys.
8


1.5


6


1.0 4

0.5

0.0 2

0.5

1.0 8
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

Gradinaru

Remark 6.0.7. Usually one cannot compute analytically fb(k) or one has to rely only on discrete D-MATH

for = 0, 1, . . . , N ; the trapezoidal rule gives


values at nodes x = N

N
X 1
1
f(k) f (x)e2ikx =: fN (k) (6.0.3)
N
=0

6.1

p. 310
6.1 Discrete Fourier Transform (DFT) Num.
Meth.
Phys.

Let n N fixed and denote the nth root of unity n := exp(2i/n) = cos(2/n) i sin(2/n)
n/2
hence nk = nk+n k Z , nn = 1 , n = 1 , (6.1.1)
n1
(
X kj n , if j = 0 ,
n = (6.1.2)
0 , if j 6= 0 .
k=0
A change of basis in Cn:

standard basis of Kn Trigonometrical Basis


0
0

1 0 0 0
n0 n0 n n
Gradinaru



n2 n1


0 1 .
.
. .
.
.

.
.
. 1
n n n

D-MATH


..

2(n2) 2(n1)

. 0 .
.
. .
.
.
. . n n
...
.. 0 .. .
..

. .
. . .


.
. .
. 1 0

.
. .
. .
.
. .
.


0

0


0 0 1 n n1
n (n1)(n2) (n1) 2
n n

6.1

p. 311
Matrix of change of basis trigonometrical basis standard basis: Fourier-matrix
Num.
Meth.

n0 n0 n0
Phys.

0 1 nn1
n n  n1
0 2 n2n2 ij
Fn = n n = n Cn,n . (6.1.3)
... ... ... i,j=0

(n1)2
n0 nn1 n

1.8 1.4
Realteil
1.6 Imaginaerteil 1.2 Realteil
Imaginaerteil
1.4 1

Gradinaru
1.2 0.8
D-MATH

1 0.6

0.8 0.4

0.6 0.2

0.4 0

0.2 0.2

0.4
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Koeffizientenindex Koeffizientenindex

6.1
Vector in standardbasis Vector in trigonometrical basis
p. 312
Num.
Definition 6.1.1 (Diskrete Fourier transform). We call linear map Fn : Cn 7 Cn, Fn(y) := Meth.
Phys.

Fny, y Cn , discrete Fourier transform (DFT), i.e. for c := Fn(y)


n1
X kj
ck = yj n , k = 0, . . . , n 1 . (6.1.4)
j=0

Convention: in the discussion of the DFT: vektor indexes run from 0 to n 1.

' $

Lemma 6.1.2.
The scaled Fourier-matrix 1 Fn is unitary: F1 1 H 1 Gradinaru

n n = n Fn = n Fn D-MATH

& %

Remark 6.1.1. n1 F2n = I and 12 F4n = I , hence the eigenvalues of Fn are in the set {1, 1, i, i}.
n

numpy-functions: c=fft(y) y=ifft(c);


Example 6.1.2 (Frequency analysis with DFT).

Some vectors of the Fourier basis (n = 16): 6.1

p. 313
Fourierbasis vector, n=16, j=1 Fourierbasis vector, n=16, j=7 Fourierbasis vector, n=16, j=15
1 1 1
Num.
0.8 0.8 0.8 Meth.
Phys.
0.6 0.6 0.6

0.4 0.4 0.4

0.2 0.2 0.2

Value
Value

Value
0 0 0

0.2 0.2 0.2

0.4 0.4 0.4

0.6 0.6 0.6

0.8 0.8 0.8


Real part Real part Real part
Imaginary p. Imaginary p. Imaginary p.
1 1 1
0 2 4 6 8 10
Vector component k
12 14 16 18 0 2 4 6 8 10
Vector component k
12 14 16 18 0 2 4 6 8 10
Vector component k
12 14
Fig. 54
16 18

"low frequence "high frequence "low frequence

Extraction of characteristical frequencies from a distorted discrete periodical signal:


Gradinaru

D-MATH

from numpy import sin, pi, linspace, random, fft


from pylab import plot, bar, show
t = linspace(0,63,64); x = sin(2*pi*t/64)+sin(7*2*pi*t/64)
y = x + random.randn(len(t)) %distortion
c = fft.fft(y); p = abs(c)**2/64
plot(t,y,-+); show()
bar(t[:32],p[:32]); show()

6.1

p. 314
3
20 Num.
Meth.
18 Phys.
2

16

1 14

12
Signal

2
|c |
k
0 10

1
6

4
2

0
3 0 5 10 15 20 25 30
0 10 20 30 40 50 60 70
Fig. 55 Coefficient index k Fig. 56
Sampling points (time)
3 Gradinaru

D-MATH

6.2 Fast Fourier Transform (FFT)

At first glance (at (6.1.4)): DFT in Cn seems to require asymptotic computational effort of O(n2)
(matrixvector multiplication with dense matrix). 6.2

p. 315
Example 6.2.1 (Efficiency of fft).
Num.
Meth.
Phys.

tic-toc-timing: compare fft, loop based implementation, and direct matrix multiplication

Code 6.2.2: timing of different implementations of DFT


1 from numpy import zeros , random , exp , p i , meshgrid , r_ , dot , fft
2 import t i m e i t
3
4 def naiveFT ( ) :
5 global y , n
6
Gradinaru
7 c = 0. 1 j y D-MATH

8 # naive
9 omega = exp(2 p i 1 j / n )
10 c [ 0 ] = y . sum ( ) ; s = omega
11 f o r j j i n xrange ( 1 , n ) :
12 c [ j j ] = y [ n 1]
13 f o r kk i n xrange ( n 2,1,1) : c [ j j ] = c [ j j ] s+y [ kk ]
14 s = omega
15 #return c
16
6.2
17 def matrixFT ( ) :
p. 316
Num.
18 global y , n Meth.
Phys.
19
20 # matrix based
21 I , J = meshgrid ( r _ [ : n ] , r _ [ : n ] )
22 F = exp(2 p i 1 j I J / n )
23 tm = 10 3
24 c = dot (F , y )
25 #return c
26
27 def f f t F T ( ) :
28 global y , n
29 c = f f t . f f t (y)
30 #return c Gradinaru

D-MATH
31
32 nrexp = 5
33 N = 2 11 # how large the vector will be
34 r e s = zeros ( ( N, 4 ) )
35 f o r n i n xrange ( 1 ,N+1) :
36 y = random . rand ( n )
37
38 t = t i m e i t . Timer ( naiveFT ( ) , from __main__ i m p o r t naiveFT )
39 t n = t . t i m e i t ( number=nrexp )
40 t = t i m e i t . Timer ( matrixFT ( ) , from __main__ i m p o r t matrixFT ) 6.2

p. 317
Num.
41 tm = t . t i m e i t ( number=nrexp ) Meth.
Phys.
42 t = t i m e i t . Timer ( f f t F T ( ) , from __main__ i m p o r t f f t F T )
43 t f = t . t i m e i t ( number=nrexp )
44 #print n, tn, tm, tf
45 r e s [ n 1] = [ n , tn , tm , t f ]
46
47 print res [ : , 3 ]
48 from p yl a b import semilogy , show , p l o t , s a v e f i g
49 semilogy ( r e s [ : , 0 ] , r e s [ : , 1 ] , b )
50 semilogy ( r e s [ : , 0 ] , r e s [ : , 2 ] , k )
51 semilogy ( r e s [ : , 0 ] , r e s [ : , 3 ] , r )
52 s e v e f i g ( f f t t i m e . eps )
53 show ( ) Gradinaru

D-MATH

6.2

p. 318
102
fft runtimes Num.
Meth.

naive DFT-implementation naive way Phys.

101
matrix way
c = 0.*1j*y numpy way
omega = exp(-2*pi*1j/n) 100
c[0] = y.sum(); s = omega
10-1

run time [s]


for jj in xrange(1,n):
c[jj] = y[n-1] 10-2
for kk in xrange(n-2,-1,-1):10-3 c[jj] = c[jj]*s+y[kk]
s *= omega
10-4

10-5 0 500 1000 1500 2000 2500


vector length n
Gradinaru

Incredible! The fft()-function clearly beats the O(n2) asymptotic complexity of the other imple- D-MATH

mentations. Note the logarithmic scale!

6.2

p. 319
The secret of fft(): Num.
Meth.
Phys.

the Fast Fourier Transform algorithm [15]

(discovered by C.F. Gauss in 1805, rediscovered by Cooley & Tuckey in 1965,


one of the top ten algorithms of the century).

An elementary manipulation of (6.1.4) for n = 2m, m N:


n1
X
2i jk
ck = yj e n
j=0
m1
X m1
X Gradinaru

2i 2jk 2i (2j+1)k
= y2j e n + y2j+1e n D-MATH

j=0 j=0 (6.2.1)


m1
X m1
X
2i jk 2i k 2i jk
= y2j e| {z } +e n
m y2k+1 | {z } .
e m
j=0 jk j=0 jk
=m =m
| {z } | {z }
even
ck
=:e codd
=:ek

Note m-periodicity: ceven


ek ceven
=e codd
k+m, ek =ecodd
k+m.
6.2

p. 320
ceven
Note: ek , e
c odd from DFTs of length m!
k
Num.
Meth.
Phys.

m1
with yeven := (y0, y2, . . . , yn2 )T Cm : eeven
ck k=0 = Fmyeven ,
 m1
with yodd := (y1, y3, . . . , yn1)T Cm : ecodd
k = Fmyodd .
k=0
 

(6.2.1): DFT of length 2m = 2 DFT of length m + 2m additions & multiplications


 

Gradinaru

D-MATH

6.2

p. 321
Code 6.2.3: Recursive FFT Num.
Meth.
1 from numpy i m p o r t linspace , random , Phys.

hstack , pi , exp
2 def f f t r e c ( y ) :
Idea: 3 n = len ( y )
4 i f n == 1 :
divide & conquer recursion
5 c = y
6 re t urn c
(for DFT of length n = 2L) 7 else :
8 c0 = f f t r e c ( y [ : : 2 ] )
9 c1 = f f t r e c ( y [ 1 : : 2 ] )
FFT-algorithm 10 r = ( 2. j p i / n )
linspace ( 0 , n 1,n ) Gradinaru

11 c = h sta ck ( ( c0 , c0 ) ) + D-MATH

exp ( r ) h sta ck ( ( c1 , c1 ) )
12 re t urn c

Computational cost of fftrec:

6.2

p. 322
1 DFT of length 2L
Num.

2 DFT of length 2L1


Meth.
Phys.

4 DFT of length 2L2

2L DFT of length 1

Code 6.2.2: each level of the recursion requires O(2L) elementary operations.

Asymptotic complexity of FFT algorithm, n = 2L: O(L2L) = O(n log2 n)

(fft-function: cost 5n log2 n).


Gradinaru

D-MATH

Remark 6.2.4 (FFT algorithm by matrix factorization).

For n = 2m, m N,
OE (1, . . . , n) = (1, 3, . . . , n 1, 2, 4, . . . , n) .
permutation Pm

6.2

p. 323
2j j Num.
As n = m: Meth.
Phys.



Fm Fm


OE
n

permutation of rows Pm Fn = n0 /2 =
n
n1 n/2+1

Fm ... Fm n
...


n/21
n nn1



I I
Fm Gradinaru

D-MATH

0 n0
n
n1 n1

Fm ... ...

n/21 n/21
n n

6.2

Example: factorization of Fourier matrix for n = 10 p. 324


Num.
Meth.

0 0 0 0 0 0 0 0 0 0 Phys.

0 2 4 6 8 0 2 4 6 8

0 4 8 2 6 0 4 8 2
6

0
6 2 8 4 0 6 2 8 4

0
8 6 4 2 0 8 6 4 2
P5OEF10 = 0 , := 10 .
1 2 3 4 5 6 7 8 9
0
3 6 9 2 5 8 1 4 7
0
5 0 5 0 5 0 5 0 5
0
7 4 1 8 5 2 9 6 3

0 9 8 7 6 5 4 3 2 1

Gradinaru

D-MATH

What if n 6= 2L? Quoted from M ATLAB manual:

To compute an n-point DFT when n is composite (that is, when n = pq ), the FFTW library decom-
poses the problem using the Cooley-Tukey algorithm, which first computes p transforms of size q ,
and then computes q transforms of size p. The decomposition is applied recursively to both the p-
6.2

and q -point DFTs until the problem can be solved using one of several machine-generated fixed-size p. 325
"codelets." The codelets in turn use several algorithms in combination, including a variation of Cooley- Num.
Meth.

Tukey, a prime factor algorithm, and a split-radix algorithm. The particular factorization of n is chosen Phys.

heuristically.
' $

The execution time for fft depends on the length of the transform. It is fastest for powers of two.
It is almost as fast for lengths that have only small prime factors. It is typically several times
slower for lengths that are prime or which have large prime factors Ex. 6.2.1.
& %

Remark 6.2.5 (FFT based on general factorization).

Fast Fourier transform algorithm for DFT of length n = pq , p, q N (Cooley-Tuckey-Algorithm)


n1
X p1 X
X q1 p1
X q1
X
2i
Gradinaru
jk [j=:lp+m] (lp+m)k l(k mod q)
ck = yj n = ylp+m e pq = nmk ylp+m q . D-MATH

j=0 m=0 l=0 m=0 l=0


(6.2.2)
q1
P
Step I: perform p DFTs of length q zm,k := ylp+m qlk , 0 m < p, 0 k < q .
l=0

Step II: for k =: rq + s, 0 r < p, 0 s < q


p1
X p1
X
2i
pq (rq+s)m ms  mr 6.2
crq+s = e zm,s = n zm,s p
p. 326
m=0 m=0
and hence q DFTs of length p give all ck . Num.
Meth.
Phys.

Step I Step II

p p

q q

Gradinaru

D-MATH

Remark 6.2.6 (FFT for prime n).

When n 6= 2L, even the Cooley-Tuckey algorithm of Rem. 6.2.5 will eventually lead to a DFT for a
vector with prime length.
6.2

Quoted from the M ATLAB manual: p. 327


When n is a prime number, the FFTW library first decomposes an n-point problem into three (n 1)- Num.
Meth.
Phys.

point problems using Raders algorithm [43]. It then uses the Cooley-Tukey decomposition described
above to compute the (n 1)-point DFTs.

Details of Raders algorithm: a theorem from number theory:

p N prime g {1, . . . , p 1}: {g k mod p: k = 1, . . . , p 1} = {1, . . . , p 1} ,

Gradinaru

D-MATH

permutation Pp,g : {1, . . . , p 1} 7 {1, . . . , p 1} , Pp,g (k) = g k mod p ,


reversing permutation Pk : {1, . . . , k} 7 {1, . . . , k} , Pk (i) = k i + 1 .

p p T
For Fourier matrix F = (fij )i,j=1: Pp1Pp,g (fij )i,j=2Pp,g is circulant.

6.2

Example for p = 13: p. 328


g = 2 , permutation: (2 4 8 3 6 12 11 9 5 10 7 1) . Num.
Meth.
Phys.

0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 4 8 3 6 12 11 9 5 10 7 1
0 1 2 4 8 3 6 12 11 9 5 10 7
0 7 1 2 4 8 3 6 12 11 9 5 10
0 10 7 1 2 4 8 3 6 12 11 9 5
0 5 10 7 1 2 4 8 3 6 12 11 9
F13 0 9 5 10 7 1 2 4 8 3 6 12 11
0 11 9 5 10 7 1 2 4 8 3 6 12
0 12 11 9 5 10 7 1 2 4 8 3 6
0 6 12 11 9 5 10 7 1 2 4 8 3
0 3 6 12 11 9 5 10 7 1 2 4 8
Gradinaru
0 8 3 6 12 11 9 5 10 7 1 2 4 D-MATH

0 4 8 3 6 12 11 9 5 10 7 1 2

Then apply fast (FFT based!) algorithms for multiplication with circulant matrices to right lower (n
1) (n 1) block of permuted Fourier matrix .

Asymptotic complexity of c=fft(y) for y Cn = O(n log n). 6.3

p. 329
6.3 Trigonometric Interpolation: Computational Aspects Num.
Meth.
Phys.

' $

Theorem 6.3.1 (Trigonometric interpolant). Let N even and z = FN y CN . The trigonometric


interpolant
N/2 ,
X 1 2ixN/2
 X
pN (x) := zk e2ikx := zN/2e + zN/2e2ixN/2 + zk e2ikx
2
k=N/2 |k|N/2
has the property that pN (x) = y, where x = /N .
& %
Gradinaru

D-MATH

Fast intepolation by means of FFT: O(n log n) asymptotic complexity, see Sect. 6.2, provide
k
uniformly distributed nodes tk = , k = 0, . . . , 2n
2n + 1

(2n + 1) (2n + 1) linear system of equations:

6.3

p. 330
Num.
2n
X jk  nk  Meth.
Phys.
j exp 2i = zk := exp 2i y , k = 0, . . . , 2n .
2n + 1 2n + 1 k
j=0
m
Lemma 6.1.2 1
F2n+1c = z , c = (0, . . . , 2n )T c= F2nz . (6.3.1)
2n + 1
(2n + 1) (2n + 1) (conjugate) Fourier matrix, see (6.1.3)

Code 6.3.2: Efficient computation of coefficient of trigonometric interpolation polynomial (equidistant


nodes)
from numpy import transpose , hstack , arange
from s c i p y import p i , exp , f f t Gradinaru

D-MATH

def t r i g i p e q u i d ( y ) :
" " " E f f i c i e n t co m p u ta ti o n o f c o e f f i c i e n t s i n expansion
\ e q r e f { eq : t r i g p r e a l } f o r a t r i g o n o m e t r i c
j
i n t e r p o l a t i o n p o l y n o m i a l i n e q u i d i s t a n t p o i n t s \ Blue { ( 2n+1 , yj ) } ,
\ Blue { j = 0, . . . , 2n }
\ t e x t t t { y } has t o be a row v e c t o r o f odd l e n g t h , r e t u r n va l u e s are
column v e c t o r s
"""
6.3

N = y . shape [ 0 ] p. 331
Num.
Meth.
i f N%2 ! = 1 : Phys.

r a i s e V a l u e E r r o r ( "Odd number o f p o i n t s r e q u i r e d ! " )

n = (N 1.0) / 2 . 0
z = arange (N)

# See (6.3.1)
c = f f t ( exp ( 2 . 0 j p i ( n /N) z ) y ) / ( 1 . 0 N)

# From (??): j = 12 (nj + n+j ) and


1 (
# j = 2i nj n+j ), j = 1, . . . , n, 0 = n
Gradinaru

a = h sta ck ( [ c [ n ] , c [ n 1:: 1]+c [ n + 1 :N ] ] ) D-MATH

b = h sta ck ( [ 1.0 j ( c [ n 1:: 1] c [ n + 1 :N ] ) ] )

re t urn ( a , b )

i f __name__ == " __main__ " :


from numpy import l i n s p a c e

y = linspace (0 ,1 ,9)
6.3

# Expected Result: p. 332


# a= Num.
Meth.
# Phys.

# 0.50000 - 0.00000i
# -0.12500 + 0.00000i
# -0.12500 + 0.00000i
# -0.12500 - 0.00000i
# -0.12500 + 0.00000i
#
# b=
#
# -0.343435 + 0.000000i
# -0.148969 + 0.000000i
# -0.072169 - 0.000000i
Gradinaru
# -0.022041 + 0.000000i D-MATH

w = trigipequid (y)

p r i n t (w)

Code 6.3.3: Computation of coefficients of trigonometric interpolation polynomial, general nodes


from numpy import hstack , arange , s i n , cos , p i , o u t e r
from numpy . l i n a l g import s o l v e
6.3
def t r i g p o l y c o e f f ( t , y ) :
p. 333
" " " Computes expansion c o e f f i c i e n t s o f t r i g o n o m e t r i c p o l y o n o m i a l s Num.
Meth.
\ e q r e f { eq : t r i g p r e a l } Phys.

\ t e x t t t { t } : row v e c t o r o f nodes \ Blue { t0, . . . , tn [0, 1[ }


\ t e x t t t { y } : row v e c t o r o f data \ Blue { y0, . . . , yn }
r e t u r n va l u e s are column v e c t o r s o f expansion c o e f f i c i e n t s \ Blue { j } ,
\ Blue { j }
"""

N = y . shape [ 0 ]

i f N%2 ! = 1 :
r a i s e V a l u e E r r o r ( "Odd number o f p o i n t s r e q u i r e d ! " )
Gradinaru
n = (N 1.0) / 2 . 0 D-MATH

M = h sta ck ( [ cos (2 p i o u t e r ( t , arange ( 0 , n +1) ) ) ,


s i n (2 p i o u t e r ( t , arange ( 1 , n +1) ) ) ] )
c = s o l v e (M, y )

a = c [ 0 : n+1]
b = c [ n+1:]

re t urn ( a , b ) 6.3

p. 334
i f __name__ == " __main__ " : Num.
Meth.
from numpy import l i n s p a c e Phys.

t = linspace (0 , 1 , 5)
y = linspace (0 , 5 , 5)

# Expected values:
# a=
#
# 2.5000e+00
# -1.1150e-16
# -2.5000e+00
#
Gradinaru
# b= D-MATH
#
# -2.5000e+00
# -1.0207e+16

z = trigpolycoeff ( t , y)

print ( z )

Example 6.3.4 (Runtime comparison for computation of coefficient of trigonometric interpolation poly-
6.3
nomials).
p. 335
Num.
100 Meth.
trigpolycoeff Phys.
trigipequid
10-1

10-2

runtime[s]
tic-toc-timings 
10-3

10-4

10-5 1
10 102 103
n Fig. 57

Gradinaru
Code 6.3.5: Runtime comparison
D-MATH
from numpy import l i n s p a c e , pi , exp , cos , a r r a y
from m a t p l o t l i b . p y p l o t import
import t i m e

from t r i g p o l y c o e f f import t r i g p o l y c o e f f
from t r i g i p e q u i d import t r i g i p e q u i d

def t r i g i p e q u i d t i m i n g ( ) :
" " " Runtime comparison between e f f i c i e n t ( Code ~ \ r e f { t r i g i p e q u i d } ) and d i r e c t
computation
( Code ~ \ r e f { t r i g p o l y c o e f f } o f c o e f f i c i e n t s o f t r i g o n o e t r i c i n t e r p o l a t i o n
polynomial i n 6.3

equidistant points .
p. 336
""" Num.
Meth.
Nruns = 3 Phys.

t i m es = [ ]

f o r n i n xrange ( 1 0 , 501 , 10) :


print (n )

N = 2 n+1

t = l i n s p a c e (0 ,1 1.0/N,N)
y = exp ( cos (2 p i t ) )

# Infinity for all practical purposes


t 1 = 10.0 10
t 2 = 10.0 10
Gradinaru
f o r k i n xrange ( Nruns ) : D-MATH
t i c = time . time ( )
a, b = trigpolycoeff ( t , y)
toc = time . time ( ) t i c
t 1 = min ( t1 , t o c )

t i c = time . time ( )
a, b = trigipequid (y)
toc = time . time ( ) t i c

t 2 = min ( t2 , t o c )

6.3
t i m es . append ( ( n , t1 , t 2 ) )
p. 337
Num.
Meth.
t i m es = a r r a y ( t i m es ) Phys.

fig = figure ()
ax = f i g . gca ( )

ax . l o g l o g ( t i m es [ : , 0 ] , t i m es [ : , 1 ] , " b+ " , l a b e l = " t r i g p o l y c o e f f " )


ax . l o g l o g ( t i m es [ : , 0 ] , t i m es [ : , 2 ] , " r " , l a b e l = " t r i g i p e q u i d " )

ax . s e t _ x l a b e l ( r " n " )
ax . s e t _ y l a b e l ( r " r u n t i m e [ s ] " )
ax . legend ( )

f i g . s a v e f i g ( " . . / PICTURES / t r i g i p e q u i d t i m i n g . eps " )

i f __name__ == " __main__ " : Gradinaru


trigipequidtiming () D-MATH
show ( )

Same observation as in Ex. 6.2.1: massive gain in efficiency through relying on FFT.

3
6.3

Remark 6.3.6 (Efficient evaluation of trigonometric interpolation polynomials). p. 338


Task: k , N > 2n.
evaluation of trigonometric polynomial (??) at equidistant points N k = Num.
Meth.
Phys.

0, . . . , N 1.
2n
X kj
e2i /N
k
(??) q(k/N ) = j exp(2i ) , k = 0, . . . , N 1 .
N
j=0
e2i /N v
kn
q(k/N ) = j with v = FN e
c, (6.3.2)

Fourier matrix, see (6.1.3).


c CN is obtained by zero padding of c := (0, . . . , 2n)T :
where e
(
j , for k = 0, . . . , 2n ,
(e
c)k =
0 , for k = 2n + 1, . . . , N 1 . Gradinaru

D-MATH

Code 6.3.7: Fast evaluation of trigonometric polynomial at equidistant points


from numpy import transpose , vsta ck , hstack , zeros , conj , exp , p i
from s c i p y import f f t

def t r i g i p e q u i d c o m p ( a , b , N) :
" " " E f f i c i e n t evaluation of tr i g o n o m e tr i c polynomial at e q u i d i sta n t
points 6.3
column v e c t o r s \ t e x t t t { a } and \ t e x t t t { b } pass c o e f f i c i e n t s \ Blue { j } ,
p. 339
\ Blue { j } i n Num.
Meth.
Phys.
r e p r e s e n t a t i o n \ e q r e f { eq : t r i g p r e a l }
"""

n = a . shape [ 0 ] 1

i f N < 2 n 1:
r a i s e V a l u e E r r o r ( "N t o o s m a l l " )

gamma = 0.5 h sta ck ( [ a[ 1:0: 1] + 1 . 0 j b [ 1:: 1] , 2 a [ 0 ] , a [ 1 : ]


1 . 0 j b [ 0 : ] ] )

# Zero padding Gradinaru

ch = h sta ck ( [ gamma, zeros ( ( N(2n +1) , ) ) ] ) D-MATH

# Multiplication with conjugate Fourier matrix


v = c o n j ( f f t ( c o n j ( ch ) ) )

# Undo rescaling
q = exp ( 2.0 j p i n arange (N) / ( 1 . 0 N) ) v

re t urn q
6.3

p. 340
Num.
i f __name__ == " __main__ " : Meth.
Phys.
from numpy import arange

a = arange ( 1 , 6 )
b = arange ( 6 , 1 0 )
N = 10

# Expected values:
#
# 15.00000 - 0.00000i 21.34656 + 0.00000i -5.94095 - 0.00000i
# 8.18514 + 0.00000i -3.31230 - 0.00000i 3.00000 + 0.00000i
# -1.68770 - 0.00000i -2.71300 - 0.00000i 0.94095 + 0.00000i
# -24.81869 - 0.00000i Gradinaru

D-MATH

print (a)
print (b)
p r i n t (N)

w = t r i g i p e q u i d c o m p ( a , b , N)

p r i n t (w)

6.3

Code 6.3.8: Equidistant points: fast on the fly evaluation of trigonometric interpolation polynomial p. 341
from numpy import zeros , f f t , s q r t , p i , r e a l , l i n a l g , l i n s p a c e , s i n Num.
Meth.
Phys.

def e v a l i p t r i g ( y , N) :
n = len ( y )
i f ( n%2) == 0 :
c = f f t . i f f t (y)
a = zeros (N, dtype=complex )
a [: n/2] = c [: n/2]
a [ Nn / 2 : ] = c [ n / 2 : ]
v = f f t . f f t (a) ;
re t urn v
else : r a i s e TypeError , odd l e n g t h
Gradinaru
f = lambda t : 1 . / s q r t ( 1 . + 0 . 5 s i n (2 p i t ) ) D-MATH

v l i n f = [ ] ; v l 2 = [ ] ; vn = [ ]
N = 4096; n = 2
while n < 1+2 7:
t = l i n s p a c e ( 0 , 1 , n +1)
y = f ( t [: 1])
v = r e a l ( e v a l i p t r i g ( y , N) )
t = l i n s p a c e ( 0 , 1 ,N+1)
fv = f ( t ) 6.3
d = abs ( vf v [ : 1 ] ) ; l i n f = d . max ( )
p. 342
l 2 = l i n a l g . norm ( d ) / s q r t (N) Num.
Meth.
v l i n f += [ l i n f ] ; v l 2 += [ l 2 ] ; vn += [ n ] Phys.

n = 2

from p yl a b import semilogy , show


semilogy ( vn , v l 2 , + ) ; show ( )

compare with

Code 6.3.9: Equidistant points: fast on the fly evaluation of trigonometric interpolation polynomial
from numpy import zeros , f f t , s q r t , p i , r e a l , l i n a l g , l i n s p a c e , s i n

def e v a l i p t r i g ( y , N) : Gradinaru

n = len ( y ) D-MATH

i f ( n%2) == 0 :
c = f f t . f f t (y) 1./n
a = zeros (N, dtype=complex )
a [: n/2] = c [: n/2]
a [ Nn / 2 : ] = c [ n / 2 : ]
v = f f t . i f f t ( a ) N ;
re t urn v
else : r a i s e TypeError , odd l e n g t h
6.3

f = lambda t : 1 . / s q r t ( 1 . + 0 . 5 s i n (2 p i t ) ) p. 343
Num.
Meth.
v l i n f = [ ] ; v l 2 = [ ] ; vn = [ ] Phys.

N = 4096; n = 2
while n < 1+2 7:
t = l i n s p a c e ( 0 , 1 , n +1)
y = f ( t [: 1])
v = r e a l ( e v a l i p t r i g ( y , N) )
t = l i n s p a c e ( 0 , 1 ,N+1)
fv = f ( t )
d = abs ( vf v [ : 1 ] ) ; l i n f = d . max ( )
l 2 = l i n a l g . norm ( d ) / s q r t (N)
v l i n f += [ l i n f ] ; v l 2 += [ l 2 ] ; vn += [ n ]
n = 2
Gradinaru

D-MATH
from p yl a b import semilogy , show
semilogy ( vn , v l 2 , + ) ; show ( )

6.4

p. 344
6.4 Trigonometric Interpolation: Error Estimates Num.
Meth.
Phys.

Linear trigonometric interpolation operator: for 1-periodic f : R 7 C


T j
Tn(f ) := p Pn1 with p( n ) = yj , j = 0, . . . , n 1 .

Example 6.4.1 (Trigonometric interpolation).

#1 step function: f (t) = 0 for |t 12 | > 41 , f (t) = 1 for |t 12 | 14


1
#2 smooth periodic function: f (t) = q .
1 + 12 sin(2t) Gradinaru

#3 hat function: f (t) = |t 21 |


D-MATH

Note: computation of the norms of the interpolation error:

t = linspace(0,1,n+1); y = f(t[:-1])
v = real(evaliptrig(y,N))
t = linspace(0,1,N+1); fv = f(t)
d = abs(v-fv[:-1]); linf = d.max()
6.4
l2 = linalg.norm(d)/sqrt(N)
p. 345
0 0
10 10
Num.
Meth.
2
10
2
10 Phys.

4 4
10 10

2
||Interpolationsfehler||
||Interpolationsfehler||

6
6
10 10

#1 #1
#2 8 #2
8 10
10 #3 #3

10
10 10
10

12
12 10
10

14
14
10
10

2 4 8 16 32 64 128 2 4 8 16 32 64 128
n+1 n+1
Gradinaru

Note: Cases #1, #3: algebraic convergence D-MATH

Case #2: exponential convergence

6.4

p. 346
1.2 1.2
f f Num.
p p Meth.
Phys.
1 1

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2

0 0

0.2 0.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t t Gradinaru

D-MATH
n = 16 n = 128
Gibbsphenomenon: ripples near discontinuities
3

Example 6.4.2 (Trigonometric interpolation of analytic functions).

6.4

p. 347
2
10
Num.
0
Meth.
10 Phys.

2
10

Interpolationsfehlernorm
4

1 10

f (t) = p auf I = [0, 1] . 6

1 sin(2t) 10

8 =0.5, L
10
=0.5, L2

approximation of the norms: oversampling in 10


10 =0.9, L

=0.9, L2
4096 points 12
10 =0.95, L
2
=0.95, L

=0.99, L
14
10

=0.99, L2
16
10
0 10 20 30 40 50 60 70 80 90 100
Polynomgrad n
Note: exponential convergence in degree n, quicker for smaller Gradinaru
3 D-MATH

Analysis uses: trigonometric polynomials = partial sums of Fourier series

' $

P
Theorem 6.4.1 (Error of DFT; aliasing formula). If f (k) absolut converges, then
kZ
X
fN (k) f(k) = f(k + jN )
6.4
jZ
j6=0
& %
p. 348
' $

Num.
Corollary 6.4.2. Let f C p with p 2 and 1-periodic. Then it holds: Meth.
Phys.

N
fN (k) f(k) = O(N p) for |k|
2
N
X 1 Z 1
1
for: h = , h f (xj ) f (x)dx = O(hp).
N 0
j=0
& %

Remark 6.4.3. fN (k) is N -periodic, while f(k) 0 quickly

fN (k) is a bad approximation of f(k) for k large (k N ), but for |k| N2 it is good enough.
' $

P Gradinaru

Theorem 6.4.3 (Error of trigonometric interpolantion). If f is 1-periodic and f (k) absolut D-MATH

kZ
converges, then
X,
|pN (x) f (x)| 2 |f(k)| x R
|k|N/2
& %

' $

Corollary 6.4.4 (Sampling-Theorem). Let f 1-periodic with maximum frequency M : f(k) = 0


6.4
for all |k| > M . Then pN (x) = f (x) for all x, if N > 2M
& %
p. 349
Num.
Meth.
data compression Phys.

Remark 6.4.4 (Aliasing).

n1 2i(N n)t
T = { nj }j=0 e2iN
|T
t=e
|T
Tn(e2iN ) = Tn(e2i(N mod n)) .

hence: The trigonometric interpolation of t e2iN t and t e2i(N mod n)t of degree n give the
same trigonometric interpolation polynomial ! Aliasing

Example for f (t) = sin(2 19t) N = 38:


Gradinaru

D-MATH
1 1 1
p p p
f f f
0.8 0.8 0.8

0.6 0.6 0.6

0.4 0.4 0.4

0.2 0.2 0.2

0 0 0

0.2 0.2 0.2

0.4 0.4 0.4

0.6 0.6 0.6

0.8 0.8 0.8

1 1 1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t t t
6.4

n=4 n=8 n = 16 p. 350


[Linearity of trigonometric interpolation]
Num.
Meth.
Phys.

For f C([0, 1]) 1-periodic, n = 2m:



X m
X
X
f (t) = fb(j)e2ijt Tn(f )(t) = j e2ijt , j = fb(j + ln) .
j= j=m+1 l=
Fourier coefficients of the error function e = f Tn(f ):

P b
f (j + ln) , if m + 1 j m ,
eb(j) = |l|=1

fb(j) , if j m j > m . Gradinaru

D-MATH

X 2 X
(6.0.1) 2
kf Tn(f )kL2(]0,1[) fb(j + ln) + |fb(j)|2 . (6.4.1)
|l|=1 |j|m

may be estimated, if we know the decay of the Fourier coefficients fb(j) smoothness
of f , see (6.0.2)

6.4

p. 351
' $

Num.
Theorem 6.4.5 (Error estimate for trigonometric interpolation). For k N: Meth.
Phys.

p

f (k) L2(]0, 1[) kf Tnf kL2(]0,1[) 1 + ck nk f (k) 2 ,
L (]0,1[)
P 2k .
with ck = 2 l=1 (2l 1)
& %

6.5 DFT and Chebychev Interpolation

Gradinaru

Let p Pn be the Chebychev interpolation polynom on [1, 1] of f : [1, 1] 7 C ( section 5.4) D-MATH

 
2k + 1
p(tk ) = f (tk ) for Chebychev nodes, see (5.4.5), tk := cos , k = 0, . . . , n .
2(n + 1)
Define now the corresponding functions g, q :

f : [1, 1] 7 C g(s) := f (cos 2s) , 1
1-periodic, symmetric wrt. 0 and
p : [1, 1] 7 C q(s) := p(cos 2s) , 2
Hence:
   
2k + 1 2k + 1 6.5
p(tk ) = f (tk ) q =g . (6.5.1)
4(n + 1) 4(n + 1) p. 352
1
and with a translation s = s + 4(n+1) we have: Num.
Meth.
Phys.

x = 1/2

0 1

Gradinaru

D-MATH

1
ge(s) := g(s + ),    
4(n + 1) k k
qe =e
g , k = 0, . . . , 2n .
1 2(n + 1) 2n + 2
qe(s) := q(s + )
4(n + 1)

! We show: q(s) is trigonometric polynomial: T , namely the trigonometric interpola-


q P2n
tion polynomial of e
g . Indeed, since p is the Chebychev interpolation polynomial:
6.5

p. 353
Num.
n
X n
X n
X Meth.
Phys.

q(s) = p(cos 2s) = j Tj (cos 2s) = j cos 2s = 0 + 1 e2ijs =


2 |j|
j=0 j=0 j=n,j6=0
n   n
X j
2i 4(n+1) 1
2ij s+ 4(n+1) X
0 + 1 e
2 |j| e = j e2ij s
j=n,j6=0 j=n
(  
1 exp j
2 2i 4(n+1) |j| , if j 6= 0 ,
with ej :=

0 , for j = 0 .
Hence
n
X
q(s) = j e2ijs Gradinaru

D-MATH
j=n
   
k k
and q = g , for all k = 0, . . . , 2n .
2(n + 1) 2(n + 1)
that means that q is the trigonometric interpolation polynomial of g .

Hence kf pkL([1,1]) = ke
g qekL([0,1])

and the error estimates from the trigonometric interpolation directly transfers here.
6.5

p. 354
Code 6.5.1: Efficient Chebyshev interpolation
Num.
1 from numpy import exp , p i , r e a l , hstack , arange Meth.
Phys.

2 from s c i p y import i f f t
3
4 def chebexp ( y ) :
5 " " " E f f i c i e n t l y compute c o e f f i c i e n t s \ Blue { j } i n t h e Chebychev
expansion
n
P
6 \ Blue { p = j Tj } o f \ Blue { p Pn } based on va l u e s \ Blue { yk } ,
j=0
7 \ Blue { k = 0, . . . , n } , i n Chebychev nodes \ Blue { tk } , \ Blue { k = 0, . . . , n } .
These va l u e s are
8 passed i n t h e row v e c t o r \ t e x t t t { y } .
9 """ Gradinaru
10 D-MATH

11 # degree of polynomial
12 n = y . shape [ 0 ] 1
13
14 # create vector z by wrapping and componentwise scaling
15
16 # r.h.s. vector
17 t = arange ( 0 , 2 n +2)
18 z = exp( p i 1.0 j n / ( n + 1 . 0 ) t ) h sta ck ( [ y , y [ : : 1 ] ] )
19 6.5

20 # Solve linear system (??) with effort O(n log n) p. 355


21 c = i f f t (z) Num.
Meth.
22 Phys.

23 # recover j , see (??)


24 t = arange(n , n +2)
25 b = r e a l ( exp ( 0 . 5 j p i / ( n + 1 . 0 ) t ) c )
26
27 # recover j , see (??)
28 a = h sta ck ( [ b [ n ] , 2 b [ n +1:2 n + 1 ] ] )
29
30 re t urn a
31
32 i f __name__ == " __main__ " :
33 from numpy import a r r a y
Gradinaru
34
D-MATH

35 # Test with arbitrary values


36 y = array ( [1 , 2 , 3 , 3.5 , 4 , 6.5 , 6.7 , 8 , 9 ])
37
38 # Expected values:
39 # 4.85556 -3.66200 0.23380 -0.25019 -0.15958 -0.36335 0.18889 0.16546 -0.27329
40
41 w = chebexp ( y )
42
43 p r i n t (w) 6.6

p. 356
6.6 Essential Skills Learned in Chapter 6 Num.
Meth.
Phys.

You should know:

the idea behind the trigonometric approximation;


what is the Gibbs phenomenon;
the dicrete Fourier transform and its use for the trigonometric interpolation;
the idea and the importance of the fast Fourier transform;
how to use the fast Fourier transform for the efficient trigonometrical interpolation; Gradinaru

the error behavior for the trionomteric interpolation; D-MATH

the aliasing formula and the Sampling-Theorem;


how to use the fast Fourier transform for the efficient Chebychev interpolation.

6.6

p. 357

Anda mungkin juga menyukai