-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathData processing and cleaning .py
174 lines (148 loc) · 5.86 KB
/
Data processing and cleaning .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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#Developed By: Tonumoy Mukherjee
import os
from tqdm import tqdm #used to wrap any iterable, creates a progress bar to visualize any loop in execution
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc, logfbank
import librosa #default audio library for handling audio files
#import pdb
#function for plotting signals
def plot_signals(signals):
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False,
sharey=True, figsize=(20,5))
#fig.set_xlabel('Time(s)')
#fig.set_ylabel('Amplitude')
axes = axes.reshape(2,1)
fig.suptitle('Time Series', size=16)
i = 0
for x in range(2):
for y in range(1):
#pdb.set_trace()
axes[x,y].set_title(list(signals.keys())[i], size=9)
axes[x,y].plot(list(signals.values())[i])
#pdb.set_trace()
axes[x,y].get_xaxis().set_visible(True)
axes[x,y].get_yaxis().set_visible(True)
axes[x,y].set_xlabel('time (s)',fontsize='large',fontweight='bold')
axes[x,y].set_ylabel('Amplitude',fontsize='large',fontweight='bold')
i += 1
#function for plotting the Fourier Transform
def plot_fft(fft):
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False,
sharey=True, figsize=(20,5))
axes = axes.reshape(2,1)
fig.suptitle('Fourier Transforms', size=16)
i = 0
for x in range(2):
for y in range(1):
data = list(fft.values())[i]
Y, freq = data[0], data[1]
axes[x,y].set_title(list(fft.keys())[i])
axes[x,y].plot(freq, Y)
axes[x,y].get_xaxis().set_visible(True)
axes[x,y].get_yaxis().set_visible(True)
axes[x,y].set_xlabel('time (s)',fontsize='large',fontweight='bold')
axes[x,y].set_ylabel('Amplitude',fontsize='large',fontweight='bold')
i += 1
#function for plotting the Filter Bank Coefficients
def plot_fbank(fbank):
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False,
sharey=True, figsize=(20,5))
axes = axes.reshape(2,1)
#fig.suptitle('Filter Bank Coefficients', size=16)
i = 0
for x in range(2):
for y in range(1):
axes[x,y].set_title(list(fbank.keys())[i],fontsize='18',fontweight='bold')
axes[x,y].imshow(list(fbank.values())[i],
cmap='Spectral', interpolation='nearest')
axes[x,y].get_xaxis().set_visible(True)
axes[x,y].get_yaxis().set_visible(True)
axes[x,y].set_xlabel('time (s)',fontsize='18', fontweight='bold')
axes[x,y].set_ylabel('Frequency(KHz)',fontsize='18', fontweight='bold')
i += 1
#function for the envelope function to detect blank spaces in the signal and
#removing them with respect to a particular threshold
def envelope(y,rate,threshold):
mask =[]
y = pd.Series(y).apply(np.abs)
y_mean = y.rolling(window=int(rate/10), min_periods=1, center=True).mean()
for mean in y_mean:
if mean > threshold :
mask.append(True)
else:
mask.append(False)
return mask
#function for calulating the FFT of the I/P signal
def calc_fft(y,rate):
n = len(y)
freq = np.fft.rfftfreq(n, d=1/rate)
Y = abs(np.fft.rfft(y)/n) #normalizing the frequency magnitude(Y)
return(Y,freq)
#function for plotting the Mel Frequency Cepstrum Coefficients (MFCC)
def plot_mfccs(mfccs):
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False,
sharey=True, figsize=(20,5))
axes = axes.reshape(2,1)
#fig.suptitle('Mel Frequency Cepstrum Coefficients', fontweight='bold',size=16)
i = 0
for x in range(2):
for y in range(1):
axes[x,y].set_title(list(mfccs.keys())[i],fontsize='20',fontweight='bold')
axes[x,y].imshow(list(mfccs.values())[i],
cmap='Blues_r', interpolation='nearest')
axes[x,y].get_xaxis().set_visible(True)
axes[x,y].get_yaxis().set_visible(True)
axes[x,y].set_xlabel('time (s)',fontsize='20', fontweight='bold')
axes[x,y].set_ylabel('MFCC Coefficients',fontsize='20', fontweight='bold')
i += 1
df = pd.read_csv('Quake.csv')
df.set_index('fname', inplace = True)
for f in df.index:
rate,signal = wavfile.read('wavfiles/'+f)
df.at[f,'length'] = signal.shape[0]/rate
classes = list(np.unique(df.label))
class_dist = df.groupby(['label'])['length'].mean()
fig, ax = plt.subplots()
ax.set_title('Class Distribution', y=1.08 )
ax.pie(class_dist, labels = class_dist.index, autopct='%1.1f%%',
shadow=False, startangle =90 )
ax.axis('equal')
plt.show()
df.reset_index(inplace=True)
signals = {}
fft = {}
fbank = {}
mfccs = {}
for c in classes:
wav_file = df[df.label == c].iloc[0,0]
signal, rate = librosa.load('wavfiles/'+wav_file, sr=1000)
mask = envelope(signal, rate, 0.02)
signal = signal[mask]
#signal = signal[0:int(10 * rate)] # Keep the first 3.5 seconds
#signal = signal.shape[0]/rate
#print(c)
signals[c] = signal
fft[c] = calc_fft(signal,rate)
bank = logfbank(signal[:rate],rate, nfilt=26, nfft=256).T
fbank[c] = bank
mel = mfcc(signal[:rate], rate, numcep=13, nfilt=26, nfft=256).T
mfccs[c] = mel
plot_signals(signals)
#plt.xlabel('Time(s)', size=7)
#plt.ylabel('Amplitude', size=7)
plt.show()
plot_fft(fft)
plt.show()
plot_fbank(fbank)
plt.show()
plot_mfccs(mfccs)
plt.show()
#Writing Clean files into folder
if len(os.listdir('clean_train')) == 0:
for f in tqdm(df.fname):
signal,rate = librosa.load('wavfiles/'+f, sr=1000)
mask = envelope(signal, rate, 0.02)
wavfile.write(filename ='clean_train/'+f, rate=rate, data = signal[mask])