From eaaa3bfcfe301cb4343ee69a5f8f8e74b9e98550 Mon Sep 17 00:00:00 2001 From: Xianjun Jiao Date: Thu, 8 Jul 2021 18:10:34 +0200 Subject: [PATCH] Improve the IQ sample Matlab script For displaying and saving I/Q sample captured by openwifi side channel feature --- .../save_iq_to_txt_for_verilog_sim.m | 39 +++++++++++++++++++ .../test_iq_2ant_file_display.m | 22 +++++++---- .../side_ch_ctl_src/test_iq_file_display.m | 16 ++++++-- 3 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 user_space/side_ch_ctl_src/save_iq_to_txt_for_verilog_sim.m diff --git a/user_space/side_ch_ctl_src/save_iq_to_txt_for_verilog_sim.m b/user_space/side_ch_ctl_src/save_iq_to_txt_for_verilog_sim.m new file mode 100644 index 0000000..2d85e24 --- /dev/null +++ b/user_space/side_ch_ctl_src/save_iq_to_txt_for_verilog_sim.m @@ -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 diff --git a/user_space/side_ch_ctl_src/test_iq_2ant_file_display.m b/user_space/side_ch_ctl_src/test_iq_2ant_file_display.m index d36670c..0181387 100644 --- a/user_space/side_ch_ctl_src/test_iq_2ant_file_display.m +++ b/user_space/side_ch_ctl_src/test_iq_2ant_file_display.m @@ -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'); diff --git a/user_space/side_ch_ctl_src/test_iq_file_display.m b/user_space/side_ch_ctl_src/test_iq_file_display.m index a3668df..5705306 100644 --- a/user_space/side_ch_ctl_src/test_iq_file_display.m +++ b/user_space/side_ch_ctl_src/test_iq_file_display.m @@ -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(:);