Created
June 20, 2019 02:13
-
-
Save ufcpp/f2646c36806c058c79cb728df18f91ce to your computer and use it in GitHub Desktop.
平均0の一様乱数で作った数列は自己相関関数がδ関数になる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
class Program | |
{ | |
static void Main() | |
{ | |
const int N = 1 << 10; | |
const int Mask = N - 1; | |
var r = new Random(); | |
// 平均 0 の乱数列。 | |
var data = Enumerable.Range(0, N).Select(_ => 2 * r.NextDouble() - 1).ToArray(); | |
// 分散。平均が 0 なので単に二乗の平均。 | |
var variance = data.Sum(x => x * x) / data.Length; | |
// 自己相関関数。 | |
// 平均値が0で十分にランダムな数列(いわゆるホワイトノイズ)の自己相関はδ関数になる。 | |
// n == 0 の時に data の分散(この場合、data.Sum(x => x * x) / data.Length) | |
// それ以外の時は誤差程度の小さな値になる。 | |
double autocorrelation(ReadOnlySpan<double> data, int n) | |
{ | |
var sum = 0.0; | |
for (int i = 0; i < N; i++) | |
{ | |
var a = data[i]; | |
var b = data[(i + n) & Mask]; | |
sum += a * b; | |
} | |
return sum / N; | |
} | |
// i == 0 の1回だけ 1、残りは全部 0 が表示される。 | |
// N の値をどう変えてもそう。 | |
for (int i = 0; i < N; i++) | |
{ | |
var a = autocorrelation(data, i) / variance; | |
Console.Write(Math.Round(a)); | |
Console.Write(", "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment