Repository navigation
Expand file tree
/
Copy pathLCSS.m
More file actions
executable file
·112 lines (103 loc) · 3.28 KB
/
Copy pathLCSS.m
File metadata and controls
executable file
·112 lines (103 loc) · 3.28 KB
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
function [DistanceVal,indxCol,indxRw] = LCSS(refSample,testSample)
% Assuming that the feature matrix are 2D/1D
% epsilon = 4*min( (std(mean(refSample,1))),(std(mean(testSample,1))) );
refMean = sum(refSample,1) ./ sum(refSample~=0,1);%mean(refSample,1);
targetMean = sum(testSample,1) ./ sum(testSample~=0,1);%mean(testSample,1);
refDif = 0;
cnt1 = 0;
for yt = 1:1:size(refSample,1)
tempRw = refSample(yt,:);
tempDif = norm(tempRw - refMean);
refDif = refDif + (tempDif^2);
cnt1 = cnt1+1;
end
refDif = sqrt(refDif)/cnt1;
targetDif = 0;
cnt2 = 0;
for yt = 1:1:size(testSample,1)
tempTarRw = testSample(yt,:);
tempTarDif = norm(tempTarRw - targetMean);
targetDif = targetDif + (tempTarDif^2);
cnt2 = cnt2+1;
end
targetDif = sqrt(targetDif)/cnt2;
epsilon = min(refDif,targetDif);
[noOfSamplesInRefSample,N] = size(refSample);
[noOfSamplesInTestSample,M] = size(testSample);
Dist = zeros(noOfSamplesInRefSample,noOfSamplesInTestSample);
for i=1:noOfSamplesInRefSample % xSize
for j=1:noOfSamplesInTestSample %ySize
total = zeros(N,1);
for goFeature=1:N %or M as N & M are same
total(goFeature,1) = sqrt(double((refSample(i,goFeature)-testSample(j,goFeature))^2));
end
Dist(i,j) = (sum(total)); %(sum(total));
end
end
delta = abs(noOfSamplesInTestSample - noOfSamplesInRefSample);
L = zeros(noOfSamplesInRefSample+1,noOfSamplesInTestSample+1);
L(1,:) = 0;
L(:,1) = 0;
b = zeros(noOfSamplesInRefSample+1,noOfSamplesInTestSample+1);
b(:,1) = 1;%%% Up
b(1,:) = 2;%%% Left
if(N == M)
for i = 2:noOfSamplesInRefSample+1
for j = 2:noOfSamplesInTestSample+1
if ( ( Dist(i-1,j-1) < epsilon) && (abs(j-i) < delta) )
L(i,j) = L(i-1,j-1) + 1;
b(i,j) = 3;%%% Up and left
else
L(i,j) = L(i-1,j-1);
end
if(L(i-1,j) >= L(i,j))
L(i,j) = L(i-1,j);
b(i,j) = 1;%Up
end
if(L(i,j-1) >= L(i,j))
L(i,j) = L(i,j-1);
b(i,j) = 2;%Left
end
end
end
% removing the first col, first row, last col, last row
L(:,1) = [];
L(1,:) = [];
b(:,1) = [];
b(1,:) = [];
dist = L(noOfSamplesInRefSample,noOfSamplesInTestSample);
DistanceVal = (dist / min(noOfSamplesInTestSample,noOfSamplesInRefSample));
% if(dist == 0)
% aLongestString = '';
% else
%%%now backtrack to find the longest subsequence
i = noOfSamplesInRefSample;
j = noOfSamplesInTestSample;
Wrapped(1,:)=[i,j];
p = dist;
aLongestString = {};
while(i>1 && j>1)
if(b(i,j) == 3)
aLongestString{p} = refSample(i);
p = p-1;
i = i-1;
j = j-1;
elseif(b(i,j) == 1)
i = i-1;
elseif(b(i,j) == 2)
j = j-1;
end
Wrapped = cat(1,Wrapped,[i,j]);
end
indxRw = Wrapped(:,1);%1:(size(refSample,1));
% indxRw = indxRw';
indxCol = Wrapped(:,2);%1:(size(testSample,1));
% indxCol = indxCol';
% if ischar(aLongestString{1})
% aLongestString = char(aLongestString)';
% else
% aLongestString = cell2mat(aLongestString);
% end
% end
end
end