Mercurial > libavutil.hg
comparison 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 |
comparison
equal
deleted
inserted
replaced
554:29d015080b3d | 555:2fb78abcf725 |
---|---|
24 */ | 24 */ |
25 | 25 |
26 #include "common.h" | 26 #include "common.h" |
27 #include "pca.h" | 27 #include "pca.h" |
28 | 28 |
29 int ff_pca_init(PCA *pca, int n){ | 29 PCA *ff_pca_init(int n){ |
30 PCA *pca; | |
30 if(n<=0) | 31 if(n<=0) |
31 return -1; | 32 return NULL; |
32 | 33 |
34 pca= av_mallocz(sizeof(PCA)); | |
33 pca->n= n; | 35 pca->n= n; |
34 pca->count=0; | 36 pca->count=0; |
35 pca->covariance= av_mallocz(sizeof(double)*n*n); | 37 pca->covariance= av_mallocz(sizeof(double)*n*n); |
36 pca->mean= av_mallocz(sizeof(double)*n); | 38 pca->mean= av_mallocz(sizeof(double)*n); |
37 | 39 |
38 return 0; | 40 return pca; |
39 } | 41 } |
40 | 42 |
41 void ff_pca_free(PCA *pca){ | 43 void ff_pca_free(PCA *pca){ |
42 av_freep(&pca->covariance); | 44 av_freep(&pca->covariance); |
43 av_freep(&pca->mean); | 45 av_freep(&pca->mean); |
46 av_free(pca); | |
44 } | 47 } |
45 | 48 |
46 void ff_pca_add(PCA *pca, double *v){ | 49 void ff_pca_add(PCA *pca, double *v){ |
47 int i, j; | 50 int i, j; |
48 const int n= pca->n; | 51 const int n= pca->n; |
155 #undef random | 158 #undef random |
156 #include <stdio.h> | 159 #include <stdio.h> |
157 #include <stdlib.h> | 160 #include <stdlib.h> |
158 | 161 |
159 int main(){ | 162 int main(){ |
160 PCA pca; | 163 PCA *pca; |
161 int i, j, k; | 164 int i, j, k; |
162 #define LEN 8 | 165 #define LEN 8 |
163 double eigenvector[LEN*LEN]; | 166 double eigenvector[LEN*LEN]; |
164 double eigenvalue[LEN]; | 167 double eigenvalue[LEN]; |
165 | 168 |
166 ff_pca_init(&pca, LEN); | 169 pca= ff_pca_init(LEN); |
167 | 170 |
168 for(i=0; i<9000000; i++){ | 171 for(i=0; i<9000000; i++){ |
169 double v[2*LEN+100]; | 172 double v[2*LEN+100]; |
170 double sum=0; | 173 double sum=0; |
171 int pos= random()%LEN; | 174 int pos= random()%LEN; |
182 // sum += random()%10; | 185 // sum += random()%10; |
183 /* for(j=0; j<LEN; j++){ | 186 /* for(j=0; j<LEN; j++){ |
184 v[j] -= sum/LEN; | 187 v[j] -= sum/LEN; |
185 }*/ | 188 }*/ |
186 // lbt1(v+100,v+100,LEN); | 189 // lbt1(v+100,v+100,LEN); |
187 ff_pca_add(&pca, v); | 190 ff_pca_add(pca, v); |
188 } | 191 } |
189 | 192 |
190 | 193 |
191 ff_pca(&pca, eigenvector, eigenvalue); | 194 ff_pca(pca, eigenvector, eigenvalue); |
192 for(i=0; i<LEN; i++){ | 195 for(i=0; i<LEN; i++){ |
193 pca.count= 1; | 196 pca->count= 1; |
194 pca.mean[i]= 0; | 197 pca->mean[i]= 0; |
195 | 198 |
196 // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x| | 199 // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x| |
197 | 200 |
198 | 201 |
199 // pca.covariance[i + i*LEN]= pow(0.5, fabs | 202 // pca.covariance[i + i*LEN]= pow(0.5, fabs |
200 for(j=i; j<LEN; j++){ | 203 for(j=i; j<LEN; j++){ |
201 printf("%f ", pca.covariance[i + j*LEN]); | 204 printf("%f ", pca->covariance[i + j*LEN]); |
202 } | 205 } |
203 printf("\n"); | 206 printf("\n"); |
204 } | 207 } |
205 | 208 |
206 #if 1 | 209 #if 1 |
208 double v[LEN]; | 211 double v[LEN]; |
209 double error=0; | 212 double error=0; |
210 memset(v, 0, sizeof(v)); | 213 memset(v, 0, sizeof(v)); |
211 for(j=0; j<LEN; j++){ | 214 for(j=0; j<LEN; j++){ |
212 for(k=0; k<LEN; k++){ | 215 for(k=0; k<LEN; k++){ |
213 v[j] += pca.covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN]; | 216 v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN]; |
214 } | 217 } |
215 v[j] /= eigenvalue[i]; | 218 v[j] /= eigenvalue[i]; |
216 error += fabs(v[j] - eigenvector[i + j*LEN]); | 219 error += fabs(v[j] - eigenvector[i + j*LEN]); |
217 } | 220 } |
218 printf("%f ", error); | 221 printf("%f ", error); |