|
| 1 | +%% ===================================================================== %% |
| 2 | +% ---------------------- RFID CSV DATA PROCESSING ---------------------- % |
| 3 | +% Author: Nedal M. Benelmekki % |
| 4 | +% Date (DD/MM/YYYY): 11/07/2025 % |
| 5 | +% % |
| 6 | +% Description: % |
| 7 | +% Batch process RFID AoA experiment CSV files, obtained from a COTS RFID % |
| 8 | +% System (Zebra FX7500, AN480 WB Antenna, Belt tag) into a structured % |
| 9 | +% MATLAB dataset. % |
| 10 | +% % |
| 11 | +% Input: % |
| 12 | +% - Filepath to the experimental data's base directory. % |
| 13 | +% % |
| 14 | +% Output: % |
| 15 | +% - rfid_array_data.mat: Structured MATLAB dataset containing processed % |
| 16 | +% phase, RSSI, and phasor data for each measurement configuration % |
| 17 | +% ======================================================================= % |
| 18 | + |
| 19 | +%% STEP 0: Clean Workspace |
| 20 | +clear; % Clear all variables from workspace |
| 21 | +clc; % Clear console/command window |
| 22 | + |
| 23 | +%% STEP 1: Configuration |
| 24 | +dataDir = 'filepath'; % TODO: Set base directory path for CSV files |
| 25 | +tagID = '000233b2ddd9014000000000'; % Target RFID tag ID |
| 26 | +c = 3e8; % Speed of light approximation [m/s] |
| 27 | +% Alt.: c = physconst('LightSpeed'); % Speed of light, requires Antenna Toolbox [m/s] |
| 28 | + |
| 29 | +%% STEP 2: Load Files |
| 30 | +fileList = dir(fullfile(dataDir, '**', '*.csv')); % Recursive search for files in subdirectories, see README.md documentation to understand the file structure and naming convention |
| 31 | +fprintf('Found %d CSV files.\n', length(fileList)); % Debug message: display number of files found |
| 32 | + |
| 33 | +%% STEP 3: Transfer Data |
| 34 | +allData = []; % Initialize storage array |
| 35 | +for i = 1:length(fileList) % Loop through each found file in STEP 2. |
| 36 | + fname = fileList(i).name; % Get the filename |
| 37 | + fpath = fullfile(fileList(i).folder, fname); % Full path to the file |
| 38 | + try |
| 39 | + % STEP 3.1: Parse filename |
| 40 | + tokens = regexp(fname, '(\d+)_(\d+\.\d)_(\d+\.\d+)_(\d+\.\d+)_(\-?\d+\.\d+).csv', 'tokens'); % Regex to extract date, f0, D, L, W from filename |
| 41 | + if isempty(tokens) % Check if regex matched + error handling |
| 42 | + warning('Skipping file (cannot parse): %s', fname); |
| 43 | + continue; |
| 44 | + end |
| 45 | + tokens = tokens{1}; % Extract tokens from the cell array |
| 46 | + dateStr = tokens{1}; % Date string in format YYYYMMDD |
| 47 | + f0 = str2double(tokens{2}) * 1e6; % Carrier frequency, f0, in Hz |
| 48 | + D = str2double(tokens{3}); % Vertical distance, D, in m |
| 49 | + L = str2double(tokens{4}); % Antenna separation, L, in m |
| 50 | + W = str2double(tokens{5}); % Horizontal tag position, W, in m |
| 51 | + lambda = c / f0; % Wavelength, lambda, in m |
| 52 | + % STEP 3.2: Load CSV |
| 53 | + T = readtable(fpath); % Load CSV file into a table |
| 54 | + % STEP 3.3: Check required columns |
| 55 | + requiredCols = {'phase', 'peakRssi', 'antenna', 'idHex'}; % Define required columns |
| 56 | + if ~all(ismember(requiredCols, T.Properties.VariableNames)) % Verify if all required columns are present |
| 57 | + warning('Skipping file (missing columns): %s', fname); % Error handling (skip the file) |
| 58 | + continue; |
| 59 | + end |
| 60 | + % STEP 3.4: Filter by antenna and tag ID |
| 61 | + t1 = T(T.antenna == 1 & strcmp(T.idHex, tagID), :); % Filter for Antenna 1 |
| 62 | + t2 = T(T.antenna == 2 & strcmp(T.idHex, tagID), :); % Filter for Antenna 2 |
| 63 | + % STEP 3.5: Sanity check |
| 64 | + if height(t1) < 3 || height(t2) < 3 % Ensure there are enough data points for processing |
| 65 | + warning('Not enough data in file: %s', fname); % Error handling (skip the file) |
| 66 | + continue; |
| 67 | + end |
| 68 | + % STEP 3.6: Unwrap and convert phase to radians |
| 69 | + phi1 = unwrap(deg2rad(t1.phase)); % Unwrap phase for Antenna 1 and convert to radians |
| 70 | + phi2 = unwrap(deg2rad(t2.phase)); % Unwrap phase for Antenna 2 and convert to radians |
| 71 | + % STEP 3.7: Convert RSSI to linear power scale |
| 72 | + rssi1 = t1.peakRssi; % Peak RSSI for Antenna 1 |
| 73 | + rssi2 = t2.peakRssi; % Peak RSSI for Antenna 2 |
| 74 | + mag1 = sqrt(10.^(rssi1 / 10)); % Convert RSSI to linear scale for Antenna 1 |
| 75 | + mag2 = sqrt(10.^(rssi2 / 10)); % Convert RSSI to linear scale for Antenna 2 |
| 76 | + % STEP 3.8: Create phasors |
| 77 | + phasor1 = mag1 .* exp(1j * phi1); % Create phasor for Antenna 1 |
| 78 | + phasor2 = mag2 .* exp(1j * phi2); % Create phasor for Antenna 2 |
| 79 | + % STEP 3.9: Save entry |
| 80 | + entry = struct(); % Initialize a new entry structure |
| 81 | + entry.filename = fname; % Store filename |
| 82 | + entry.date = dateStr; % Store date string |
| 83 | + entry.f0 = f0; % Store carrier frequency |
| 84 | + entry.lambda = lambda; % Store wavelength |
| 85 | + entry.D = D; % Store vertical distance |
| 86 | + entry.L = L; % Store antenna separation |
| 87 | + entry.W = W; % Store horizontal tag position (offset) |
| 88 | + entry.phi1 = phi1; % Store unwrapped phase for Antenna 1 |
| 89 | + entry.phi2 = phi2; % Store unwrapped phase for Antenna 2 |
| 90 | + entry.rssi1 = rssi1; % Store RSSI for Antenna 1 |
| 91 | + entry.rssi2 = rssi2; % Store RSSI for Antenna 2 |
| 92 | + entry.phasor1 = phasor1; % Store phasor for Antenna 1 |
| 93 | + entry.phasor2 = phasor2; % Store phasor for Antenna 2 |
| 94 | + % STEP 3.10: Append to array + Confirmation |
| 95 | + allData = [allData; entry]; % Append the new entry to the allData array |
| 96 | + fprintf('Processed: %s\n', fname); % Debug message: display processed file name |
| 97 | + catch ME |
| 98 | + warning('Error processing file %s: %s', fname, ME.message); % Error handling: catch any errors during processing |
| 99 | + continue; |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +%% STEP 4: Save Output |
| 104 | +save('rfid_array_data.mat', 'allData'); |
| 105 | +fprintf('\n Saved processed data to rfid_array_data.mat (%d valid entries).\n', length(allData)); |
0 commit comments