forked from alexanderlerch/ACA-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputeNoveltyFunction.m
79 lines (65 loc) · 2.5 KB
/
ComputeNoveltyFunction.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
% ======================================================================
%> @brief computes the novelty function for onset detection
%>
%> supported novelty measures are:
%> 'Flux',
%> 'Laroche',
%> 'Hainsworth'
%>
%> @param cNoveltyName: name of the novelty measure
%> @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: 512 samples)
%>
%> @retval f frequency
%> @retval t time stamp for the frequency value
%> @retval iPeaks indices of picked onset times
% ======================================================================
function [d, t, iPeaks] = ComputeNoveltyFunction (cNoveltyName, afAudioData, f_s, afWindow, iBlockLength, iHopLength)
% set function handle
hNoveltyFunc = str2func (['Novelty' cNoveltyName]);
% set default parameters if necessary
if (nargin < 6)
iHopLength = 512;
end
if (nargin < 5)
iBlockLength = 4096;
end
if (nargin < 4 || isempty(afWindow))
afWindow = hann(iBlockLength,'periodic');
end
% compute FFT window function
if (length(afWindow) ~= iBlockLength)
error('window length mismatch');
end
fLengthLpInS = 0.3;
iLengthLp = max(2,ceil(fLengthLpInS*f_s/iHopLength));
% pre-processing: down-mixing
if (size(afAudioData,2)> 1)
afAudioData = mean(afAudioData,2);
end
% pre-processing: normalization (not necessary for many features)
if (size(afAudioData,2)> 1)
afAudioData = afAudioData/max(abs(afAudioData));
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;
% novelty function
d = hNoveltyFunc(X, f_s);
% smooth novelty function
b = ones(10,1)/10;
d = filtfilt (b,1,d);
d(d<0) = 0;
% compute threshold
b = ones(iLengthLp,1)/iLengthLp;
G_T = .5*mean(d(2:end)) + filtfilt (b,1,d);
[fPeaks,iPeaks] = findpeaks(max(0,d-G_T));
end