fishmastaflex asked a question about using the trapz function in MATLAB to calculate the average temperature on a channel where the samples come in irregularly. There's no built in function to do this, but it's only a couple lines of code.
I have a similar challenge with one of my temperature sensors. It only sends data when there is a significant change:
>> data = thingSpeakRead(32188,'NumPoints',5,'OutputFormat','table')
15-Sep-2015 13:28:33 68.584
15-Sep-2015 13:29:34 68.685
15-Sep-2015 13:30:34 68.861
15-Sep-2015 13:34:36 69.047
15-Sep-2015 13:36:36 69.188
To use trapz, you need to convert the timestamps into X coordinates. Do this by calculating the relative time for each timestamp in seconds:
X = seconds(data.Timestamps - data.Timestamps(1))
Calculate the area under the curve (approximate integral) using trapz, then divide by the number of elapsed seconds to get the mean average:
>> avg = trapz(X,data.TempF) / X(end)
More about trapz: http://www.mathworks.com/help/matlab/ref/trapz.html
Thanks Rob. Glad to spark the conversation.
My method works pretty good too using the posix time function. It's only one line of code:
areaUnderCurve = trapz(posixtime(X),Y);
X is my matrix of timestamps
Y is my data
Watch out for NaN values though as well. Even if you have just one, the trapz function will spit out NaN. I used isnan to delete values in the matrix.
Most Users Ever Online: 114
Currently Online: jacktheripper125
Currently Browsing this Page:
Guest Posters: 1
Administrators: Hans, lee