将数据从8位转换为12位

时间:2012-07-15 13:54:27

标签: algorithm

我收到的信号存储为char数据的缓冲区(8位)。 我也得到相同的信号加上24分贝,我的老板告诉我,应该可以从这两个缓冲区重建,一个(将用作输出)将存储为12位。 我想知道可以做到这一点的数学运算以及选择+ 24dB的原因。 谢谢(我很笨><)。

1 个答案:

答案 0 :(得分:1)

从问题陈述中,我猜你有一个模拟信号,它是在两个时候采样的。两个信号的分辨率都是8位,但有一个被移位和截断。

你可以通过组合第一个信号的高4位,并将它们与第二个信号连接来获得12位信号。

sOut = ((sIn1 & 0xF0) << 4) | sIn2

如果想要获得更好的准确度,可以尝试计算两个信号的公共位的平均值。通常,第一信号的低4位应近似等于第二信号的高4位。由于舍入误差或噪声,值可能略有不同。其中一个值甚至可能溢出,并移动到范围的另一端。

int Combine(byte sIn1, byte sIn2)
{
    int a = sIn1 >> 4; // Upper 4 bits
    int b1 = sIn1 & 0x0F; // Common middle 4 bits
    int b2 = sIn2 >> 4;  // Common middle 4 bits
    int c = sIn2 & 0x0F; // Lower 4 bits

    int b;

    if (b1 >= 12 && b2 < 4)
    {
        // Assume b2 has overflowed, and wrapped around to a smaller value.
        // We need to add 16 to it to compensate the average.
        b = (b1 + b2 + 16)/2;
    }
    else if (b1 < 4 && b2 >= 12)
    {
        // Assume b2 has underflowed, and wrapped around to a larger value.
        // We need to subtract 16 from it to compensate the average.
        b = (b1 + b2 - 16)/2;
    }
    else
    {
        // Neither or both has overflowed. Just take the average.
        b = (b1 + b2)/2;
    }

    // Construct the combined signal.
    return a * 256 + b * 16 + c;
}

当我测试它时,它比第一个公式更准确地再现信号。

相关问题