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

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Anna on 28 Jun 2013

Hi,

I have a time series which has vector values recorded at a frequency greater than 1 hz (1 measurement per second) with uneven intervals and I would like to average the vector values for each second so that I effectively have a time series with a 1 hz frequency.

Is it possible to create a loop with variable increments to achieve this? Or might ACCUMARRAY be more suitable?

Here is an example of my data set.

Time SO4 77237.3 0.165 77247.3 0.177 77276 0.137 77304.4 0.093 77304.4 0.093 77332.8 0.103 77361.3 0.072

Any help would be greatly appreciated. Thanks

*No products are associated with this question.*

Answer by Roger Stafford on 28 Jun 2013

Accepted answer

One possibility is to use 'interp1' set to interpolate at some appropriate multiple of one hertz, say, 20 Hz. You can select the type of interpolation that best suits your data. Read the documentation at

http://www.mathworks.com/help/matlab/ref/interp1.html

Then take a straightforward average over each twenty (or whatever multiple you choose) points to get the average for the corresponding second.

Anna on 14 Jul 2013

Thank you for your reply Roger Stafford! I have been trying to use 'interp1' but am still having some problems. I was able to use interp1 successfully for the above example because the two vectors were the same length.

I have another variable 'CCN' which has 9000 data points at a 1 hz frequency and I want to compare this to the 'Time' variable which has 300 data points. The range I am trying to compare in the two variables is from 77237 seconds (from 00 UTC) to 86357 seconds.

Here is my code: `for k = 1:1:303
Time(end:end+numel(CCN)-numel(Time))=nan;
CCN_new = interp1(Time,CCN,(77237:86357))`
but I get the followig error message `Error using griddedInterpolant The coordinates of the input points must be finite values; Inf and Nan are not permitted. Error in interp1>Interp1D (line 346) F = griddedInterpolant(Xext,V,method); Error in interp1 (line 241) Vq = Interp1D(X,V,Xq,method);`
Is there a way of making the vectors the same length without adding 'Nan' to the shorter one?

Please help if you can, Thanks

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/80582#comment_157479

I didn't realise the example data I inserted had posted as linear text. I'm not sure how to rectify this so apologies but I hope it's clear that the 'Time' data is 77237.3, 77247.3, 77276.0 ...and the SO4 data is 0.165, 0.177, 0.137...