Trinh @ Bath

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.

That's a lot to take in. You do not have to understand this theory on fft beyond the basic understanding we presented in Lecture 34.

The rest is about more fun stuff.

Pitch modification


% Example of pitch modification
Fs = 2^13; T = 5;
t = 0:1/Fs:T;

% This is an A note of 440 Hz
fA = sin(2*pi*440*t);
sound(fA, Fs)

%% This is an E note of 659.3 Hz
fE = sin(2*pi*659.3*t);
sound(fE, Fs)

%% We can make our A note sound like an E note
%  by essentially changing our definition of 'time' or changing the
%  sampling frequency. This is a cheap way of modifying pitch.
sound(fA, 659.3/440*Fs)

Tuning an instrument


% Example of tuning
Fs = 2^13; T = 8;
t = 0:1/Fs:T;

% This is how tuning works. Play a 440Hz note with an off-tune 442Hz note
% signal
f = sin(2*pi*440*t) + sin(2*pi*442*t);
sound(f, Fs)


% This is easy to show mathematically. When you add two sine waves together
% that differ very slightly in frequency, it gives a beating pattern
plot(t, f)