/* calculate regression quadratic polynomial coefficients */ void GetCoefficient(double *input, double *output, int dw_num, int *position, int TOTAL, int total, int length, int *win_size_forward, int *win_size_backward) { int i, j, l, t, d, index, width; double T0, T1, T2, T3, T4, b[3]; double **Matrix = (double **) malloc(sizeof(double *) * 3); double *tmpMat = dgetmem(3 * 3); Boolean boundary_begin = FA, boundary_end = FA; for (i = 0, j = 0; i < 3; i++, j += 3) { Matrix[i] = tmpMat + j; } for (d = 0; d < dw_num - 1; d++) { if (MAGIC == TR) { for (t = 0; t < TOTAL; t++) { for (l = 0; l < length; l++) { if (d == 0) { output[dw_num * length * t + length + l] = magic; } else if (d == 1) { output[dw_num * length * t + length * 2 + l] = magic; } } } } for (t = 0; t < total; t++) { T0 = T1 = T2 = T3 = T4 = 0.0; boundary_begin = boundary_end = FA; for (i = -win_size_backward[d]; i <= win_size_forward[d]; i++) { index = t + i; if (index < 0) { boundary_begin = TR; width = (int) (-1.0E30); /* point at infinity */ } else if (index >= total) { boundary_end = TR; width = (int) (1.0E30); /* point at infinity */ } else { width = position[index] - position[t]; } T0++; T1 += width; T2 += pow(width, 2); T3 += pow(width, 3); T4 += pow(width, 4); } Matrix[0][0] = T0; Matrix[0][1] = T1; Matrix[0][2] = T2; Matrix[1][0] = T1; Matrix[1][1] = T2; Matrix[1][2] = T3; Matrix[2][0] = T2; Matrix[2][1] = T3; Matrix[2][2] = T4; LU(3, Matrix); /* LU decomposition */ for (l = 0; l < length; l++) { b[0] = 0.0; b[1] = 0.0; b[2] = 0.0; for (i = -win_size_backward[d]; i <= win_size_forward[d]; i++) { int tmp; if (t + i < 0) { tmp = position[0]; width = position[0] - position[t]; } else if (t + i > total) { tmp = position[total - 1]; width = position[total - 1] - position[t]; } else { tmp = position[t + i]; width = position[t + i] - position[t]; } b[0] += input[length * (tmp) + l]; b[1] += width * input[length * (tmp) + l]; b[2] += pow(width, 2) * input[length * (tmp) + l]; } SOLVE(3, Matrix, b); /* solve linear equation */ if (d == 0) { /* output static */ output[dw_num * length * position[t] + l] = input[length * position[t] + l]; /* output delta */ if (boundary_begin == TR && win_size_backward[d] == 1) { output[dw_num * length * position[t] + length + l] = (input[length * position[t + 1] + l] - input[length * position[t] + l]) / (position[t + 1] - position[t]); } else if (boundary_end == TR && win_size_forward[d] == 1) { output[dw_num * length * position[t] + length + l] = (input[length * position[t] + l] - input[length * position[t - 1] + l]) / (position[t] - position[t - 1]); } else { output[dw_num * length * position[t] + length + l] = b[1]; } } else if (d == 1) { /* output delta-delta */ if (boundary_begin == TR && win_size_backward[d] == 1) { output[dw_num * length * position[t] + length * 2 + l] = 0.0; } else if (boundary_end == TR && win_size_forward[d] == 1) { output[dw_num * length * position[t] + length * 2 + l] = 0.0; } else { output[dw_num * length * position[t] + length * 2 + l] = 2 * b[2]; } } } } } }
// Returns a pointer to solution array. int* solve-ls(int A[][], int b[], int ) { LUP-Decomposition(); int* result = LUP-SOLVE(); return result; }