root/trunk/lib/cc3-ilp/cc3_math.c

Revision 348, 2.8 kB (checked in by anthony_rowe, 2 years ago)

fixed cc3_math compile problem

Line 
1 #include "cc3_math.h"
2
3
4 #define iter1(N) \
5     try = root + (1 << (N)); \
6     if (n >= try << (N))   \
7     {   n -= try << (N);   \
8         root |= 2 << (N); \
9     }
10
11 uint32_t cc3_isqrt (uint32_t n)
12 {
13     uint32_t root = 0, try;
14     iter1 (15);    iter1 (14);    iter1 (13);    iter1 (12);
15     iter1 (11);    iter1 (10);    iter1 ( 9);    iter1 ( 8);
16     iter1 ( 7);    iter1 ( 6);    iter1 ( 5);    iter1 ( 4);
17     iter1 ( 3);    iter1 ( 2);    iter1 ( 1);    iter1 ( 0);
18     return root >> 1;
19 }
20
21
22
23 double cc3_mean( uint8_t data[], uint32_t size )
24 {
25
26   double mean = 0.0;
27   uint32_t sum = 0;
28   uint32_t i;
29
30   for (i = 0; i < size; i++) {
31     sum = sum + data[i];
32   }
33
34   mean = sum / size;
35   return mean;
36
37 }
38
39
40
41
42 /**
43
44   Calculate the linear regression coefficients, a and b, along with
45   the standard regression error (e.g., the error of b), the standard
46   deviation of the points and the correlation.
47
48   A regression line is described by the equation y' = a + bx.  The
49   coefficients a and b are returned in a lineInfo object, along
50   with the other values.
51
52   Formally, linear regression of a set of {x,y} points is described in
53   terms of independent and dependent variables.  The array x contains
54   the independent variable, which is exactly known.  The array y
55   contains the dependent variable, which is "measured".  The y values
56   are assumed to be random values, dependent on the x values.
57
58  */
59
60 void cc3_linear_reg(uint8_t x_data[], uint8_t y_data[], uint8_t size,cc3_linear_reg_data_t *reg_out )
61 {
62
63
64   if (size > 0) {
65
66
67
68     double muX = cc3_mean( x_data,size );
69     double muY = cc3_mean( y_data,size );
70
71
72     //     N-1
73     //     ---
74     //     \   (Xi - meanX)(Yi - meanY)
75     //     /__
76     //     i=0
77     // b = -----------------------------
78     //     N-1
79     //     ---             2
80     //     \   (Xi - meanX)
81     //     /__
82     //     i=0
83     //
84
85     double SSxy = 0;
86     double SSxx = 0;
87     double SSyy = 0;
88     double Sx = 0;
89     double Sy = 0;
90     double Sxy = 0;
91     double SSy = 0;
92     double SSx = 0;
93
94     for (uint8_t i = 0; i < size; i++) {
95       double subX = (x_data[i] - muX);
96       double subY = (y_data[i] - muY);
97       Sx = Sx + x_data[i];
98       Sy = Sy + y_data[i];
99       Sxy = Sxy + (x_data[i] * y_data[i]);
100       SSx = SSx + (x_data[i] * x_data[i]);
101       SSy = SSy + (y_data[i] * y_data[i]);
102       SSyy = SSyy + subY * subY;
103       SSxy = SSxy + subX * subY;
104       SSxx = SSxx + subX * subX;
105
106     }
107
108    
109
110     // slope
111     double m = SSxy / SSxx;
112     // intercept
113     double b = muY - m * muX;
114
115     double r2Numerator = (size * Sxy) - (Sx * Sy);
116     double r2Denominator = ((size*SSx) - (Sx * Sx))*((size*SSy) - (Sy * Sy));
117     double r2 = (r2Numerator * r2Numerator) / r2Denominator;
118
119     reg_out->r_sqr = r2;
120     reg_out->b = b;
121     reg_out->m = m;
122   } // if N > 0
123
124 } // lineInfo
125
126
Note: See TracBrowser for help on using the browser.