Home > data-import > supporting_functions > cevent2cstream_v2.m

cevent2cstream_v2

PURPOSE ^

SYNOPSIS ^

function base = cevent2cstream_v2(cevent, sample_rate, time_period, time_base)

DESCRIPTION ^

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function base = cevent2cstream_v2(cevent, sample_rate, time_period, time_base)
0002 
0003 if ~exist('time_base', 'var')
0004     %define shorthand
0005     sr = sample_rate;
0006     tp = time_period;
0007     
0008     %turn events into cevent with category 1
0009     
0010     if size(cevent, 2) == 2
0011         cevent(:,3) = 1;
0012     end
0013     
0014     %subtract sample rate from 2nd column
0015     cevent(:,2) = cevent(:,2) - sample_rate;
0016     
0017     %make sure cevent data is in order
0018     cevent = sortrows(cevent, [1 2]);
0019     
0020     %establish time base series with given sample rate and start end time
0021     base = tp(1):sr:tp(end);
0022     base = base';
0023     
0024     %time_base input overrides above base
0025 else
0026     base = time_base;
0027     tp = [base(1) base(end)];
0028 %     sr = mode(diff(base)); %can use this to exclude cevents that fall outside of trial range
0029 end
0030 
0031 base = [base zeros(numel(base),1)];
0032 
0033 if ~isempty(cevent)
0034     for c = 1:size(cevent,1)
0035         if (cevent(c,2) >= tp(1) || tp(1) - cevent(c,2) < 0.0001) && (cevent(c,1) <= tp(2) || cevent(c,1) - tp(2) < 0.0001) %makes sure whole cevent fits into time-base range;
0036             %also this partially captures cevents that are cut off by tp(1) and tp(2);
0037             [~, i] = min(abs(base - cevent(c,1)));
0038             [~, i2] = min(abs(base - cevent(c,2)));
0039             check = base(i:i2,2);
0040             if sum(check) > 0
0041                 fprintf('overlap detected: %d\n', sum(check>0));
0042                 disp(cevent(c,:));
0043             end
0044             base(i:i2,2) = cevent(c,3);
0045         end
0046     end
0047 else
0048     base = base(:,1);
0049 end
0050 
0051 end

Generated on Tue 23-May-2017 03:00:58 by m2html © 2005