Fourier Demos with Audio¶

In [1]:
import numpy as np
from IPython.display import Audio
import scipy.io.wavfile
import matplotlib.pyplot as plt
from numpy.fft import fft, ifft, fft2, ifft2, fftshift, ifftshift
#from scipy import ndimage, misc
from scipy.signal import gaussian
from joSigProc import *

Examples of Frequency Modulation (filtering)¶

Frequency Filtering in Music¶

In [2]:
# Widening filter on voice
# Hear how the voice starts muffled, but gets more clear?
Omega, f = scipy.io.wavfile.read('audio/Stamp.wav')
Audio(f[15*Omega:32*Omega,0], rate=Omega)
#scipy.io.wavfile.write('Stamp_15-32.wav', Omega, f[15*Omega:32*Omega,0])
Out[2]:
Your browser does not support the audio element.
In [3]:
# Let's remove the HIGH frequencies
Omega, f = scipy.io.wavfile.read('audio/Stamp.wav')
f1 = f[32*Omega:42*Omega,0]
N = len(f1)
g1 = FilterSignal(f1, 0.05*N, band='low')
PlotFT(f1, Omega, fig=1, color='0.75')
PlotFT(g1, Omega, fig=1, clf=False)
Audio(g1, rate=Omega)
Out[3]:
Your browser does not support the audio element.
In [4]:
# Let's remove the LOW frequencies.
g2 = FilterSignal(f1, 0.05*N, band='high')
PlotFT(f1, Omega, fig=1, color='0.75')
PlotFT(g2, Omega, fig=1, clf=False)
Audio(g2, rate=Omega)
Out[4]:
Your browser does not support the audio element.
In [5]:
# Let's add those 2 sounds together.
Audio(g1+g2, rate=Omega)
Out[5]:
Your browser does not support the audio element.
In [6]:
# Another example.
Omega, r = scipy.io.wavfile.read('audio/amazing_sound.wav')
Audio(r[:,0], rate=Omega)
Out[6]:
Your browser does not support the audio element.
In [7]:
#    :-P
In [7]:
# Narrowing filter on banjo
Omega, f = scipy.io.wavfile.read('audio/TwoHeads.wav')
Audio(f[25*Omega:37*Omega,0], rate=Omega)
Out[7]:
Your browser does not support the audio element.

Separating components¶

In [8]:
# This sound has 2 main components: the voice, and the piano
ff = f[150*Omega:165*Omega,0].copy()
PlotSignal(ff, Omega)
Audio(ff, rate=Omega)
Out[8]:
Your browser does not support the audio element.
In [9]:
FF = np.fft.fftshift(np.fft.fft(ff))
shifted_omega = ShiftedFreqSamples(ff, Omega)
PlotFT(ff, Omega)
In [10]:
# Filter it, leaving only the low frequencies (below 600 Hz).
T = 600
G = FF.copy()
G[abs(shifted_omega)>T] = 0.
plt.figure(1); plt.clf()
PlotFT_raw(shifted_omega, abs(G), color='r');
In [11]:
g_low = np.real(ifft(ifftshift(G)))
g = g_low
Audio(np.real(g), rate=Omega)
Out[11]:
Your browser does not support the audio element.
In [12]:
plt.plot(abs(G));
In [13]:
# A close-up of the non-zero frequencies.
N2 = len(G)//2
w = 10000
plt.figure(1); plt.clf()
PlotFT_raw(shifted_omega[N2-w:N2+w], abs(G[N2-w:N2+w]), color='r');
In [14]:
# Now let's filter it, leaving only the frequencies above 1000 Hz.
T = 1000
G = FF.copy()
G[abs(shifted_omega)<T] = 0.
PlotFT_raw(shifted_omega, abs(G), fig=1, clf=False)
g_high = np.real(ifft(ifftshift(G)))
g = g_high
Audio(np.real(g), rate=Omega)
Out[14]:
Your browser does not support the audio element.
In [ ]: