File Exchange

## downsample_ts

version 1.1.0.0 (5.54 MB) by Chad Greene

Calculate monthly means, annual medians, and other statistics for 1D or 3D datasets.

Updated 30 Dec 2014

This function downsamples 1D or 3D data to monthly, yearly, hourly, minutely, or secondly data. This function was originally designed to create monthly mean time series from daily geospatial climate data.
Syntax
Z_downsamp = downsample_ts(Z,t)
Z_downsamp = downsample_ts(...,'DownsamplingPeriod')
Z_downsamp = downsample_ts(...,'function')
[Z_downsamp,t_downsamp] = downsample_ts(...)

Description
Z_downsamp = downsample_ts(Z,t) downsamples Z, which must be provided with a corresponding time vector t. Z can be 1D if its length matches the length of t. If Z is three-dimensional, the length of its third dimension must match the length of t. For geospatial climate data arrays, dimensions of Z might correspond to lat x lon x time or lon x lat x time.

Z_downsamp = downsample_ts(...,'DownsamplingPeriod') specifies a downsampling period as

'year'
'month' (default)
'day'
'hour'
'minute'
'second'

Z_downsamp = downsample_ts(...,'function') specifies a function to perform on the data. By default, monthly averages are taken, but you may wish to return the monthly median or monthly standard deviation or any of the functions listed below.

A note on functions which ignore NaNs: To get the monthly means of data while ignoring NaN values, you can use the 'nanmean' option. The nanmean function is part of the Statistics Toolbox, but may also be found as part of the NaN Suite on File Exchange. However, the File Exchange versions mix up the order of dimensions and flags for nanstd, nanvar, nanmin, and nanmax, so you will need the Statistics Toolbox for those particular functions. In all, the following functions are available:

'mean' (default)
'nanmean' ignores NaN values in Z. Requires Statistics toolbox or NaN Suite.
'median'
'nanmedian' ignores NaN values in Z. Requires Statistics toolbox or NaN Suite.
'min'
'nanmin' ignores NaN values in Z. Requires Statistics toolbox.
'max'
'nanmax' ignores NaN values in Z. Requires Statistics toolbox.
'std' standard deviation.
'nanstd' ignores NaN values in Z. Requires Statistics toolbox.
'var' variance.
'nanvar' ignores NaN values in Z. Requires Statistics toolbox.
'mode'
'sum'
'nansum'

[Z_downsamp,t_downsamp] = downsample_ts(...) also returns a time array corresponding to Z_downsamp. If Z is 3D or, t_downsamp corresponds to the third dimension of Z_downsamp. Each value in t_downsamp represents the mean time of all data contributing to that slice of Z_downsamp.

### Cite As

Chad Greene (2020). downsample_ts (https://www.mathworks.com/matlabcentral/fileexchange/48361-downsample_ts), MATLAB Central File Exchange. Retrieved .

Veronica Bozzini

### Veronica Bozzini (view profile)

thanks, exactly what I was looking for!

Peter Mills

### Peter Mills (view profile)

My data has lots of missing timestamps. I get the message
"Warning: Some time steps in input t are more than a 2 hours apart. This may cause strange results when taking hourly
averages.
> In downsample_ts (line 207) "
Does that mean I can't trust the result?
How can I convert 1 min wind speeds to hourly wind speeds when the data has lots of missing values. So far I have rounded the timestamps to the hour and taken an average of the values with the same hourly timestamp. Like this:

% Convert the time stamps to 60 minutes invervals rounding down to the
% last 60 min
dateTime60=datetime(year(dateTime),month(dateTime),day(dateTime),hour(dateTime),ceil(dateTime.Minute/60)*60,0);
dateTime60min=unique(dateTime60)';

%pre define
windNew60=ones(length(dateTime60min),1)*-8888;

for i=1:length(dateTime60min)
nancount(i,1)=sum(isnan(windSpeed(dateTime60==dateTime60min(i))));
if sum(isnan(windSpeed(dateTime60==dateTime60min(i))))==0
windNew60(i,1)=mean(windSpeed(dateTime60==dateTime60min(i)));
else
windNew60(i,1)=NaN;
end
end

mohammed ali

### mohammed ali (view profile)

very nice script,thanks for your excellent work

pedro dollar

### pedro dollar (view profile)

rajitha reddy putta