pca.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Principal component analysis
  3. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file pca.c
  23. * Principal component analysis
  24. */
  25. #include "common.h"
  26. #include "pca.h"
  27. typedef struct PCA{
  28. int count;
  29. int n;
  30. double *covariance;
  31. double *mean;
  32. }PCA;
  33. PCA *ff_pca_init(int n){
  34. PCA *pca;
  35. if(n<=0)
  36. return NULL;
  37. pca= av_mallocz(sizeof(PCA));
  38. pca->n= n;
  39. pca->count=0;
  40. pca->covariance= av_mallocz(sizeof(double)*n*n);
  41. pca->mean= av_mallocz(sizeof(double)*n);
  42. return pca;
  43. }
  44. void ff_pca_free(PCA *pca){
  45. av_freep(&pca->covariance);
  46. av_freep(&pca->mean);
  47. av_free(pca);
  48. }
  49. void ff_pca_add(PCA *pca, double *v){
  50. int i, j;
  51. const int n= pca->n;
  52. for(i=0; i<n; i++){
  53. pca->mean[i] += v[i];
  54. for(j=i; j<n; j++)
  55. pca->covariance[j + i*n] += v[i]*v[j];
  56. }
  57. pca->count++;
  58. }
  59. int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){
  60. int i, j, k, pass;
  61. const int n= pca->n;
  62. double z[n];
  63. memset(eigenvector, 0, sizeof(double)*n*n);
  64. for(j=0; j<n; j++){
  65. pca->mean[j] /= pca->count;
  66. eigenvector[j + j*n] = 1.0;
  67. for(i=0; i<=j; i++){
  68. pca->covariance[j + i*n] /= pca->count;
  69. pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j];
  70. pca->covariance[i + j*n] = pca->covariance[j + i*n];
  71. }
  72. eigenvalue[j]= pca->covariance[j + j*n];
  73. z[j]= 0;
  74. }
  75. for(pass=0; pass < 50; pass++){
  76. double sum=0;
  77. for(i=0; i<n; i++)
  78. for(j=i+1; j<n; j++)
  79. sum += fabs(pca->covariance[j + i*n]);
  80. if(sum == 0){
  81. for(i=0; i<n; i++){
  82. double maxvalue= -1;
  83. for(j=i; j<n; j++){
  84. if(eigenvalue[j] > maxvalue){
  85. maxvalue= eigenvalue[j];
  86. k= j;
  87. }
  88. }
  89. eigenvalue[k]= eigenvalue[i];
  90. eigenvalue[i]= maxvalue;
  91. for(j=0; j<n; j++){
  92. double tmp= eigenvector[k + j*n];
  93. eigenvector[k + j*n]= eigenvector[i + j*n];
  94. eigenvector[i + j*n]= tmp;
  95. }
  96. }
  97. return pass;
  98. }
  99. for(i=0; i<n; i++){
  100. for(j=i+1; j<n; j++){
  101. double covar= pca->covariance[j + i*n];
  102. double t,c,s,tau,theta, h;
  103. if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3
  104. continue;
  105. if(fabs(covar) == 0.0) //FIXME shouldnt be needed
  106. continue;
  107. if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
  108. pca->covariance[j + i*n]=0.0;
  109. continue;
  110. }
  111. h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
  112. theta=0.5*h/covar;
  113. t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
  114. if(theta < 0.0) t = -t;
  115. c=1.0/sqrt(1+t*t);
  116. s=t*c;
  117. tau=s/(1.0+c);
  118. z[i] -= t*covar;
  119. z[j] += t*covar;
  120. #define ROTATE(a,i,j,k,l) {\
  121. double g=a[j + i*n];\
  122. double h=a[l + k*n];\
  123. a[j + i*n]=g-s*(h+g*tau);\
  124. a[l + k*n]=h+s*(g-h*tau); }
  125. for(k=0; k<n; k++) {
  126. if(k!=i && k!=j){
  127. ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j))
  128. }
  129. ROTATE(eigenvector,k,i,k,j)
  130. }
  131. pca->covariance[j + i*n]=0.0;
  132. }
  133. }
  134. for (i=0; i<n; i++) {
  135. eigenvalue[i] += z[i];
  136. z[i]=0.0;
  137. }
  138. }
  139. return -1;
  140. }
  141. #ifdef TEST
  142. #undef printf
  143. #undef random
  144. #include <stdio.h>
  145. #include <stdlib.h>
  146. int main(){
  147. PCA *pca;
  148. int i, j, k;
  149. #define LEN 8
  150. double eigenvector[LEN*LEN];
  151. double eigenvalue[LEN];
  152. pca= ff_pca_init(LEN);
  153. for(i=0; i<9000000; i++){
  154. double v[2*LEN+100];
  155. double sum=0;
  156. int pos= random()%LEN;
  157. int v2= (random()%101) - 50;
  158. v[0]= (random()%101) - 50;
  159. for(j=1; j<8; j++){
  160. if(j<=pos) v[j]= v[0];
  161. else v[j]= v2;
  162. sum += v[j];
  163. }
  164. /* for(j=0; j<LEN; j++){
  165. v[j] -= v[pos];
  166. }*/
  167. // sum += random()%10;
  168. /* for(j=0; j<LEN; j++){
  169. v[j] -= sum/LEN;
  170. }*/
  171. // lbt1(v+100,v+100,LEN);
  172. ff_pca_add(pca, v);
  173. }
  174. ff_pca(pca, eigenvector, eigenvalue);
  175. for(i=0; i<LEN; i++){
  176. pca->count= 1;
  177. pca->mean[i]= 0;
  178. // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x|
  179. // pca.covariance[i + i*LEN]= pow(0.5, fabs
  180. for(j=i; j<LEN; j++){
  181. printf("%f ", pca->covariance[i + j*LEN]);
  182. }
  183. printf("\n");
  184. }
  185. #if 1
  186. for(i=0; i<LEN; i++){
  187. double v[LEN];
  188. double error=0;
  189. memset(v, 0, sizeof(v));
  190. for(j=0; j<LEN; j++){
  191. for(k=0; k<LEN; k++){
  192. v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN];
  193. }
  194. v[j] /= eigenvalue[i];
  195. error += fabs(v[j] - eigenvector[i + j*LEN]);
  196. }
  197. printf("%f ", error);
  198. }
  199. printf("\n");
  200. #endif
  201. for(i=0; i<LEN; i++){
  202. for(j=0; j<LEN; j++){
  203. printf("%9.6f ", eigenvector[i + j*LEN]);
  204. }
  205. printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
  206. }
  207. return 0;
  208. }
  209. #endif