将 Matlab 扰码器函数翻译成 C
Translating Matlab scrambler function to C
我正在尝试在 c 中实现以下 matlab 函数:
Mathworks scrambler Function Documentation
这是我写的代码:
#define N 5
#define MAX 70
#define BITS 7
//char* msg = "This message is Coming to You!";
//char* msg = "Hello world 000\nHello world 001\nHello world 002\nHello world 003\n";
//char* msg = "Hello world 000\nHello world 001\n";
char* msg = "Hel";
uint8_t current[N-1] = {0};
uint8_t to_Xor[N-1] = {0};
uint8_t bin[MAX*BITS];
int main()
{
int len=strlen(msg);
int kk = 0;
int m = 0;
uint8_t letter = 0;
uint8_t chars = 0;
for(int nn = 0; nn < len*BITS; nn++)
{
bin[nn] = 0;
}
for(int ii = 0; ii < len; ii++)
{
letter = msg[ii];
for(int jj = 6; jj>=0; jj--)
{
bin[kk]=(letter&(1<<jj))>>jj;
//printf("%d\n",bin[k]);
kk++;
}
}
uint8_t xor = 0;
uint8_t polynom = 0x0;
uint8_t scrmbld_output[len*BITS];
for(int c = 0; c < len*BITS; c++)
{
scrmbld_output[c] = 0;
}
for(int bin_ndx = 0; bin_ndx < len*BITS; bin_ndx++)
{
for(int n = 0; n < N-1; n++)
{
if((1<<n)&polynom)
{
xor ^= current[n];
}
}
for (int k = N-2; k > 0; k--)
{
current[k]=current[k-1];
}
current[0] = xor ^ bin[bin_ndx] ^ 1;
scrmbld_output[bin_ndx] = current[0];
xor = 0;
printf("original: %d scrmbld: %d\n",bin[bin_ndx], scrmbld_output[bin_ndx]);
}
int k = 0;
m = 6;
for(int i = 0; i < len; i++)
{
for(int j = 6; j>=0; j--)
{
chars |= bin[k]<<m;
k++;
m--;
}
printf("%c", chars);
chars = 0;
m = 6;
}
printf("\nlen: %d", len);
return 0;
}
我用matlab得到的结果不一样
如果我在 matlab 中创建以下矢量和扰码器:
a = [ 1 0 0 1 0 0 0 1 1 0 ];
scrmblr = comm.Scrambler(2,[1 1 1 0 1], [0 0 0 0]);
ans = scrmblr(a');
ans' = [1 1 0 0 1 0 1 0 1 1]
但我得到的是:
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 0
original: 1 scrmbld: 0
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
我不明白我哪里做错了。非常感谢任何帮助,谢谢!
嗯,
你的C代码可能有点复杂,可以简化一下。
请注意,您犯了一些错误:
polynom
未设置
- 在
current[0] = xor ^ bin[bin_ndx] ^ 1
中,^1
没有理由。
我修改了代码以使其更易于理解:
#include <stdio.h>
#include <stdint.h>
#define N 5
uint8_t bin[] = { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0};
uint8_t state[N-1] = {0};
int main()
{
int len=sizeof bin;
uint8_t state [N-1] = {0,0,0,0};
/* looking at documentation, the first element in poly is ignored... */
uint8_t poly [N-1] = {1,1,0,1};
for(int bin_ndx = 0; bin_ndx < len; bin_ndx++)
{
uint8_t xor = 0;
for(int n = 0; n < N-1; n++)
{
if(poly[n])
{
xor ^= state[n];
}
}
for (int k = N-2; k > 0; k--)
{
state[k]=state[k-1];
}
state[0] = xor ^ bin[bin_ndx] ;
printf("input: %d, output: %d, state: %d %d %d %d %d\n", bin[bin_ndx],
state[0], state[0], state[1], state[2], state[3], state[4]);
}
return 0;
}
如您所见,我删除了不相关的 hex to bin 部分。
在重新添加之前,我建议你把你的代码分成函数,一个用于加扰,一个用于转换,等等...
例如:
#include <stdint.h>
#include <stdio.h>
/* Binary scramble
input: array of size *size* to be processed
output: array to store the result, must be as big as input
poly: polynom to use: array of *order* size
state: internal state: array of *order* size
order: order of polynom
*/
void scramble (uint8_t *input, uint8_t *output,size_t size, uint8_t *poly, uint8_t *state, size_t order)
{
/* ignore the order 0 */
poly++;
state++;
order--;
/* for each input bits */
for(int i = 0; i < size; i++)
{
/* compute the value to be xored to input */
uint8_t xor = 0;
for(int n = 0; n < order; n++)
{
if(poly[n])
{
xor ^= state[n];
}
}
/* shift the state */
for (int k = order-1; k > 0; k--)
{
state[k] = state[k-1];
}
/* compute the ouput */
output[i] = state[0] = xor ^ input[i] ;
}
}
int main(void)
{
uint8_t bin[10] = { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0};
uint8_t out[10];
uint8_t poly[5] = {1, 1, 1, 0, 1};
uint8_t state[5] = {0};
scramble(bin, out, 10, poly, state, 5);
for (int i = 0; i < 10; ++i)
{
printf("%d -> %d\n", bin[i], out[i]);
}
return 0;
}
我正在尝试在 c 中实现以下 matlab 函数: Mathworks scrambler Function Documentation
这是我写的代码:
#define N 5
#define MAX 70
#define BITS 7
//char* msg = "This message is Coming to You!";
//char* msg = "Hello world 000\nHello world 001\nHello world 002\nHello world 003\n";
//char* msg = "Hello world 000\nHello world 001\n";
char* msg = "Hel";
uint8_t current[N-1] = {0};
uint8_t to_Xor[N-1] = {0};
uint8_t bin[MAX*BITS];
int main()
{
int len=strlen(msg);
int kk = 0;
int m = 0;
uint8_t letter = 0;
uint8_t chars = 0;
for(int nn = 0; nn < len*BITS; nn++)
{
bin[nn] = 0;
}
for(int ii = 0; ii < len; ii++)
{
letter = msg[ii];
for(int jj = 6; jj>=0; jj--)
{
bin[kk]=(letter&(1<<jj))>>jj;
//printf("%d\n",bin[k]);
kk++;
}
}
uint8_t xor = 0;
uint8_t polynom = 0x0;
uint8_t scrmbld_output[len*BITS];
for(int c = 0; c < len*BITS; c++)
{
scrmbld_output[c] = 0;
}
for(int bin_ndx = 0; bin_ndx < len*BITS; bin_ndx++)
{
for(int n = 0; n < N-1; n++)
{
if((1<<n)&polynom)
{
xor ^= current[n];
}
}
for (int k = N-2; k > 0; k--)
{
current[k]=current[k-1];
}
current[0] = xor ^ bin[bin_ndx] ^ 1;
scrmbld_output[bin_ndx] = current[0];
xor = 0;
printf("original: %d scrmbld: %d\n",bin[bin_ndx], scrmbld_output[bin_ndx]);
}
int k = 0;
m = 6;
for(int i = 0; i < len; i++)
{
for(int j = 6; j>=0; j--)
{
chars |= bin[k]<<m;
k++;
m--;
}
printf("%c", chars);
chars = 0;
m = 6;
}
printf("\nlen: %d", len);
return 0;
}
我用matlab得到的结果不一样
如果我在 matlab 中创建以下矢量和扰码器:
a = [ 1 0 0 1 0 0 0 1 1 0 ];
scrmblr = comm.Scrambler(2,[1 1 1 0 1], [0 0 0 0]);
ans = scrmblr(a');
ans' = [1 1 0 0 1 0 1 0 1 1]
但我得到的是:
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 1 scrmbld: 0
original: 1 scrmbld: 0
original: 1 scrmbld: 1
original: 0 scrmbld: 1
original: 1 scrmbld: 1
original: 1 scrmbld: 0
original: 0 scrmbld: 1
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
original: 0 scrmbld: 0
我不明白我哪里做错了。非常感谢任何帮助,谢谢!
嗯, 你的C代码可能有点复杂,可以简化一下。
请注意,您犯了一些错误:
polynom
未设置- 在
current[0] = xor ^ bin[bin_ndx] ^ 1
中,^1
没有理由。
我修改了代码以使其更易于理解:
#include <stdio.h>
#include <stdint.h>
#define N 5
uint8_t bin[] = { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0};
uint8_t state[N-1] = {0};
int main()
{
int len=sizeof bin;
uint8_t state [N-1] = {0,0,0,0};
/* looking at documentation, the first element in poly is ignored... */
uint8_t poly [N-1] = {1,1,0,1};
for(int bin_ndx = 0; bin_ndx < len; bin_ndx++)
{
uint8_t xor = 0;
for(int n = 0; n < N-1; n++)
{
if(poly[n])
{
xor ^= state[n];
}
}
for (int k = N-2; k > 0; k--)
{
state[k]=state[k-1];
}
state[0] = xor ^ bin[bin_ndx] ;
printf("input: %d, output: %d, state: %d %d %d %d %d\n", bin[bin_ndx],
state[0], state[0], state[1], state[2], state[3], state[4]);
}
return 0;
}
如您所见,我删除了不相关的 hex to bin 部分。
在重新添加之前,我建议你把你的代码分成函数,一个用于加扰,一个用于转换,等等...
例如:
#include <stdint.h>
#include <stdio.h>
/* Binary scramble
input: array of size *size* to be processed
output: array to store the result, must be as big as input
poly: polynom to use: array of *order* size
state: internal state: array of *order* size
order: order of polynom
*/
void scramble (uint8_t *input, uint8_t *output,size_t size, uint8_t *poly, uint8_t *state, size_t order)
{
/* ignore the order 0 */
poly++;
state++;
order--;
/* for each input bits */
for(int i = 0; i < size; i++)
{
/* compute the value to be xored to input */
uint8_t xor = 0;
for(int n = 0; n < order; n++)
{
if(poly[n])
{
xor ^= state[n];
}
}
/* shift the state */
for (int k = order-1; k > 0; k--)
{
state[k] = state[k-1];
}
/* compute the ouput */
output[i] = state[0] = xor ^ input[i] ;
}
}
int main(void)
{
uint8_t bin[10] = { 1, 0, 0, 1, 0, 0, 0, 1, 1, 0};
uint8_t out[10];
uint8_t poly[5] = {1, 1, 1, 0, 1};
uint8_t state[5] = {0};
scramble(bin, out, 10, poly, state, 5);
for (int i = 0; i < 10; ++i)
{
printf("%d -> %d\n", bin[i], out[i]);
}
return 0;
}