Frequency-domain audio experiments

In [2]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
In [3]:
wave = np.load("cs357.npy")
In [4]:
pt.plot(wave)
Out[4]:
[<matplotlib.lines.Line2D at 0x7f29ecb727f0>]
In [5]:
from html5_audio import DEFAULT_RATE, get_html5_wave_player
get_html5_wave_player(wave)
Out[5]:

Now build the Fourier Vandermonde matrix. This is also called the Discrete Fourier Transform (DFT) matrix.

In [6]:
n = 255
assert n % 2 == 1

cos_k = np.arange(0, n//2 + 1, dtype=np.float64)
sin_k = np.arange(1, n//2 + 1, dtype=np.float64)

x = np.linspace(0, 2*np.pi, n, endpoint=False)

DFT = np.zeros((n,n))
DFT[:, ::2] = np.cos(cos_k*x.reshape(-1, 1))
DFT[:, 1::2] = np.sin(sin_k*x.reshape(-1, 1))

IDFT = la.inv(DFT)

And transform the signal:

In [7]:
n_chunks = len(wave)//n

chunked = wave[:n_chunks*n].reshape(-1, n)
coeffs = np.dot(IDFT, chunked.T).T
In [8]:
pt.figure(figsize=(10,8))
pt.imshow(np.log10(1e-5+np.abs(coeffs)).T)
pt.colorbar()
Out[8]:
<matplotlib.colorbar.Colorbar at 0x7f29ec90b860>
  • What do you observe around the "s" sounds?
In [41]:
modified = coeffs.copy()

#modified[:,5:] *= 1e-2

#modified[:,:30] *= 1e-2

nshift = 3
modified.fill(0)
modified[:, nshift:] = coeffs[:, 0:-nshift]

pt.figure(figsize=(8,5))
pt.imshow(np.log10(1e-5+abs(modified)).T)
pt.colorbar()
Out[41]:
<matplotlib.colorbar.Colorbar at 0x7f29e640f780>
In [42]:
result = np.dot(DFT, modified.T).T
result = result.reshape(-1)
In [43]:
new_wave = np.zeros_like(wave)
new_wave[:len(result)] = result
new_wave = new_wave/np.max(np.abs(new_wave))
In [47]:
get_html5_wave_player(new_wave)
Out[47]:
In [46]:
pt.plot(new_wave)
Out[46]:
[<matplotlib.lines.Line2D at 0x7f29e63fa6d8>]
In []: