-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal_classification.py
100 lines (89 loc) · 3.93 KB
/
signal_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""Untitled0.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1LZ9BX53fhzO6o-zbbmju_Mi6HV1yu7GK
"""
import librosa
import matplotlib.pyplot as plt
import librosa.display
#part1 - preprocessing the signal
#---------------------------------------------------------------------------
#A audio(Music-Jazz) is played for 30 secs and recorded.
#15 lakh plus samples are collected and sampling frequency is 48000.
#Therefore in one second 48000 samples are collected that means
#the total length of signal in time domain is approximately 32
#secs. for analysation we need to have approximately only 50,000
#samples therefore we need to downsample the signal by
#1516541/50,0000 == 30.33 or aproximately 31.
x, sr = librosa.load('/content/jazz-mp3.mp3')
print(sr)
print(len(x))
#length comes out to be 696663, far greater than 50,000. We see that Sr is 22050 meaning that in 1 second
#22050 samples are collected, this shows that the music ran for 699663/22050 secs == 31.73 secs which is true.
#to reduce the no of samples approximately we have to change the sampling rate.
#To find the new sampling rate
audio_time = len(x)//sr
new_sr = 50000//audio_time
print(new_sr)
#new_sr is equal to approx 1582.544
#Load the signal again but this with the new sr
x, sr = librosa.load('/content/jazz-mp3.mp3', sr=new_sr)
print(len(x))
#no of samples now is 50000 exact.
#plotting the graph
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr=new_sr)
#display Spectrogram
#to display the spectogram-change of frequency with time.
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(14, 5))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()
#----------------------------------------------------------------------
#part2 - extracting features
#--------------------------------------------------------------------------
#1 finding Zero crossing point
zero_crossings = librosa.zero_crossings(x[0:50000], pad=False)
print(sum(zero_crossings))
#To visualise the zero crossing rate we zoom in the time v/s amplitude graph
#Zooming in
n0 = 3000
n1 = 3005
plt.figure(figsize=(14, 5))
plt.plot(x[n0:n1])
plt.grid()
#after zooming in by taking the window between 0 to 5 secs we see that the graph crosses 0.000 three times
#thus Zero_crossing_rate is 3 thus the result.
zero_crossings = librosa.zero_crossings(x[0:5], pad=False)
print(sum(zero_crossings))
#2. Spectral centroid
import sklearn
spectral_centroids = librosa.feature.spectral_centroid(x, sr=sr)[0]
#this calculates spectral centroids for each frame in the spectrum.
spectral_centroids.shape
frames = range(len(spectral_centroids))
t = librosa.frames_to_time(frames)
def normalize(x, axis=0):
return sklearn.preprocessing.minmax_scale(x, axis=axis)
#librosa.display.waveplot(x, sr=sr, alpha=0.4)
#plt.plot(t, normalize(spectral_centroids), color='r')
#By observing the first graph we observe that spectral centroids are prominent at the beginning.To combat this we add a constant at the beginning
#and plot again.
spectral_centroids = librosa.feature.spectral_centroid(x+0.5, sr=sr)[0]
#librosa.display.waveplot(x, sr=sr, alpha=0.4)#alpha denotes the opacity of the grapn which is less than in order to visualise.
#plt.plot(t, normalize(spectral_centroids), color='b')
#3 Spectral Bandwidth
spectral_bandwidth_2 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr)[0]
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_bandwidth_2), color='g')
#3. Spectral RollOff
spectral_rolloff = librosa.feature.spectral_rolloff(x+0.01, sr=sr)[0]
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_rolloff), color='y')
#4. Tempo
tempo = librosa.beat.tempo(x, sr=sr)
print(tempo.item(0))
#this gives the tempo of the audio signal which is approximately 188.90 for jazz music.
#-------------------------------------------------------------------------------------------------