Audio Signal Processing
67. Audio Signal Processing#
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
plt.style.use('seaborn-poster')
import scipy as sp
from scipy import signal
from scipy.io.wavfile import read
import sys
import soundfile as sf
I obtained free sound samples from
%%bash
tree .
.
├── audio
│ ├── Elephant-SoundBible.com-551032783.wav
│ ├── Humpback-Whale-Feeding-Calls-SoundBible.com-195157311.wav
│ ├── Humpback-Whale-SoundBible.com-156495049.wav
│ ├── Humpback-Whale-SoundBible.com-93645231.wav
│ ├── baby-snoring-daniel_simon.wav
│ ├── flock-of-seagulls.wav
│ └── snoring-male.wav
├── audio-processing.ipynb
├── geodata.ipynb
└── time-series-data.ipynb
1 directory, 10 files
# path = "./audio/snoring-male.wav"
path = "./audio/baby-snoring-daniel_simon.wav"
# path = "./audio/Elephant-SoundBible.com-551032783.wav"
# path = "./audio/flock-of-seagulls.wav"
# path = "./audio/Humpback-Whale-SoundBible.com-93645231.wav"
# ## Using Scipy.io
# data = read(path)
# fs = data[0]
# audio = data[1]
# audio = np.transpose(audio)
# ampl = np.max(audio[0])
# ampr = np.max(audio[1])
# time = np.arange( len(audio[0]) ) / float(fs)
## Using SF
data = sf.read(path)
fs = data[1]
audio = data[0][:50000]
audio = np.transpose(audio)
ampl = np.max(audio[0])
ampr = np.max(audio[1])
time = np.arange( len(audio[0]) ) / float(fs)
plt.plot(time, audio[0],'r-',label="left")
plt.plot(time, audio[1],'k--',label='right')
plt.ylabel("Amplitude")
plt.xlabel("Time (s)")
plt.title("Snoring Wav")
plt.legend()
plt.show()
winlen = 1024
# norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()),
# cmap='PuBu_r'
f, t, Zxx = signal.stft(audio[0], fs, nperseg=winlen)
plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, cmap='hot')
# plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, norm=colors.LogNorm(vmin=0.01,vmax=10), cmap='hot')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.ylim([0,2000])
# plt.xlim([0,2.5])
plt.show()
winlen = 512
# norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()),
# cmap='PuBu_r'
f, t, Zxx = signal.stft(audio[0], fs, nperseg=winlen)
plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, cmap='hot')
# plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, norm=colors.LogNorm(vmin=0.01,vmax=10), cmap='hot')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.ylim([0,2000])
# plt.xlim([0,2.5])
plt.show()
winlen = 256
# norm=colors.LogNorm(vmin=Z1.min(), vmax=Z1.max()),
# cmap='PuBu_r'
f, t, Zxx = signal.stft(audio[0], fs, nperseg=winlen)
plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, cmap='hot')
# plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=ampl, norm=colors.LogNorm(vmin=0.01,vmax=10), cmap='hot')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.ylim([0,2000])
# plt.xlim([0,2.5])
plt.show()