Home > FMAToolbox > General > Interpolate.m

Interpolate

PURPOSE ^

Interpolate - Interpolate samples (positions, spikes, LFP, etc.) at given timestamps.

SYNOPSIS ^

function [interpolated,discarded] = Interpolate(samples,timestamps,varargin)

DESCRIPTION ^

Interpolate - Interpolate samples (positions, spikes, LFP, etc.) at given timestamps.

 Interpolate samples (positions, spikes, LFP, etc.) at given timestamps. As a special
 case, a point process can also be 'interpolated' by changing each original timestamp
 t to the closest target timestamp t* such that t* <= t.

  USAGE

    [interpolated,discarded] = Interpolate(samples,timestamps,<options>)

    samples        list of <a href="matlab:help samples">samples</a> to interpolate
    timestamps     timestamps where the samples should be interpolated
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'type'        'linear' if samples are linear values (default), or
                   'circular' otherwise
     'trim'        discard ('on', default) or keep ('off') samples that would
                   require extrapolation. If kept, these samples are set to
                   NaN (this can be useful when the output is required to
                   have the same size as the input) (default = 'on')
     'maxGap'      time gaps between original and target times exceeding this
                   threshold will be ignored (default = Inf)
    =========================================================================

  OUTPUT

    interpolated   list of interpolated samples
    discarded      logical vector indicating for which timestamps the samples were
                   not interpolated (see option 'maxGap')

  SEE

    See also Diff.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [interpolated,discarded] = Interpolate(samples,timestamps,varargin)
0002 
0003 %Interpolate - Interpolate samples (positions, spikes, LFP, etc.) at given timestamps.
0004 %
0005 % Interpolate samples (positions, spikes, LFP, etc.) at given timestamps. As a special
0006 % case, a point process can also be 'interpolated' by changing each original timestamp
0007 % t to the closest target timestamp t* such that t* <= t.
0008 %
0009 %  USAGE
0010 %
0011 %    [interpolated,discarded] = Interpolate(samples,timestamps,<options>)
0012 %
0013 %    samples        list of <a href="matlab:help samples">samples</a> to interpolate
0014 %    timestamps     timestamps where the samples should be interpolated
0015 %    <options>      optional list of property-value pairs (see table below)
0016 %
0017 %    =========================================================================
0018 %     Properties    Values
0019 %    -------------------------------------------------------------------------
0020 %     'type'        'linear' if samples are linear values (default), or
0021 %                   'circular' otherwise
0022 %     'trim'        discard ('on', default) or keep ('off') samples that would
0023 %                   require extrapolation. If kept, these samples are set to
0024 %                   NaN (this can be useful when the output is required to
0025 %                   have the same size as the input) (default = 'on')
0026 %     'maxGap'      time gaps between original and target times exceeding this
0027 %                   threshold will be ignored (default = Inf)
0028 %    =========================================================================
0029 %
0030 %  OUTPUT
0031 %
0032 %    interpolated   list of interpolated samples
0033 %    discarded      logical vector indicating for which timestamps the samples were
0034 %                   not interpolated (see option 'maxGap')
0035 %
0036 %  SEE
0037 %
0038 %    See also Diff.
0039 
0040 % Copyright (C) 2004-2011 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 trim = 'on';
0049 maxGap = Inf;
0050 type = 'linear';
0051 
0052 if nargin < 2 | mod(length(varargin),2) ~= 0,
0053     error('Incorrect number of parameters (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).');
0054 end
0055 if ~isdvector(timestamps),
0056     error('Incorrect timestamps - should be a vector (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).');
0057 end
0058 timestamps = timestamps(:);
0059 
0060 % Parse parameter list
0061 for j = 1:2:length(varargin),
0062     if ~ischar(varargin{j}),
0063         error(['Parameter ' num2str(j+2) ' is not a property (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).']);
0064     end
0065     switch(lower(varargin{j})),
0066         case 'type',
0067             type = varargin{j+1};
0068             if ~isastring(type,'linear','circular'),
0069                 error('Incorrect value for property ''type'' (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).');
0070             end
0071         case 'trim',
0072             trim = lower(varargin{j+1});
0073             if ~isastring(trim,'on','off'),
0074                 error('Incorrect value for property ''trim'' (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).');
0075             end
0076         case 'maxgap',
0077             maxGap = varargin{j+1};
0078             if ~isdscalar(maxGap,'>=0'),
0079                 error('Incorrect value for property ''maxGap'' (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).');
0080             end
0081         otherwise,
0082             error(['Unknown property ''' num2str(varargin{j}) ''' (type ''help <a href="matlab:help Interpolate">Interpolate</a>'' for details).']);
0083     end
0084 end
0085 
0086 interpolated = [];
0087 if isempty(timestamps) | isempty(samples), return; end
0088 
0089 pointProcess = isvector(samples);
0090 
0091 % Determine which target timestamps are too far away from sample timestamps (isolated) and should be discarded
0092 [~,isolated] = Match(timestamps,samples(:,1),'match','closest','error',maxGap);
0093 
0094 if pointProcess,
0095     % 'Interpolate' samples at timestamps (see help above)
0096     [interpolated,~,discarded] = Match(samples,timestamps,'error',maxGap);
0097 else
0098     % Determine which timestamps would require extrapolation
0099     outside = logical(zeros(size(timestamps)));
0100     if strcmp(trim,'on'),
0101         outside = timestamps<samples(1,1)|timestamps>samples(end,1);
0102     end
0103     % Discard isolated timestamps and timestamps that would require extrapolation
0104     discarded = outside|isolated;
0105     timestamps(discarded) = [];
0106     % Circular data?
0107     if strcmp(type,'circular'),
0108         range = isradians(samples(:,2:end));
0109         samples(:,2:end) = exp(i*samples(:,2:end));
0110     end
0111     % Interpolate samples at timestamps
0112     interpolated = [timestamps interp1(samples(:,1),samples(:,2:end),timestamps)];
0113     % Circular data?
0114     if strcmp(type,'circular'),
0115         interpolated(:,2:end) = wrap(angle(interpolated(:,2:end)),range);
0116     end
0117 end

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