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