aboutsummaryrefslogtreecommitdiffstats
path: root/libavutil/lfg.c
diff options
context:
space:
mode:
authorStefano Sabatini <stefano.sabatini-lala@poste.it>2009-12-12 16:24:37 +0000
committerStefano Sabatini <stefano.sabatini-lala@poste.it>2009-12-12 16:24:37 +0000
commit3ba69a15c680596feeef1d213ce2b85823dde985 (patch)
treef0921738d1342b2c1edd70c69cd54a8bd771d9a4 /libavutil/lfg.c
parent62d75662391733cb97edfe1ed000afe2360bc0f3 (diff)
downloadffmpeg-3ba69a15c680596feeef1d213ce2b85823dde985.tar.gz
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
Diffstat (limited to 'libavutil/lfg.c')
-rw-r--r--libavutil/lfg.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/libavutil/lfg.c b/libavutil/lfg.c
index e876d18fef..a6828634ae 100644
--- a/libavutil/lfg.c
+++ b/libavutil/lfg.c
@@ -39,6 +39,21 @@ void av_cold av_lfg_init(AVLFG *c, unsigned int seed){
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 @@ int main(void)
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