Libav
timefilter.c
Go to the documentation of this file.
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  }
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  printf(" [%f %f %9f]", bestpar0, bestpar1, best_error);
75  }
76  printf("\n");
77  }
78  return 0;
79 }
Definition: lfg.h:25
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
Definition: timefilter.c:55
#define LFG_MAX
Definition: timefilter.c:26
Opaque type representing a time filter state.
Definition: timefilter.c:30
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
int main(void)
Definition: timefilter.c:28
#define SAMPLES
TimeFilter * ff_timefilter_new(double clock_period, double feedback2_factor, double feedback3_factor)
Create a new Delay Locked Loop time filter.
Definition: timefilter.c:40
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
double ff_timefilter_update(TimeFilter *self, double system_time, double period)
Update the filter.
Definition: timefilter.c:65
common internal and external API header