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 *
# 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])
# 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)
# 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)
# Let's add those 2 sounds together.
Audio(g1+g2, rate=Omega)
# Another example.
Omega, r = scipy.io.wavfile.read('audio/amazing_sound.wav')
Audio(r[:,0], rate=Omega)
# :-P
# Narrowing filter on banjo
Omega, f = scipy.io.wavfile.read('audio/TwoHeads.wav')
Audio(f[25*Omega:37*Omega,0], rate=Omega)
# 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)
FF = np.fft.fftshift(np.fft.fft(ff))
shifted_omega = ShiftedFreqSamples(ff, Omega)
PlotFT(ff, Omega)
# 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');
g_low = np.real(ifft(ifftshift(G)))
g = g_low
Audio(np.real(g), rate=Omega)
plt.plot(abs(G));
# 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');
# 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)