Time Series Indexing and Averaging

Outputs windowed means of a continuous set of data given a vector of labels and time stamps
Updated 19 Feb 2015

View License

Doing stimulus triggered averaging? Continuous EEG analysis? Want to know what the average hourly barometric pressures were for the last 30 years, but you need individual results for each month?
A common problem in time series analysis is trying to get the average signal that follows (or precedes) a set of time points. This function will output means for an arbitrary set of indices. The function takes as its inputs the time series itself, a vector of labels, and matching time stamps. The output will be an array with mean signal in the rows with a column for each unique label.
Important: the time stamps must be in samples at the sample rate of the time series.

TIMESERIES_INDEXER windows and sums timeseries given time and labels
[mean_data,label_counts] = TIMESERIES_INDEXER(labels,timestamps,win,data)
Given a time series (vector), a set of grouping labels, time stamps,
and an analysis window this function will output an array of mean data.

This is useful for instances where you know when each particular
stimulus type occurred and you wish to find the average response
following (or preceding) these stimuli.

Input args:
labels (int) an array of labels
time (samples) stamps associated with the stimulus labels
win (samples) beginning and end of the analysis window, e.g.,[-10 40]
data (float, int) the time series to analyze

data = rand(88200,1)+sin(2*pi*5*linspace(0,2,88200)');
labels = [1 2 1 2 1 2 1 2 1 2];
timestamps = linspace(0,44099-4410,10);
win = [-441 4410];
mean_data = TIMESERIES_INDEXER(labels,timestamps,win,data);

Full output can include the pre-meaned data array:
[~,~,full_array] = timeseries_indexer(labels,timestamps,win,data);

Note: The function will return means even if the total counts for each
label are not the same. Care should be taken as the variance in the
means will be higher for labels with fewer instances (this is

Author: Owen Brimijoin - MRC/CSO Institute of Hearing Research
Date: 03/07/14

Cite As

W. Owen Brimijoin (2024). Time Series Indexing and Averaging (https://www.mathworks.com/matlabcentral/fileexchange/47142-time-series-indexing-and-averaging), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Matrix Indexing in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Changed title.

added a force input to columns in case of vector inputs with different orientations

Update of description

Bug fix: If the user specified labels of all one kind, then the code would fail. Now this is properly handled.

Changed the padding method to use NaNs so that means are accurate for windows preceding the label triggers.

Added the note that this function works also for labels with unequal counts.

Updated description

Updated help text