-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputeFeature.m
executable file
·106 lines (95 loc) · 3.29 KB
/
ComputeFeature.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
% ======================================================================
%> @brief computes a feature from the audio data
%>
%> supported features are:
%> 'SpectralCentroid',
%> 'SpectralCrest',
%> 'SpectralDecrease',
%> 'SpectralFlatness',
%> 'SpectralFlux',
%> 'SpectralKurtosis',
%> 'SpectralMfccs',
%> 'SpectralPitchChroma',
%> 'SpectralRolloff',
%> 'SpectralSkewness',
%> 'SpectralSlope',
%> 'SpectralSpread',
%> 'SpectralTonalPowerRatio',
%> 'TimeAcfCoeff',
%> 'TimeMaxAcf',
%> 'TimePeakEnvelope',
%> 'TimePredictivityRatio',
%> 'TimeRms',
%> 'TimeStd',
%> 'TimeZeroCrossingRate',
%>
%> @param cFeatureName: feature to compute, e.g. 'SpectralSkewness'
%> @param afAudioData: time domain sample data, dimension channels X samples
%> @param f_s: sample rate of audio data
%> @param afWindow: FFT window of length iBlockLength (default: hann), can be [] empty
%> @param iBlockLength: internal block length (default: 4096 samples)
%> @param iHopLength: internal hop length (default: 2048 samples)
%>
%> @retval v feature value
%> @retval t time stamp for the feature value
% ======================================================================
function [v, t] = ComputeFeature (cFeatureName, afAudioData, f_s, afWindow, iBlockLength, iHopLength)
% set feature function handle
hFeatureFunc = str2func (['Feature' cFeatureName]);
% set default parameters if necessary
if (nargin < 6)
iHopLength = 2048;
end
if (nargin < 5)
iBlockLength = 4096;
end
% pre-processing: down-mixing
if (size(afAudioData,2)> 1)
afAudioData = mean(afAudioData,2);
end
% pre-processing: normalization (not necessary for many features)
if (length(afAudioData)> 1)
afAudioData = afAudioData/max(abs(afAudioData));
end
if (IsSpectral(cFeatureName))
if (nargin < 4 || isempty(afWindow))
afWindow = hann(iBlockLength,'periodic');
end
% compute FFT window function
if (length(afWindow) ~= iBlockLength)
error('window length mismatch');
end
% in the real world, we would do this block by block...
[X,f,t] = spectrogram( afAudioData,...
afWindow,...
iBlockLength-iHopLength,...
iBlockLength,...
f_s);
% magnitude spectrum
X = abs(X)*2/iBlockLength;
% compute feature
v = hFeatureFunc(X, f_s);
end %if (IsSpectral(cFeatureName))
if (IsTemporal(cFeatureName))
% compute feature
[v,t] = hFeatureFunc(afAudioData, iBlockLength, iHopLength, f_s);
end %if (IsTemporal(cFeatureName))
end
function [bResult] = IsSpectral(cName)
bResult = false;
iIdx = strfind(cName, 'Spectral');
if (~isempty(iIdx))
if (iIdx(1) == 1)
bResult = true;
end
end
end
function [bResult] = IsTemporal(cName)
bResult = false;
iIdx = strfind(cName, 'Time');
if (~isempty(iIdx))
if (iIdx(1) == 1)
bResult = true;
end
end
end