a b/Supporting Functions/Burst_supression.m
1
function [ bur, sup ] = Burst_supression( x, Fs )
2
%UNTITLED2 Summary of this function goes here
3
%   Detailed explanation goes here
4
supression_threshold = 10;%10;
5
6
% DETECT EMG ARTIFACTS.
7
% [be ae] = butter(6, [30 49]./(Fs/2)); % bandpass filter
8
% demg=filtfilt(be,ae,x);
9
% i0=1; i1=1; ct=0; dn=0;
10
% Nt = size(x,2);
11
% chunkSize = 5; % 5 second chunks
12
% a = zeros(1,Nt);
13
% while ~dn
14
%     %% get next data chunk
15
%     i0=i1;
16
%     if i1 == Nt
17
%         dn=1;
18
%     end
19
%     
20
%     i1=i0+round(Fs*chunkSize);
21
%     i1=min(i1,Nt);
22
%     i01=i0:i1;
23
%     ct=ct+1; % get next data chunk
24
%     
25
%     A(ct)=0; % set to 1 if artifact is detected
26
%     de=demg(:,i01);
27
%     
28
%     %% check for emg artifact
29
%     v=std(de);
30
%     if v > 5
31
%         A(ct)=1;
32
%     end
33
%     a(i01)=A(ct);
34
% end
35
36
% [c,l]=wavedec(x,9,'db2');
37
% A9=wrcoef('a',c,l,'db2',9);
38
% 
39
% [c,l]=wavedec(x,10,'db2');
40
% A10=wrcoef('a',c,l,'db2',10);
41
% 
42
% [c,l]=wavedec(x,11,'db2');
43
% A11=wrcoef('a',c,l,'db2',11);
44
% 
45
% x_tmp = x - A9;
46
47
% CALCULATE ENVELOPE
48
x = smooth(x,10);
49
ME = abs(hilbert(x));
50
% ME = smooth(e,Fs/4); % apply 1/2 second smoothing
51
% e = ME;
52
ME_temp = sort(ME);
53
baseline = mean(ME_temp(1:Fs*1.5));
54
ME = ME-baseline;
55
% DETECT SUPRESSIONS
56
% apply threshold -- 10uv
57
z = (ME<supression_threshold);
58
% remove too-short suppression segments
59
z = fcnRemoveShortEvents(z,Fs/4);
60
% remove too-short burst segments
61
b = fcnRemoveShortEvents(1-z,Fs/4);
62
z = 1-b;
63
z = z';
64
65
%% RUN 'BS' ALGORITHM
66
went_low  = find((z(1:end-1) == 0) & (z(2:end) == 1));
67
went_high  = find((z(1:end-1) == 1) & (z(2:end) == 0));
68
69
if ~isempty(went_low)&&~isempty(went_high)
70
starting = went_high(1) < went_low(1);
71
72
if(starting == 0)
73
    bur =  [[1, went_high(1:length(went_low)-1)]; went_low]';
74
    sup = [went_low(1:length(went_high)); went_high]';
75
end
76
77
if(starting == 1)
78
    sup =  [[1, went_low(1:length(went_high)-1)]; went_high]';
79
    bur = [went_high(1:length(went_low)); went_low]';
80
end
81
82
else
83
    bur = [];
84
    sup = [];
85
86
end
87