-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_visualization.py
104 lines (84 loc) · 3.72 KB
/
data_visualization.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
#Konrad Maciejczyk, 2023, Wrocław Uniwersity of Science and Technology
import pandas as pd
import librosa
from random import randint, choice
import matplotlib.pyplot as plt
import numpy as np
from algorithms import fft
def loadCSVtoDataframe(path):
datapd = pd.read_csv(path)
genres = datapd['label'].unique()
datapd['label'] = pd.factorize(datapd['label'])[0]
return datapd, genres, datapd.shape
def loadAudioFile(genre = 'metal', track_num = '00054'):
file_path = f'genres_original/{genre}/{genre}.{track_num}.wav'
(samples, sampling_rate) = librosa.load(file_path, sr = None, mono = True, offset = 0.0, duration = None)
duration = len(samples) / sampling_rate
return samples, sampling_rate
def showAudioWavePlot(samples, sampling_rate, track="metal.00054", interval_start = 5, interval_length = 5):
a = sampling_rate * interval_start
b = (interval_length + interval_start) * sampling_rate
plt.figure(figsize = (14, 6), dpi = 120)
librosa.display.waveshow(y = samples[a: b], sr = sampling_rate)
plt.xlabel('Time (seconds)')
plt.ylabel('Amplitude')
plt.title(track + f' ({str(interval_start)}s - {str(interval_start + interval_length)}s)')
plt.show()
def showFrequencies(samples, sampling_rate, track="metal.00054", interval_start = 5, interval_length = 5):
a = sampling_rate * interval_start
b = (interval_length + interval_start) * sampling_rate
T = 1/sampling_rate
n = len(samples[a: b])
y = fft(samples[a: b])
x = np.linspace(0.0, 1.0/(2.0*T), n//2)
plt.figure(figsize = (14, 6), dpi = 120)
plt.plot(x, 2.0/n * np.abs(y[:n//2]))
plt.grid()
plt.xlabel('Frequency')
plt.ylabel('Magnitude')
plt.title(track + f' ({str(interval_start)}s - {str(interval_start + interval_length)}s)')
plt.show()
def showFeatureCorrelationHeatmap(datapd):
pear_corr=datapd.drop(['filename', 'label'], axis=1).corr(method='pearson', numeric_only=True)
fig, ax = plt.subplots(figsize=(10,10))
im = ax.imshow(pear_corr, interpolation='nearest')
plt.title("Feature correlation heatmap")
fig.colorbar(im, orientation='vertical', fraction = 0.05)
plt.show()
def showScore(scores, x, dataset_name, colors):
y = []
for idx, classifier in enumerate(x):
y.append(scores[idx, :])
xpos = np.arange(len(y[0]))
barwidth = 0.1
plt.figure(figsize = (10, 7))
for idx, classifier in enumerate(x):
plt.bar(xpos + (0.15 * idx), y[idx], color=colors[idx], width = barwidth, label=classifier)
plt.legend(bbox_to_anchor=(1.0, 1.0), fontsize="20")
plt.title(f"{dataset_name} - classifiers predicition accuracy")
plt.xlabel("Repeats")
plt.ylabel("Scorage in %")
plt.show()
if __name__ == "__main__":
#Load CSV
datapd, genres, dataset_shape = loadCSVtoDataframe('gtzan_extracted_features.csv')
print(f"Genres ({len(genres)}): ", genres)
print(f"Objects: {dataset_shape[0]}, features: {dataset_shape[1] - 2}")
#Show feature correlation heatmap
showFeatureCorrelationHeatmap(datapd)
#Load and visualize audio data
genre = choice(genres)
track_num = randint(1, 100)
track = None
if track_num < 10:
track = '0000' + str(track_num)
elif track_num >= 10 and track_num < 99:
track = '000' + str(track_num)
else:
track = '00100'
samples, sampling_rate = loadAudioFile(genre, track)
duration = len(samples) // sampling_rate
print('Duration of single sound file: {:.1f} seconds.'.format(duration))
showAudioWavePlot(samples, sampling_rate, track=f"{genre}.{track}", interval_start = 5, interval_length = 5)
#Visualize frequencies
showFrequencies(samples, sampling_rate, track=f"{genre}.{track}", interval_start = 5, interval_length = 5)