-
Notifications
You must be signed in to change notification settings - Fork 0
/
cosine_3d.m
115 lines (91 loc) · 3.54 KB
/
cosine_3d.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
clear all;
close all;
clc;
rng(100); % For reproducibility
% Define script params
userRange_min = 1;
userRange_max = 10;
% 1. Data Loading and Preprocessing
% Define file patterns for each user
filePatternsTrain = 'Acc_TimeD_FreqD_FDay';
filePatternsTest = 'Acc_TimeD_FreqD_MDay';
% First: Load and combine features for each user separately
fprintf('Loading data for each user...\n');
% Initialize storage datasets
userData = struct('trainFeatures', [], 'testFeatures', []);
userData = repmat(userData, 1, userRange_max);
% Load data for each user
for user = userRange_min:userRange_max
userStr = sprintf('U%02d', user);
% Load training and test data
trainFile = [userStr '_' filePatternsTrain '.mat'];
testFile = [userStr '_' filePatternsTest '.mat'];
if exist(trainFile, 'file') && exist(testFile, 'file')
trainData = load(trainFile);
testData = load(testFile);
% Debug print
fprintf('Loading data for user %d\n', user);
fprintf('Train file: %s\n', trainFile);
fprintf('Test file: %s\n', testFile);
% Store data in structure
userData(user).trainFeatures = trainData.(char(fieldnames(trainData)));
userData(user).testFeatures = testData.(char(fieldnames(testData)));
% Verify data loading
[r, c] = size(userData(user).trainFeatures);
fprintf('Loaded data dimensions for user %d: %d samples x %d features\n', user, r, c);
else
fprintf('Missing data files for user %d\n', user);
fprintf('Tried to load:\n');
fprintf('Train: %s\n', trainFile);
fprintf('Test: %s\n', testFile);
end
end
% 2. Visualization
for user = userRange_min:userRange_max
% Skip if no data
if isempty(userData(user).trainFeatures) || isempty(userData(user).testFeatures)
fprintf('Skipping user %d - no data\n', user);
continue;
end
% Create new figure for each user
figure('Name', sprintf('User %d Point-wise Similarity', user));
% Get data
fday_data = userData(user).trainFeatures; % 36x131
mday_data = userData(user).testFeatures; % 36x131
% Calculate point-wise cosine similarity for each feature
[numSamples, numFeatures] = size(fday_data);
similarities = zeros(numSamples, numSamples);
for i = 1:numSamples
for j = 1:numSamples
% Get the corresponding points
point_fday = fday_data(i,:);
point_mday = mday_data(j,:);
% Calculate cosine similarity
similarities(i,j) = dot(point_fday, point_mday) / ...
(norm(point_fday) * norm(point_mday));
end
end
% Create meshgrid for 3D plotting
[X, Y] = meshgrid(1:numSamples, 1:numSamples);
% Plot
scatter3(X(:), Y(:), similarities(:), 50, similarities(:), 'filled');
% Customize plot
colormap(jet);
c = colorbar;
c.Label.String = 'Cosine Similarity';
caxis([0 1]); % Set color limits
xlabel('FDay Sample Index');
ylabel('MDay Sample Index');
zlabel('Cosine Similarity');
title(sprintf('User %d: Point-wise Similarity\nMean Similarity = %.3f', ...
user, mean(similarities(:))));
% Add grid and enable rotation
grid on;
rotate3d on;
% Set view angle and axis limits
view(45, 30);
xlim([1 numSamples]);
ylim([1 numSamples]);
zlim([0 1]);
end
fprintf('\nVisualization complete!\n');