-
Notifications
You must be signed in to change notification settings - Fork 41
/
envelope_follower.m
52 lines (37 loc) · 1.34 KB
/
envelope_follower.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
function [silent_indices] = envelope_follower(x, ba)
%amplitude envelope tracker for data x
%ba - frame preceding or following silent frame
%1 - following(after), 0-preceding(before)
%Envelope Detection based on Hilbert Transform
analy=hilbert(x);
env=abs(analy);
N = length(x);
%applying moving average filter to further smooth envelope
M = 10;
b = 1/M * ones(1,M);
a = 1;
env = filter(b,a,env);
%find signal indices with very low value of amplitude envelope
n = find(abs(env) < 0.5*max(abs(env)));
silent_indices = [];
if(~isempty(n))
%there needs to be a consecutive array of silent indices.
%We will ignore single values that dip in amplitude, because they
%are most likely caused by computational errors. Consecutively 10
%or more values need to be below the threshold for region to be silent.
%the following lines find the longest sequence of consecutive numbers
%in array n - very smart solution from Stack Overflow
temp = [0 cumsum(diff(n)~=1)];
elems = n(temp==mode(temp));
%silent regions can only be at the beginning or end of frame, not in the
%middle. The number of indices belonging to silent frames has to be
%greater than 10
if(numel(elems) >= 10)
if(ba == 1)
silent_indices = 1:max(elems);
else
silent_indices = min(elems):N;
end
end
end
end