-
Notifications
You must be signed in to change notification settings - Fork 1
/
kombMfccSpektar.py
77 lines (57 loc) · 2.21 KB
/
kombMfccSpektar.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
import numpy as np
from matplotlib import pyplot as plt
from scipy.io import wavfile
import scipy.fftpack as fourier
import random
import pandas as pd
import os, csv
from numpy.core.fromnumeric import shape
from numpy.core.numeric import correlate
import pydub
import numpy as np
import scipy
from audiolazy import *
from scipy.fftpack import dct
from python_speech_features import mfcc
data = pd.read_csv('data.csv')
paths = np.array(data['path'])
genres = np.array(data['genre'])
brojmfcc = 12
brojSpektar = 64
cep_lifter = 22
n = brojmfcc + brojSpektar
features = np.zeros((1,n))
for song_path in paths:
# Reading audio
Fs, signal = scipy.io.wavfile.read(song_path)
mfccKoef = mfcc(signal, samplerate = Fs,numcep = brojmfcc,highfreq=Fs/2,winfunc = np.hamming)
mfccKoef = np.sum(mfccKoef, axis = 0)
mfccKoef = np.reshape(mfccKoef, (1,len(mfccKoef)) )
furije = abs(fourier.fft(signal))
furije = furije[:len(furije)//2]
furije = furije[0:len(furije)//32 *32] #odsecemo poslednje elemente niza tako da duzina bude deljiva sa 32
logaritam = np.log(furije)
duzinaSegmenta = len(furije)//32
furije = np.reshape(furije, (32, duzinaSegmenta) )
logaritam = np.reshape(logaritam, (32, duzinaSegmenta) )
usrednjeno = np.array([ sum(x) // duzinaSegmenta for x in furije ])
usrednjeno = np.reshape(usrednjeno, (1, len(usrednjeno)) )
usrednjenLogaritam = np.array([ sum(x) / duzinaSegmenta for x in logaritam ])
usrednjenLogaritam = np.reshape(usrednjenLogaritam, (1, len(usrednjenLogaritam)) )
red = np.concatenate((mfccKoef, usrednjeno, usrednjenLogaritam), axis = 1)
features = np.concatenate((features, red), axis = 0)
features = features[1:]
print(features.shape)
header = []
header.append('genre')
for i in range(n):
header.append('feature'+str(i))
with open("mfccIspektar/mfccIspektarGtzan.csv", "w", newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(header)
for i in range(len(genres)):
x = genres[i]
genre = np.array([x])
feature = features[i]
row = np.concatenate((genre, feature))
writer.writerow(row)