29
Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016 COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

  • Upload
    lethien

  • View
    276

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Lecture 16: Discrete Fourier Transform,Spherical Harmonics

COMPSCI/MATH 290-04

Chris Tralie, Duke University

3/8/2016

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 2: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Announcements

B Mini Assignment 3 due Sunday 3/13 11:55PMB Group Assignment 2 Out around Friday/Saturday, due

Monday 3/28B Final projects assigned, first milestone due Wednesday 4/6B Midterm Thursday

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 3: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Table of Contents

I Numpy Squared Euclidean Distances

B Discrete Fourier Transform

B Spherical Harmonics

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 4: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Numpy: More Broadcasting

import numpy as npimport matplotlib.pyplot as pltX = np.arange(4)Y = np.arange(6)Z = X[:, None] + Y[None, :]print Z

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 5: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Squared Euclidean Distances in Matrix Form

Notice that

||~a− ~b||2 = (~a− ~b) · (~a− ~b)

||~a− ~b||2 = ~a · ~a + ~b · ~b − 2~a · ~b

Given points clouds X and Y expressed as 2×M and 2× Nmatrices, respectively, write code to compute an M × N matrixD so that

D[i , j] = ||X [:, i]− Y [:, j]||2

Without using any for loops! Can use for ranking with Euclideandistance or D2 shape histograms, for example

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 6: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Squared Euclidean Distances in Matrix Form

Notice that

||~a− ~b||2 = (~a− ~b) · (~a− ~b)

||~a− ~b||2 = ~a · ~a + ~b · ~b − 2~a · ~b

Given points clouds X and Y expressed as 2×M and 2× Nmatrices, respectively, write code to compute an M × N matrixD so that

D[i , j] = ||X [:, i]− Y [:, j]||2

Without using any for loops! Can use for ranking with Euclideandistance or D2 shape histograms, for example

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 7: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Brute Force Nearest Neighbors

import numpy as npimport matplotlib.pyplot as pltt = np.linspace(0, 2*np.pi, 100)X = np.zeros((2, len(t)))X[0, :] = tX[1, :] = np.cos(t)Y = np.zeros((2, len(t)))Y[0, :] = tY[1, :] = np.sin(t**1.2)##FILL THIS IN TO COMPUTE DISTANCE MATRIX Didx = np.argmin(D, 1) #Find index of closest point in Y

to point in Xplt.plot(X[0, :], X[1, :], ’.’)plt.hold(True)plt.plot(Y[0, :], Y[1, :], ’.’, color = ’red’)for i in range(len(idx)):

plt.plot([X[0, i], Y[0, idx[i]]], [X[1, i], Y[1, idx[i]]], ’b’)

plt.axes().set_aspect(’equal’, ’datalim’); plt.show()

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 8: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Mini Assignment 3 API

def getCentroid(PC):return np.zeros((3, 1)) #Dummy value

def getCorrespondences(X, Y, Cx, Cy, Rx):return np.zeros(X.shape[1], dtype=np.int64) #dummy

value

def getProcrustesAlignment(X, Y, idx):return (Cx, Cy, R)

#what the ICP algorithm diddef doICP(X, Y, MaxIters):

CxList = [np.zeros((3, 1))]CyList = [np.zeros((3, 1))]RxList = [np.eye(3)]#TODO: Fill the rest of this inreturn (CxList, CyList, RxList)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 9: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Table of Contents

B Numpy Squared Euclidean Distances

I Discrete Fourier Transform

B Spherical Harmonics

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 10: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 11: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

What is

N=kT∑t=1

cos2(2πtT

)

?Note that cos2(A) = 1+cos(2A)

2

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 12: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 13: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

Why is∑N=kT

t=1 cos(2πtT ) sin(2πt

T ) zero?Note that

cos(A) sin(A) =12

sin(2A)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 14: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 15: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

Why does the product of two different cosines integrate to zero?

cos(A + B) = cos(A) cos(B)− sin(A) sin(B)

Hint: cos(A + B) + cos(A− B) =?

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 16: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

Why does the product of two different cosines integrate to zero?

cos(A + B) = cos(A) cos(B)− sin(A) sin(B)

Hint: cos(A + B) + cos(A− B) =?

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 17: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 18: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Cosine Integration

Dot product interpretation

~s1 =

[1, cos

(2πT1

), cos

(2π2T1

). . . , cos

(2πNT1

)]

~s2 =

[1, cos

(2πT2

), cos

(2π2T2

). . . , cos

(2πNT2

)]Dot product is linear!

~s2 · (a ~s1 + b ~s2) = a ~s1 · ~s2 + b ~s2 · ~s2

Can use this type of dot product / integration to detect / test forthe presence of different frequencies

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 19: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Cosine Phase Separation

The general form of a sinusoid is A cos(ωt + φ)

A cos(ωt + φ) = A cos(φ) cos(ωt)− A sin(φ) sin(ωt)

cos(ωt)(A cos(ωt+φ)) = A cos(φ) cos2(ωt)−A cos(ωt) sin(φ) sin(ωt)

B The “cosine component” of the sinusoid is A cos(φ)B The “sinusoid component” of the sinusoid is A sin(φ)B The magnitude A of this sinusoid is√

(A cos(φ))2 + (A sin(φ))2 = A (sanity check)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 20: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Cosine Phase Separation

The general form of a sinusoid is A cos(ωt + φ)

A cos(ωt + φ) = A cos(φ) cos(ωt)− A sin(φ) sin(ωt)

cos(ωt)(A cos(ωt+φ)) = A cos(φ) cos2(ωt)−A cos(ωt) sin(φ) sin(ωt)

B The “cosine component” of the sinusoid is A cos(φ)B The “sinusoid component” of the sinusoid is A sin(φ)B The magnitude A of this sinusoid is√

(A cos(φ))2 + (A sin(φ))2 = A (sanity check)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 21: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Cosine Phase Separation

The general form of a sinusoid is A cos(ωt + φ)

A cos(ωt + φ) = A cos(φ) cos(ωt)− A sin(φ) sin(ωt)

cos(ωt)(A cos(ωt+φ)) = A cos(φ) cos2(ωt)−A cos(ωt) sin(φ) sin(ωt)

B The “cosine component” of the sinusoid is A cos(φ)B The “sinusoid component” of the sinusoid is A sin(φ)

B The magnitude A of this sinusoid is√(A cos(φ))2 + (A sin(φ))2 = A (sanity check)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 22: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Cosine Phase Separation

The general form of a sinusoid is A cos(ωt + φ)

A cos(ωt + φ) = A cos(φ) cos(ωt)− A sin(φ) sin(ωt)

cos(ωt)(A cos(ωt+φ)) = A cos(φ) cos2(ωt)−A cos(ωt) sin(φ) sin(ωt)

B The “cosine component” of the sinusoid is A cos(φ)B The “sinusoid component” of the sinusoid is A sin(φ)B The magnitude A of this sinusoid is√

(A cos(φ))2 + (A sin(φ))2 = A (sanity check)

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 23: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Sinusoidal Coordinates BasisFor a signal of length N (let N be odd for the moment), define the following matrix

F =

1 1 1 . . . 11 cos( 2π

N ) cos(2 2πN ) . . . cos((N − 1) 2π

N )

0 sin( 2πN ) sin(2 2π

N ) . . . sin((N − 1) 2πN )

1 cos( 4πN ) cos(2 4π

N ) . . . cos((N − 1) 4πN )

0 sin( 4πN ) sin(2 4π

N ) . . . sin((N − 1) 4πN )

.

.

....

.

.

. . . .

.

.

.1 cos( ((N−1)π

N ) cos(2 (N−1)πN ) . . . cos((N − 1) (N−1)π

N )

0 sin( (N−1)πN ) sin(2 (N−1)π

N ) . . . sin((N − 1) (N−1)πN )

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 24: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Each row is perpendicular to each other row. This means that

Fx

Is a high dimensional rotation! This means these sinusoidsform a basis for all signals of length N. This is known as theDiscrete Fourier Transform

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 25: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Fourier Transform: Example

Show sinusoidal additionCOMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 26: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Fourier Transform: Example

Show sinusoidal additionCOMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 27: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Discrete Fourier Transform: Formal Definition

The formal definition of the Discrete Fourier Transform usescomplex numbers to store the phase, for convenience

F [k ] =N−1∑t=0

X [n]e−i2π kN n

F [k ] =N−1∑t=0

X [n] cos(

2πkN

n)+ i

N−1∑t=0

X [n] sin(−2π

kN

n)

F [k ] = Re(F [k ]) + i Imag(F [k ])

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 28: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Inverse Discrete Fourier Transform

DFT decomposed X into a bunch of sinusoids, now add themback together

X [n] =N−1∑k=0

F [k ]ei2π kN n

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics

Page 29: Lecture 16: Discrete Fourier Transform, Spherical Harmonics · Lecture 16: Discrete Fourier Transform, Spherical Harmonics COMPSCI/MATH 290-04 Chris Tralie, Duke University 3/8/2016

Functions on The Circle

COMPSCI/MATH 290-04 Lecture 16: Discrete Fourier Transform, Spherical Harmonics