|
a |
|
b/envelop_hilbert.m |
|
|
1 |
function alarm = envelop_hilbert(y,Smooth_window,threshold_style,DURATION,gr) |
|
|
2 |
|
|
|
3 |
% y = Raw input signal to be analyzed |
|
|
4 |
% Smooth_window :this is the window length used for smoothing your signal in terms of number of samples |
|
|
5 |
% threshold_style : set it 1 to have an adaptive threshold OR set it 0 to manually select the threshold from a plot |
|
|
6 |
% DURATION : Number of the samples that the signal should stay in terms of number of samples |
|
|
7 |
% gr = make it 1 if you want a plot and 0 when you dont want a plot |
|
|
8 |
|
|
|
9 |
% Tuning parameters for the best results; |
|
|
10 |
% 1. DURATION is correlated to your sampling frequency, you can use a multiple of your sampling frequency e.g. round(0.050*SamplingFrequency) |
|
|
11 |
% 2. Smooth_window is correlated to your sampling frequency, you can use a multiple of your sampling frequency e.g. round(0.0500*SamplingFrequency), this is the window length used for smoothing your signal |
|
|
12 |
|
|
|
13 |
% alarm : vector resembeling the active parts of the signal |
|
|
14 |
|
|
|
15 |
%Method |
|
|
16 |
% Calculates the analytical signal with the help of hilbert transfrom, |
|
|
17 |
% takes the envelope and smoothes the signal. Finally , with the help of an |
|
|
18 |
% adaptive threshold detects the activity of the signal where at least a |
|
|
19 |
% minimum number of samples with the length of |
|
|
20 |
% (DURATION) Samples should stay above the threshold). The threshold is a |
|
|
21 |
% computation of signal noise and activity level which is updated online. |
|
|
22 |
|
|
|
23 |
%% Input handling |
|
|
24 |
|
|
|
25 |
%--------------------------- input handling ---------------------------% |
|
|
26 |
if nargin < 5 |
|
|
27 |
gr = 1; |
|
|
28 |
if nargin < 4 |
|
|
29 |
DURATION = 20; % default |
|
|
30 |
if nargin < 3 |
|
|
31 |
threshold_style = 1; % default 1 , means it is done automatic |
|
|
32 |
if nargin < 2 |
|
|
33 |
Smooth_window = 20; % default for smoothing length |
|
|
34 |
if nargin < 1 |
|
|
35 |
v = repmat([.1*ones(200,1);ones(100,1)],[10 1]); % generate true variance profile |
|
|
36 |
y = sqrt(v).*randn(size(v)); |
|
|
37 |
end |
|
|
38 |
|
|
|
39 |
end |
|
|
40 |
end |
|
|
41 |
end |
|
|
42 |
end |
|
|
43 |
|
|
|
44 |
%% calculate the analytical signal and get the envelope |
|
|
45 |
test=y(:); |
|
|
46 |
analytic = hilbert(test); |
|
|
47 |
env = abs(analytic); |
|
|
48 |
|
|
|
49 |
%% take the moving average of analytical signal |
|
|
50 |
env = conv(env,ones(1,Smooth_window)/Smooth_window); % smooth |
|
|
51 |
env = env(:) - mean(env); % get rid of offset |
|
|
52 |
env = env/max(env); % normalize |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
%% threshold the signal |
|
|
56 |
if threshold_style == 0 |
|
|
57 |
hg=figure;plot(env);title('Select a threshold on the graph') |
|
|
58 |
[~,THR_SIG] =ginput(1); |
|
|
59 |
close(hg); |
|
|
60 |
end |
|
|
61 |
% ------------------------- Threshold Style ---------------------- % |
|
|
62 |
if threshold_style |
|
|
63 |
THR_SIG = 4*mean(env); |
|
|
64 |
end |
|
|
65 |
|
|
|
66 |
nois = mean(env)*(1/3); % noise level |
|
|
67 |
threshold = mean(env); % signal level |
|
|
68 |
|
|
|
69 |
% ------------------- Initialize Buffers -------------------------% |
|
|
70 |
thres_buf = zeros(1,length(env)-DURATION); |
|
|
71 |
nois_buf = zeros(1,length(env)-DURATION); |
|
|
72 |
THR_buf = zeros(1,length(env)); |
|
|
73 |
h=1; |
|
|
74 |
alarm =zeros(1,length(env)); |
|
|
75 |
|
|
|
76 |
for i = 1:DURATION/2:length(env)-DURATION |
|
|
77 |
%------ update threshold 10% of the maximum peaks found ------------% |
|
|
78 |
if env(i:i+DURATION) > THR_SIG |
|
|
79 |
alarm(i) = max(env); |
|
|
80 |
threshold = 0.1*mean(env(i:i+DURATION)); |
|
|
81 |
h=h+1; |
|
|
82 |
else |
|
|
83 |
% ----------- update noise --------------% |
|
|
84 |
if mean(env(i:i+DURATION)) < THR_SIG |
|
|
85 |
nois = mean(env(i:i+DURATION)); |
|
|
86 |
else |
|
|
87 |
if ~isempty(nois_buf) |
|
|
88 |
nois = mean(nois_buf); |
|
|
89 |
end |
|
|
90 |
end |
|
|
91 |
end |
|
|
92 |
|
|
|
93 |
thres_buf(i) = threshold; |
|
|
94 |
nois_buf(i) = nois; |
|
|
95 |
% ------------------------- update threshold -------------------------- % |
|
|
96 |
if h > 1 |
|
|
97 |
THR_SIG = nois + 0.50*(abs(threshold - nois)); |
|
|
98 |
end |
|
|
99 |
THR_buf(i) = THR_SIG; |
|
|
100 |
end |