-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudioFeatureExtraction.m
120 lines (90 loc) · 2.49 KB
/
audioFeatureExtraction.m
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
function [ output_args ] = audioFeatureExtraction( y, Fs, blockSize, hopSize )
%********************************************
% Computational Music Analysis
% Assignment 2 - Genre Classification
%
% Audio Feature Extraction Function
%
% Imankalyan Mukherjee, Govinda Ram Pingali
%********************************************
%-----------------------------------------------------------------------
% a) Implement 5 audio features with a 2048 block size and 1024 hop size
%-----------------------------------------------------------------------
nSamples = length(y);
nBlocks = floor(nSamples/hopSize);
% STFT of signal
stftCoefs = shortTermFT(y,Fs,blockSize,hopSize,0);
nBands = length(stftCoefs);
%*** 5 Audio Features ***%
% i) Spectral Centroid
spCentroid = zeros(nBlocks,1);
for n=1:nBlocks
numr = 0;
denr = 0;
for k=1:nBands
square = abs(stftCoefs(k,n)^2);
numrTemp = k*square;
numr = numr + numrTemp;
denr = denr + square;
end
% Checking for NaNs
if denr==0
spCentroid(n) = 0;
else
spCentroid(n) = numr/denr;
end
end
% ii) Max Envelope
maxEnv = zeros(nBlocks,1);
index = 1;
for n=1:hopSize:nSamples-blockSize
maxEnv(index) = max(y(n:n+blockSize));
index = index+1;
end
% iii) Zero Crossing Rate
zcr = zeros(nBlocks,1);
index = 1;
for n=1:hopSize:nSamples-blockSize
zcSum = 0;
for i=n+1:n+blockSize
sig = abs(sign(y(i))-sign(y(i-1)));
zcSum = zcSum + sig;
end
zcr(index) = zcSum/(2*blockSize);
index = index+1;
end
% iv) Spectral Crest Factor
spCrest = zeros(nBlocks,1);
for n=1:nBlocks
numr = max(abs(stftCoefs(:,n)));
denr = sum(abs(stftCoefs(:,n)));
% Checking for NaNs
if denr==0
spCrest(n) = 0;
else
spCrest(n) = numr/denr;
end
end
% v) Spectral Flux
spFlux = zeros(nBlocks,1);
for n=2:nBlocks
tempSum = 0;
for k=1:nBands
temp = (abs(stftCoefs(k,n)) - abs(stftCoefs(k,n-1))) ^ 2;
tempSum = tempSum + temp;
end
spFlux(n) = (2*sqrt(tempSum))/blockSize;
end
% Computing mean and standard deviation of each audio feature
meanSpCentroid = mean(spCentroid);
meanMaxEnv = mean(maxEnv);
meanZcr = mean(zcr);
meanSpCrest = mean(spCrest);
meanSpFlux = mean(spFlux);
stdSpCentroid = std(spCentroid);
stdMaxEnv = std(maxEnv);
stdZcr = std(zcr);
stdSpCrest = std(spCrest);
stdSpFlux = std(spFlux);
output_args = [meanSpCentroid meanMaxEnv meanZcr meanSpCrest meanSpFlux stdSpCentroid stdMaxEnv stdZcr stdSpCrest stdSpFlux];
end