Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Averaging time series data

Subject: Averaging time series data

From: Jasim Uddin

Date: 1 Dec, 2010 01:03:04

Message: 1 of 4

Hi,

I would like to avarage a set of time sreis data (10 Hz frequency) for a certain interval like 60 mins.

The avearge would be (12:00 - 13:00; 13:00 - 14:00; 14:00 - 15:00 and so on)

Can anybody help me giving instructions how can I write a program to do this?

Example of data:

Date/Time Temp
6/09/2009 12:00 20.08548562
6/09/2009 12:15 20.17942794
6/09/2009 12:30 20.54052205
6/09/2009 12:45 21.07857338
6/09/2009 13:00 21.76388834
6/09/2009 13:15 21.36606862
6/09/2009 13:30 21.58583481
6/09/2009 13:45 21.84405203
6/09/2009 14:00 21.76810748
6/09/2009 14:15 21.64377218
6/09/2009 14:30 21.46605614
6/09/2009 14:45 21.5566405
6/09/2009 15:00 21.93934029
6/09/2009 15:15 21.53546439
6/09/2009 15:30 22.00349016
6/09/2009 15:45 21.99704974
6/09/2009 16:00 21.63492037
6/09/2009 16:15 21.62890214
6/09/2009 16:30 21.40934962
6/09/2009 16:45 20.9687894
6/09/2009 17:00 20.41361129
6/09/2009 17:15 20.01431728
6/09/2009 17:30 19.58113712
6/09/2009 17:45 19.21327592
6/09/2009 18:00 18.86060374
6/09/2009 18:15 18.7428065
6/09/2009 18:30 18.76495194
6/09/2009 18:45 18.57097182
6/09/2009 19:00 18.18031066
6/09/2009 19:15 17.98656724
6/09/2009 19:30 17.45900696
6/09/2009 19:45 16.99727307
6/09/2009 20:00 16.67336436
6/09/2009 20:15 16.49176298
6/09/2009 20:30 16.00970029
6/09/2009 20:45 15.68216468
6/09/2009 21:00 15.64732246
6/09/2009 21:15 15.63762656
6/09/2009 21:30 15.83806566
6/09/2009 21:45 15.84914442
6/09/2009 22:00 15.81413106
6/09/2009 22:15 15.73960461
6/09/2009 22:30 15.69615527
6/09/2009 22:45 15.70732995
6/09/2009 23:00 15.68631716
6/09/2009 23:15 15.66894038
6/09/2009 23:30 15.67705367
6/09/2009 23:45 15.67332331


Regards.

Carl

Subject: Averaging time series data

From: Walter Roberson

Date: 1 Dec, 2010 02:24:15

Message: 2 of 4

On 30/11/10 7:03 PM, Jasim Uddin wrote:

> I would like to avarage a set of time sreis data (10 Hz frequency) for a
> certain interval like 60 mins.
>
> The avearge would be (12:00 - 13:00; 13:00 - 14:00; 14:00 - 15:00 and so
> on)
>
> Can anybody help me giving instructions how can I write a program to do
> this?

This gets asked about once a week, it sometimes feels like.

Take your time data and use datenum() to convert it from strings into
serial dates. Find the minimum of those and use fix() to take its
integer part. That will be the serial date of the beginning of the
starting day. Find the maximum of them serial dates and use ceil() to
find the serial date of the beginning of the day after the last day.

Construct the vector which is the beginning of the starting day to the
beginning of the day after the last day, with increments of 1/24 . The
values in that vector will be the serial dates of the beginning of each
hour within the range of days.

Take your converted serial dates and use histc() on them, with the
boundaries being the serial dates of the hours as described above. You
can throw away the data counts, but keep the bin numbers (the second
output from histc).

Take the bin numbers determined by histc() and convert that to a column
vector (important!), and take the data vector (e.g., your 22.00349016)
and conver that to a column vector, and use the column of bin numbers as
the first argument to accumarray and column of data as the second
argument to accumarray, pass [] as the third argument to accumarray to
take its default value, and pass @mean as the fourth argument to accumarray.

The output of accumarray will be a column vector, one entry per hour
throughout the range, zero for hours that had no data, and the average
within the hour for entries that did have data. If you need to form a
human readable version of the times involved, convert the boundary times
back to readable dates.

If your data can never be 0 or negative, then you can condense the
accumarray output by throwing away the 0 elements (and corresponding
boundary times), but if your data can include 0 or negatives, then it
takes more work to determine whether a 0 indicates no data or data that
happened to average to 0. One easy way to avoid this problem is to
specify NaN as the fillvalue to accumarray()

Subject: Averaging time series data

From: Olivier

Date: 26 Jan, 2012 00:52:11

Message: 3 of 4

I am trying to average time series values contained within one year on a monthly basis.

I followed the instructions above, but am encountering a few difficulties...

----------------------------------------
load filename.mat jd Hs

% Date string [ jd ] 1 x 27960360 (= 1 calendar year in length)
% Values [ Hs ] 1 x 27960360 (contains NaNs, cannot be 0 or negative)

% Average Hs values on a monthly basis:
interval = find(datenum(1998,1,1) <= jd & jd <= datenum(1998,12,31));
for
     month = 1:12
     f = find(datenum(1998,month,1) <= jd & jd <= datenum(1998,month+1,1)-1);
end
[unused, bin] = histc(interval, f);
monthlymeans = accumarray(bin.', Hs(:,1), [], @mean);

% ...But I get the following error:
% second input VAL in accumarray must be a vector with one element for each row in SUBS, or a scalar.

-----------------
% I also tried without logical indexing:

% Specify time interval
startdate = datenum(1998,1,1);
enddate = datenum(1998,12,31);
f1 = jd < startdate | jd > enddate;
jd(f1) = []; Hs(f1) = [];

% Average Hs on a monthly basis
for
     month = 1:12
     f2 = datenum(1998,month,1) <= jd & jd <= datenum(1998,month+1,1)-1;
     Hs_m = mean(~isnan(Hs(f2)));
end

% ...But Hs_m turns out to be 1x1 instead of 1x12 (for 12 monthly averages). ???
-------------
Any help would be much appreciated, thanks!

Subject: Averaging time series data

From: Greg Heath

Date: 26 Jan, 2012 15:37:25

Message: 4 of 4

On Jan 25, 7:52 pm, "Olivier " <opf...@gmail.com> wrote:
> I am trying to average time series values contained within one year on a monthly basis.
>
> I followed the instructions above, but am encountering a few difficulties...
>
> ----------------------------------------
> load filename.mat jd Hs
>
> % Date string [ jd ] 1 x 27960360 (= 1 calendar year in length)
> % Values [ Hs ] 1 x 27960360 (contains NaNs, cannot be 0 or negative)
>
> % Average Hs values on a monthly basis:
> interval = find(datenum(1998,1,1) <= jd & jd <= datenum(1998,12,31));
> for
>      month = 1:12
>      f = find(datenum(1998,month,1) <= jd & jd <= datenum(1998,month+1,1)-1);
> end
> [unused, bin] = histc(interval, f);
> monthlymeans = accumarray(bin.', Hs(:,1), [], @mean);
>
> % ...But I get the following error:
> % second input VAL in accumarray must be a vector with one element for each row in SUBS, or a scalar.
>
> -----------------
> % I also tried without logical indexing:
>
> % Specify time interval
> startdate = datenum(1998,1,1);
> enddate = datenum(1998,12,31);
> f1 = jd < startdate | jd > enddate;
> jd(f1) = []; Hs(f1) = [];
>
> % Average Hs on a monthly basis
> for
>      month = 1:12
>      f2 = datenum(1998,month,1) <= jd & jd <= datenum(1998,month+1,1)-1;
>      Hs_m = mean(~isnan(Hs(f2)));
> end
>
> % ...But Hs_m turns out to be 1x1 instead of 1x12 (for 12 monthly averages). ???
> -------------
> Any help would be much appreciated, thanks!

The first character ( e.g., the 6 in 6/xx/xxxx) indicates the month.
The third
data column indicates the temperature.

What else is needed? Or am I missing something?

Hope this helps.

Greg

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us