Skip to content

Instantly share code, notes, and snippets.

@laevandus
Created December 12, 2012 18:41
Show Gist options
  • Save laevandus/4270395 to your computer and use it in GitHub Desktop.
Save laevandus/4270395 to your computer and use it in GitHub Desktop.
NRZI
void nrzi_encode(uint8_t* buf, uint8_t* output, uint16_t len)
{
uint8_t prev_nrzi_bit = 0;
uint8_t nrz_bit;
uint8_t nrzi_bit;
uint8_t i, j;
for (i=0; i<len; i++)
{
for (j=0; j<8; j++)
{
nrz_bit = buf[i]>>j & 0x01;
if (nrz_bit == 0)
{
nrzi_bit = prev_nrzi_bit ^ 1;
}
else
{
nrzi_bit = prev_nrzi_bit;
}
output[i] = output[i] | ((nrzi_bit & 0x01) << j);
printf("[%u] %1X -> %1X\n", j, nrz_bit, nrzi_bit);
if (nrzi_bit)
buf[i] |= (1 << j);
else
buf[i] &= ~(1 << j);
prev_nrzi_bit = nrzi_bit;
}
}
}
void nrzi_decode(uint8_t* buf, uint8_t* output, uint16_t len)
{
uint8_t prev_nrzi_bit = 0;
uint8_t nrz_bit;
uint8_t nrzi_bit;
uint8_t i, j;
for (i=0; i<len; i++)
{
for (j=0; j<8; j++)
{
nrzi_bit = buf[i]>>j & 0x01;
if (nrzi_bit != prev_nrzi_bit)
{
nrz_bit = 0;
}
else
{
nrz_bit = 1;
}
output[i] = output[i] | ((nrz_bit & 0x01) << j);
printf("[%u] %1X -> %1X\n", j, nrzi_bit, nrz_bit);
prev_nrzi_bit = nrzi_bit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment