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
3535 #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
3636
3737 #define LIBAVUTIL_VERSION_MAJOR 50
38−#define LIBAVUTIL_VERSION_MINOR 5
39−#define LIBAVUTIL_VERSION_MICRO 1
38+#define LIBAVUTIL_VERSION_MINOR 6
39+#define LIBAVUTIL_VERSION_MICRO 0
4040
4141 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
4242 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
3939 c->index=0;
4040 }
4141
42+void 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+
4257 #ifdef TEST
4358 #include "log.h"
4459 #include "common.h"
@@ -59,6 +74,24 @@ int main(void) · 14 unmodified lines
5974 STOP_TIMER("624 calls of av_lfg_get");
6075 }
6176 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+
6295 return 0;
6396 }
6497 #endif
libavutil/lfg.h+8−0
@@ -51,4 +51,12 @@ static inline unsigned int av_mlfg_get(AVLFG *c){ · 50 unmodified lines
5151 return c->state[c->index++ & 63] = 2*a*b+a+b;
5252 }
5353
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+ */
60+void av_bmg_get(AVLFG *lfg, double out[2]);
61+
5462 #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