lls.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * linear least squares model
  3. *
  4. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "version.h"
  30. #include "lls.h"
  31. static void update_lls(LLSModel *m, double *var)
  32. {
  33. int i, j;
  34. for (i = 0; i <= m->indep_count; i++) {
  35. for (j = i; j <= m->indep_count; j++) {
  36. m->covariance[i][j] += var[i] * var[j];
  37. }
  38. }
  39. }
  40. void avpriv_solve_lls(LLSModel *m, double threshold, unsigned short min_order)
  41. {
  42. int i, j, k;
  43. double (*factor)[MAX_VARS_ALIGN] = (void *) &m->covariance[1][0];
  44. double (*covar) [MAX_VARS_ALIGN] = (void *) &m->covariance[1][1];
  45. double *covar_y = m->covariance[0];
  46. int count = m->indep_count;
  47. for (i = 0; i < count; i++) {
  48. for (j = i; j < count; j++) {
  49. double sum = covar[i][j];
  50. for (k = i - 1; k >= 0; k--)
  51. sum -= factor[i][k] * factor[j][k];
  52. if (i == j) {
  53. if (sum < threshold)
  54. sum = 1.0;
  55. factor[i][i] = sqrt(sum);
  56. } else {
  57. factor[j][i] = sum / factor[i][i];
  58. }
  59. }
  60. }
  61. for (i = 0; i < count; i++) {
  62. double sum = covar_y[i + 1];
  63. for (k = i - 1; k >= 0; k--)
  64. sum -= factor[i][k] * m->coeff[0][k];
  65. m->coeff[0][i] = sum / factor[i][i];
  66. }
  67. for (j = count - 1; j >= min_order; j--) {
  68. for (i = j; i >= 0; i--) {
  69. double sum = m->coeff[0][i];
  70. for (k = i + 1; k <= j; k++)
  71. sum -= factor[k][i] * m->coeff[j][k];
  72. m->coeff[j][i] = sum / factor[i][i];
  73. }
  74. m->variance[j] = covar_y[0];
  75. for (i = 0; i <= j; i++) {
  76. double sum = m->coeff[j][i] * covar[i][i] - 2 * covar_y[i + 1];
  77. for (k = 0; k < i; k++)
  78. sum += 2 * m->coeff[j][k] * covar[k][i];
  79. m->variance[j] += m->coeff[j][i] * sum;
  80. }
  81. }
  82. }
  83. static double evaluate_lls(LLSModel *m, double *param, int order)
  84. {
  85. int i;
  86. double out = 0;
  87. for (i = 0; i <= order; i++)
  88. out += param[i] * m->coeff[order][i];
  89. return out;
  90. }
  91. av_cold void avpriv_init_lls(LLSModel *m, int indep_count)
  92. {
  93. memset(m, 0, sizeof(LLSModel));
  94. m->indep_count = indep_count;
  95. m->update_lls = update_lls;
  96. m->evaluate_lls = evaluate_lls;
  97. if (ARCH_X86)
  98. ff_init_lls_x86(m);
  99. }
  100. #ifdef TEST
  101. #include <stdio.h>
  102. #include <limits.h>
  103. #include "lfg.h"
  104. int main(void)
  105. {
  106. LLSModel m;
  107. int i, order;
  108. AVLFG lfg;
  109. av_lfg_init(&lfg, 1);
  110. avpriv_init_lls(&m, 3);
  111. for (i = 0; i < 100; i++) {
  112. LOCAL_ALIGNED(32, double, var, [4]);
  113. double eval;
  114. var[0] = (av_lfg_get(&lfg) / (double) UINT_MAX - 0.5) * 2;
  115. var[1] = var[0] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  116. var[2] = var[1] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  117. var[3] = var[2] + av_lfg_get(&lfg) / (double) UINT_MAX - 0.5;
  118. m.update_lls(&m, var);
  119. avpriv_solve_lls(&m, 0.001, 0);
  120. for (order = 0; order < 3; order++) {
  121. eval = m.evaluate_lls(&m, var + 1, order);
  122. printf("real:%9f order:%d pred:%9f var:%f coeffs:%f %9f %9f\n",
  123. var[0], order, eval, sqrt(m.variance[order] / (i + 1)),
  124. m.coeff[order][0], m.coeff[order][1],
  125. m.coeff[order][2]);
  126. }
  127. }
  128. return 0;
  129. }
  130. #endif