History

Implement av_bmg_next(), a Box-Muller Gaussian random generator.

See the thread: "[FFmpeg-devel] [PATCH] Box-Muller gaussian generator". Originally committed as revision 20808 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stefano Sabatini committed 16 years ago

Commit 3ba69a11 parent 62d75663 files · +43 −2Browse files

Unified · Split

3 files
  1. libavutil/avutil.h +2 −2
  2. libavutil/lfg.c +33 −0
  3. libavutil/lfg.h +8 −0
libavutil/avutil.h+2−2
@@ -35,8 +35,8 @@ · 34 unmodified lines
35#define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)35#define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
36 36
37#define LIBAVUTIL_VERSION_MAJOR 5037#define LIBAVUTIL_VERSION_MAJOR 50
38#define LIBAVUTIL_VERSION_MINOR 538#define LIBAVUTIL_VERSION_MINOR 6
39#define LIBAVUTIL_VERSION_MICRO 139#define LIBAVUTIL_VERSION_MICRO 0
40 40
41#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \41#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
42 LIBAVUTIL_VERSION_MINOR, \42 LIBAVUTIL_VERSION_MINOR, \
libavutil/lfg.c+33−0
@@ -39,6 +39,21 @@ void av_cold av_lfg_init(AVLFG *c, unsigned int seed){ · 38 unmodified lines
39 c->index=0;39 c->index=0;
40}40}
41 41
42void av_bmg_get(AVLFG *lfg, double out[2])
43{
44 double x1, x2, w;
45
46 do {
47 x1 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
48 x2 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
49 w = x1*x1 + x2*x2;
50 } while (w >= 1.0);
51
52 w = sqrt((-2.0 * log(w)) / w);
53 out[0] = x1 * w;
54 out[1] = x2 * w;
55}
56
42#ifdef TEST57#ifdef TEST
43#include "log.h"58#include "log.h"
44#include "common.h"59#include "common.h"
@@ -59,6 +74,24 @@ int main(void) · 14 unmodified lines
59 STOP_TIMER("624 calls of av_lfg_get");74 STOP_TIMER("624 calls of av_lfg_get");
60 }75 }
61 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);76 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
77
78 /* BMG usage example */
79 {
80 double mean = 1000;
81 double stddev = 53;
82
83 av_lfg_init(&state, 42);
84
85 for (i = 0; i < 1000; i += 2) {
86 double bmg_out[2];
87 av_bmg_get(&state, bmg_out);
88 av_log(NULL, AV_LOG_INFO,
89 "%f\n%f\n",
90 bmg_out[0] * stddev + mean,
91 bmg_out[1] * stddev + mean);
92 }
93 }
94
62 return 0;95 return 0;
63}96}
64#endif97#endif
libavutil/lfg.h+8−0
@@ -51,4 +51,12 @@ static inline unsigned int av_mlfg_get(AVLFG *c){ · 50 unmodified lines
51 return c->state[c->index++ & 63] = 2*a*b+a+b;51 return c->state[c->index++ & 63] = 2*a*b+a+b;
52}52}
53 53
54/**
55 * Gets the next two numbers generated by a Box-Muller Gaussian
56 * generator using the random numbers issued by lfg.
57 *
58 * @param out[2] array where are placed the two generated numbers
59 */
60void av_bmg_get(AVLFG *lfg, double out[2]);
61
54#endif /* AVUTIL_LFG_H */62#endif /* AVUTIL_LFG_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t