Karhunen-Loeve transforms part 01

Transforms · Part 1

Fourier Is the Eigenbasis of Convolution

Blur an image through the FFT and structures from the right border turn up on the left side. You never picked a border mode, so who picked wrap?

Thomas Stephan · · Series index

The left edge knows the right edge

Say you want to blur an image sideways, the way a camera smears a scene that moves during the exposure. The kernel is nothing fancy, a row of 81 equal weights. And there is a fast way to do it: the convolution theorem turns convolution into multiplication, so you Fourier transform the image and the kernel, multiply the two, and transform back. The star comes out the way a moving star should. But at the left edge, where the image was dark, a strip of teal, orange and magenta has appeared, in the same order as the bands that leave the picture on the right.

A yellow five-pointed star on a dark ground, and to its right a slanted block of horizontal bands in teal, orange, magenta and off-white that runs off the right edge of the image. The left edge of the image is dark.

the image

The same image blurred horizontally through the FFT. The star and the bands are smeared sideways. At the left edge a strip of teal, orange, magenta and off-white bands has appeared where the image was dark, and the bands fade towards dark where they meet the right edge.

blurred by FFT ✗

The same blur computed pixel by pixel with the border clamped. The star and the bands are smeared the same way, the left edge stays dark, and the bands stay bright up to the right edge.

blurred pixel by pixel ✓

Fig. 1. The same blur, twice. Away from the borders the two agree down to rounding. Through the FFT, what leaves on the right comes back in on the left.

Convolve pixel by pixel and you have to decide what lies beyond the edge. The filter functions you know all take a border mode: constant, replicate, reflect, wrap. The Fourier route asked you nothing, and it picked wrap anyway. The standard advice is to pad the image with zeros first. That makes the strip go away, but it does not tell you who put it there.

So who glued the right edge to the left? The FFT algorithm, the theorem, or something you asked for without noticing?

To answer that, we have to step back from the picture for a while. The trail leads through a list you already know, and the picture comes back as soon as we know where that list comes from.

The list everyone knows

You know the Fourier transform by what it does. Somewhere in a course or a textbook you met a table like this one, and you have used most of its lines since.

In the signalIn the spectrum
Convolve with a kernelMultiply by its spectrum
ShiftMultiply by a phase
DifferentiateMultiply by the frequency
Filter a sine waveSame frequency comes out
Sum the squaresSame sum (Parseval)
Take N samplesThey repeat with period N

In most textbooks each line comes with its own proof. An integral is rearranged, a variable substituted, two sums swapped, the 2π parked wherever your textbook keeps it, and the line is done. The next line gets a new proof with a new trick, and nothing in those proofs connects one line to the next.

That is a lot of luck for one transform.

The six lines share a single cause. To find it, we ask convolution which signals it only stretches.

Stop describing, start diagonalizing

A matrix is easiest to understand through its eigenvectors, the directions it only stretches:

Ax=λx

Measure a vector along those directions instead of along the usual axes, and the matrix becomes harmless. It stretches each coordinate by its own number, and the coordinates never mix. That is all a diagonal matrix is: n separate multiplications.

So we go looking for the same thing in convolution: a signal that comes out the same shape it went in, just at a different height. Fig. 2 blurs two candidates with the same box kernel, half a period long.

square wave in out shape changed ✗ cosine in out shape kept, 64% of the height ✓
Fig. 2. A box kernel slides along two signals of the same period. Only the cosine comes out in the shape it went in.

The search is shorter than it looks, because a convolution does the same thing everywhere on the signal. That is written right in the integral:

(h∗f)(t)=∫−∞∞h(τ)f(t−τ)dτ

Read it as a recipe. Shift f by every τ, weight each copy by h(τ), add them up. A convolution is a weighted sum of shifts, so whatever survives every shift in shape survives every convolution too.

So ask the shift. A signal survives a shift by τ in shape if, for some factor c(τ) and for every t,

f(t−τ)=c(τ)f(t)

Differentiate with respect to τ at τ=0 and this becomes f′=sf with s=−c′(0), whose solutions are f(t)=est up to a constant factor. For a signal that does not blow up in either direction, s must be imaginary, s=iω. What survives every shift is the complex exponential eiωt, and nothing else. The whole argument rested on one thing: f(t−τ), for every t and every τ. On the real line that value always exists.

Now put f(t)=eiωt into the convolution:

∫−∞∞h(τ)eiω(t−τ)dτ=(∫−∞∞h(τ)e−iωτdτ)·eiωt

The exponential comes out as itself, times the number in brackets. That number is the Fourier transform of the kernel, H(ω), written exactly as the textbook defines it. Call the exponential eω(t)=eiωt and the result reads like the matrix line above:

h∗eω=H(ω)eω

eω is an eigenvector of every convolution, and H(ω) is its eigenvalue.

A spectrum is a list of eigenvalues.

The Fourier transform is the same move we made on the vector: it measures the signal along the exponentials. Every reasonable signal can be put together from them, and putting it back together is the inverse transform's job. Side by side, the matrix version and the signal version:

A=VΛV−1h∗x=ℱ−1(H·ℱx)

Read the right one from the inside out. ℱ takes the signal into the eigenbasis, H multiplies each coordinate by its own eigenvalue, ℱ−1 brings it back. It is the recipe you used on the star in Fig. 1, and it is line 1 of the list: convolve in the signal, multiply in the spectrum. The rest of the list goes just as fast.

The list, one line at a time

Convolution becomes multiplication. Already done. Apply ℱ to both sides of the diagonalization:

ℱ(h∗x)=H·ℱx

A shift becomes a phase. A shift by τ is a convolution too: its kernel is a single unit spike sitting at τ, zero everywhere else. Its spectrum costs nothing, because the integral ∫h(u)e−iωudu only sees the one point where the spike sits:

H(ω)=e−iωτ

By the line above, a shift multiplies each coordinate of the spectrum by e−iωτ: the shift's eigenvalues, all of magnitude one. A shift makes nothing louder or quieter; it only turns each frequency by an angle. That is the phase.

A derivative becomes a multiplication by the frequency. The difference quotient f(t)−f(t−Δ)Δ is a difference of two shifts, and both shifts return eω as a multiple of itself, so the quotient does too, for every Δ. Which multiple the limit lands on is plain differentiation:

ddteiωt=iωeiωt

A sine wave comes out as a sine wave. Fig. 2 showed it. A cosine is two exponentials, cosωt=eω+e−ω2, and each comes out scaled by its own eigenvalue. For a real kernel the two eigenvalues are complex conjugates, so the sum is a cosine again, scaled by |H(ω)| and shifted by the angle of H(ω). For the box in Fig. 2 that angle is zero and H=2π, which is the 64%.

Energy stays the same. A shift moves a signal without changing its energy. For an operator like that, eigenvectors with different eigenvalues are orthogonal, just as the eigenvectors of a symmetric matrix are. Switching to an orthogonal basis is a rotation, and a rotation keeps lengths. With X=ℱx:

∫−∞∞|x(t)|2dt=12π∫−∞∞|X(ω)|2dω

The 12π in front is the parked 2π, turning up again.

Five lines, one fact.

Convolve with a kernelMultiply by its spectrum✓
ShiftMultiply by a phase✓
DifferentiateMultiply by the frequency✓
Filter a sine waveSame frequency comes out✓
Sum the squaresSame sum (Parseval)✓
Take N samplesThey repeat with period N?

Every line of the list has fallen out of the eigenbasis. All but the last.

Theory or algorithm?

Back to the star in Fig. 1, and to the three suspects. The FFT goes first, and it is cleared at once: it is a fast way to compute the discrete Fourier transform, the DFT, and nothing more. Compute the DFT the slow way, straight from its definition, and the colored strip turns up at the left edge all the same, down to rounding.

So the FFT is innocent. The theorem is next. A reader who knows the DFT has two answers ready, and both are true.

“The basis functions repeat.” The DFT builds every signal from e2πikn/N, each of those repeats after N samples, so anything built from them repeats too. Correct, and it moves the question one level down. On the real line, the derivation found an eigenvector for every frequency ω, a continuum of them. On N samples the DFT offers exactly N, the frequencies 2πk/N. Step through one of those sample by sample and after N steps you are back at the start: the walk closes into a circle. For every other ω it never closes. Why exactly those?

a DFT frequency 0 = 8 1 2 3 4 5 6 7 ω = 2π/8: step 8 is step 0 ✓ any other frequency start ω = 2.4: never back at the start ✗
Fig. 3. From one sample to the next, the exponential turns by ω on the unit circle. At ω = 2π/8 the eighth step lands back on the start and the walk closes. At ω = 2.4 no step ever lands there.

“The convolution theorem only holds for cyclic convolution.” Textbooks that state the discrete theorem say so. Also correct, and it moves the question down as well: why only there, and not for the clamped border that the pixel-by-pixel blur in Fig. 1 used?

Both answers describe the circle. Neither says why the eigenbasis needs one. To find out, go back to the one step in the derivation that leaned on the real line: f(t−τ) for every t and every τ. On N samples, that is the first thing to break.

Eight samples and a shift

Take eight samples and do what the derivation did: assume the exponentials survive the shift, and see what that demands. Write eiωn into positions 0 to 7 and shift everything one place to the right. Positions 1 to 7 are no problem. Each receives its left neighbor:

eiω(n−1)=e−iω·eiωn,n=1,…,7

So the factor is fixed at e−iω. But position 0 has no left neighbor, the neighbor the real line always provided. Someone has to put a value there, and that someone is the border mode. For the exponential to survive, it must be e−iω, the value one step before the start.

x0 x1 x2 x3 x4 x5 x6 x7 n = −2−1 0123 4567 cell −1 = e−iω? constant 00 never ✗ replicate x0 x0 only ω = 0 ✗ reflect x1 x0 only ω = 0 ✗ wrap x6 x7 e8iω = 1 ✓
Fig. 4. Each border mode fills the dashed cells in front of the array, arrows marking where it copies from. After the shift, position 0 receives the cell at −1; the exponential survives only if that cell holds the value one step before the start.

Constant offers 0, and an exponential is never 0. Replicate and reflect offer x0=1, which matches only for ω=0: the one exponential left is the flat line. Wrap offers x7=e7iω, which matches when

e8iω=1ω=2πk8

Eight frequencies on eight samples: a full basis, and exactly the one the DFT uses. Asking for the exponentials leaves one border mode standing, and it brings the DFT’s frequencies with it.

The same goes for every convolution, because the shift is one of them, with a single spike as its kernel. The discrete convolution theorem needs the eigenbasis, and the eigenbasis needs wrap. That is why the theorem only holds for cyclic convolution.

So it was you. Multiplying in the Fourier domain means asking for the eigenbasis, and of the border modes on offer, only the circle keeps the exponentials. The right edge was glued to the left the moment you chose the Fourier route. In two dimensions the same argument runs down the columns, so the top is glued to the bottom as well. The image you blurred in Fig. 1 was a donut.

The bill for the circle

Each of the standard remedies is a way of living on the circle.

Padding makes the circle larger than the image. Pad each row by at least half the kernel, blur through the FFT, crop, and the wrap happens in the padding, where nobody looks. What you pad with is the border mode. Pad with the edge values and the result matches the pixel-by-pixel blur of Fig. 1. The zero padding from the forums is the constant mode.

The image of Fig. 1 blurred horizontally through the FFT without padding. A strip of the colored bands from the right edge has appeared at the left edge.

by FFT, no padding ✗

The same blur through the FFT after padding each row with its edge values. The left edge stays dark and the bands stay bright up to the right edge, as in the pixel-by-pixel blur of Fig. 1.

by FFT, padded with the edge values ✓

Fig. 5. The blur of Fig. 1 through the FFT, once as it was and once with each row padded by its edge values. The padded one matches the pixel-by-pixel blur to rounding.

Windowing pays the same bill in spectral analysis. The DFT reads a stretch of signal as one period of a circle. If the two ends do not meet, the circle has a jump at the seam, and a jump takes exponentials at every frequency to build. That spread is leakage. A window tapers both ends towards zero so the seam closes, and reflect padding mirrors the signal so the seam has no jump at all.

Five lines of the list were one fact, and the sixth was its price. One consequence of the first line is still waiting. If convolution becomes a multiplication, then deconvolution obviously becomes a division.

Case closed?

The argument, the outline and the final wording are mine, argued out sentence by sentence. AI assistance wrote the drafts in between, built the figures and recomputed the numbers.