I tried to implement PCM by A-law. The transform function took from the wiki. But in the end it turns out too much noise. I suspect that this is due to incorrect transformations (BitArray -> short, short -> byte, etc.), but I checked them several times. Tell me what I did wrong. Coding function:

private static byte Encode(bool[] word) { var bits = new BitArray(word); var number = bits.ToInt16(); var x = number / (float)Max; var absX = Abs(x); float y; if (0 <= absX && absX < (1 / A)) { y = (float) (Sign(x)*(A*absX/(1 + Log(A)))); } else { y = (float) (Sign(x)*((1 + Log(A*absX))/(1 + Log(A)))); } y *= byte.MaxValue; return (byte) y; } 

Decoding function:

  private static short Decode(bool[] code) { var bits = new BitArray(code); var number = bits.ToByte(); var y = number / (float)Max; var absY = Abs(y); float x; if (absY < 1 / (1 + Log(A))) { x = (float) (Sign(y)*(absY*(1 + Log(A))/A)); } else { x = (float) (Sign(y)*(Exp(absY*(1 + Log(A)) - 1)/A)); } x *= short.MaxValue; return (short)x; } 

The input to the functions is the bit representations of the short and byte numbers, respectively.

  • one
    If you specify what PCM is, your question will be useful to a much larger circle of people. - VladD

1 answer 1

Understood. The problem was using unsigned byte type. Solution: use sbyte .