# Muhammad Hafiz Wan Rosli
# Media Arts & Technology
# Music Information Retrieval
# MAT240E: Homework03b - Audio Feature Extraction I
import essentia
import essentia.standard
import essentia.streaming
import os
from scipy.signal import decimate
%pylab inline
Populating the interactive namespace from numpy and matplotlib
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (16,4)
rShankar = []
for root, dirs, files in os.walk('./Music/iTunes/iTunes Media/Music/Ravi Shankar/Master Of Sitar'):
#print 'album: {0}\n'.format(root)
for name in files:
loader = essentia.standard.AudioLoader(filename = os.path.join(root, name))
rShankar.append(loader())
rShankar[0][0].shape
(57893760, 2)
rShankar0_mono = sum(rShankar[0][0]/2, axis=1)
rShankar0_mono.shape
(57893760,)
rShankar0_mono_dec = decimate(rShankar0_mono, 99)
/usr/local/lib/python2.7/site-packages/scipy/signal/filter_design.py:338: BadCoefficients: Badly conditioned filter coefficients (numerator): the results may be meaningless "results may be meaningless", BadCoefficients)
rShankar0_snippet = rShankar0_mono[5*44100:15*44100]
plot(rShankar0_snippet)
[<matplotlib.lines.Line2D at 0x10057d690>]
win_size = 2048
hop = 1024
window_start = arange(0, len(rShankar0_snippet), hop)
#window_start
rms = []
for start in window_start:
w = rShankar0_snippet[start: start+win_size].astype(float)
rms_inst = sqrt(mean(w**2))
rms.append(rms_inst)
plot(rms)
[<matplotlib.lines.Line2D at 0x113dcf210>]
win_sizes = [512, 1024, 2048, 4096]
rms_windows = []
for win_size in win_sizes:
hop = win_size/2
window_start = arange(0, len(rShankar0_snippet), hop)
rms = []
for start in window_start:
w = rShankar0_snippet[start: start+win_size].astype(float)
rms_inst = sqrt(mean(w**2))
rms.append(rms_inst)
rms_windows.append(rms)
for rms_plot in rms_windows:
plot(rms_plot)
plot(linspace(0, len(rShankar0_snippet), len(rms_windows[-1])), rms_windows[-1], lw=5, color='k')
#???
for rms_plot in rms_windows:
plot(linspace(0, len(rShankar0_snippet), len(rms_plot)), rms_plot)
for rms_plot in rms_windows:
plot(linspace(0, len(rShankar0_snippet), len(rms_plot)), rms_plot, 'x-')
xlim((350000, 380000))
ylim((0, 0.05));
legend(win_sizes)
<matplotlib.legend.Legend at 0x1005ed450>
def windowed_rms(input_sig, win_sizes = [512, 1024, 2048, 4096], hop=None):
rms_windows = []
for win_size in win_sizes:
if not hop:
hop = win_size/2
window_start = arange(0, len(input_sig) - win_size, hop)
rms = []
for start in window_start:
w = input_sig[start: start+win_size].astype(float)
rms_inst = sqrt(mean(w**2))
rms.append(rms_inst)
rms_windows.append(rms)
return rms_windows, win_sizes
audioSource = rShankar0_snippet
w_rms, win_sizes = windowed_rms(audioSource)
for rms_plot in w_rms:
plot(linspace(0, len(audioSource), len(rms_plot)), rms_plot)
plot(linspace(0, len(audioSource), len(w_rms[-1])), w_rms[-1], lw=5, color='k', alpha = 0.5)
[<matplotlib.lines.Line2D at 0x1136d4c50>]
sr = 44100
L = 0.1 # 100ms
win_len = sr * L
print win_len
4410.0
audioSource = rShankar0_mono_dec
w_rms, win_lens = windowed_rms(audioSource, win_sizes=[win_len, win_len*2, win_len*3, win_len*5])
for rms_plot in w_rms:
plot(linspace(0, len(audioSource), len(rms_plot)), rms_plot)
plot(linspace(0, len(audioSource), len(w_rms[-1])), w_rms[-1], lw=5, color='k', alpha = 0.5)
#find rms difference per window/sample
xlabel(len(audioSource))
legend(win_lens)
<matplotlib.legend.Legend at 0x1261bfe50>
for raag in rShankar:
print raag[0]
raag_mono = sum(raag[0]/2, axis=1)
raag_mono_dec = decimate(raag_mono, 99)
audioSource = raag_mono_dec
w_rms, win_lens = windowed_rms(audioSource, win_sizes=[win_len])
for rms_plot in w_rms:
plot(linspace(0, len(audioSource), len(rms_plot)), rms_plot)
#plot(linspace(0, len(audioSource), len(w_rms[-1])), w_rms[-1], lw=5, color='k', alpha = 0.5)
#find rms difference per window/sample
#xlabel(len(audioSource))
legend(win_lens)
[[ 0. 0.] [ 0. 0.] [ 0. 0.] ..., [ 0. 0.] [ 0. 0.] [ 0. 0.]] [[ 0. 0.] [ 0. 0.] [ 0. 0.] ..., [ 0. 0.] [ 0. 0.] [ 0. 0.]] [[ 0. 0.] [ 0. 0.] [ 0. 0.] ..., [ 0. 0.] [ 0. 0.] [ 0. 0.]] [[ 0. 0.] [ 0. 0.] [ 0. 0.] ..., [ 0. 0.] [ 0. 0.] [ 0. 0.]]
<matplotlib.legend.Legend at 0x1161b5810>