Using Mean(Array) To Average Just A Portion Of An Array.


cadcoke5

Recommended Posts

I want to view an average of just a portion of an array.  Specifically, the last 10 readings from a sensor, and another set of readings from earlier in the array.  My ultimate goal is to do analyze if the temperature of n object has finished cooling, and is no longer changing temperature.  I also want to predict how long it will take for it to finish cooling, so I  will eventually analyze the curve, and make the prediction based on that.  I suppose I could just choose one data point for each point of the curve, but it seemed wiser to do an average to reduce the influence of noise.

 

A program I have is not working, and while I realize I may have other problems in the program I wanted to make sure my understanding of how subsetting and Mean should work.  And, in particular, to confirm that I can refer to a subset in the Mean function. The array is called Temperature.

 

I think the term Temperature[0,9] will return the last 10 readings.  So, the term Mean(Temperature[0,9]) should give me the average of  those same temperatures.  Is that correct?

 

-Joe

Link to comment
Share on other sites

Yes, that is correct.  Mean() takes an array, and both Temperature and Temperature[0,9] are arrays (provided Temperature is a channel, or variable array).  You can always check this by doing this:

 

1) go into safe mode

2) go to the Command / Alert and type:

 

? temperature[0,9]

? mean(temperature[0,9])

 

then you can verify it yourself.

 

Also, not that you can also subset by time, so:

 

temperature[systime(), systime() - 60]

 

returns all the data points in the last 60 seconds.  Wrap that in mean() and you get the mean of the last minute's worth of data.  You can put any time stamps you want in there.  For example, you have this variable called starttime.  You could do:

 

temperature[starttime, starttime + 60]

 

which is all the data from the 60 seconds after starttime.

Link to comment
Share on other sites

  • 2 weeks later...

I am not clear on the subsetting parameters.

 

Since temperature[0,9] returns the last 10 values, I was thinking that the terms refer to the end time with zero being the most recent, and then the 2nd term is how many seconds before the end time to select. 

 

However, in the statement, temperature[systime(), systime() - 60] my above "rules" don't seem to apply.  In this statement, you are using the time stamp for the data, and they are not relative, but instead are absolute.

 

What do the two parameters of the data subset refer to?

 

-Joe

Link to comment
Share on other sites

It depends.  If the subset numbers are less than 10 million, it is the index into the history array, so:

 

temperature[0,9] returns the last 10 points no matter what the time is (and assuming there are actually 10 points available, otherwise it will return whatever it can).

 

If the subset numbers are > 10 million, the numbers are assumed to be absolute time stamps, and DAQFactory returns all the values that have a time stamp between (and including I believe) the two numbers you provide, so:

 

temperature[systime(), systime()-60]  returns the last 60 seconds worth of data whether that is 0, 1 or 1000 data points.  Systime() is a function that returns the absolute current time.  Time in DAQFactory is unix time: seconds since 1970, and at present is on the order of 1.4 billion seconds.

Link to comment
Share on other sites

Is there a direction to the parameters?  I.e. in absolute time stamp mode, do you put the end time as the 1st parameter, and the start time as the 2nd parameter?  But, in relative mode I think it means put the end time 2nd, and then the start time. Then, not only are they reversed, but refer to how many seconds before the present time?

 

Trying to figure this out by trial an error is frustrating.

 

-Joe

Link to comment
Share on other sites

Section 4.6 does not describe the modal nature of the parameters.  I.e. greater than 10 million it being the time stamp.

 

I did some tests, and now see that parameter order does not matter when subsetting.  And the results are always returned starting with the earliest indes.  I.e.both myarray[0,10] and myarray[10,0} result in the exact same thing.

 

-Joe

Link to comment
Share on other sites

Truthfully, I don't even know if its 10 million or 100 million or what.  It doesn't really matter unless you actually have 10 million data points in your array, which you would almost never have.  In fact I've never heard of anyone running into a problem of trying to subset up in the 10's of millions by index and accidentally getting subsetting by time.  Just know that if you provide absolute time stamps in the [], you'll be subsetting by time.  Otherwise you're subsetting by index.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.