FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pca.c
Go to the documentation of this file.
1 /*
2  * principal component analysis (PCA)
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 /**
23  * @file
24  * principal component analysis (PCA)
25  */
26 
27 #include "common.h"
28 #include "pca.h"
29 
30 typedef struct PCA{
31  int count;
32  int n;
33  double *covariance;
34  double *mean;
35  double *z;
36 }PCA;
37 
39  PCA *pca;
40  if(n<=0)
41  return NULL;
42 
43  pca= av_mallocz(sizeof(*pca));
44  if (!pca)
45  return NULL;
46 
47  pca->n= n;
48  pca->z = av_malloc_array(n, sizeof(*pca->z));
49  pca->count=0;
50  pca->covariance= av_calloc(n*n, sizeof(double));
51  pca->mean= av_calloc(n, sizeof(double));
52 
53  if (!pca->z || !pca->covariance || !pca->mean) {
54  ff_pca_free(pca);
55  return NULL;
56  }
57 
58  return pca;
59 }
60 
61 void ff_pca_free(PCA *pca){
62  av_freep(&pca->covariance);
63  av_freep(&pca->mean);
64  av_freep(&pca->z);
65  av_free(pca);
66 }
67 
68 void ff_pca_add(PCA *pca, const double *v){
69  int i, j;
70  const int n= pca->n;
71 
72  for(i=0; i<n; i++){
73  pca->mean[i] += v[i];
74  for(j=i; j<n; j++)
75  pca->covariance[j + i*n] += v[i]*v[j];
76  }
77  pca->count++;
78 }
79 
80 int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){
81  int i, j, pass;
82  int k=0;
83  const int n= pca->n;
84  double *z = pca->z;
85 
86  memset(eigenvector, 0, sizeof(double)*n*n);
87 
88  for(j=0; j<n; j++){
89  pca->mean[j] /= pca->count;
90  eigenvector[j + j*n] = 1.0;
91  for(i=0; i<=j; i++){
92  pca->covariance[j + i*n] /= pca->count;
93  pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j];
94  pca->covariance[i + j*n] = pca->covariance[j + i*n];
95  }
96  eigenvalue[j]= pca->covariance[j + j*n];
97  z[j]= 0;
98  }
99 
100  for(pass=0; pass < 50; pass++){
101  double sum=0;
102 
103  for(i=0; i<n; i++)
104  for(j=i+1; j<n; j++)
105  sum += fabs(pca->covariance[j + i*n]);
106 
107  if(sum == 0){
108  for(i=0; i<n; i++){
109  double maxvalue= -1;
110  for(j=i; j<n; j++){
111  if(eigenvalue[j] > maxvalue){
112  maxvalue= eigenvalue[j];
113  k= j;
114  }
115  }
116  eigenvalue[k]= eigenvalue[i];
117  eigenvalue[i]= maxvalue;
118  for(j=0; j<n; j++){
119  double tmp= eigenvector[k + j*n];
120  eigenvector[k + j*n]= eigenvector[i + j*n];
121  eigenvector[i + j*n]= tmp;
122  }
123  }
124  return pass;
125  }
126 
127  for(i=0; i<n; i++){
128  for(j=i+1; j<n; j++){
129  double covar= pca->covariance[j + i*n];
130  double t,c,s,tau,theta, h;
131 
132  if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3
133  continue;
134  if(fabs(covar) == 0.0) //FIXME should not be needed
135  continue;
136  if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
137  pca->covariance[j + i*n]=0.0;
138  continue;
139  }
140 
141  h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
142  theta=0.5*h/covar;
143  t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
144  if(theta < 0.0) t = -t;
145 
146  c=1.0/sqrt(1+t*t);
147  s=t*c;
148  tau=s/(1.0+c);
149  z[i] -= t*covar;
150  z[j] += t*covar;
151 
152 #define ROTATE(a,i,j,k,l) {\
153  double g=a[j + i*n];\
154  double h=a[l + k*n];\
155  a[j + i*n]=g-s*(h+g*tau);\
156  a[l + k*n]=h+s*(g-h*tau); }
157  for(k=0; k<n; k++) {
158  if(k!=i && k!=j){
159  ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j))
160  }
161  ROTATE(eigenvector,k,i,k,j)
162  }
163  pca->covariance[j + i*n]=0.0;
164  }
165  }
166  for (i=0; i<n; i++) {
167  eigenvalue[i] += z[i];
168  z[i]=0.0;
169  }
170  }
171 
172  return -1;
173 }
174 
175 #ifdef TEST
176 
177 #undef printf
178 #include <stdio.h>
179 #include <stdlib.h>
180 #include "lfg.h"
181 
182 int main(void){
183  PCA *pca;
184  int i, j, k;
185 #define LEN 8
186  double eigenvector[LEN*LEN];
187  double eigenvalue[LEN];
188  AVLFG prng;
189 
190  av_lfg_init(&prng, 1);
191 
192  pca= ff_pca_init(LEN);
193 
194  for(i=0; i<9000000; i++){
195  double v[2*LEN+100];
196  double sum=0;
197  int pos = av_lfg_get(&prng) % LEN;
198  int v2 = av_lfg_get(&prng) % 101 - 50;
199  v[0] = av_lfg_get(&prng) % 101 - 50;
200  for(j=1; j<8; j++){
201  if(j<=pos) v[j]= v[0];
202  else v[j]= v2;
203  sum += v[j];
204  }
205 /* for(j=0; j<LEN; j++){
206  v[j] -= v[pos];
207  }*/
208 // sum += av_lfg_get(&prng) % 10;
209 /* for(j=0; j<LEN; j++){
210  v[j] -= sum/LEN;
211  }*/
212 // lbt1(v+100,v+100,LEN);
213  ff_pca_add(pca, v);
214  }
215 
216 
217  ff_pca(pca, eigenvector, eigenvalue);
218  for(i=0; i<LEN; i++){
219  pca->count= 1;
220  pca->mean[i]= 0;
221 
222 // (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x|
223 
224 
225 // pca.covariance[i + i*LEN]= pow(0.5, fabs
226  for(j=i; j<LEN; j++){
227  printf("%f ", pca->covariance[i + j*LEN]);
228  }
229  printf("\n");
230  }
231 
232  for(i=0; i<LEN; i++){
233  double v[LEN];
234  double error=0;
235  memset(v, 0, sizeof(v));
236  for(j=0; j<LEN; j++){
237  for(k=0; k<LEN; k++){
238  v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN];
239  }
240  v[j] /= eigenvalue[i];
241  error += fabs(v[j] - eigenvector[i + j*LEN]);
242  }
243  printf("%f ", error);
244  }
245  printf("\n");
246 
247  for(i=0; i<LEN; i++){
248  for(j=0; j<LEN; j++){
249  printf("%9.6f ", eigenvector[i + j*LEN]);
250  }
251  printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
252  }
253 
254  return 0;
255 }
256 #endif
Definition: lfg.h:25
Definition: pca.c:30
#define NULL
Definition: coverity.c:32
float v
const char * s
Definition: avisynth_c.h:631
int count
Definition: pca.c:31
PCA * ff_pca_init(int n)
Definition: pca.c:38
double * z
Definition: pca.c:35
int n
Definition: pca.c:32
#define FFMAX(a, b)
Definition: common.h:64
#define pass
Definition: fft_template.c:509
#define ROTATE(a, i, j, k, l)
#define LEN
#define FFMIN(a, b)
Definition: common.h:66
int n
Definition: avisynth_c.h:547
int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue)
Definition: pca.c:80
void ff_pca_free(PCA *pca)
Definition: pca.c:61
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
void ff_pca_add(PCA *pca, const double *v)
Definition: pca.c:68
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
void * av_calloc(size_t nmemb, size_t size)
Allocate a block of nmemb * size bytes with alignment suitable for all memory accesses (including vec...
Definition: mem.c:258
principal component analysis (PCA)
double * mean
Definition: pca.c:34
common internal and external API header
static double c[64]
#define av_free(p)
#define av_freep(p)
#define av_malloc_array(a, b)
int main(int argc, char **argv)
Definition: main.c:22
double * covariance
Definition: pca.c:33
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250