This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt_movingAverage.m
executable file
·121 lines (108 loc) · 3.44 KB
/
mt_movingAverage.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function [result,delay,movStd] = mt_movingAverage(varargin)
%moving average over time data, pads data on both ends to fit original length
%% Metadata-----------------------------------------------------------
% cwlVersion: v1.0-extended
% class: matlabfunction
% baseCommand: mt_movingAverage
%
% inputs:
% data:
% type: File
% inputBinding:
% prefix: data
% doc: "1- or 2-dimensional float array"
% ns:
% type: integer?
% inputBinding:
% prefix: ns
% doc: "number of samples to be averaged. Default: 3"
% dim:
% type: integer?
% inputBinding:
% prefix: dim
% doc: "dimension of the array, that should be averaged. Default: 1(rows)"
% debug:
% type: boolean?
% inputBinding:
% prefix: debug
% doc: "Debug mode - basically some output messages. Default: false"
%
% outputs:
% result:
% type: float?
% outputBinding:
% glob: "*_getTDS.mat"
% doc: "Matlab data file (.mat) containing results of a Time-Delay-Stability (TDS) analysis performed on a Polysomnography in EDF format. File can be loaded in Matlab and Octave environments."
% tds_all:
% type: File?
% outputBinding:
% glob: "*_getTDS_all.mat"
% doc: "Matlab data file (.mat) containing results of a Time-Delay-Stability (TDS) analysis performed on a Polysomnography in EDF format. File can be loaded in Matlab and Octave environments. The tds_all file contains more Matlab objects than the tds file."
%
% s:author:
% - class: s:Person
% s:identifier: https://orcid.org/0000-0002-7238-5339
% s:email: mailto:[email protected]
% s:name: Dagmar Krefting
%
% s:dateCreated: "2019-01-12"
% s:license: https://spdx.org/licenses/Apache-2.0
%
% s:keywords: edam:topic_3063, edam:topic_2082
% doc: 3063: medical informatics, 2082: matrix
% s:programmingLanguage: matlab
%
% $namespaces:
% s: https://schema.org/
% edam: http://edamontology.org/
%
% $schemas:
% - https://schema.org/docs/schema_org_rdfa.html
% - http://edamontology.org/EDAM_1.18.owl
%------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code starts here
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 0. Parse Inputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% required input
myinput.data = NaN;
% number of samples to be averaged
myinput.ns = 3;
% dimension to be averaged
myinput.dim = 1;
% dimension to be averaged
myinput.debug = 0;
try
myinput = mt_parameterparser('myinputstruct',myinput,'varargins',varargin);
catch ME
disp(ME)
return
end
if (myinput.debug)
myinput
end
%% calculate moving average
%number of samples excluding actual sample
ns = myinput.ns-1;
%dimensions of datarecord
% transpose to have the signals in first dimension
if (myinput.dim == 2); myinput.data = myinput.data'; end
dim = size(myinput.data);
%length of datarecord (number of rows)
l = dim(1);
%allocate buffer
result = zeros(l-ns,dim(2));
%length of datarecord is supposed to be on dim 1
for i = 1:l-ns
result(i,:) = mean(myinput.data(i:i+ns,:));
movStd(i,:) = std(myinput.data(i:i+ns,:));
end
%delay
delay = floor(ns/2);
%pad result with constant values (first and last averaged value
result = [repmat(result(1,:),delay,1); result; repmat(result(end,:),delay,1)];
%check for dimensions
if (myinput.dim == 2); result = result'; end
end