FFmpeg
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 "mem.h"
29 #include "pca.h"
30 
31 typedef struct PCA{
32  int count;
33  int n;
34  double *covariance;
35  double *mean;
36  double *z;
37 }PCA;
38 
39 PCA *ff_pca_init(int n){
40  PCA *pca;
41  if(n<=0)
42  return NULL;
43 
44  pca= av_mallocz(sizeof(*pca));
45  if (!pca)
46  return NULL;
47 
48  pca->n= n;
49  pca->z = av_malloc_array(n, sizeof(*pca->z));
50  pca->count=0;
51  pca->covariance= av_calloc(n*n, sizeof(double));
52  pca->mean= av_calloc(n, sizeof(double));
53 
54  if (!pca->z || !pca->covariance || !pca->mean) {
55  ff_pca_free(pca);
56  return NULL;
57  }
58 
59  return pca;
60 }
61 
62 void ff_pca_free(PCA *pca){
63  av_freep(&pca->covariance);
64  av_freep(&pca->mean);
65  av_freep(&pca->z);
66  av_free(pca);
67 }
68 
69 void ff_pca_add(PCA *pca, const double *v){
70  int i, j;
71  const int n= pca->n;
72 
73  for(i=0; i<n; i++){
74  pca->mean[i] += v[i];
75  for(j=i; j<n; j++)
76  pca->covariance[j + i*n] += v[i]*v[j];
77  }
78  pca->count++;
79 }
80 
81 int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){
82  int i, j, pass;
83  int k=0;
84  const int n= pca->n;
85  double *z = pca->z;
86 
87  memset(eigenvector, 0, sizeof(double)*n*n);
88 
89  for(j=0; j<n; j++){
90  pca->mean[j] /= pca->count;
91  eigenvector[j + j*n] = 1.0;
92  for(i=0; i<=j; i++){
93  pca->covariance[j + i*n] /= pca->count;
94  pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j];
95  pca->covariance[i + j*n] = pca->covariance[j + i*n];
96  }
97  eigenvalue[j]= pca->covariance[j + j*n];
98  z[j]= 0;
99  }
100 
101  for(pass=0; pass < 50; pass++){
102  double sum=0;
103 
104  for(i=0; i<n; i++)
105  for(j=i+1; j<n; j++)
106  sum += fabs(pca->covariance[j + i*n]);
107 
108  if(sum == 0){
109  for(i=0; i<n; i++){
110  double maxvalue= -1;
111  for(j=i; j<n; j++){
112  if(eigenvalue[j] > maxvalue){
113  maxvalue= eigenvalue[j];
114  k= j;
115  }
116  }
117  eigenvalue[k]= eigenvalue[i];
118  eigenvalue[i]= maxvalue;
119  for(j=0; j<n; j++){
120  double tmp= eigenvector[k + j*n];
121  eigenvector[k + j*n]= eigenvector[i + j*n];
122  eigenvector[i + j*n]= tmp;
123  }
124  }
125  return pass;
126  }
127 
128  for(i=0; i<n; i++){
129  for(j=i+1; j<n; j++){
130  double covar= pca->covariance[j + i*n];
131  double t,c,s,tau,theta, h;
132 
133  if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3
134  continue;
135  if(fabs(covar) == 0.0) //FIXME should not be needed
136  continue;
137  if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){
138  pca->covariance[j + i*n]=0.0;
139  continue;
140  }
141 
142  h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]);
143  theta=0.5*h/covar;
144  t=1.0/(fabs(theta)+sqrt(1.0+theta*theta));
145  if(theta < 0.0) t = -t;
146 
147  c=1.0/sqrt(1+t*t);
148  s=t*c;
149  tau=s/(1.0+c);
150  z[i] -= t*covar;
151  z[j] += t*covar;
152 
153 #define ROTATE(a,i,j,k,l) {\
154  double g=a[j + i*n];\
155  double h=a[l + k*n];\
156  a[j + i*n]=g-s*(h+g*tau);\
157  a[l + k*n]=h+s*(g-h*tau); }
158  for(k=0; k<n; k++) {
159  if(k!=i && k!=j){
160  ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j))
161  }
162  ROTATE(eigenvector,k,i,k,j)
163  }
164  pca->covariance[j + i*n]=0.0;
165  }
166  }
167  for (i=0; i<n; i++) {
168  eigenvalue[i] += z[i];
169  z[i]=0.0;
170  }
171  }
172 
173  return -1;
174 }
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
PCA::z
double * z
Definition: pca.c:36
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
PCA::n
int n
Definition: pca.c:33
PCA::count
int count
Definition: pca.c:32
ff_pca_init
PCA * ff_pca_init(int n)
Definition: pca.c:39
s
#define s(width, name)
Definition: cbs_vp9.c:198
PCA
Definition: pca.c:31
ROTATE
#define ROTATE(a, i, j, k, l)
ff_pca_free
void ff_pca_free(PCA *pca)
Definition: pca.c:62
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
pca.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
common.h
PCA::mean
double * mean
Definition: pca.c:35
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
mem.h
PCA::covariance
double * covariance
Definition: pca.c:34
ff_pca
int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue)
Definition: pca.c:81
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
h
h
Definition: vp9dsp_template.c:2038
ff_pca_add
void ff_pca_add(PCA *pca, const double *v)
Definition: pca.c:69