Repository navigation
Expand file tree
/
Copy pathFinalProject_Model.py
More file actions
292 lines (267 loc) · 13.9 KB
/
Copy pathFinalProject_Model.py
File metadata and controls
292 lines (267 loc) · 13.9 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import numpy as np
from scipy.io import wavfile
import matplotlib.pyplot as plt
from pydub import AudioSegment
from scipy.signal import find_peaks
import os
class Model:
def __init__(self, file_name):
self.spectrum = None
self.freqs = None
self.t = None
self.im = None
self.file_name = file_name
self.rt60high = None
self.rt60mid = None
self.high_frequency = None
self.mid_frequency = None
self.low_frequency = None
self.resonance_frequency = None
self.fft_min = None
self.frequencies = None
self.fft_result = None
self.time = None
self.length = None
self.data = None
self.sample_rate = None
self.rt60low = None
# use property decorator to use getters & setters
# define file an attribute of Model
@property
def file(self):
return self.file
@file.setter
def file(self, value):
if '.wav' in value.lower():
self.file_name = value
elif '.mp3' in value.lower():
wav_file = os.path.splitext(value)[0] + '.wav'
sound = AudioSegment.from_mp3(value)
sound.export(wav_file, format='wav')
self.file_name = wav_file
else:
raise ValueError(f'{value} is neither a MP3 nor a WAV file)')
def laf(self):
# Merges Audio Channels
sound = AudioSegment.from_wav(self.file_name)
sound = sound.set_channels(1)
sound.export('mono_channel.wav', format="wav")
self.file_name = 'mono_channel.wav'
self.sample_rate, self.data = wavfile.read(self.file_name)
self.length = self.data.shape[0] / self.sample_rate
self.time = np.linspace(0., self.length, self.data.shape[0])
# Perform FFT on the audio data
self.fft_result = abs(np.fft.fft(self.data))
self.frequencies = abs(np.fft.fftfreq(len(self.fft_result), d=1 / self.sample_rate))
self.fft_min = np.max(self.fft_result) * .1
# Find peaks in the frequency domain
peaks, _ = find_peaks(np.abs(self.fft_result), self.fft_min)
# Find the highest peak (resonance frequency)
highest_peak_index = np.argmax(np.abs(self.fft_result[peaks]))
self.resonance_frequency = self.frequencies[peaks[highest_peak_index]]
# Find the low frequency
self.low_frequency = np.min(self.frequencies[peaks])
# Find the mid frequency
self.mid_frequency = np.median(np.abs(self.frequencies[peaks]))
# Find the high frequency
self.high_frequency = np.max(self.frequencies[peaks])
self.spectrum, self.freqs, self.t, self.im = plt.specgram(self.data, Fs=self.sample_rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
# function to plot waveform of the FFT
def plotFFT(self):
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('FFT Waveform')
fig.supxlabel('Frequency (Hz)')
fig.supylabel('Amplitude')
ax = fig.add_subplot(111).plot(self.frequencies, self.fft_result)
fig.legend(ax, ['Channel 1'])
return fig
# Function to plot spectrogram (one additional plot assigned)
def plotSpectrogram(self):
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('Spectrogram')
fig.supxlabel('Time (s)')
fig.supylabel('Frequency (Hz)')
ax = fig.add_subplot(111).specgram(self.data, Fs=self.sample_rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
cbar = fig.colorbar(self.im)
cbar.set_label('Intensity (dB)')
return fig
#waveform graph
def plotTimeAmp(self):
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('Time/Amplitude Waveform')
fig.supxlabel('Time(s)')
fig.supylabel('Amplitude')
ax = fig.add_subplot(111).plot(self.time, self.data)
fig.legend(ax, ['Channel 1'])
return fig
#rt60 mid graph
def plotRT60mid(self):
data_in_db_mid = self.frequency_check_mid()
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('RT60 Mid Frequency')
fig.supxlabel('Time (s)')
fig.supylabel('Power (dB)')
index_of_max_mid = np.argmax(data_in_db_mid)
value_of_max_mid = data_in_db_mid[index_of_max_mid]
sliced_array_mid = data_in_db_mid[index_of_max_mid:]
value_of_max_less_5_mid = value_of_max_mid - 5
value_of_max_less_5_mid = self.find_nearest_value(sliced_array_mid, value_of_max_less_5_mid)
index_of_max_less_5_mid = np.where(data_in_db_mid == value_of_max_less_5_mid)
value_of_max_less_25_mid = value_of_max_mid - 25
value_of_max_less_25_mid = self.find_nearest_value(sliced_array_mid,value_of_max_less_25_mid)
index_of_max_less_25_mid = np.where(data_in_db_mid == value_of_max_less_25_mid)
rt20 = (self.t[index_of_max_less_5_mid]-self.t[index_of_max_less_25_mid])[0]
rt60 = 3 * rt20
self.rt60mid = round(abs(rt60),2)
ax = fig.add_subplot(111).plot(self.t, data_in_db_mid, '-m',
self.t[index_of_max_mid], data_in_db_mid[index_of_max_mid], 'go',
self.t[index_of_max_less_5_mid], data_in_db_mid[index_of_max_less_5_mid], 'yo',
self.t[index_of_max_less_25_mid], data_in_db_mid[index_of_max_less_25_mid], 'ro',
linewidth=1)
fig.legend(ax, ['Mid Frequency'])
return fig
def plotRT60low(self):
data_in_db_low = self.frequency_check_low()
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('RT60 Low Frequency')
fig.supxlabel('Time (s)')
fig.supylabel('Power (dB)')
index_of_max_low = np.argmax(data_in_db_low)
value_of_max_low = data_in_db_low[index_of_max_low]
sliced_array_low = data_in_db_low[index_of_max_low:]
value_of_max_less_5_low = value_of_max_low - 5
value_of_max_less_5_low = self.find_nearest_value(sliced_array_low, value_of_max_less_5_low)
index_of_max_less_5_low = np.where(data_in_db_low == value_of_max_less_5_low)
value_of_max_less_25_low = value_of_max_low - 25
value_of_max_less_25_low = self.find_nearest_value(sliced_array_low,value_of_max_less_25_low)
index_of_max_less_25_low = np.where(data_in_db_low == value_of_max_less_25_low)
rt20 = (self.t[index_of_max_less_5_low]-self.t[index_of_max_less_25_low])[0]
rt60 = 3 * rt20
self.rt60low = round(abs(rt60),2)
ax = fig.add_subplot(111).plot(self.t, data_in_db_low, '-c',
self.t[index_of_max_low], data_in_db_low[index_of_max_low], 'go',
self.t[index_of_max_less_5_low], data_in_db_low[index_of_max_less_5_low], 'yo',
self.t[index_of_max_less_25_low], data_in_db_low[index_of_max_less_25_low], 'ro',
linewidth=1)
fig.legend(ax, ['Low Frequency'])
return fig
def plotRT60high(self):
data_in_db_high = self.frequency_check_high()
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('RT60 High Frequency')
fig.supxlabel('Time (s)')
fig.supylabel('Power (dB)')
index_of_max_high = np.argmax(data_in_db_high)
value_of_max_high = data_in_db_high[index_of_max_high]
sliced_array_high = data_in_db_high[index_of_max_high:]
value_of_max_less_5_high = value_of_max_high - 5
value_of_max_less_5_high = self.find_nearest_value(sliced_array_high, value_of_max_less_5_high)
index_of_max_less_5_high = np.where(data_in_db_high == value_of_max_less_5_high)
value_of_max_less_25_high = value_of_max_high - 25
value_of_max_less_25_high = self.find_nearest_value(sliced_array_high,value_of_max_less_25_high)
index_of_max_less_25_high = np.where(data_in_db_high == value_of_max_less_25_high)
rt20 = (self.t[index_of_max_less_5_high]-self.t[index_of_max_less_25_high])[0]
rt60 = 3 * rt20
self.rt60high = round(abs(rt60),2)
ax = fig.add_subplot(111).plot(self.t, data_in_db_high, '-k',
self.t[index_of_max_high], data_in_db_high[index_of_max_high], 'go',
self.t[index_of_max_less_5_high], data_in_db_high[index_of_max_less_5_high], 'yo',
self.t[index_of_max_less_25_high], data_in_db_high[index_of_max_less_25_high], 'ro',
linewidth=1)
fig.legend(ax, ['High Frequency'])
return fig
def plotRT60Combined(self):
#figure setup
fig = plt.Figure(figsize=(5, 4), dpi=100)
fig.suptitle('RT60 Combined')
fig.supxlabel('Time (s)')
fig.supylabel('Power (dB)')
#data for low
data_in_db_low = self.frequency_check_low()
index_of_max_low = np.argmax(data_in_db_low)
value_of_max_low = data_in_db_low[index_of_max_low]
sliced_array_low = data_in_db_low[index_of_max_low:]
value_of_max_less_5_low = value_of_max_low - 5
value_of_max_less_5_low = self.find_nearest_value(sliced_array_low, value_of_max_less_5_low)
index_of_max_less_5_low = np.where(data_in_db_low == value_of_max_less_5_low)
value_of_max_less_25_low = value_of_max_low - 25
value_of_max_less_25_low = self.find_nearest_value(sliced_array_low, value_of_max_less_25_low)
index_of_max_less_25_low = np.where(data_in_db_low == value_of_max_less_25_low)
#data for mid
data_in_db_mid = self.frequency_check_mid()
index_of_max_mid = np.argmax(data_in_db_mid)
value_of_max_mid = data_in_db_mid[index_of_max_mid]
sliced_array_mid = data_in_db_mid[index_of_max_mid:]
value_of_max_less_5_mid = value_of_max_mid - 5
value_of_max_less_5_mid = self.find_nearest_value(sliced_array_mid, value_of_max_less_5_mid)
index_of_max_less_5_mid = np.where(data_in_db_mid == value_of_max_less_5_mid)
value_of_max_less_25_mid = value_of_max_mid - 25
value_of_max_less_25_mid = self.find_nearest_value(sliced_array_mid, value_of_max_less_25_mid)
index_of_max_less_25_mid = np.where(data_in_db_mid == value_of_max_less_25_mid)
#data for high
data_in_db_high = self.frequency_check_high()
index_of_max_high = np.argmax(data_in_db_high)
value_of_max_high = data_in_db_high[index_of_max_high]
sliced_array_high = data_in_db_high[index_of_max_high:]
value_of_max_less_5_high = value_of_max_high - 5
value_of_max_less_5_high = self.find_nearest_value(sliced_array_high, value_of_max_less_5_high)
index_of_max_less_5_high = np.where(data_in_db_high == value_of_max_less_5_high)
value_of_max_less_25_high = value_of_max_high - 25
value_of_max_less_25_high = self.find_nearest_value(sliced_array_high, value_of_max_less_25_high)
index_of_max_less_25_high = np.where(data_in_db_high == value_of_max_less_25_high)
#add to fig
ax = fig.add_subplot(111).plot(self.t, data_in_db_low, '-c',
self.t, data_in_db_mid, '-m',
self.t, data_in_db_high, '-k',
#dots for low
self.t[index_of_max_low], data_in_db_low[index_of_max_low], 'go',
self.t[index_of_max_less_5_low], data_in_db_low[index_of_max_less_5_low], 'yo',
self.t[index_of_max_less_25_low], data_in_db_low[index_of_max_less_25_low], 'ro',
#dots for mid
self.t[index_of_max_mid], data_in_db_mid[index_of_max_mid], 'go',
self.t[index_of_max_less_5_mid], data_in_db_mid[index_of_max_less_5_mid], 'yo',
self.t[index_of_max_less_25_mid], data_in_db_mid[index_of_max_less_25_mid], 'ro',
#dots for high
self.t[index_of_max_high], data_in_db_high[index_of_max_high], 'go',
self.t[index_of_max_less_5_high], data_in_db_high[index_of_max_less_5_high], 'yo',
self.t[index_of_max_less_25_high], data_in_db_high[index_of_max_less_25_high], 'ro',
linewidth = 1)
fig.legend(ax,['Low Frequency', 'Medium Frequency', 'High Frequency'])
return fig
def find_low_frequency(self, freqs):
for x in freqs:
if x > self.low_frequency:
break
return x
def find_high_frequency(self, freqs):
for x in freqs:
if x > self.high_frequency:
break
return x
def find_mid_frequency(self, freqs):
for x in freqs:
if x > self.mid_frequency:
break
return x
def frequency_check_mid(self):
target_frequency = self.find_mid_frequency(self.freqs)
index_of_frequency = np.where(self.freqs == target_frequency)[0][0]
data_for_frequency = self.spectrum[index_of_frequency]
data_in_db = 10*np.log10(data_for_frequency)
return data_in_db
def frequency_check_low(self):
target_frequency = self.find_low_frequency(self.freqs)
index_of_frequency = np.where(self.freqs == target_frequency)[0][0]
data_for_frequency = self.spectrum[index_of_frequency]
data_in_db = 10*np.log10(data_for_frequency)
return data_in_db
def frequency_check_high(self):
target_frequency = self.find_high_frequency(self.freqs)
index_of_frequency = np.where(self.freqs == target_frequency)[0][0]
data_for_frequency = self.spectrum[index_of_frequency]
data_in_db = 10*np.log10(data_for_frequency)
return data_in_db
def find_nearest_value(self,array,value):
array = np.asarray(array)
idx = (np.abs(array-value)).argmin()
return array[idx]