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

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
SOLUTION: Calculating the average of channel with uneven timestamps using trapz
No permission to create posts
September 15, 2015
1:55 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 46
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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!

September 15, 2015
3:30 pm
Avatar
fishmastaflex

Silver
Members
Forum Posts: 11
Member Since:
June 18, 2015
sp_UserOfflineSmall Offline

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.

September 15, 2015
7:17 pm
Avatar
Rob Purser
Natick, MA

MathWorks
Members
Forum Posts: 46
Member Since:
August 22, 2015
sp_UserOfflineSmall Offline

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:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457