# coding: utf-8 # # 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) # In[5]: from html5_audio import DEFAULT_RATE, get_html5_wave_player get_html5_wave_player(wave) # 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() # * 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() # 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) # In[46]: pt.plot(new_wave) # In[ ]: