Split global .gitignore file into per-directory files
[libav.git] / libavdevice / tests / timefilter.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav 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.1 of the License, or (at your option) any later version.
8 *
9 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdio.h>
20
21 #include "libavutil/common.h"
22 #include "libavutil/lfg.h"
23
24 #include "libavdevice/timefilter.h"
25
26 #define LFG_MAX ((1LL << 32) - 1)
27
28 int main(void)
29 {
30 AVLFG prng;
31 double n0, n1;
32 #define SAMPLES 1000
33 double ideal[SAMPLES];
34 double samples[SAMPLES];
35 for (n0 = 0; n0 < 40; n0 = 2 * n0 + 1) {
36 for (n1 = 0; n1 < 10; n1 = 2 * n1 + 1) {
37 double best_error = 1000000000;
38 double bestpar0 = 1;
39 double bestpar1 = 0.001;
40 int better, i;
41
42 av_lfg_init(&prng, 123);
43 for (i = 0; i < SAMPLES; i++) {
44 ideal[i] = 10 + i + n1 * i / (1000);
45 samples[i] = ideal[i] + n0 * (av_lfg_get(&prng) - LFG_MAX / 2) / (LFG_MAX * 10LL);
46 }
47
48 do {
49 double par0, par1;
50 better = 0;
51 for (par0 = bestpar0 * 0.8; par0 <= bestpar0 * 1.21; par0 += bestpar0 * 0.05) {
52 for (par1 = bestpar1 * 0.8; par1 <= bestpar1 * 1.21; par1 += bestpar1 * 0.05) {
53 double error = 0;
54 TimeFilter *tf = ff_timefilter_new(1, par0, par1);
55 if (!tf) {
56 printf("Could not allocate memory for timefilter.\n");
57 exit(1);
58 }
59 for (i = 0; i < SAMPLES; i++) {
60 double filtered;
61 filtered = ff_timefilter_update(tf, samples[i], 1);
62 error += (filtered - ideal[i]) * (filtered - ideal[i]);
63 }
64 ff_timefilter_destroy(tf);
65 if (error < best_error) {
66 best_error = error;
67 bestpar0 = par0;
68 bestpar1 = par1;
69 better = 1;
70 }
71 }
72 }
73 } while (better);
74 #if 0
75 double lastfil = 9;
76 TimeFilter *tf = ff_timefilter_new(1, bestpar0, bestpar1);
77 for (i = 0; i < SAMPLES; i++) {
78 double filtered;
79 filtered = ff_timefilter_update(tf, samples[i], 1);
80 printf("%f %f %f %f\n", i - samples[i] + 10, filtered - samples[i],
81 samples[FFMAX(i, 1)] - samples[FFMAX(i - 1, 0)], filtered - lastfil);
82 lastfil = filtered;
83 }
84 ff_timefilter_destroy(tf);
85 #else
86 printf(" [%f %f %9f]", bestpar0, bestpar1, best_error);
87 #endif
88 }
89 printf("\n");
90 }
91 return 0;
92 }