Dealing with large time series and time stamps in matlab

When dealing with time series, like LFP or EEG in electrophysiology, it is usually a good idea to keep around the time stamps that assign a time to each sample. This way, aligning data to triggers is convenient, and cropping and combining data sets requires no additional effort.

The obvious downside to this strategy is that the time stamps take up as much memory as one channel of data. Not a big deal when loading a lot of channels that share the same time stamps  but for combining multiple recordings where different channels might or might not be from the same recording, keeping tabs on which time stamp goes with which data channel is unnecessary messy.

The best solution I found for this is to keep the data in a very simple matlab object, and linking to the appropriate time stamp via a handle.  For example, lfps{1}.ts and lfps{2}.ts can both point to the same time stamp and save memory this way, but lfps{3}.ts can point to another one. This way I can bundle a bunch of channels and my analysis code will always grab the correct time stamps.

Setting up a system like this is pretty simple – we need a minimal time stamp object that just holds a ts array (and maybe some useful extra info like the sampling rate). By making this an instance of handle we’ll be able to link to it later :

classdef timestamp < handle
        ts % time in seconds
        Hz % sample rate

    end % properties
        function obj = n_timestamp(varargin)
            switch nargin
                case  0  % just make empty obj
                    obj.ts = [];
        end % constructor

This way multiple channels of data form the same recording can share the same time stamp with no memory overhead, and no additional bookkeeping is required.

Now load the data to structs, the time stamps to instances of the time stamp class, and assign:

 lfps{1}.samples=randn(1,50); % make 2 channels
ts1=timestamp; % make up timestamps
ts1.Hz=50; ts1.ts=linspace(0,1,50);

lfps{3}.samples=randn(1,100); % make 3rd channel
ts2=timestamp; % make another timestamp
ts2.Hz=100; ts2.ts=linspace(0,1,100);

% now assign ts1 to lfp 1 and 2, and ts2 to lfp 3

lfps{1}.t.ts % display first timestamp

lfps{2}.t.ts(end)=3; % change timestamp on lfp 2
lfps{1}.t.ts  % change is reflected in lfp 1<

Time stamps are now automatically associated with their LFP and only take up as much space as they need to. You still need to make sure that everything is consistent though – if you crop one channel and its time stamp  but forget to crop the data in another channel, things won’t align.

This entry was posted in Matlab. Bookmark the permalink.

Comments are closed.