Trinh @ Bath

This is an old revision of the document!


Lecture 35: Maths of music II

I don't want to dwell on the details of how the fft command works but it is enough to talk about analogies. Suppose we give you an $N$ vector $f_n$ with elements $n = 0, 1, 2, \ldots N-1$ that represents the signal we want to process.

Think instead of a function $f(n)$ on the domain $[0, 2L] = [0, N]$. So here $L = N/2$. Consider the Fourier series constructed from $N$ terms: $$ f(n) = \sum_{k=0}^{N-1} \left[a_k \cos\left(\frac{k\pi n}{L}\right) + b_k \sin\left(\frac{k\pi n}{L}\right)\right] $$ (For ease, I have combined the usual frontal constant into the full sum). There is a way for you to instead write this as $$ f(n) = \sum_{k=0}^{N-1} F_k \exp\left(i\frac{k\pi n}{L}\right), $$ where $F_k$ is a complex number. If you take the analogous formula to PS7 Q4, you would write $$ F_k = \frac{1}{2L}\int_0^{2L} f(n) \exp\left(-i\frac{k\pi n}{L}\right) \, \mathrm{d}n. $$ If I now set $L = N/2$, and I approximate the integral as as sum, I have $$ F_k = \frac{1}{N} \sum_{n=0}^{N-1} f_n \exp\left(-i\frac{2k\pi n}{N}\right). $$ which is exactly how Matlab computes the Discrete Fourier Transform of a vector $f_n$.

Make a sound

Fs = 2^(13); T = 5; t = 0:(1/Fs):T;
f = sin(2*pi*440*t); sound(f, Fs);
N = length(f);
Fk = fft(f);

A = abs(Fk)/N;
K = (Fs/2)*(1:ceil(N/2))/ceil(N/2);
A = A(1:ceil(N/2));
plot(K, A)

This is what the above code does. It takes the Fourier transform (or Fourier series) of f, then plots the amplitudes. The funny code around it is because Matlab places the Fourier transform coefficients in a 'deranged' fashion.

Real sounds

That's a lot to take in. The rest is about more fun stuff.