Hi,

I’m using MATLAB to extract data from a PI server via the PI OLEDB Provider. I would typically use an SQL query string like the one below to extract 10-min averages of a particular tag.

sql=strcat('SELECT value FROM piarchive..piavg WHERE tag = ''',tag,''' AND time BETWEEN ''',starttime,''' AND ''',finishtime,''' AND timestep = 600s')

If the tag links to directional data in degrees, I would like to calculate a 10-min vector average. Mathematically, this can be achieved by combining the 10-min averages of the SIN and COS components of the tag using ATAN2, i.e.

ATAN2(tag_sin,tag_cos) where

tag_sin = average of SIN(tag) for all records in 10-min period

tag_cos = average of COS(tag) for all records in 10-min period

I understand this might be possible using an expression, i.e. WHERE expr ... but I am unsure of the correct syntax. Also, does the tag have to be converted to radians before use with SIN and COS?

Can anyone help?

A further correction is required to ensure all averages are in the range 0-360deg, but I can do this afterwards.

Thanks

For my understanding:

Atan2 returns an angle, and you provide an angle in the beginning, is this detour just for your averaging ?

PI easily returns a 10 minute average of a tag (preferably a real=timeweighted average), and you can easily apply the SIN, COS and ATN2 functions on that.

Why do you need it the other way round: Avg(Sin(x)) instead of Sin(Avg(x)) ?

Why do you need averages at all?

PS:

The SIN function takes Radian units as input, sin(3.14159) is close to zero ;)

If raw data does not exceed limits ( 0.. 360 deg), an average on that is within limits as well.