-
Notifications
You must be signed in to change notification settings - Fork 0
/
stompSelf.m
146 lines (131 loc) · 4.95 KB
/
stompSelf.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
% Compute the self-similarity join of a given time series A
% Yan Zhu 03/09/2016 modified by Chin-Chia Michael Yeh 03/10/2016
%
% [matrixProfile, matrixProfileIndex] = stompSelf(A, subLen)
% Output:
% matrixProfile: matrix porfile of the self-join (vector)
% matrixProfileIndex: matrix porfile index of the self-join (vector)
% Input:
% A: input time series (vector)
% subLen: interested subsequence length (scalar)
%
% Yan Zhu, Zachary Zimmerman, Nader Shakibay Senobari, Chin-Chia Michael Yeh, Gareth Funning,
% Abdullah Mueen, Philip Brisk and Eamonn Keogh, "Matrix Profile II: Exploiting a Novel
% Algorithm and GPUs to break the one Hundred Million Barrier for Time Series Motifs and
% Joins," ICDM 2016, http://www.cs.ucr.edu/~eamonn/MatrixProfile.html
%
function [matrixProfile, profileIndex,skipLoc] = stompSelf(data, subLen,va)
%% set trivial match exclusion zone
excZone = round(subLen/2);
%% check input
if subLen > length(data)/2
error('Error: Time series is too short relative to desired subsequence length');
end
if subLen < 4
error('Error: Subsequence length must be at least 4');
end
if length(data) == size(data, 2)
data = data';
end
%% check skip position
proLen = length(data) - subLen + 1;
skipLoc = false(proLen, 1);
for i = 1:proLen
% skip NA and inf
if any(isnan(data(i:i+subLen-1))) || any(isinf(data(i:i+subLen-1)))
skipLoc(i) = true;
end
% skip sequence for standard deviation < 10e-3
if std(data(i:i+subLen-1))< va
skipLoc(i) = true;
end
% skip begin-end sequence
end
data(isnan(data)) = 0;
data(isinf(data)) = 0;
%% initialization
matrixProfile = zeros(proLen, 1);
profileIndex = zeros(proLen, 1);
[dataFreq, dataLen, dataMean, dataSig] = fastFindNNPre(data, subLen);
%% compute the matrix profile
pickedIdx = 1:proLen;
distProfile=zeros(proLen,1);
lastProduct=zeros(proLen,1);
for i = 1:proLen
%disp(i)
% compute the distance profile
idx = pickedIdx(i);
query = data(idx:idx+subLen-1);
if i==1
[distProfile(:), lastProduct(:), querySum, query2Sum, querySig] = ...
fastFindNN(dataFreq, query, dataLen, subLen, dataMean, dataSig);
distProfile(:) = real(distProfile);
firstProduct=lastProduct;
else
querySum = querySum-dropVal+query(subLen);
query2Sum = query2Sum-dropVal^2+query(subLen)^2;
queryMean=querySum/subLen;
querySig2 = query2Sum/subLen-queryMean^2;
querySig = sqrt(querySig2);
lastProduct(2:dataLen-subLen+1) = lastProduct(1:dataLen-subLen) - ...
data(1:dataLen-subLen)*dropVal + data(subLen+1:dataLen)*query(subLen);
lastProduct(1)=firstProduct(idx);
distProfile(:) = 2*(subLen - ...
(lastProduct-subLen*dataMean*queryMean)./(dataSig*querySig));
distProfile(:) = real(distProfile);
end
dropVal=query(1);
% apply exclusion zone
excZoneStart = max(1, idx-excZone);
excZoneEnd = min(proLen, idx+excZone);
distProfile(excZoneStart:excZoneEnd) = inf;
distProfile(dataSig<eps) = inf;
if skipLoc(i) || (querySig < eps)
distProfile = inf(size(distProfile));
end
distProfile(skipLoc) = inf;
% figure out and store the neareest neighbor
if i == 1
matrixProfile = inf(proLen, 1);
profileIndex = inf(proLen, 1);
end
updatePos = distProfile < matrixProfile;
profileIndex(updatePos) = idx;
matrixProfile(updatePos) = distProfile(updatePos);
end
matrixProfile = sqrt(matrixProfile);
matrixProfile(skipLoc) = inf;
profileIndex(skipLoc) = inf;
%% The following two functions are modified from the code provided in the following URL
% http://www.cs.unm.edu/~mueen/FastestSimilaritySearch.html
function [dataFreq, dataLen, dataMean, dataSig] = fastFindNNPre(data, subLen)
% compute stats about data
dataLen = length(data);
data(dataLen+1:2*dataLen) = 0;
dataFreq = fft(data);
dataCum = cumsum(data);
data2Cum = cumsum(data.^2);
data2Sum = data2Cum(subLen:dataLen)-[0;data2Cum(1:dataLen-subLen)];
dataSum = dataCum(subLen:dataLen)-[0;dataCum(1:dataLen-subLen)];
dataMean = dataSum./subLen;
dataSig2 = (data2Sum./subLen)-(dataMean.^2);
dataSig = sqrt(dataSig2);
function [distProfile, lastProduct, querySum, query2Sum, querySig] = ...
fastFindNN(dataFreq, query, dataLen, subLen, dataMean, dataSig)
% proprocess query for fft
query = query(end:-1:1);
query(subLen+1:2*dataLen) = 0;
% compute the product
queryFreq = fft(query);
productFreq = dataFreq.*queryFreq;
product = ifft(productFreq);
% compute the stats about query
querySum = sum(query);
query2Sum = sum(query.^2);
queryMean=querySum/subLen;
querySig2 = query2Sum/subLen-queryMean^2;
querySig = sqrt(querySig2);
% compute the distance profile
distProfile = 2*(subLen-(product(subLen:dataLen)-subLen*dataMean*queryMean)./...
(dataSig*querySig));
lastProduct=real(product(subLen:dataLen));