| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 1 | PCA *ff_pca_init(int n){ | |
| 40 | PCA *pca; | ||
| 41 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(n<=0) |
| 42 | ✗ | return NULL; | |
| 43 | |||
| 44 | 1 | pca= av_mallocz(sizeof(*pca)); | |
| 45 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!pca) |
| 46 | ✗ | return NULL; | |
| 47 | |||
| 48 | 1 | pca->n= n; | |
| 49 | 1 | pca->z = av_malloc_array(n, sizeof(*pca->z)); | |
| 50 | 1 | pca->count=0; | |
| 51 | 1 | pca->covariance= av_calloc(n*n, sizeof(double)); | |
| 52 | 1 | pca->mean= av_calloc(n, sizeof(double)); | |
| 53 | |||
| 54 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | if (!pca->z || !pca->covariance || !pca->mean) { |
| 55 | ✗ | ff_pca_free(pca); | |
| 56 | ✗ | return NULL; | |
| 57 | } | ||
| 58 | |||
| 59 | 1 | return pca; | |
| 60 | } | ||
| 61 | |||
| 62 | 1 | void ff_pca_free(PCA *pca){ | |
| 63 | 1 | av_freep(&pca->covariance); | |
| 64 | 1 | av_freep(&pca->mean); | |
| 65 | 1 | av_freep(&pca->z); | |
| 66 | 1 | av_free(pca); | |
| 67 | 1 | } | |
| 68 | |||
| 69 | 9000000 | void ff_pca_add(PCA *pca, const double *v){ | |
| 70 | int i, j; | ||
| 71 | 9000000 | const int n= pca->n; | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 72000000 times.
✓ Branch 1 taken 9000000 times.
|
81000000 | for(i=0; i<n; i++){ |
| 74 | 72000000 | pca->mean[i] += v[i]; | |
| 75 |
2/2✓ Branch 0 taken 324000000 times.
✓ Branch 1 taken 72000000 times.
|
396000000 | for(j=i; j<n; j++) |
| 76 | 324000000 | pca->covariance[j + i*n] += v[i]*v[j]; | |
| 77 | } | ||
| 78 | 9000000 | pca->count++; | |
| 79 | 9000000 | } | |
| 80 | |||
| 81 | 1 | int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){ | |
| 82 | int i, j, pass; | ||
| 83 | 1 | int k=0; | |
| 84 | 1 | const int n= pca->n; | |
| 85 | 1 | double *z = pca->z; | |
| 86 | |||
| 87 | 1 | memset(eigenvector, 0, sizeof(double)*n*n); | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for(j=0; j<n; j++){ |
| 90 | 8 | pca->mean[j] /= pca->count; | |
| 91 | 8 | eigenvector[j + j*n] = 1.0; | |
| 92 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 8 times.
|
44 | for(i=0; i<=j; i++){ |
| 93 | 36 | pca->covariance[j + i*n] /= pca->count; | |
| 94 | 36 | pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j]; | |
| 95 | 36 | pca->covariance[i + j*n] = pca->covariance[j + i*n]; | |
| 96 | } | ||
| 97 | 8 | eigenvalue[j]= pca->covariance[j + j*n]; | |
| 98 | 8 | z[j]= 0; | |
| 99 | } | ||
| 100 | |||
| 101 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | for(pass=0; pass < 50; pass++){ |
| 102 | 8 | double sum=0; | |
| 103 | |||
| 104 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 8 times.
|
72 | for(i=0; i<n; i++) |
| 105 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 64 times.
|
288 | for(j=i+1; j<n; j++) |
| 106 | 224 | sum += fabs(pca->covariance[j + i*n]); | |
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if(sum == 0){ |
| 109 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for(i=0; i<n; i++){ |
| 110 | 8 | double maxvalue= -1; | |
| 111 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 8 times.
|
44 | for(j=i; j<n; j++){ |
| 112 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 20 times.
|
36 | if(eigenvalue[j] > maxvalue){ |
| 113 | 16 | maxvalue= eigenvalue[j]; | |
| 114 | 16 | k= j; | |
| 115 | } | ||
| 116 | } | ||
| 117 | 8 | eigenvalue[k]= eigenvalue[i]; | |
| 118 | 8 | eigenvalue[i]= maxvalue; | |
| 119 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 8 times.
|
72 | for(j=0; j<n; j++){ |
| 120 | 64 | double tmp= eigenvector[k + j*n]; | |
| 121 | 64 | eigenvector[k + j*n]= eigenvector[i + j*n]; | |
| 122 | 64 | eigenvector[i + j*n]= tmp; | |
| 123 | } | ||
| 124 | } | ||
| 125 | 1 | return pass; | |
| 126 | } | ||
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 7 times.
|
63 | for(i=0; i<n; i++){ |
| 129 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 56 times.
|
252 | for(j=i+1; j<n; j++){ |
| 130 | 196 | double covar= pca->covariance[j + i*n]; | |
| 131 | double t,c,s,tau,theta, h; | ||
| 132 | |||
| 133 |
4/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 112 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 63 times.
|
196 | if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3 |
| 134 | 21 | continue; | |
| 135 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 149 times.
|
175 | if(fabs(covar) == 0.0) //FIXME should not be needed |
| 136 | 26 | continue; | |
| 137 |
6/6✓ Branch 0 taken 86 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 55 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 1 times.
|
149 | if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){ |
| 138 | 30 | pca->covariance[j + i*n]=0.0; | |
| 139 | 30 | continue; | |
| 140 | } | ||
| 141 | |||
| 142 | 119 | h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]); | |
| 143 | 119 | theta=0.5*h/covar; | |
| 144 | 119 | t=1.0/(fabs(theta)+sqrt(1.0+theta*theta)); | |
| 145 |
2/2✓ Branch 0 taken 59 times.
✓ Branch 1 taken 60 times.
|
119 | if(theta < 0.0) t = -t; |
| 146 | |||
| 147 | 119 | c=1.0/sqrt(1+t*t); | |
| 148 | 119 | s=t*c; | |
| 149 | 119 | tau=s/(1.0+c); | |
| 150 | 119 | z[i] -= t*covar; | |
| 151 | 119 | 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 |
2/2✓ Branch 0 taken 952 times.
✓ Branch 1 taken 119 times.
|
1071 | for(k=0; k<n; k++) { |
| 159 |
4/4✓ Branch 0 taken 833 times.
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 714 times.
✓ Branch 3 taken 119 times.
|
952 | if(k!=i && k!=j){ |
| 160 | 714 | ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j)) | |
| 161 | } | ||
| 162 | 952 | ROTATE(eigenvector,k,i,k,j) | |
| 163 | } | ||
| 164 | 119 | pca->covariance[j + i*n]=0.0; | |
| 165 | } | ||
| 166 | } | ||
| 167 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 7 times.
|
63 | for (i=0; i<n; i++) { |
| 168 | 56 | eigenvalue[i] += z[i]; | |
| 169 | 56 | z[i]=0.0; | |
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | ✗ | return -1; | |
| 174 | } | ||
| 175 |