Statistics
| Branch: | Revision:

ffmpeg / libavutil / pca.c @ f43ad0fe

History | View | Annotate | Download (6.13 KB)

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