Skip to content

Commit 3177105

Browse files
Add irregular sampling detection with helpful error for EDF/BDF export
- Detects irregular sampling (>1% deviation) before EDF/BDF export - Throws clear error with resampling instructions for users - EDF/BDF require regular sampling; cannot preserve irregular timestamps - Users must resample their data before BIDS export
1 parent 17b06f2 commit 3177105

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

bids_check_regular_sampling.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
end
2828

2929
if nargin < 2
30-
tolerance = 0.15; % 15% tolerance - resampling for <15% deviation may cause issues
30+
tolerance = 0.01; % 1% tolerance for regular sampling detection
3131
end
3232

3333
if isempty(EEG.data)

bids_export.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,16 @@ function copy_data_bids_eeg(sIn, subjectStr, sess, fileStr, opt)
914914
if isequal(opt.exportformat, 'eeglab')
915915
pop_saveset(EEG, 'filename', [ fileOutNoExt '.set' ], 'filepath', filePathTmp);
916916
else
917-
% Note: EDF/BDF formats assume regular sampling and store only the sampling rate.
918-
% Irregular timestamps in EEG.times are not preserved in EDF/BDF output.
919-
% Event timing has already been mapped to sample indices during import.
917+
% Check for regular sampling when exporting to EDF/BDF
918+
if strcmpi(opt.exportformat, 'edf') || strcmpi(opt.exportformat, 'bdf')
919+
[isRegular, avgFreq] = bids_check_regular_sampling(EEG);
920+
if ~isRegular
921+
error(['EDF/BDF export requires regular sampling. Your data has irregular sampling (avg %.2f Hz).\n' ...
922+
'Please resample your data before exporting:\n' ...
923+
' EEG = pop_resample(EEG, %.0f);\n' ...
924+
'Then re-run bids_export.'], avgFreq, round(avgFreq));
925+
end
926+
end
920927

921928
% For EMG (and other modalities), save without events in EDF
922929
% Events are saved separately in events.tsv

0 commit comments

Comments
 (0)