-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv1.m
44 lines (36 loc) · 981 Bytes
/
env1.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
function envelopeOut = env1(signalIn,FsIn,FsOut,lowPass,winSz)
% Jarne, C. (2017). A heuristic approach to obtain signal envelope with a
% simple software implementation. arXiv preprint arXiv:1703.06812.
% This method uses an arbitrary window size to determine the moving
% maxima. The default to this optional argument is 250 samples.
% Set window sample size
if exist('winSz','var')
if isempty(winSz)
winSz = 250;
end
elseif ~exist('winSz','var')
winSz = 250;
end
env = abs(signalIn);
numWin = length(env)/winSz;
if mod(numWin,1) ~= 0
numWin = numWin - 1;
end
% Moving max
for i = 1:winSz:numel(env)
idx = i:i+winSz-1;
if i == numWin
idx = i:numel(env);
end
idx(idx>numel(env)) = [];
env(idx) = max(env(idx));
end
% Low pass filter
[z,p,k] = butter(8,lowPass/(round(FsIn/2)),'low');
[sos,g] = zp2sos(z,p,k);
env = filtfilt(sos,g,env);
% Resample
[p,q] = rat(FsOut/FsIn);
env = resample(env,p,q);
envelopeOut = env;
end