Browse code

Principal component analysis (will be cleaned up in next commits)

Originally committed as revision 14802 to svn://svn.ffmpeg.org/ffmpeg/trunk

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