0
$\begingroup$

I am trying to implement the following matlab function in c: Mathworks scrambler Function Documentation

This is the code I've written:

#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;
}

The results I get from matlab are different.

If I create the following vector and scrambler in 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]

but what I'm getting is:

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

I don't see where I went wrong. Any help greatly appreciated, thank you!

$\endgroup$
0
$\begingroup$

I think the mistake would be this line

current[0] = xor ^ bin[bin_ndx] ^ 1;

Remove the xor with 1 at the end.

current[0] = xor ^ bin[bin_ndx];

The MATLAB figure shows 1 because that is the multiplier (scale factor) and may not mean xor with 1.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.