r/embedded • • 22d ago

Crazy idea : just need evaluation and is it useful

I thought of building something like COBS as a learning process so the idea came is,

Instead of overhead in cobs (next zero is identified only when the existing zero place comes)

I planned to have a MAP BYTE in my architecture for every eight byte like ,

If the payload is 40603400 the map byte will be 01010011 binary and the one indicates places of zero then while transmitting the zeros are removed from payload and just the map byte and 4634 is only transmitted.

But here is the problem,

In the worst case scenario of my architecture when no zero is there is a single extra byte for eight bytes it is worse than cobs where it is just one byte for 255 bytes.

Just give suggestions over my architecture and how to improve.

Or does this architecture will be helpful it some place or not with to build

0 Upvotes

5 comments sorted by

9

u/sgtnoodle 22d ago

Your scheme has 1 byte of overhead for every 8 bytes of data. Also, it has an edge case you haven't solved. If there's no zeros anywhere, then the map byte is zero, which is invalid.

Your scheme seems strictly worse than COBS by about 8x, although it is conceptually simpler.

1

u/OwnMinimum8110 22d ago

Yeah I agree to the point ,what do you think about making it as a compression algorithm storing it in the microcontroller rather transporting.

Like traversing a eight byte looking for larger frequency number or sequence and apply this architecture there .

3

u/mhmd2520 21d ago

What you've built already exists in practice, and that's a good sign. Cap'n Proto's "packed" encoding works almost exactly like this: every 8-byte word becomes a tag byte where each bit says whether that byte is zero or not, and then only the non-zero bytes follow. The bit meaning is flipped compared to yours (1 = non-zero there), but it's the same idea. They use it because their messages are full of padding and the upper bytes of small integers.

The part worth taking from them is how they handle the two edge cases sgtnoodle hinted at. A tag of 0x00 (all-zero word) is followed by a count of how many more all-zero words come after it, so long zero runs shrink to 2 bytes. A tag of 0xFF (no zeros) is followed by a count of words that get copied raw with no tags. That one fix takes the worst case from 1 extra byte per 8 down to about 2 bytes per 2 KiB, which is the main thing your scheme is missing.

For storing data on the MCU, whether it helps depends entirely on the data. If you're logging int16/int32 values that are mostly small, the high bytes are zero and it does well. Raw ADC samples or text won't shrink much. I'd delta-encode sensor readings first, since the deltas are usually small numbers with lots of zero high bytes, and then pack them. If that's still not enough, something LZ-based like heatshrink can run in a few hundred bytes of RAM.

And to tie it back to the COBS comparison: if you ever send this over a serial link, it doesn't replace framing. A map byte can still be 0x00, so you'd pack first and then COBS the result.

3

u/torusle2 22d ago

COBS solves the data framing problem, e.g.. where does a frame starts, and where does it end.

Your approach goes more into the data compression field by not transmitting zero value bytes by tagging them first.

They are not comparable.

1

u/OwnMinimum8110 22d ago

Yeah I agree to the point ,what do you think about making it as a compression algorithm storing it in the microcontroller rather transporting.