function [Bits, Symbols, Scpe_cell, found_sync] = loadAndSyncSignalDataFromDb(dataTable, options) % LOADSIGNALDATA Loads and synchronizes signal data from storage % % Inputs:d % dataTable - Table with file paths and configuration % options - Struct with storage_path and max_occurences % % Outputs: % Symbols_mapped - Mapped symbols from bits % Symbols - Original symbols % Scpe_cell - Cell array of synchronized signals % found_sync - Boolean indicating if synchronization was successful found_sync = 0; tempLocalStorage = 1; % Define the fixed storage directory relative to the user's MATLAB preferences directory storage_dir = fullfile(prefdir, 'temp_sync_data'); % Part A: Check and load from local storage if available- if tempLocalStorage == 1 local_filename = fullfile(storage_dir, sprintf('sync_data_run_%s.mat', num2str(dataTable.run_id))); if exist(local_filename, 'file') % Load from local storage and return try load(local_filename, 'Bits', 'Symbols', 'Scpe_cell'); found_sync = 1; return catch delete(local_filename); end end end % If not locally saved, load from storage if ~found_sync % Load transmitted bits Bits = load(fullfile([options.storage_path, char(dataTable.tx_bits_path)])); Bits = Bits.Bits; % Map bits to symbols M = double(dataTable.pam_level); fsym = dataTable.symbolrate; Symbols_mapped = PAMmapper(M,0).map(Bits); Symbols_mapped.fs = fsym; % Load original symbols Symbols = load(fullfile([options.storage_path, char(dataTable.tx_symbols_path)])); Symbols = Symbols.Symbols; found_sync = 0; Scpe_cell = {}; % Try to load pre-synchronized data try Scpe_load = load(fullfile([options.storage_path, char(dataTable.rx_sync_path)])); Scpe_cell = Scpe_load.S; [~,~,~,found_sync] = Scpe_cell{2}.tsynch("reference", Symbols, "fs_ref", fsym, "debug_plots", 1); catch % Continue to next method if this fails end end % If not found, try with raw data if ~found_sync try Scpe_sig_raw = load([options.storage_path, char(dataTable.rx_raw_path(1))]); Scpe_sig_raw = Scpe_sig_raw.Scpe_sig_raw; Scpe_sig_resampled = Scpe_sig_raw.resample("fs_in", Scpe_sig_raw.fs, "fs_out", 2*fsym); [~, Scpe_cell, ~, found_sync] = Scpe_sig_resampled.tsynch("reference", Symbols, "fs_ref", fsym, "debug_plots", 1); catch % Continue to next method if this fails end end % Last attempt with mapped symbols if ~found_sync && exist('Scpe_sig_raw', 'var') if length(Symbols_mapped.signal) ~= sum(Symbols_mapped.signal == Symbols.signal) [~, Scpe_cell, ~, found_sync] = Scpe_sig_raw.tsynch("reference", Symbols_mapped, "fs_ref", fsym, "debug_plots", 0); end end % Part B: Save to local storage if data was loaded and synced if tempLocalStorage == 1 && found_sync % Create directory if it doesn't exist if ~exist(storage_dir, 'dir') mkdir(storage_dir); end % local_filename = fullfile(storage_dir, sprintf('sync_data_run_%s.mat', num2str(dataTable.run_id))); % List existing files and remove oldest if more than N max_local_files = 10; % Store up to N files files = dir(fullfile(storage_dir, 'sync_data_run_*.mat')); if length(files) >= max_local_files % Sort by date [~, idx] = sort([files.datenum]); % Delete oldest file delete(fullfile(storage_dir, files(idx(1)).name)); end % Save current data save(local_filename, 'Bits', 'Symbols', 'Scpe_cell'); end % Limit number of occurrences if found_sync record_realizations = min(options.max_occurences, length(Scpe_cell)); Scpe_cell = Scpe_cell(1:record_realizations); else warning('Could not synchronize the received signal with the stored symbols!'); end end