0

I have used the WinFilter Program to compute the FIR filter on C code but I got a issue:

The program only provide the 16 bits signed array and i need to that this vector be a unsigned int. So i am looking for a simple solution to relocated the array values to the next "values".

int16_t FIRCoef[Ntap] = {
    -1029,
    -1560,
    -1188,
        0,
     1405,
     2186,
     1718,
        0,
    -2210,
    -3647,
    -3095,
        0,
     5160,
    10947,
    15482,
    17197,
    15482,
    10947,
     5160,
        0,
    -3095,
    -3647,
    -2210,
        0,
     1718,
     2186,
     1405,
        0,
    -1188,
    -1560,
    -1029,
        0
};

uint16_t fir(uint16_t NewSample) {
    static uint16_t x[Ntap]; //input samples
    uint32_t y=0;            //output sample
    int n;

    //shift the old samples
    for(n=Ntap-1; n>0; n--)
       x[n] = x[n-1];

    //Calculate the new output
    x[0] = NewSample;
    for(n=0; n<Ntap; n++)
        y += FIRCoef[n] * x[n]; // calculo da convolucao na amostra
                                // Calculation of the convolution in the sample    
    return y / DCgain;
}

I think that one solution should be like this:

uint16_t--------int16_t---------index
0               -32767            1
1               -32766            2
2               -32765            3
...              ...             ...
65535            32767            65535

any hint?

4

1 回答 1

2

an 的值范围int16_t是 -32768 到 32767。您的问题在这一点上还不清楚,但似乎您只想将这些值转换为 a uint16_t,0 到 65535 的范围。这是合理的,因为可表示值的数量这两种类型是一样的;这将通过将 an 的最小可能值的倒数添加int16_t到输入来完成。

当然,魔鬼在细节中。当有符号加法溢出时,会产生未定义的行为。当超出范围的值转换为有符号整数类型时,结果是实现定义的,并且可能是(实现定义的)异常。避免实现定义的行为是可取的,避免未定义的行为是必不可少的;在这种情况下,只需稍加注意即可完成:

uint16_t convert(int16_t in) {
    return (uint16_t) 32768 + (uint16_t) in;
}

这在任何首先提供uint16_tandint16_t类型的一致系统上可靠地做正确的事情,因为转换和加法以模一加 a 的最大值进行uint16_t。负输入值转换为 范围上半部分的无符号值uint16_t,然后加法旋转所有值,将范围上半部分的值带到下半部分。

至于对整个数组执行此操作,如果您只想依赖定义明确的 C 行为(即,如果您想要一个严格符合标准的解决方案),那么您需要制作数据的副本。您可以使用上述功能来填充副本。

于 2017-06-01T20:26:45.787 回答