@@ -421,9 +421,56 @@ function perceiveModular(files, sub, sesMedOffOn01, extended, gui, localsettings
421421 fulldata.time{1 }=fulldata.timeInSecDerivedFromTicks{1 }; % the fulldate.time comes from perceive_extract_bstd d.time{1} = linspace(rel_start, rel_start + size(d.trial{1}, 2)/fsample, size(d.trial{1}, 2));
422422 otime = bsl.data.time{1 };
423423 %% CRUCIAL PART where BSL is being added to fulldata of BSTD
424+ % this snippet streches the BSL stream and put them
425+ % over the BSTD stream, which is typically longer in
426+ % the sense that is starts earlier and ends later.
427+ % It aligns the TicksInMSes
428+ % to the same time point, and then crops the trials
429+ % The first TicksInMSes is stored in sampleinfo [1]
430+
431+ BSTD_first_TickInMSes = fulldata .sampleinfo(1 );
432+ BSL_first_TickInMSes = bsl .TicksInMs(1 );
433+ if (BSTD_first_TickInMSes < BSL_first_TickInMSes ) && (BSTD_first_TickInMSes - BSL_first_TickInMSes ) < 2000
434+ starttimedifference_sec = (BSL_first_TickInMSes - BSTD_first_TickInMSes )/1000 ;
435+ elseif (BSTD_first_TickInMSes < BSL_first_TickInMSes )
436+ warning(' BSL recording started more than 2 seconds after BSTD.\n BSTD FirstPacketDateTime: %s\n BSL: %s ' , ...
437+ fulldata .FirstPacketDateTime, bsl .FirstPacketDateTime);
438+ error(' BSDT_first_TickInMSes < BSL_first_TickInMSes is larger than 2 seconds' )
439+ else
440+ warning([' BSL MTicks started before matched BSTD MTicks, which indicates unexpected json outcomes...' newline ...
441+ ' BSL FirstPacketDateTime: %s with TicksInMs: %d ' newline ...
442+ ' BSTD FirstPacketDateTime: %s with TicksInMs: %d ' ], ...
443+ bsl .FirstPacketDateTime, BSL_first_TickInMSes , fulldata .FirstPacketDateTime, BSTD_first_TickInMSes )
444+ t1 = datetime(fulldata .FirstPacketDateTime, ' InputFormat' , ' yyyy-MM-dd HH:mm:ss.SSS' );
445+ t2 = datetime(bsl .FirstPacketDateTime, ' InputFormat' , ' yyyy-MM-dd HH:mm:ss.SSS' );
446+ diffSeconds = seconds(t2 - t1 );
447+ if diffSeconds > 0
448+ starttimedifference_sec = diffSeconds ; % Store the time difference for alignment
449+ else
450+ warning(' BSL recording started before BSTD, which should be impossible.\n fulldata.FirstPacketDateTime: %s\n bsl.FirstPacketDateTime: %s ' , ...
451+ fulldata .FirstPacketDateTime, bsl .FirstPacketDateTime);
452+ starttimedifference_sec = 0 ;
453+ end
454+ end
455+
456+ % Find the indices of all numbers greater or equal to
457+ % num, in order to let BSL start afer the time
458+ % difference to BSTD
459+ indices = find(fulldata.time{1 } >= starttimedifference_sec );
460+
461+ if isempty(indices )
462+ % If no number is greater or equal, return empty or handle accordingly
463+ nextIndex = 1 ;
464+ else
465+ % Take the first index from those candidates (the next closest)
466+ nextIndex = indices(1 );
467+ end
468+ fulldata.timeAlignedTicks = fulldata .trial;
424469 for c = 1 : 4
425- fulldata.trial {1 }(c + 2 ,: ) = interp1(otime - otime(1 ),bsl.data.trial{1 }(c ,: ),fulldata.time{1 }-fulldata.time{1 }(1 ),' nearest' );
470+ fulldata.timeAlignedTicks {1 }(c + 2 ,: ) = interp1(otime - otime(1 ),bsl.data.trial{1 }(c ,: ),fulldata.time{1 }-fulldata.time{1 }(nextIndex ),' nearest' );
426471 end
472+ fulldata.trial= fulldata .timeAlignedTicks; % this is here in order to be able to checke fulldata time vs the interpolation in debugging mode
473+ fulldata = rmfield(fulldata , ' timeAlignedTicks' ); % clean up here
427474 %% determine StimOff or StimOn
428475
429476 acq= regexp(bsl .data.fname,' Stim.*(?=_mod)' ,' match' ); % Search for StimOff StimOn
0 commit comments