Mercurial > libavutil.hg
annotate pca.c @ 787:664b42a6cf75 libavutil
Add PIX_FMT_VDPAU_MPEG4 to pixdesc.c.
author | cehoyos |
---|---|
date | Wed, 11 Nov 2009 00:02:36 +0000 |
parents | 5d344280a1f8 |
children | 0795a743bda1 |
rev | line source |
---|---|
550 | 1 /* |
633 | 2 * principal component analysis (PCA) |
550 | 3 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> |
4 * | |
563
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
5 * This file is part of FFmpeg. |
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
6 * |
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
550 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
563
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
550 | 11 * |
563
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
550 | 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 | |
563
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
daccf2fe1053
Copy and paste LGPL from tree.h, the previous one referred to a non-existing
michael
parents:
557
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
550 | 20 */ |
21 | |
22 /** | |
642
70bdd5501662
Use full internal pathname in doxygen @file directives.
diego
parents:
633
diff
changeset
|
23 * @file libavutil/pca.c |
633 | 24 * principal component analysis (PCA) |
550 | 25 */ |
26 | |
551 | 27 #include "common.h" |
550 | 28 #include "pca.h" |
29 | |
557 | 30 typedef struct PCA{ |
31 int count; | |
32 int n; | |
33 double *covariance; | |
34 double *mean; | |
35 }PCA; | |
36 | |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
37 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
|
38 PCA *pca; |
550 | 39 if(n<=0) |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
40 return NULL; |
550 | 41 |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
42 pca= av_mallocz(sizeof(PCA)); |
550 | 43 pca->n= n; |
44 pca->count=0; | |
45 pca->covariance= av_mallocz(sizeof(double)*n*n); | |
46 pca->mean= av_mallocz(sizeof(double)*n); | |
47 | |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
48 return pca; |
550 | 49 } |
50 | |
51 void ff_pca_free(PCA *pca){ | |
52 av_freep(&pca->covariance); | |
53 av_freep(&pca->mean); | |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
54 av_free(pca); |
550 | 55 } |
56 | |
57 void ff_pca_add(PCA *pca, double *v){ | |
58 int i, j; | |
59 const int n= pca->n; | |
60 | |
61 for(i=0; i<n; i++){ | |
62 pca->mean[i] += v[i]; | |
63 for(j=i; j<n; j++) | |
64 pca->covariance[j + i*n] += v[i]*v[j]; | |
65 } | |
66 pca->count++; | |
67 } | |
68 | |
69 int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){ | |
585 | 70 int i, j, pass; |
71 int k=0; | |
550 | 72 const int n= pca->n; |
73 double z[n]; | |
74 | |
75 memset(eigenvector, 0, sizeof(double)*n*n); | |
76 | |
77 for(j=0; j<n; j++){ | |
78 pca->mean[j] /= pca->count; | |
79 eigenvector[j + j*n] = 1.0; | |
80 for(i=0; i<=j; i++){ | |
81 pca->covariance[j + i*n] /= pca->count; | |
82 pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j]; | |
83 pca->covariance[i + j*n] = pca->covariance[j + i*n]; | |
84 } | |
85 eigenvalue[j]= pca->covariance[j + j*n]; | |
86 z[j]= 0; | |
87 } | |
88 | |
89 for(pass=0; pass < 50; pass++){ | |
90 double sum=0; | |
91 | |
92 for(i=0; i<n; i++) | |
93 for(j=i+1; j<n; j++) | |
94 sum += fabs(pca->covariance[j + i*n]); | |
95 | |
96 if(sum == 0){ | |
97 for(i=0; i<n; i++){ | |
98 double maxvalue= -1; | |
99 for(j=i; j<n; j++){ | |
100 if(eigenvalue[j] > maxvalue){ | |
101 maxvalue= eigenvalue[j]; | |
102 k= j; | |
103 } | |
104 } | |
105 eigenvalue[k]= eigenvalue[i]; | |
106 eigenvalue[i]= maxvalue; | |
107 for(j=0; j<n; j++){ | |
108 double tmp= eigenvector[k + j*n]; | |
109 eigenvector[k + j*n]= eigenvector[i + j*n]; | |
110 eigenvector[i + j*n]= tmp; | |
111 } | |
112 } | |
113 return pass; | |
114 } | |
115 | |
116 for(i=0; i<n; i++){ | |
117 for(j=i+1; j<n; j++){ | |
118 double covar= pca->covariance[j + i*n]; | |
119 double t,c,s,tau,theta, h; | |
120 | |
121 if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3 | |
122 continue; | |
633 | 123 if(fabs(covar) == 0.0) //FIXME should not be needed |
550 | 124 continue; |
125 if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){ | |
126 pca->covariance[j + i*n]=0.0; | |
127 continue; | |
128 } | |
129 | |
130 h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]); | |
131 theta=0.5*h/covar; | |
132 t=1.0/(fabs(theta)+sqrt(1.0+theta*theta)); | |
133 if(theta < 0.0) t = -t; | |
134 | |
135 c=1.0/sqrt(1+t*t); | |
136 s=t*c; | |
137 tau=s/(1.0+c); | |
138 z[i] -= t*covar; | |
139 z[j] += t*covar; | |
140 | |
554
29d015080b3d
Do not mix declarations and statements (by ramiro).
michael
parents:
553
diff
changeset
|
141 #define ROTATE(a,i,j,k,l) {\ |
550 | 142 double g=a[j + i*n];\ |
143 double h=a[l + k*n];\ | |
144 a[j + i*n]=g-s*(h+g*tau);\ | |
554
29d015080b3d
Do not mix declarations and statements (by ramiro).
michael
parents:
553
diff
changeset
|
145 a[l + k*n]=h+s*(g-h*tau); } |
550 | 146 for(k=0; k<n; k++) { |
147 if(k!=i && k!=j){ | |
148 ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j)) | |
149 } | |
150 ROTATE(eigenvector,k,i,k,j) | |
151 } | |
152 pca->covariance[j + i*n]=0.0; | |
153 } | |
154 } | |
155 for (i=0; i<n; i++) { | |
156 eigenvalue[i] += z[i]; | |
157 z[i]=0.0; | |
158 } | |
159 } | |
160 | |
161 return -1; | |
162 } | |
163 | |
553 | 164 #ifdef TEST |
550 | 165 |
166 #undef printf | |
167 #include <stdio.h> | |
168 #include <stdlib.h> | |
701
ae6e96434bec
Replace random() usage in test programs by av_lfg_*().
diego
parents:
642
diff
changeset
|
169 #include "lfg.h" |
550 | 170 |
596
11efcc64c2f6
Add missing 'void' keyword to parameterless function declarations.
diego
parents:
585
diff
changeset
|
171 int main(void){ |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
172 PCA *pca; |
550 | 173 int i, j, k; |
174 #define LEN 8 | |
175 double eigenvector[LEN*LEN]; | |
176 double eigenvalue[LEN]; | |
726
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
177 AVLFG prng; |
701
ae6e96434bec
Replace random() usage in test programs by av_lfg_*().
diego
parents:
642
diff
changeset
|
178 |
726
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
179 av_lfg_init(&prng, 1); |
550 | 180 |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
181 pca= ff_pca_init(LEN); |
550 | 182 |
183 for(i=0; i<9000000; i++){ | |
184 double v[2*LEN+100]; | |
185 double sum=0; | |
726
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
186 int pos = av_lfg_get(&prng) % LEN; |
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
187 int v2 = av_lfg_get(&prng) % 101 - 50; |
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
188 v[0] = av_lfg_get(&prng) % 101 - 50; |
550 | 189 for(j=1; j<8; j++){ |
190 if(j<=pos) v[j]= v[0]; | |
191 else v[j]= v2; | |
192 sum += v[j]; | |
193 } | |
194 /* for(j=0; j<LEN; j++){ | |
195 v[j] -= v[pos]; | |
196 }*/ | |
726
5d344280a1f8
cosmetics: Rename prn variable to prng (Pseudo Random Number Generator).
diego
parents:
701
diff
changeset
|
197 // sum += av_lfg_get(&prng) % 10; |
550 | 198 /* for(j=0; j<LEN; j++){ |
199 v[j] -= sum/LEN; | |
200 }*/ | |
201 // 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
|
202 ff_pca_add(pca, v); |
550 | 203 } |
204 | |
205 | |
555
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
206 ff_pca(pca, eigenvector, eigenvalue); |
550 | 207 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
|
208 pca->count= 1; |
2fb78abcf725
Make ff_pca_init() allocate it struct instead of letting the user provide
michael
parents:
554
diff
changeset
|
209 pca->mean[i]= 0; |
550 | 210 |
211 // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x| | |
212 | |
213 | |
214 // pca.covariance[i + i*LEN]= pow(0.5, fabs | |
215 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
|
216 printf("%f ", pca->covariance[i + j*LEN]); |
550 | 217 } |
218 printf("\n"); | |
219 } | |
220 | |
221 #if 1 | |
222 for(i=0; i<LEN; i++){ | |
223 double v[LEN]; | |
224 double error=0; | |
225 memset(v, 0, sizeof(v)); | |
226 for(j=0; j<LEN; j++){ | |
227 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
|
228 v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN]; |
550 | 229 } |
230 v[j] /= eigenvalue[i]; | |
231 error += fabs(v[j] - eigenvector[i + j*LEN]); | |
232 } | |
233 printf("%f ", error); | |
234 } | |
235 printf("\n"); | |
236 #endif | |
237 for(i=0; i<LEN; i++){ | |
238 for(j=0; j<LEN; j++){ | |
239 printf("%9.6f ", eigenvector[i + j*LEN]); | |
240 } | |
241 printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]); | |
242 } | |
243 | |
244 return 0; | |
245 } | |
246 #endif |