-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchsegments.m
38 lines (30 loc) · 1.4 KB
/
matchsegments.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
%MATCHEDSEGMENTS Function to fit some data to a set number of samples
% points which last a certain duration.
% Stretches or shrinks data with a set duration to have a different number
% of sample points.
% Input arguments:
% originalData - the original data to fit
% matchLength - the number of samples to fit the data
% duration - the duration of the data in seconds
function[matchedValues] = matchsegments(originalData, matchLength, duration)
% Setup the time domain
matchedTimeDomain = linspace(0, duration, length(originalData));
% Determine the length of each sample in time
segmentLength = matchedTimeDomain(end) / matchLength;
% Initialise value for storing output
matchedValues = zeros(matchLength, 1);
% Loop over each sample in output
for n=1:matchLength
% Get the original data's value that falls within the time segment
value = originalData(matchedTimeDomain <= n * segmentLength & matchedTimeDomain >= (n - 1) * segmentLength);
% If multiple values are found, calculate the mean
if length(value) > 1
value = mean(value);
% If no values are found, then use the previous values
elseif length(value) < 1 && n > 1
value = matchedValues(n - 1);
end
% Update the current sample for the output
matchedValues(n) = value;
end
end