changeset 798:41da9d9d39b7 libavutil

Implement av_bmg_next(), a Box-Muller Gaussian random generator. See the thread: "[FFmpeg-devel] [PATCH] Box-Muller gaussian generator".
author stefano
date Sat, 12 Dec 2009 16:24:37 +0000
parents e2fd9fd487ae
children cb32a271f4cd
files avutil.h lfg.c lfg.h
diffstat 3 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/avutil.h	Thu Dec 03 18:15:54 2009 +0000
+++ b/avutil.h	Sat Dec 12 16:24:37 2009 +0000
@@ -35,8 +35,8 @@
 #define AV_VERSION(a, b, c) AV_VERSION_DOT(a, b, c)
 
 #define LIBAVUTIL_VERSION_MAJOR 50
-#define LIBAVUTIL_VERSION_MINOR  5
-#define LIBAVUTIL_VERSION_MICRO  1
+#define LIBAVUTIL_VERSION_MINOR  6
+#define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
--- a/lfg.c	Thu Dec 03 18:15:54 2009 +0000
+++ b/lfg.c	Sat Dec 12 16:24:37 2009 +0000
@@ -39,6 +39,21 @@
     c->index=0;
 }
 
+void av_bmg_get(AVLFG *lfg, double out[2])
+{
+    double x1, x2, w;
+
+    do {
+        x1 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
+        x2 = 2.0/UINT_MAX*av_lfg_get(lfg) - 1.0;
+        w = x1*x1 + x2*x2;
+    } while (w >= 1.0);
+
+    w = sqrt((-2.0 * log(w)) / w);
+    out[0] = x1 * w;
+    out[1] = x2 * w;
+}
+
 #ifdef TEST
 #include "log.h"
 #include "common.h"
@@ -59,6 +74,24 @@
         STOP_TIMER("624 calls of av_lfg_get");
     }
     av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
+
+    /* BMG usage example */
+    {
+        double mean   = 1000;
+        double stddev = 53;
+
+        av_lfg_init(&state, 42);
+
+        for (i = 0; i < 1000; i += 2) {
+            double bmg_out[2];
+            av_bmg_get(&state, bmg_out);
+            av_log(NULL, AV_LOG_INFO,
+                   "%f\n%f\n",
+                   bmg_out[0] * stddev + mean,
+                   bmg_out[1] * stddev + mean);
+        }
+    }
+
     return 0;
 }
 #endif
--- a/lfg.h	Thu Dec 03 18:15:54 2009 +0000
+++ b/lfg.h	Sat Dec 12 16:24:37 2009 +0000
@@ -51,4 +51,12 @@
     return c->state[c->index++ & 63] = 2*a*b+a+b;
 }
 
+/**
+ * Gets the next two numbers generated by a Box-Muller Gaussian
+ * generator using the random numbers issued by lfg.
+ *
+ * @param out[2] array where are placed the two generated numbers
+ */
+void av_bmg_get(AVLFG *lfg, double out[2]);
+
 #endif /* AVUTIL_LFG_H */