You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.2KB

  1. /*
  2. * linear least squares model
  3. *
  4. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * linear least squares model
  25. */
  26. #include <math.h>
  27. #include <string.h>
  28. #include "attributes.h"
  29. #include "config.h"
  30. #include "internal.h"
  31. #include "version.h"
  32. #include "lls.h"
  33. static void update_lls(LLSModel *m, double *var)
  34. {
  35. int i, j;
  36. for (i = 0; i <= m->indep_count; i++) {
  37. for (j = i; j <= m->indep_count; j++) {
  38. m->covariance[i][j] += var[i] * var[j];
  39. }
  40. }
  41. }
  42. void avpriv_solve_lls(LLSModel *m, double threshold, unsigned short min_order)
  43. {
  44. int i, j, k;
  45. double (*factor)[MAX_VARS_ALIGN] = (void *) &m->covariance[1][0];
  46. double (*covar) [MAX_VARS_ALIGN] = (void *) &m->covariance[1][1];
  47. double *covar_y = m->covariance[0];
  48. int count = m->indep_count;
  49. for (i = 0; i < count; i++) {
  50. for (j = i; j < count; j++) {
  51. double sum = covar[i][j];
  52. for (k = i - 1; k >= 0; k--)
  53. sum -= factor[i][k] * factor[j][k];
  54. if (i == j) {
  55. if (sum < threshold)
  56. sum = 1.0;
  57. factor[i][i] = sqrt(sum);
  58. } else {
  59. factor[j][i] = sum / factor[i][i];
  60. }
  61. }
  62. }
  63. for (i = 0; i < count; i++) {
  64. double sum = covar_y[i + 1];
  65. for (k = i - 1; k >= 0; k--)
  66. sum -= factor[i][k] * m->coeff[0][k];
  67. m->coeff[0][i] = sum / factor[i][i];
  68. }
  69. for (j = count - 1; j >= min_order; j--) {
  70. for (i = j; i >= 0; i--) {
  71. double sum = m->coeff[0][i];
  72. for (k = i + 1; k <= j; k++)
  73. sum -= factor[k][i] * m->coeff[j][k];
  74. m->coeff[j][i] = sum / factor[i][i];
  75. }
  76. m->variance[j] = covar_y[0];
  77. for (i = 0; i <= j; i++) {
  78. double sum = m->coeff[j][i] * covar[i][i] - 2 * covar_y[i + 1];
  79. for (k = 0; k < i; k++)
  80. sum += 2 * m->coeff[j][k] * covar[k][i];
  81. m->variance[j] += m->coeff[j][i] * sum;
  82. }
  83. }
  84. }
  85. static double evaluate_lls(LLSModel *m, double *param, int order)
  86. {
  87. int i;
  88. double out = 0;
  89. for (i = 0; i <= order; i++)
  90. out += param[i] * m->coeff[order][i];
  91. return out;
  92. }
  93. av_cold void avpriv_init_lls(LLSModel *m, int indep_count)
  94. {
  95. memset(m, 0, sizeof(LLSModel));
  96. m->indep_count = indep_count;
  97. m->update_lls = update_lls;
  98. m->evaluate_lls = evaluate_lls;
  99. if (ARCH_X86)
  100. ff_init_lls_x86(m);
  101. }
  102. #ifdef TEST
  103. #include <stdio.h>
  104. #include <limits.h>
  105. #include "lfg.h"
  106. int main(void)
  107. {
  108. LLSModel m;
  109. int i, order;
  110. AVLFG lfg;
  111. av_lfg_init(&lfg, 1);
  112. avpriv_init_lls(&m, 3);
  113. for (i = 0; i < 100; i++) {
  114. LOCAL_ALIGNED(32, double, var, [4]);
  115. double eval;
  116. var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2;
  117. var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  118. var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  119. var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  120. m.update_lls(&m, var);
  121. avpriv_solve_lls(&m, 0.001, 0);
  122. for (order = 0; order < 3; order++) {
  123. eval = m.evaluate_lls(&m, var + 1, order);
  124. printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n",
  125. var[0], order, eval, sqrt(m.variance[order] / (i + 1)),
  126. m.coeff[order][0], m.coeff[order][1],
  127. m.coeff[order][2]);
  128. }
  129. }
  130. return 0;
  131. }
  132. #endif