forked from jmarkow/nndetector-live
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnndetector_live_spmd_proc.m
More file actions
86 lines (68 loc) · 2.19 KB
/
nndetector_live_spmd_proc.m
File metadata and controls
86 lines (68 loc) · 2.19 KB
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
function nndetector_live_spmd_proc(NETWORK)
%% SPMD DETAILS
lab_dsp = 1;
%% STAGE: SETUP
freq_idx = NETWORK.spec_params.freq_range_ds;
layer0_size = size(NETWORK.layer_weights{1},2);
fft_size = NETWORK.spec_params.fft_size;
win_size = NETWORK.spec_params.win_size;
win = hamming(win_size);
if NETWORK.spec_params.win_overlap > 0
overlap = NETWORK.spec_params.win_overlap;
gap = 0;
else
overlap = 0;
gap = 0 - NETWORK.spec_params.win_overlap;
end
buffer_audio = [];
buffer_fft = [];
% send confirmation
if labSendReceive(lab_dsp, lab_dsp, 1) ~= 1
return
end
%% STAGE: LOOP
while true
% has enough audio data
while length(buffer_audio) >= win_size
% calculate FFT
s = fft(buffer_audio((1 + gap):(win_size + gap)) .* win);
% perform scaling
s = abs(s(freq_idx));
switch NETWORK.spec_params.amp_scaling
case 'db'
s=20*log10(s);
case 'log'
s=log(s);
end
% append to fft buffer (inefficient)
buffer_fft = [buffer_fft; s];
% remove from audio buffer (really inefficient)
buffer_audio = buffer_audio((1 + gap + win_size - overlap):end);
end
% has enough fft data
act = 0;
while length(buffer_fft) >= layer0_size
% only run until first detection (minor optimization)
if ~act
% get input data
in = buffer_fft(1:layer0_size);
% perform scaling (normc or zscore)
switch NETWORK.spec_params.inp_scaling
case 'zscore'
in = zscore(in);
case 'normc'
% equivalent of normc for our purposes
in = bsxfun(@rdivide, in, sqrt(sum(in .^ 2)));
end
% flow activation
[~, act]=nndetector_live_sim_network(in(:), NETWORK);
end
% remove from audio buffer (really inefficient)
buffer_fft = buffer_fft((1 + length(freq_idx)):end);
end
% get audio data
audio_data = labSendReceive(lab_dsp, lab_dsp, act);
% append to audio buffer (inefficient)
buffer_audio = [buffer_audio; audio_data];
end
end