Home > FMAToolbox > Analyses > Sync.m

Sync

PURPOSE ^

Sync - Make sample timestamps relative to synchronizing events.

SYNOPSIS ^

function [synchronized,indices] = Sync(samples,sync,varargin)

DESCRIPTION ^

Sync - Make sample timestamps relative to synchronizing events.

 Select samples that fall around synchronizing events, and make their
 timestamps relative to the synchronizing events. This can be used to
 build e.g. spike raster plots or successive evoked potentials.

  USAGE

    [synchronized,indices] = Sync(samples,sync,<options>)

    samples        <a href="matlab:help samples">samples</a> to synchronize
    sync           timestamps to synchronize on (e.g., brain stimulations)
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'durations'   durations before and after synchronizing events for each
                   trial (in s) (default = [-0.5 0.5])
     'verbose'     display information about ongoing processing
                   (default = 'off')
    =========================================================================

  OUTPUT

    synchronized   resynchronized samples
    indices        for each sample, the corresponding synchronizing event index

  EXAMPLE

    [raster,indices] = Sync(spikes,stimuli);     % compute spike raster data
    figure;PlotSync(raster,indices);             % plot spike raster

  SEE

    See also SyncHist, SyncMap, PlotSync, PETHTransition.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [synchronized,indices] = Sync(samples,sync,varargin)
0002 
0003 %Sync - Make sample timestamps relative to synchronizing events.
0004 %
0005 % Select samples that fall around synchronizing events, and make their
0006 % timestamps relative to the synchronizing events. This can be used to
0007 % build e.g. spike raster plots or successive evoked potentials.
0008 %
0009 %  USAGE
0010 %
0011 %    [synchronized,indices] = Sync(samples,sync,<options>)
0012 %
0013 %    samples        <a href="matlab:help samples">samples</a> to synchronize
0014 %    sync           timestamps to synchronize on (e.g., brain stimulations)
0015 %    <options>      optional list of property-value pairs (see table below)
0016 %
0017 %    =========================================================================
0018 %     Properties    Values
0019 %    -------------------------------------------------------------------------
0020 %     'durations'   durations before and after synchronizing events for each
0021 %                   trial (in s) (default = [-0.5 0.5])
0022 %     'verbose'     display information about ongoing processing
0023 %                   (default = 'off')
0024 %    =========================================================================
0025 %
0026 %  OUTPUT
0027 %
0028 %    synchronized   resynchronized samples
0029 %    indices        for each sample, the corresponding synchronizing event index
0030 %
0031 %  EXAMPLE
0032 %
0033 %    [raster,indices] = Sync(spikes,stimuli);     % compute spike raster data
0034 %    figure;PlotSync(raster,indices);             % plot spike raster
0035 %
0036 %  SEE
0037 %
0038 %    See also SyncHist, SyncMap, PlotSync, PETHTransition.
0039 
0040 % Copyright (C) 2004-2014 by Michaƫl Zugaro
0041 %
0042 % This program is free software; you can redistribute it and/or modify
0043 % it under the terms of the GNU General Public License as published by
0044 % the Free Software Foundation; either version 3 of the License, or
0045 % (at your option) any later version.
0046 
0047 % Default values
0048 durations = [-0.5 0.5];
0049 verbose = false;
0050 
0051 % Check number of parameters
0052 if nargin < 2 | mod(length(varargin),2) ~= 0,
0053   error('Incorrect number of parameters (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0054 end
0055 
0056 % Check parameter sizes
0057 if ~isdvector(sync),
0058     error('Parameter ''sync'' is not a vector (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0059 end
0060 if isempty(sync) || isempty(samples),
0061     synchronized = [];
0062     indices = [];
0063 end
0064 
0065 % Parse parameter list
0066 for i = 1:2:length(varargin),
0067     if ~ischar(varargin{i}),
0068         error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help Sync">Sync</a>'' for details).']);
0069     end
0070     switch(lower(varargin{i})),
0071         case 'durations',
0072             durations = varargin{i+1};
0073             if ~isdvector(durations,'#2','<='),
0074                 error('Incorrect value for property ''durations'' (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0075             end
0076         case 'verbose',
0077             verbose = varargin{i+1};
0078             if ~isastring(verbose,'on','off'),
0079                 error('Incorrect value for property ''verbose'' (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0080             end
0081             verbose = strcmp(verbose,'on');
0082         otherwise,
0083             error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help Sync">Sync</a>'' for details).']);
0084     end
0085 end
0086 
0087 % Make sure samples and sync events are sorted in time
0088 samples = sortrows(samples,1);
0089 sync = sortrows(sync,1);
0090 nSync = length(sync);
0091 
0092 if verbose,
0093     disp([num2str(nSync) ' synchronizing events to process...']) ;
0094 end
0095 
0096 % Output matrices will be allocated in arbitrarily large blocks (and will be trimmed down later)
0097 % This will be much faster than increasing matrix size by the exact appropriate amount for each synchronizing event
0098 blockLength = 1e6;
0099 synchronized = nan(blockLength,size(samples,2));
0100 indices = nan(blockLength,1);
0101 
0102 % This variable is used to speed up computations: to find points within each sync time window, start from the beginning
0103 % of the previous sync window rather than from the very beginning of the sample data
0104 previous = 1;
0105 
0106 k = 1;
0107 for i = 1:nSync,
0108     % Find samples within time window around this synchronizing event
0109     j = FindInInterval(samples,[sync(i)+durations(1) sync(i)+durations(2)],previous);
0110     if ~isempty(j),
0111         previous = j(1);
0112         j = (j(1):j(2))';
0113         nj = length(j);
0114         if verbose, disp([' sync ' int2str(i) ' (t=' num2str(sync(i)) '): ' int2str(length(j)) ' samples']); end
0115         % Synchronize samples
0116         if ~isempty(j),
0117             if length(indices) < k + nj,
0118                 % Enlarge matrices if necessary
0119                 synchronized = [synchronized;nan(blockLength,size(samples,2))];
0120                 indices = [indices;nan(blockLength,1)];
0121             end
0122             synchronized(k:k+nj-1,:) = [samples(j,1)-sync(i) samples(j,2:end)];
0123             indices(k:k+nj-1,1) = i*ones(size(j));
0124         end
0125         k = k + nj;
0126     end
0127 end
0128 indices(k:end) = [];
0129 synchronized(k:end,:) = [];
0130

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005