function m = tseriesinterp(m,trorig,trnew,dim,numsamples) <m> is a matrix with time-series data along some dimension. can also be a cell vector of things like that. <trorig> is the sampling time of <m> (e.g. 1 second) <trnew> is the new desired sampling time <dim> (optional) is the dimension of <m> with time-series data. default to 2 if <m> is a row vector and to 1 otherwise. <numsamples> (optional) is the number of desired samples. default to the number of samples that makes the duration of the new data match or minimally exceed the duration of the original data. use interp1 to cubic-interpolate <m> (with extrapolation) such that the new version of <m> coincides with the original version of <m> at the first time point. example: x0 = 0:.1:10; y0 = sin(x0); y1 = tseriesinterp(y0,.1,.23); figure; hold on; plot(x0,y0,'r.-'); plot(0:.23:.23*(length(y1)-1),y1,'go');
0001 function m = tseriesinterp(m,trorig,trnew,dim,numsamples) 0002 0003 % function m = tseriesinterp(m,trorig,trnew,dim,numsamples) 0004 % 0005 % <m> is a matrix with time-series data along some dimension. 0006 % can also be a cell vector of things like that. 0007 % <trorig> is the sampling time of <m> (e.g. 1 second) 0008 % <trnew> is the new desired sampling time 0009 % <dim> (optional) is the dimension of <m> with time-series data. 0010 % default to 2 if <m> is a row vector and to 1 otherwise. 0011 % <numsamples> (optional) is the number of desired samples. 0012 % default to the number of samples that makes the duration of the new 0013 % data match or minimally exceed the duration of the original data. 0014 % 0015 % use interp1 to cubic-interpolate <m> (with extrapolation) such that 0016 % the new version of <m> coincides with the original version of <m> 0017 % at the first time point. 0018 % 0019 % example: 0020 % x0 = 0:.1:10; 0021 % y0 = sin(x0); 0022 % y1 = tseriesinterp(y0,.1,.23); 0023 % figure; hold on; 0024 % plot(x0,y0,'r.-'); 0025 % plot(0:.23:.23*(length(y1)-1),y1,'go'); 0026 0027 % internal constants 0028 numchunks = 20; 0029 0030 % input 0031 if ~exist('dim','var') || isempty(dim) 0032 dim = choose(isrowvector(m),2,1); 0033 end 0034 if ~exist('numsamples','var') || isempty(numsamples) 0035 numsamples = []; 0036 end 0037 0038 % prep 0039 if iscell(m) 0040 leaveascell = 1; 0041 else 0042 leaveascell = 0; 0043 m = {m}; 0044 end 0045 0046 % do it 0047 for p=1:length(m) 0048 0049 % prep 2D 0050 msize = size(m{p}); 0051 m{p} = reshape2D(m{p},dim); 0052 0053 % calc 0054 if isempty(numsamples) 0055 numsamples = ceil((size(m{p},1)*trorig)/trnew); 0056 end 0057 0058 % do it 0059 timeorig = linspacefixeddiff(0,trorig,size(m{p},1)); 0060 timenew = linspacefixeddiff(0,trnew,numsamples); 0061 0062 % do in chunks 0063 chunks = chunking(1:size(m{p},2),ceil(size(m{p},2)/numchunks)); 0064 temp = {}; 0065 parfor q=1:length(chunks) 0066 temp{q} = interp1(timeorig,m{p}(:,chunks{q}),timenew,'cubic','extrap'); 0067 end 0068 m{p} = catcell(2,temp); 0069 clear temp; 0070 0071 % prepare output 0072 msize(dim) = numsamples; 0073 m{p} = reshape2D_undo(m{p},dim,msize); 0074 0075 end 0076 0077 % prepare output 0078 if ~leaveascell 0079 m = m{1}; 0080 end