lacunary - Mathnotes

The demo above shows how closed contours can be approximated via sums of complex exponentials. The process involves taking samples, performing a Discrete Fourier Transform on them, and then using the resulting Fourier Coefficients for complex exponentials. This is sometimes referred to as epicycles. This page gets into how the Discrete Fourier Transform Works.

Preliminary rambling notes

  1. Take a closed contour C.
  1. Take N sample points along it, separated evenly by arc length.
  1. Calculate the Fourier coefficient cn at each of the N points. These correspond to the frequencies n=[N/2],,[N/2]. We're basically measuring how much of each frequency the contour has at each of the N different points.

We calculate the Fourier Coefficient cn as

cn=1Nk=0N1zke2πink/N

  1. Only about half of these are unique because the frequency Nm,m<N is the same as m (just like moving back 2 hours from 12 on a clock to 10 it the same as moving forward by 10 hours).
  1. Each cn represents a vector in the complex plane with:
  • length = |cn|
  • phase angle = Arg(cn)
  • rotation speed = n

Seems to be measuring how well the sample points move around the unit circle at the same rate as the rotation vector?

Let's take N=9 and talk about what happens when our curve is a unit circle.

With frequency n=0: we have no rotations, so, for each sample point, we add the sample point directly to the summation vector. The result is the average of all the sample points, the center of gravity, or DC component. For the unit circle this is 0.

With frequency n=1:

Starting with k=0, we have z0, which is at 1. Our first rotation doesn't rotate at all, so our summation is now pointing at 1.

Now at k=1, our second sample point z1 is pointing at 2π9. The imaginary component of our exponential e2πink/N gives us

2πi(1)(1)/9=2π9.

So, our sample point and our rotation are the same. When we multiply them in exponential form, we add the angles, and get 4π9. We take the exponential from our first sample and add it to this exponential, which gives us their average, and we get 2π9 as our summation vector so far.

Next at k=2, our sample point is at 4π9 and our rotation vector is too. The resulting angle is 8π9. Our summation vector now points at 5π9, midway between its previous value and the value of this sample.

The demo below shows how each individual Fourier Coefficient calculation works by taking a sample point and rotating it.

Math on the Unit Circle

The nth roots of unity. There are n nth roots of 1. So, z1=1 has 1 solution, 1 itself. z2=1 has 2 solutions, 1 and 1. Note that 1 is on the opposite side of the unit circle from 1. z3 has 3 solutions: 1,e2π/3 and e4π/3. The general pattern is that the nth roots of unity are given by

ei(2kπ)/n,k=0,,n1.

Now, multiplying these roots by each other can be accomplished by adding their exponents, which are just terms in iπ/n, i.e. the product of 1 and 1 is

11=e0(i2π/2)e1(i2π/2)=e(0+1)(i2π/2)=eiπ=1.

Note that the resultant product is a root of unity as well, and in general the nth roots of unit form a @cyclic abelian group under multiplication.

It also turns out that this group is isomorphic to Zn under addition modulo n. The multiplication we gave above is equivalent to 0+1=1 on Z2, and just like 11=1, 1+1=0 on Z2.

Theorem \@{theorem-1}

The nth roots of unity under multiplication are isomorphic to Zn under addition.

Proof \@{proof-of-theorem-1}

Let

μn={e2piik/n:k=0,1,,n1}

be the nth roots of unity, and

Zn={0,1,,n1}

be the first n non-negative integers under addition modulo n.

Let

ϕ:Znμn,ϕ(k)=e2πik/n

be a mapping we will show to be an isomorphism.

First, note that μn and Zn are obviously the same cardinality, by their definitions.

Now, suppose a,b<n1. Then

ϕ(a+b)=e2πi(a+b)/n=e2πia/ne2πib/n=ϕ(a)ϕ(b),

so ϕ is a @group-homomorphism.

Now, eμn is 1, that is, e2πi(0)/n, so kerϕ=0. But, 0 is eZn, and so ϕ is @injective, and an @isomorphism, and μn and Zn are isomorphic.

One more fact worth noting, now that we've established this isomorphism. On the unit circle, a negative rotation (clockwise) by ϕ is equal to a positive rotation (counterclockwise) by 2πϕ. This is easy to see:

(ϕ)2π+(ϕ)(mod2π)=2πϕ.

Thus, the kth nth root of unit, e2πi(k) equals the (nk)th root of unity (because the nth nth root of unity corresponds to a full rotation). This corresponds to knk(modn) on Zn. For example, 4=94=5 on Z9, and e2πi(5)/9=e2πi(4)/9 on μn.

Coming soon... roots of unity as the elements of basis vectors for Cn, and DFT as computing the inner product (basically, cosine similarity * magnitude) between the sample points and these basis vectors. That is, DFT is a change of basis onto these basis vectors, it is literally projecting onto these.

Sample Points as Vectors in Cn.

Let's say we have a contour on the complex plane, and we take n sample points on it, in order, so z0,z1,,zn. We can view these sample points (which form a sequence) as forming a vector in Cn:

(z0,z1,,zn).

Now, the standard basis for Cn is the same as that of Rn, which is {e1,e2,,en}, where ek is a n-dimensional vector with a 1 in the kth position and 0's in the other position, i.e.

e1=(1,0,,0),e2=(0,1,,0),

It's easy to see that these basis vectors are orthogonal (and therefore @linearly-independent); to see that they span Cn, suppose w=(w1,w2,,wn.). Then, w1e1+w2e2++wnen=w, that is, the components of w (which are complex numbers) are exactly the scalar coefficients we need to multiply by our basis vectors to form w, so this basis spans Cn.

Cn is an Inner Product Space

Now, we can equip this vector space with an inner product

Theorem: CN is an inner product space \@{complex-inner-product-space}

The function

z,w=k=0N1zkwk

defined on z,wCN is an inner product, and so CN (N a positive integer) forms an @inner-product-space.

Proof \@{proof-of-complex-inner-product-space}

Since C is a field, CN is obviously a vector space.

To see that this is indeed an inner product, we'll show the required properties hold.

For @conjugate-symmetry, let zk=a+bi and let wk=c+di. Then,

zkwk=(a+bi)(cdi)=acadi+cbi+bd=(ac+bd)+(cbad)i,

and

wkzk=(c+di)(abi)=ac+adicbi+bd=(ac+bd)(adcb)i=(ac+bd)+(cbad)i,

and since k is arbitrary, it holds for any respective pair of components in z and w, and so z,w=w,z.

For @linearity in the first argument, let w,v,zCn,a,bC. Then

ax+by,z=(ax1+by1)z1+(ax2+by2)z2+=a(x1z1)+a(x2z2)++b(y1z1)+b(y2z2)+=ax,z+bx,z.

Now, to show @positive-definiteness let z=a+bi. Then,

z,z=zz=1>0.

Remark \@{remark-5}

Since Cn is an inner-product space, we can use the inner product to compare how well aligned two vectors are, and to perform projections.

Fourier Basis

We can use complex numbers representing varying degrees of rotation around the unit circle to form a basis vector. Given a sample count N, we can make a basis from the N vectors whose elements are defined as

ϕn=(e2πink/N)k=0N1,n=0,,N1.

To work on intuition some, let's take the case N=3.

Then for n=0,

ϕ0=(e2πi(0)(0)/3,e2πi(0)(1)/3,e2πi(0)(2)/3)=(1,1,1),

n=1,

ϕ1=(e2πi(1)(0)/3,e2πi(1)(1)/3,e2πi(1)(2)/3)=(1,e2πi/3,e2πi/3),

and n=2,

ϕ2=(e2πi(2)(0)/3,e2πi(2)(1)/3,e2πi(2)(2)/3)=(1,e2πi/3,e2πi/3),

If we use our isomorphism Z3, we have a ϕ0=(0,0,0),ϕ1=(0,1,2),ϕ2=(0,2,1).

Each of these values n=0,1,2 represents a different frequency. For n=0, our total angle swept across all points is 2π0=0. For n=1 the total angle swept is 2π1=2π. For n=2 the total angle swept is 2pi2=4π. So, we can say that basis vector ϕn is associated with (represents?) rotation at frequency n.

Now note that ϕ2=ϕ1 equals the conjugate of ϕ1. It appears that the conjugate of the basis vector representing rotation at frequency n represents rotation at frequency n.

For odd N, one basis vector represents no rotation - no frequency - aka DC - this is for n=0. Then, for each n up to (N1)/2, we have a positive n frequency vector and a matching n frequency vector. The vectors representing each pair of frequencies are made up of sample points which are conjugates of each other (reflections of each other across the x-axis, or alternatively, rotations by the same increments in opposite directions.) That is, the kth point in the vector for n is the conjugate of the kth point in the vector for n.

For even N, we have a similar situation, except there is an additional frequency at N/2 that is sometimes called the Nyquist frequency. This frequency has only real components and is therefore its own conjugate. The kth element of this vector is given by

ϕN/2(k)=eπik=(1)k.

Theorem: Fourier Basis \@{fourier-basis}

Let N be a positive integer. Then the vectors

ϕn=(e2πink/N)k=0N1

for n=0,,N1 form a basis for CN.

Proof \@{proof-of-fourier-basis}

To show linear independence, we will show a stronger condition, orthogonality, holds.

For m,n,kZN,mn we can write that the kth entry of ϕm is

ϕm(k)=e2πiN(km(modN)),

and that the conjugate of the kth entry of ϕn is

ϕn(k)=e2πiN(kn(modN)).

Then, in our inner product between ϕm and ϕn, the kth term is

ϕm(k)ϕn(k)=e2πiN(km(modN))e2πiN(kn(modN))=e2πiN(k(mn)(modN)).

Thus, for r=mn(modN),

ϕm,ϕn=k=0N1e2πiNkr.

Now we need to show that this sum is 0. Note that if we let w=e2πiNr, we can rewrite this as

ϕm,ϕn=k=0N1wk.

From Finite Geometric Series, we have that

k=0N1wk=1wN1w.

Substituting back in e2πiNr for w we get

k=0N1e2πiNrk=1e2πiNrN1e2πiNr=11r1e2πiNr=0.

Thus, ϕm,ϕn=0, and so when mn, ϕm an ϕn are orthogonal and therefore @linearly-independent.

Now we need to show that ϕ0ϕN1 span CN.

Let u=(z0,z1,,zN1)CN. We need to find c0,c1,,cN1 such that

u=c0ϕ0+c1ϕ1++cN1ϕN1.

That is, we want constants c0cN1 such that

uu=n=0N1cnϕn.

To find some specific cm, we can take the inner product of both sides with respect to ϕm to get

u,ϕm=n=0N1cnϕn,ϕm.

@Linearity in the first argument of the inner product allows us to rewrite the right side to get

u,ϕm=n=0N1cnϕn,ϕm.

Now, we know that

ϕn,ϕm={N,n=m,0,nm.

so the inner product on the right-hand side is 0 except when n=m, and this reduces to

u,ϕm=cmN.

Therefore,

cm=u,ϕmN.

This shows that the Fourier basis described here indeed spans CN, and shows how to compute the Fourier coefficients c0cN1.

Remark \@{remark-8}

The Fourier Coefficient cm, given as

cm=u,ϕmN.

Is just the @scalar-projection of u onto ϕm. It is a complex number, and |cm| is proportional to the amplitude of the frequency m present in u, while Argcm gives the phase of frequency m in u.

Note \@{remark-8-note}

We've given this cm as the inner product divided by N. This is allowed but not typical. In order to make

IDFT(DFT(x))=x,

we need 1N as a factor in either the DFT or the IDFT, but typically, it's done in the IDFT.

Now we're ready to see the DFT as a change of basis from the standard basis for CN to the DFT (Fourier) Basis for CN. The DFT is given as

X[n]=x[k],ϕn=k=0N1x[k]e2πink/N,

and the IDFT is given as

x[k]=1Nn=0N1X[n]e2πink/N.

If we collect the conjugates of the ϕk vectors as rows in a matrix we get

F=(ϕ0ϕ1ϕN1)=(e2πi00/Ne2πi01/Ne2πi0(N1)/Ne2πi10/Ne2πi11/Ne2πi1(N1)/Ne2πi(N1)0/Ne2πi(N1)1/Ne2πi(N1)(N1)/N).

Then, given a vector x in CN, we can compute its DFT via matrix vector multiplication as

X=Fx,

and we can invert it via IDFT using the conjugate rows of F as

x=1NFX.

Note \@{note-10}

In practice, while this matrix multiplication approach works, it can be optimized. This isn't an arbitrary matrix - it has some particular structure, and it can be factored to come up with a more efficient operation. This is what the Fast Fourier Transform does.

The demo below allows drawing a contour and then editing the Fourier coefficients. Playing with this helps gives some intuition for what the coefficients represent, and it also shows one of the applications of DFT - filtering and amplifying components of a signal. Try drawing a closed contour that wraps around the origin three times counter-clockwise. What's the biggest component? Try drawing a similar shape, but go clockwise, maybe go clockwise 4 times.