在电子学中有标准的伪二进制序列。在文献中,他们讨论了这些序列的生成多项式:
PRBS-7 : x^7 + x^6 + 1
PRBS-10 : x^11 + x^10 + 1
PRBS-15 : x^15 + x^14 + 1
PRBS-23 : x^23 + x^18 + 1
PRBS-31 : x^31 + x^28 + 1
我能够在Wikipedia - Pseudo-random binary sequence上找到用于生成PRBS-7的C代码片段,但没有迹象表明如何转换它以创建其他序列。
这就是我所拥有的,但除了PRBS-7之外,它只生成零。这段代码是C#,但我可以使用C或C ++样本。
private static void Generate(int v1, int v2, StringBuilder roots, StringBuilder bits)
{
// This works fine for PRBS7 with v1=7, v2=6 but fails for 15,10; 23,18; 31,28
const byte start = 2;
byte a = start;
for (int i = 1; i < 2000; i++) // I added the 2000 limit, otherwise was infinite loop
{
roots.AppendFormat("{0:X2} ", a);
int newbit = (((a >> v1 - 1) ^ (a >> v2 - 1)) & 1); // my naive attempt to generalize the algorithm
a = (byte)((byte)((a << 1) | newbit) & 0x7f);
bits.Append(newbit);
if (i % 32 == 0)
{
roots.AppendLine();
bits.AppendLine();
}
if (a == start)
{
roots.AppendFormat("\r\nRepetition Period = {0}", i);
break;
}
}
}
答案 0 :(得分:1)
我不确定这是不是你想要的:
private static void Main(string[] args)
{
var sequence = GeneratePrbSsequences(15);
Console.WriteLine(string.Join("\n",sequence));
Console.WriteLine("Repetition Period = {0}", sequence.Count);
Console.ReadLine();
}
static List<string> GeneratePrbSsequences(int number)
{
var a = 0x02;
var period = (int)(Math.Pow(2, number) - 1);
var sequenceList = new List<string>();
do
{
var v1 = number - 1;
var v2 = number - 2;
var newbit = (((a >> v1) ^ (a >> v2)) & 1);
a = ((a << 1) | newbit) & period;
sequenceList.Add(string.Format("{0:X2}", a));
Console.WriteLine();
} while (a != 0x02);
return sequenceList;
}
}
答案 1 :(得分:0)
Ammar给出了一个几乎正确的答案,所以我将投票给他答案。但这是我如何修改他在生成多项式中使用两个因子的答案。我仍然不肯定这是完全正确的,但似乎确实提供了可用的结果。
static void GeneratePrbSsequences(int v1, int v2)
{
int a = 0x02;
int period = (int)(Math.Pow(2, v1) - 1);
v1--;
v2--;
for (int i=1;;i++)
{
var newbit = (((a >> v1) ^ (a >> v2)) & 1);
System.Diagnostics.Debug.Write(newbit);
if (i%8 == 0)
{
System.Diagnostics.Debug.Write(" ");
if (i%128 == 0) System.Diagnostics.Debug.WriteLine("");
}
a = ((a << 1) | newbit) & period;
if (a == 0x02)
{
System.Diagnostics.Debug.WriteLine("");
System.Diagnostics.Debug.WriteLine("Computed period = {0}, Actual period = {1}", period, i);
break;
}
}
}