通过消除无意义的位来压缩数据的方法

发布于 2025-01-30 19:14:11 字数 259 浏览 3 评论 0原文

我正在处理时间序列数据。

在每个示例时序,我都会得到一个样本,并且该值的大小不是全字节。

例如,如果样本是7位数据,则​​仅在字节上使用7位。

第一个示例= 0B 0100 1000, 第二个样本= 0B 0110 0010 等等……

最初 0100 1000 0110 0010

,但我想获得 10 0100 0110 0010

如何将此压缩方法应用于编码和解码?

使用这种方法有任何官方的压缩理论吗?

I am dealing with time-series data.

At each sample timing, i get a sample and the size of this value is not full-byte.

for example, if sample is data of 7 bits, it uses only 7 bits on a byte.

first sample = 0b 0100 1000,
second sample = 0b 0110 0010
and so on…

originally,
0100 1000 0110 0010

but i want to get
10 0100 0110 0010

how can i apply this compressing method to both encode and decode??

is there any official compressing theory using this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2025-02-06 19:14:11

使用位操作一次将七个位写入您在两个变量中维护的位缓冲区的缓冲区,并在积累它们时写出字节。最后,您仍然需要写出完整的字节,因此最后一个字节可能有一些未使用的位。解码使用相同的方法一次从比特流读取七个位。

编码(在C中):

unsigned buf = 0, bits = 0;
for (int i = 0; i < sample_count; i++) {
    buf |= sample[i] << bits;
    bits += 7;
    if (bits >= 8) {
        putchar(buf & 0xff);
        buf >>= 8;
        bits -= 8;
    }
}
if (bits > 0)
    putchar(buf);

解码(掩盖您如何知道如何制作样本[]):

unsigned buf = 0, bits = 0, n = 0;
for (;;) {
    int ch = getchar();
    if (ch == EOF)
        break;
    buf |= ch << bits;
    bits += 8;
    do {
        sample[n++] = buf & 0x7f;
        buf >>= 7;
        bits -= 7;
    } while (bits >= 7);
}

Use bit operations to write seven bits at a time to a buffer of bits you maintain in two variables, writing out bytes as you accumulate them. In the end, you will still need to write out full bytes, so the last byte may have some unused bits. Decoding reads seven bits at a time from the bitstream, using the same approach.

Encode (in C):

unsigned buf = 0, bits = 0;
for (int i = 0; i < sample_count; i++) {
    buf |= sample[i] << bits;
    bits += 7;
    if (bits >= 8) {
        putchar(buf & 0xff);
        buf >>= 8;
        bits -= 8;
    }
}
if (bits > 0)
    putchar(buf);

Decode (glossing over how you know how big to make sample[]):

unsigned buf = 0, bits = 0, n = 0;
for (;;) {
    int ch = getchar();
    if (ch == EOF)
        break;
    buf |= ch << bits;
    bits += 8;
    do {
        sample[n++] = buf & 0x7f;
        buf >>= 7;
        bits -= 7;
    } while (bits >= 7);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文