Improve the IQ sample Matlab script

For displaying and saving I/Q sample captured by openwifi side channel feature
This commit is contained in:
Xianjun Jiao 2021-07-08 18:10:34 +02:00
parent b68f51fb20
commit eaaa3bfcfe
3 changed files with 66 additions and 11 deletions

View File

@ -0,0 +1,39 @@
% Xianjun Jiao. xianjun.jiao@imec.be; putaoshu@msn.com
function save_iq_to_txt_for_verilog_sim(mat_filename, varargin)
a = load(mat_filename);
var_names = fieldnames(a);
var_cells = struct2cell(a);
[len_iq, num_frame] = size(var_cells{1});
if nargin>=2
idx_set = varargin{1};
else
idx_set = 1:num_frame;
end
if nargin >= 3
sp = varargin{2}(1);
ep = varargin{2}(2);
else
sp = 1;
ep = len_iq;
end
for name_idx = 1 : length(var_names)
filename_txt = [var_names{name_idx} '.txt'];
fid = fopen(filename_txt,'w');
if fid == -1
disp('fopen failed');
return;
end
var_tmp = var_cells{name_idx};
for j=1:length(idx_set)
idx = idx_set(j);
iq = var_tmp(:,idx);
for i=sp:ep
fprintf(fid, '%d %d\n', round(real(iq(i))), round(imag(iq(i))));
end
end
fclose(fid);
end

View File

@ -1,10 +1,15 @@
% Xianjun Jiao. xianjun.jiao@imec.be; putaoshu@msn.com
clear all;
close all;
% clear all;
% close all;
function test_iq_2ant_file_display(varargin)
% iq_len = 8187; % default for big fpga
iq_len = 4095; % for small fpga
if nargin == 0
iq_len = 8187; % default for big fpga
% iq_len = 4095; % for small fpga
else
iq_len = varargin{1};
end
a = load('iq_2ant.txt');
len_a = floor(length(a)/4)*4;
@ -16,14 +21,17 @@ num_iq_capture = floor(size(b,1)/num_data_in_each_iq_capture);
iq0_capture = zeros(iq_len, num_iq_capture);
iq1_capture = zeros(iq_len, num_iq_capture);
timestamp = zeros(1, num_iq_capture);
for i=1:num_iq_capture
sp = (i-1)*num_data_in_each_iq_capture + 1;
ep = i*num_data_in_each_iq_capture;
timestamp(i) = b(sp,1) + (2^16)*b(sp,2) + (2^32)*b(sp,3) + (2^48)*b(sp,4);
iq0_capture(:,i) = b((sp+1):ep,1) + 1i.*b((sp+1):ep,2);
iq1_capture(:,i) = b((sp+1):ep,3) + 1i.*b((sp+1):ep,4);
iq0_capture(:,i) = 1i.*b((sp+1):ep,1) + b((sp+1):ep,2);
iq1_capture(:,i) = 1i.*b((sp+1):ep,3) + b((sp+1):ep,4);
end
save(['iq_2ant_' num2str(iq_len) '.mat'], 'iq0_capture', 'iq1_capture');
iq0_capture = iq0_capture(:);
iq1_capture = iq1_capture(:);
@ -38,7 +46,7 @@ plot(real(iq1_capture)); hold on; plot(imag(iq1_capture),'r'); title('rx1 I (blu
figure;
a = abs(iq0_capture);
b = abs(iq1_capture);
a(a==0) = max(b);
% a(a==0) = max(b);
plot(a); hold on;
plot(b,'r'); title('rx0 and rx1 abs'); xlabel('sample'); ylabel('abs'); grid on;
legend('rx0','rx1');

View File

@ -1,9 +1,15 @@
% Xianjun Jiao. xianjun.jiao@imec.be; putaoshu@msn.com
clear all;
close all;
% clear all;
% close all;
iq_len = 8187;
function test_iq_file_display(varargin)
if nargin == 0
iq_len = 8187;
else
iq_len = varargin{1};
end
a = load('iq.txt');
len_a = floor(length(a)/4)*4;
@ -22,10 +28,12 @@ for i=1:num_iq_capture
sp = (i-1)*num_data_in_each_iq_capture + 1;
ep = i*num_data_in_each_iq_capture;
timestamp(i) = b(sp,1) + (2^16)*b(sp,2) + (2^32)*b(sp,3) + (2^48)*b(sp,4);
iq_capture(:,i) = b((sp+1):ep,1) + 1i.*b((sp+1):ep,2);
iq_capture(:,i) = 1i.*b((sp+1):ep,1) + b((sp+1):ep,2);
agc_gain(:,i) = b((sp+1):ep,3);
rssi_half_db(:,i) = b((sp+1):ep,4);
end
save(['iq_' num2str(iq_len) '.mat'], 'iq_capture');
iq_capture = iq_capture(:);
agc_gain = agc_gain(:);
rssi_half_db = rssi_half_db(:);