SOLUTION: Calculating the average of channel with uneven timestamps using trapz | MATLAB | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
SOLUTION: Calculating the average of channel with uneven timestamps using trapz
Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
1
September 15, 2015 - 1:55 pm
sp_Permalink sp_Print

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')

data =

Timestamps TempF
____________________ ______

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))

X =

0
61
121
363
483

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)

avg =

68.9317

More about trapz: http://www.mathworks.com/help/matlab/ref/trapz.html

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Avatar
fishmastaflex

Silver
Forum Posts: 11
sp_UserOfflineSmall Offline
2
September 15, 2015 - 3:30 pm
sp_Permalink sp_Print

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.

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
3
September 15, 2015 - 7:17 pm
sp_Permalink sp_Print

I like it.

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: jacktheripper125
18 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

Nsneilotof

angieus2

goldiepw11

BinanceoSt

gnarender123@gmail.com

edenderest

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee