-
Notifications
You must be signed in to change notification settings - Fork 9
/
analyze_raw_data.m
139 lines (117 loc) · 3.49 KB
/
analyze_raw_data.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
%
% Process captures of raw capacitance values, and plot the mutual capacitance images.
%
% Assumes that the user previously
% 1) captured a number of background mutual capacitance images (with nothing on
% the touch screen):
% for var in $(seq 1 10) ; do ./dump_raw_data.sh > background_$var.raw ; done
%
% 2) Captured a number of forground mutual capacitance images:
% for var in $(seq 1 1000) ; do ./dump_raw_data.sh > frame_$var.raw ; done
%
% Will perform the following with that data
% 1) Calculate the average background intensity.
% 2) Normalize the intensities in each frame (individually) to fully span from
% black (lowest mutual capacitance) to white (highest mutual capacitance).
% 3) Plot the result, as well as save it to disk
%
%
% Original author Simon Gustafsson (www.optisimon.com)
%
% Copyright (c) 2016 Simon Gustafsson (www.optisimon.com)
%
% Do whatever you like with this code, but please refer to me as the original author.
%
page_screen_output(0)
% Background capacitance values
bg_files_unsorted = glob("background_*.raw");
% Foreground capacitance values
fg_files_unsorted = glob("frame_*.raw");
% Number of rows and columns in the capacitance matrix to use
w = 12
h = 22
% Assumes filenames named similar to "background_5.raw"
% (underscore before number, and dot after number)
function output = customNumericSort(input)
tmp = [];
for n = 1:size(input, 1)
fname = input{n};
numAsStr = strsplit(fname, {'_', '.' })(2){};
num = str2num(numAsStr);
tmp = [tmp; num];
end
[_, index] = sort(tmp);
for n = 1:size(input, 1)
output{n,1} = input{index(n)};
end
endfunction
bg_files = customNumericSort(bg_files_unsorted)
fg_files = customNumericSort(fg_files_unsorted)
%
% Read raw capacitance values from dump file.
%
% A dumpfile (generated by dump_raw_data.sh) contains a number of lines similar to this one:
% 0x2153 0x2114 0x2191 0x2195 0x20c7 0x2057 0x20b6 0x21c3 0x20ba 0x207c 0x2065 0x213d
%
function out = readRawData(filename)
f = fopen(filename, 'r');
out = [];
while (line = fgets(f)) ~= -1
data = sscanf(line, "%x")';
out = [ out; data];
endwhile
fclose(f);
endfunction
%
% Normalize in to span values between 0 and 1.
%
function out = normalize(in)
span = max(in(:)) - min(in(:));
out = (in - min(in(:))) ./span;
endfunction
%
% Average all the background samples
%
avg_bg = zeros(h, w);
for n = 1:size(bg_files, 1)
fname = bg_files{n};
X = readRawData(fname)(1:h, 1:w);
avg_bg = avg_bg + X;
end
avg_bg = avg_bg / size(bg_files,1)
%
% Blow up the size of the image (using nearest neighbor)
%
function out = expand(in, factor)
out = imresize(in, factor, 'nearest');
endfunction
%
% playback all the forground samples
%
global_max = -Inf;
global_min = Inf;
for n = 1:size(fg_files, 1)
fname = fg_files{n};
raw = readRawData(fname)(1:h, 1:w);
X = raw - avg_bg;
%
% Draw the capacitance image in a plot window
%
imagesc(X);
sleep(0.01);
global_max = max([global_max; X(:)]);
global_min = min([global_min; X(:)]);
% Normal way of normalizing each frame individually
img = normalize(X);
% Test normalizing all frames to the same amplitude
% (coefficients needs to have been determined in an earlier run)
%img = (X - (-163.83) + 0.01) / (386.44 - (-163.83) + 0.02);
%
% Save an up-scaled version of the capacitance image to disk.
%
out_fname = sprintf("out_%06d.png", n)
imwrite(expand(img, 32), out_fname)
end
% Print global max and min capacitance value (after background correction)
global_max
global_min