


EMBED Time-series lag embedding.
EMBEDDED = EMBED(TIMESERIES, LAG_SAMPLES, DIMENSION) takes a scalar
(M x 1) time series and returns a vector time series of dimension
((M - lag_samples*(dimension-1)) x dimension) time series by lag
embedding the time series. E.g.,
embed([x(1); x(2); x(3); ... ; x(N)], 1, 3)
returns
[x(1), x(2), x(3); x(2), x(3), x(4); ... ; x(N-1), x(N)]
NOTE: A matrix input 'timeseries' is treated as 'timeseries(:)'

0001 function embedded = embed(timeseries, lag, D) 0002 %EMBED Time-series lag embedding. 0003 % EMBEDDED = EMBED(TIMESERIES, LAG_SAMPLES, DIMENSION) takes a scalar 0004 % (M x 1) time series and returns a vector time series of dimension 0005 % ((M - lag_samples*(dimension-1)) x dimension) time series by lag 0006 % embedding the time series. E.g., 0007 % embed([x(1); x(2); x(3); ... ; x(N)], 1, 3) 0008 % returns 0009 % [x(1), x(2), x(3); x(2), x(3), x(4); ... ; x(N-1), x(N)] 0010 % 0011 % NOTE: A matrix input 'timeseries' is treated as 'timeseries(:)' 0012 0013 if (nargin < 3) 0014 error('Incorrect number of arguments.'); 0015 end 0016 0017 % Column-ize the time series. 0018 timeseries = timeseries(:); 0019 0020 M = length(timeseries); % length of input time series 0021 N = M - lag * (D-1); % length of output time series 0022 0023 % Now do the embedding efficiently by indexing into the original timeseries 0024 embedded = timeseries(repmat((1:N)', 1, D) + repmat(lag * (0:(D-1)), N, 1)); 0025