# Muhammad Hafiz Wan Rosli
# Media Arts & Technology
# Music Information Retrieval
# MAT240E: Homework04 - Audio Feature Extraction II
#Spectral Centroid: A measure used in digital signal processing to characterise a spectrum.
#It indicates where the "center of mass" of the spectrum is.
#Perceptually, it has a robust connection with the impression of "brightness" of a sound.
#Ragas are precise melody forms. A raga is not a mere scale. Nor is it a mode.
#Each raga has it's own ascending and descending movement.
#And those subtle touches and usage of microtones, and stresses on particular tones.
#-Ravi Shankar
#Raag: literally colour, hue
#In the Indian musical tradition, ragas are associated with different times of the day, or with seasons.
#Does the spectral content of a raag have any correlation with it's associated time?
#This experiment attempts to find the correlation between the associated time of each raag and it's spectral centroid
import essentia
import essentia.standard
import essentia.streaming
import os
from os.path import join, getsize
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)
sr=44100
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:
# filename = join(root,name)
# songname, filetype = name.split('.')
loader = essentia.standard.AudioLoader(filename = os.path.join(root, name))
rShankar.append(loader())
morningRaag = sum(rShankar[0][0]/2, axis=1)
afternoonRaag = sum(rShankar[1][0]/2, axis=1)
eveningRaag = sum(rShankar[2][0]/2, axis=1)
nightRaag = sum(rShankar[3][0]/2, axis=1)
Pxx1, freqs1, times1, im1 = specgram(morningRaag, NFFT=1028, Fs=sr, window=window_hanning, noverlap=512);
Pxx2, freqs2, times2, im2 = specgram(afternoonRaag, NFFT=1028, Fs=sr, window=window_hanning, noverlap=512);
Pxx3, freqs3, times3, im3 = specgram(eveningRaag, NFFT=1028, Fs=sr, window=window_hanning, noverlap=512);
Pxx4, freqs4, times4, im4 = specgram(nightRaag, NFFT=1028, Fs=sr, window=window_hanning, noverlap=512);
X1 = sqrt(Pxx1)
X2 = sqrt(Pxx2)
X3 = sqrt(Pxx3)
X4 = sqrt(Pxx4)
centroid1 = []
centroid2 = []
centroid3 = []
centroid4 = []
for spec in X1.T:
sc = sum(spec*freqs1)/sum(spec)
centroid1.append(sc)
for spec in X2.T:
sc = sum(spec*freqs2)/sum(spec)
centroid2.append(sc)
for spec in X3.T:
sc = sum(spec*freqs3)/sum(spec)
centroid3.append(sc)
for spec in X4.T:
sc = sum(spec*freqs4)/sum(spec)
centroid4.append(sc)
plot(times1, centroid1, color='r')
plot(times2, centroid2, color='g')
plot(times3, centroid3, color='b')
plot(times4, centroid4, color='k')
axhline(nanmean(centroid1), linewidth=5, color='r')
axhline(nanmean(centroid2), linewidth=5, color='g')
axhline(nanmean(centroid3), linewidth=5, color='b')
axhline(nanmean(centroid4), linewidth=5, color='k')
-c:7: RuntimeWarning: invalid value encountered in double_scalars -c:10: RuntimeWarning: invalid value encountered in double_scalars -c:13: RuntimeWarning: invalid value encountered in double_scalars -c:16: RuntimeWarning: invalid value encountered in double_scalars
<matplotlib.lines.Line2D at 0x1549ae1d0>
axhline(nanmean(centroid1), linewidth=3, color='r')
axhline(nanmean(centroid2), linewidth=3, color='g')
axhline(nanmean(centroid3), linewidth=3, color='b')
axhline(nanmean(centroid4), linewidth=3, color='k')
grid()
ylabel('spectral centroid mean')
figure_title = 'Spectral centroid of four different raags'
legend(('Morning: Raag Alahya Bilawal', 'Afternoon: Raag Samant Sarang', 'Evening: Raag Pancham Se Ghara', 'Night: Raag Bihag'))
<matplotlib.legend.Legend at 0x15b7aa6d0>