-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver_code.m
More file actions
184 lines (145 loc) · 5.46 KB
/
Copy pathreceiver_code.m
File metadata and controls
184 lines (145 loc) · 5.46 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
clc; clear; close all;
import java.awt.Robot
import java.awt.Rectangle
import java.awt.GraphicsEnvironment
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
% ========= OVERALL TIMERS =========
cpu_t_start = cputime; % total CPU time at start
wall_t_start = tic; % total wall time start
%% ========== CONFIGURATION ==========
blockSize = 10;
original_rows = 24;
original_cols = 32;
%% ========== STAGE 1: Capture ==========
fprintf('Capturing in 20 seconds... CLICK THE WINDOW!\n');
pause(20);
cpu_t_cap_0 = cputime; t_cap = tic;
try
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
mode = ge.getDefaultScreenDevice().getDisplayMode();
rob = Robot;
jImage = rob.createScreenCapture( ...
Rectangle(0, 0, mode.getWidth(), mode.getHeight()));
h = jImage.getHeight;
w = jImage.getWidth;
pixels = reshape(typecast(jImage.getData.getDataStorage, 'uint8'), ...
4, w, h);
img = permute(pixels(3:-1:1, :, :), [3, 2, 1]);
catch e
error('Capture error: %s', e.message);
end
cpu_t_cap = cputime - cpu_t_cap_0;
t_cap = toc(t_cap);
fprintf('Stage1 (Capture): CPU %.3f s, wall %.3f s\n', cpu_t_cap, t_cap);
%% ========== STAGE 2: Green Crop & Sample ==========
cpu_t_proc_0 = cputime; t_proc = tic;
fprintf('Processing...\n');
R = img(:,:,1); G = img(:,:,2); B = img(:,:,3);
mask = (G > 120) & (R < 100) & (B < 100);
mask = bwareafilt(imfill(padarray(mask,[5 5],0), 'holes'), 1);
stats = regionprops(mask, 'BoundingBox');
if isempty(stats), error('Green border not found!'); end
gray = rgb2gray(imcrop(img, stats.BoundingBox));
thresh = graythresh(gray) * 255;
[h_c, w_c] = size(gray);
logical_W = 320 + 40;
logical_H = 240 + 40;
total_blocks_w = logical_W / blockSize;
total_blocks_h = logical_H / blockSize;
stepX = w_c / total_blocks_w;
stepY = h_c / total_blocks_h;
border_blocks = 2;
rxBits = [];
figure; imshow(gray); hold on; title('Green Dots = Sample Points');
for r = 1:original_rows
for c = 1:original_cols
cx = (c + border_blocks - 1) * stepX + stepX/2;
cy = (r + border_blocks - 1) * stepY + stepY/2;
win = 1;
x1 = max(1,round(cx)-win); x2 = min(w_c,round(cx)+win);
y1 = max(1,round(cy)-win); y2 = min(h_c,round(cy)+win);
val = mean2(gray(y1:y2, x1:x2));
rxBits(end+1) = val > thresh;
plot(cx,cy,'g.');
end
end
rxBits = rxBits(:);
cpu_t_proc = cputime - cpu_t_proc_0;
t_proc = toc(t_proc);
fprintf('Stage2 (Crop+Sample): CPU %.3f s, wall %.3f s\n', ...
cpu_t_proc, t_proc);
%% ========== STAGE 3: Decode (BCH -> AES) ==========
cpu_t_dec_0 = cputime; t_dec = tic;
fprintf('Demodulating %d bits...\n', length(rxBits));
n_bch = 63;
k_bch = 36;
bchDec = comm.BCHDecoder(n_bch, k_bch);
nChunks = floor(length(rxBits)/n_bch);
decodedBits = [];
for i = 1:nChunks
chunk = rxBits((i-1)*n_bch+1 : i*n_bch);
decodedBits = [decodedBits; step(bchDec, chunk)];
end
numBytes = floor(length(decodedBits)/8);
dataBytes = uint8(bi2de( ...
reshape(decodedBits(1:numBytes*8), 8, [])', 'left-msb'));
key = uint8(hex2dec(['21';'1e';'91';'dc';'e6';'82';'d2';'d5'; ...
'14';'02';'2d';'9e';'72';'a2';'c0';'13'; ...
'a1';'c8';'13';'11';'33';'26';'dd';'29'; ...
'0e';'94';'dc';'09';'da';'22';'9c';'72']));
iv = uint8(hex2dec(['65';'45';'69';'dc';'8b';'e6';'92';'bb'; ...
'de';'4f';'32';'89';'fa';'51';'06';'10']));
ks = SecretKeySpec(key, 'AES');
is = IvParameterSpec(iv);
c = Cipher.getInstance('AES/CBC/PKCS5Padding');
c.init(Cipher.DECRYPT_MODE, ks, is);
try
maxLen = floor(length(dataBytes)/16)*16;
validData = dataBytes(1:maxLen);
res = typecast(c.doFinal(typecast(validData, 'int8')), 'uint8');
decryptedText = char(res');
% Remove non-printable characters
decryptedText = regexprep(decryptedText,'[^\x20-\x7E]','');
fprintf('\n============================\n');
fprintf('SUCCESS: %s\n', decryptedText);
fprintf('============================\n');
end
cpu_t_dec = cputime - cpu_t_dec_0;
t_dec = toc(t_dec);
fprintf('Stage3 (BCH+AES Decode): CPU %.3f s, wall %.3f s\n', ...
cpu_t_dec, t_dec);
%% ========== STAGE 4: WINDOWS TEXT TO SPEECH ==========
cpu_t_tts_0 = cputime; t_tts = tic;
disp("Speaking decrypted text...");
% Ensure .NET Framework mode (R2022a+)
if exist('dotnetenv','file')
dotnetenv("framework");
end
% Load System.Speech assembly
NET.addAssembly('System.Speech');
% Create synthesizer
synth = System.Speech.Synthesis.SpeechSynthesizer;
synth.Volume = 100; % 0–100
synth.Rate = 0; % -10 to 10
% Speak the decrypted text
if exist('decryptedText','var') && ~isempty(decryptedText)
synth.Speak(string(decryptedText));
else
synth.Speak("Decryption failed or message empty.");
end
cpu_t_tts = cputime - cpu_t_tts_0;
t_tts = toc(t_tts);
fprintf('Stage4 (TTS): CPU %.3f s, wall %.3f s\n', cpu_t_tts, t_tts);
%% ========= OVERALL SUMMARY =========
cpu_t_end = cputime;
wall_t_end = toc(wall_t_start);
cpu_time_total = cpu_t_end - cpu_t_start;
wall_time_total = wall_t_end;
fprintf('=== PERFORMANCE SUMMARY ===\n');
fprintf('Total CPU time : %.3f s\n', cpu_time_total);
fprintf('Total wall time : %.3f s\n', wall_time_total);
% Approximate average CPU utilization (single-core assumption)
cpu_util_approx = (cpu_time_total / wall_time_total) * 100;
fprintf('Approx CPU util (single-core): %.1f %%\n', cpu_util_approx);