Home > data-analysis > manip > cstream > cstream2cevent.m

cstream2cevent

PURPOSE ^

cstream2cevent Convert cstream data to cevent data

SYNOPSIS ^

function [res, sample_rate] = cstream2cevent(cstream, sample_rate, include_zero)

DESCRIPTION ^

 cstream2cevent   Convert cstream data to cevent data
 
 cevent = cstream2cevent(cstream, include_zero)

 cstream - (input)category cstream. list of pair [timestamp categorynumber]
               e.g.
               344.7000   32.0000
               344.8000   34.0000
               344.9000   34.0000
               345.0000   34.0000
               345.1000   34.0000
               345.2000   34.0000
               345.3000   34.0000
               345.4000   32.0000
               345.5000   32.0000
               345.6000   32.0000
 include_zero: a flag determing whether 0 segments should be treated as events
 or not. include_zero == 0 means not including 0 events, which is the
 default.

 cevent: (output)category event. list of [start_time end_time categorynumber]
        344.7000   344.8000  32.0000
        344.8000   345.4000  34.0000
        345.4000   345.7000  32.0000

  This function is copied from Ikhyun's function
  make_cevent_from_cstream.   Feb 19,2009
  
  Last modified by txu@indiana.edu, Jun. 19, 2014

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [res, sample_rate] = cstream2cevent(cstream, sample_rate, include_zero)
0002 % cstream2cevent   Convert cstream data to cevent data
0003 %
0004 % cevent = cstream2cevent(cstream, include_zero)
0005 %
0006 % cstream - (input)category cstream. list of pair [timestamp categorynumber]
0007 %               e.g.
0008 %               344.7000   32.0000
0009 %               344.8000   34.0000
0010 %               344.9000   34.0000
0011 %               345.0000   34.0000
0012 %               345.1000   34.0000
0013 %               345.2000   34.0000
0014 %               345.3000   34.0000
0015 %               345.4000   32.0000
0016 %               345.5000   32.0000
0017 %               345.6000   32.0000
0018 % include_zero: a flag determing whether 0 segments should be treated as events
0019 % or not. include_zero == 0 means not including 0 events, which is the
0020 % default.
0021 %
0022 % cevent: (output)category event. list of [start_time end_time categorynumber]
0023 %        344.7000   344.8000  32.0000
0024 %        344.8000   345.4000  34.0000
0025 %        345.4000   345.7000  32.0000
0026 %
0027 %  This function is copied from Ikhyun's function
0028 %  make_cevent_from_cstream.   Feb 19,2009
0029 %
0030 %  Last modified by txu@indiana.edu, Jun. 19, 2014
0031 
0032 % MAX_SAMPLE_RATE = 0.1001;
0033 
0034 num = size(cstream,1);
0035 res = zeros(num,3);
0036 
0037 if (isempty(cstream))
0038     return
0039 end
0040 
0041 if ~exist('sample_rate', 'var')
0042 %     warning('Sample_rate is a neccesary input for this function');
0043 %     chunk_len = size(cstream, 1);
0044     sr_list = cstream(2:end,1) - cstream(1:end-1,1);
0045     sample_rate = mode(sr_list);
0046 %     if sample_rate > MAX_SAMPLE_RATE
0047 %         error(['Our estimate sample rate is larger than 0.1, which is ' ...
0048 %             'the largerest sample rate in multisensory project, please enter sample rate manually']);
0049 %     end
0050 end
0051 
0052 if ~exist('include_zero', 'var')
0053     include_zero = 0;
0054 end
0055 
0056 max_gap = sample_rate * 2;
0057 
0058 gap = 0;
0059 res(1,1) = cstream(1,1);   % start timestamp
0060 res(1,3) = cstream(1,2);   % value
0061 % end_time = res(1,1) + gap; % temporal end timestamp
0062 idx = 1;
0063 for i=2:num;
0064     gap = cstream(i,1) - cstream(i-1,1);
0065     if gap > max_gap || ~isequaln(cstream(i,2), cstream(i-1,2)) % isequaln handles NaNs
0066         idx = idx + 1;
0067         res(idx-1,2) = cstream(i-1,1) + sample_rate;
0068         res(idx,1) = cstream(i,1);
0069         res(idx,3) = cstream(i,2);
0070     end
0071 end
0072 res(idx,2) = max(res(idx,1) + gap, cstream(end, 1) + gap);
0073 res = res(1:idx,:);
0074 
0075 if (include_zero == 0) % not including 0 events
0076     nonzeros = res(:, 3) ~= 0;
0077     res = res(nonzeros, :);
0078 end;
0079 
0080 log = ~isnan(res(:,3));
0081 res = res(log,:);
0082 
0083

Generated on Wed 24-May-2017 00:00:56 by m2html © 2005