FFmpeg
Loading...
Searching...
No Matches
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#include "libavutil/pca.c"
23#include "libavutil/lfg.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27
28int main(void){
29 PCA *pca;
30 int i, j, k;
31#define LEN 8
32 double eigenvector[LEN*LEN];
33 double eigenvalue[LEN];
34 AVLFG prng;
35
36 av_lfg_init(&prng, 1);
37
38 pca= ff_pca_init(LEN);
39
40 for(i=0; i<9000000; i++){
41 double v[2*LEN+100];
42// double sum=0;
43 int pos = av_lfg_get(&prng) % LEN;
44 int v2 = av_lfg_get(&prng) % 101 - 50;
45 v[0] = av_lfg_get(&prng) % 101 - 50;
46 for(j=1; j<8; j++){
47 if(j<=pos) v[j]= v[0];
48 else v[j]= v2;
49// sum += v[j];
50 }
51/* for(j=0; j<LEN; j++){
52 v[j] -= v[pos];
53 }*/
54// sum += av_lfg_get(&prng) % 10;
55/* for(j=0; j<LEN; j++){
56 v[j] -= sum/LEN;
57 }*/
58// lbt1(v+100,v+100,LEN);
59 ff_pca_add(pca, v);
60 }
61
62
63 ff_pca(pca, eigenvector, eigenvalue);
64 for(i=0; i<LEN; i++){
65 pca->count= 1;
66 pca->mean[i]= 0;
67
68// (0.5^|x|)^2 = 0.5^2|x| = 0.25^|x|
69
70
71// pca.covariance[i + i*LEN]= pow(0.5, fabs
72 for(j=i; j<LEN; j++){
73 printf("%f ", pca->covariance[i + j*LEN]);
74 }
75 printf("\n");
76 }
77
78 for(i=0; i<LEN; i++){
79 double v[LEN];
80 double error=0;
81 memset(v, 0, sizeof(v));
82 for(j=0; j<LEN; j++){
83 for(k=0; k<LEN; k++){
84 v[j] += pca->covariance[FFMIN(k,j) + FFMAX(k,j)*LEN] * eigenvector[i + k*LEN];
85 }
86 v[j] /= eigenvalue[i];
87 error += fabs(v[j] - eigenvector[i + j*LEN]);
88 }
89 printf("%f ", error);
90 }
91 printf("\n");
92
93 for(i=0; i<LEN; i++){
94 for(j=0; j<LEN; j++){
95 printf("%9.6f ", eigenvector[i + j*LEN]);
96 }
97 printf(" %9.1f %f\n", eigenvalue[i], eigenvalue[i]/eigenvalue[0]);
98 }
99
100 return 0;
101}
#define LEN
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
__device__ int printf(const char *,...)
static __device__ float fabs(float a)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
principal component analysis (PCA)
PCA * ff_pca_init(int n)
Definition pca.c:39
void ff_pca_add(PCA *pca, const double *v)
Definition pca.c:69
int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue)
Definition pca.c:81
unsigned int pos
Definition spdifenc.c:431
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
Definition pca.c:31
double * covariance
Definition pca.c:34
double * mean
Definition pca.c:35
int count
Definition pca.c:32
static void error(const char *err)
static void prng(CheckasmRand *restrict xs, uint8_t *restrict buf, size_t size)
Definition utils.c:154
int main(void)
Definition pca.c:28