Taking Averages Of One Channel As A Function Of Time


Recommended Posts

Hello,

 

I am trying to write a sequence that enables me to create a channel (Average TOC) that contains the averages of a certain channel (TOC) in as function of time.

 

We have three samples taken by a TOC analyzer, the three-sample loop is for 8 minutes. The way we want to run it is as follows:

1. First sample: delay for one minute, take the TOC average for 1 minutes

 

2. Second sample: delay one minute, take the TOC average for 2 minutes

 

3. Third sample: delay one minute, take the TOC average for 2 minutes

 

4. Loop the above.

 

Below is a pseudo-code of the process, at least what I think makes sense.

 

Thanks.

 

device.TOCOutput.purge()

while(1)

try

            private string detain = device.tocoutput.ReadUntil (13)

 

//Modifying string for IC

private string vIC =delete(detain,1,32)

vIC=delete(vIC,5,17)

 

//Modifying string for TC

private string vTC =delete(detain,1,36)

vIC=delete(vTC,5,13)

 

//Modifying string for TOC

private string vTOC =delete(detain,1,27)

vIC=delete(vTOC,5,22)

 

//Add channels for each output

IC.AddValue(StrToDouble(vIC))

TC.AddValue(StrToDouble(vTC))

TOC.AddValue(StrToDouble(vTOC))

catch()

System.ErrorMessage(strLastError)

delay(0.01)

endcatch

 

//Add channel

Average.TOC.AddValue(StrToDouble(vTOC))

 

Private next time=12hr00m //first time to start the first loop

Private loop time=720 //Loop =12 min

 

//First sample

delay(60)

for (private next time =12h00m, x>n, x++)

execute(“ch”+DoubleToStr(x)+”=average”)

wait(120)

delay(60)

 

//Second sample

for (private next time =12h00m, x>n, x++)

execute(“ch”+DoubleToStr(x)+”=average”)

wait(120)

delay(60)

 

//Third sample

for (private next time =12h00m, x>n, x++)

execute(“ch”+DoubleToStr(x)+”=average”)

wait(120)

delay(60)

 

endwhile

Link to comment
Share on other sites

OK, first, watch your indentation in your script.  Without it, your script is very hard to read.  

 

Next, I'd create two sequences, one to take data and put it in your live readings, then a second one to take the averages.  The first one would loop every second or so, filling in your TOC and other channels with the serial data.  The second sequence would use that data to do the averages for TOC.  Let's assume you created three test channels to hold the TOC averages called "TOCAverage1", "TOCAverage2" and "TOCAverage3".  The script would simply be:

 

while(1)

   wait(120) // wait 2 minutes

   TOCAverage1.addValue(mean(TOC[systime(), systime()-60]))

   wait(180)

   TOCAverage2.addValue(mean(TOC[systime(), systime()-120]))

   wait(180)

   TOCAverage3.addValue(mean(TOC[systime(), systime()-120]))

endwhile

 

A number of things:

1) I wait until the end of the average period, then average the last 60 or 120 seconds worth of data points.  

2) I use wait() instead of delay() in this case because you want it to be non-propogating and have an exactly 8 minute loop.  In most every other situation you would use delay(), but this is a perfect example of where wait() is appropriate (and what its designed for).

Link to comment
Share on other sites

Since the average TOC values are stored in channels, you can just add TOCAverage1, 2 and 3 to your logging set.  Note that since these values don't update as often, you may see gaps in the logging set for these channels, depending on the logging set settings.

Link to comment
Share on other sites

  • 1 month later...

Thanks for that!


 


Everything seems to be working fine with the above code for both averages and standard deviations however we have a little problem.


 


The TOC analyzer (our device) stops every 20 hours to initialize for a couple of minutes, meaning that there is no outcome of TOC values, in this case, the above sequence stops, is there a way we can use the try and catch function to detect the errors but instead of going back to the beginning of the sequence, once the error seizes we can go back to the last step that has been stopped before the error has occurred?


 


I was thinking of using the code below however, the problem is that when the error disappears the sequence will start from the beginning and we would like for it to start from the last step is has stopped at, any modifications for the sequence below?


 



while(1)

   try

      wait(200) // wait 3.3 minutes

      AverageTOC1.addValue(mean(TOC[systime(), systime()-40]))

      StdDevTOC1.addValue(stdDev(TOC[systime(), systime()-40]))

      wait(200)  //wait 3.3 minutes

      AverageTOC2.addValue(mean(TOC[systime(), systime()-40]))

      StdDevTOC2.addValue(stdDev(TOC[systime(), systime()-40]))

      wait(200)   //wait 3.3 minutes

      AverageTOC3.addValue(mean(TOC[systime(), systime()-40]))

      StdDevTOC3.addValue(stdDev(TOC[systime(), systime()-40]))

   catch()

      ? strLastError

      delay(0.1)

   endcatch

endwhile

Link to comment
Share on other sites

Archived

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