forked from simsum/oscat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARRAY_TREND.EXP
executable file
·54 lines (46 loc) · 1.38 KB
/
ARRAY_TREND.EXP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(* @NESTEDCOMMENTS := 'Yes' *)
(* @PATH := '\/Mathematical\/Array' *)
(* @OBJECTFLAGS := '0, 8' *)
(* @SYMFILEFLAGS := '2048' *)
FUNCTION ARRAY_TREND : REAL
VAR_INPUT
pt : POINTER TO ARRAY[0..32000] OF REAL;
size : UINT;
END_VAR
VAR
i: UINT;
stop: UINT;
x: REAL;
stop2: UINT;
END_VAR
(*
version 1.3 10. mar. 2009
programmer hugo
tested by tobias
this function will calculate the trend of a given array.
trend will calculate the avg of the first half of the array and then the avg of the second half, trend = avg2 - avg1.
for example: [0,1,4,5,3,4,6,3] = 4 - 2.5 = 1.5
the function needs to be called: array_trend(adr("array"),sizeof("array"));
because this function works with pointers its very time efficient and it needs no extra memory.
*)
(* @END_DECLARATION := '0' *)
stop := SHR(size,2)-1;
stop2 := SHR(stop,1);
FOR i := 0 TO stop2 DO x := x - pt^[i]; END_FOR;
IF EVEN(UINT_TO_INT(stop)) THEN
FOR i := stop2 TO stop DO X := X + pt^[i]; END_FOR;
ELSE
FOR i := stop2 + 1 TO stop DO X := X + pt^[i]; END_FOR;
END_IF;
ARRAY_TREND := x / UINT_TO_REAL(stop2 + 1);
(* revision history
hm 2 oct 2007 rev 1.0
original version
hm 12 dec 2007 rev 1.1
changed code for better performance
hm 16. mar. 2008 rev 1.2
changed type of input size to uint
hm 10. mar. 2009 rev 1.3
added type conversions for compatibility reasons
*)
END_FUNCTION