Home > data-analysis > stats > cstream > xcorrmod.m

xcorrmod

PURPOSE ^

% Summary

SYNOPSIS ^

function res = xcorrmod(d1, d2, trials, range)

DESCRIPTION ^

% Summary
 low-level function to do cross correlations for categorical data
 d1 is an Nx1 array of category values
 d2 is an Nx1 array of category values
 trials is an Nx1 array of category values
       -- trials information prevents correlating data that are from
          different trials
 range is array of timeshifts, range = [-150:150] for -5 seconds to 5
 seconds, assuming 30 hz data.

% How correlation is calculated
 For each lag point, we shift d1 or d2. The tail ends of the d1 and d2
 shift are not part of the final correlation calculation.
 The correlation value is the logical expression,
   logic = (d1_not_zero) & (d1 == d2) & (trials_equal) & (trial_not_zero)
   correlation = sum(logic) / sum(trials_equal & trial-not-zero)
 positive means d2 leads, negative means d1 leads
%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function res = xcorrmod(d1, d2, trials, range)
0002 %% Summary
0003 % low-level function to do cross correlations for categorical data
0004 % d1 is an Nx1 array of category values
0005 % d2 is an Nx1 array of category values
0006 % trials is an Nx1 array of category values
0007 %       -- trials information prevents correlating data that are from
0008 %          different trials
0009 % range is array of timeshifts, range = [-150:150] for -5 seconds to 5
0010 % seconds, assuming 30 hz data.
0011 %
0012 %% How correlation is calculated
0013 % For each lag point, we shift d1 or d2. The tail ends of the d1 and d2
0014 % shift are not part of the final correlation calculation.
0015 % The correlation value is the logical expression,
0016 %   logic = (d1_not_zero) & (d1 == d2) & (trials_equal) & (trial_not_zero)
0017 %   correlation = sum(logic) / sum(trials_equal & trial-not-zero)
0018 % positive means d2 leads, negative means d1 leads
0019 %%
0020 if size(d1,1) > 1
0021     d1 = d1';
0022 end
0023 if size(d2,1) > 1
0024     d2 = d2';
0025 end
0026 if size(trials,1) > 1
0027     trials = trials';
0028 end
0029 
0030 res = zeros(1, length(range));
0031 
0032 for l = 1:length(range)
0033     lag = range(l);
0034     if lag == 0
0035         trialsmatch = trials ~= 0;
0036         res(l) = sum(d1 == d2 & d1 ~= 0 & trialsmatch) / sum(trialsmatch);
0037     end
0038     if lag < 0
0039         d2part = d2(abs(lag)+1:end);
0040         d1part = d1(1:length(d2part));
0041         trials2part = trials(abs(lag)+1:end);
0042         trials1part = trials(1:length(trials2part));
0043         trialsmatch = trials1part == trials2part & trials1part ~= 0;
0044         log = (d1part == d2part & d1part ~= 0) & trialsmatch;
0045 %         view = cat(1, d1part, d2part, trials1part, trials2part, log);
0046         res(l) = sum(log) / sum(trialsmatch);
0047     end
0048     if lag > 0
0049         d1part = d1(lag+1:end);
0050         d2part = d2(1:length(d1part));
0051         trials1part = trials(lag+1:end);
0052         trials2part = trials(1:length(trials1part));
0053         trialsmatch = trials1part == trials2part & trials1part ~= 0;
0054         log = (d1part == d2part & d1part ~= 0) & trialsmatch;
0055 %         view = cat(1, d1part, d2part, trials1part, trials2part, log);
0056         res(l) = sum(log) / sum(trialsmatch);
0057     end
0058 end
0059 end

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