I逆量化器dq+SrSp步长调节q预测器
图2.3 解码原理框图
2.2解码原理
解码是编码的逆过程,ADPCM的解码过程如图2.3所示。解码输入即编码输出I,逆量化器将I逆量化成差值dq。差值dq与预测采样Sp相加得到解码输出Sr。解码过程同编码过程中求预测采样的原理是一样的,当编码端的Sr能很好的跟踪上Si时,解码端的Sr也能同样再现Si。这里涉及算法本身的时延和实现平台、实现方法的实时性等因素。
解码时首先通过步长索引查步长调整表得到量化步长,差值量化编码I经逆量化器得到语音差值d,这是求I的逆过程;然后与前次预测值一起重建当前
q语音信号S,最后用固定预测Spr?S,更新预测采样S,用I更新量化步长
rp索引值。
三、程序代码
MATLAB程序如下: ADPCM编码:
function adpcm_y = adpcm_encoder(raw_y)
IndexTable = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];%索引调整表
StepSizeTable = [7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767];%步长索引表
prevsample = 0; previndex = 1; Ns = length(raw_y); n = 1;
raw_y = 32767 * raw_y; % 降输入信号变为16bit while (n <= Ns)
predsample = prevsample;%预测值传递 index = previndex;%索引指针 step = StepSizeTable(index);
diff = raw_y(n) - predsample;%信号现在值与预测值差 if (diff >= 0)%如果差值大于0,则负号位为0,否则为1 code = 0; else code = 8;
diff = -diff;%取正值 end
tempstep = step;%步长,量化级 if (diff >= tempstep)
code = bitor(code, 4);%按位或运算,将第三位编码为1 diff = diff - tempstep; end
tempstep = bitshift(tempstep, -1);%二进制移位操作,目的是变成1/2 if (diff >= tempstep)
code = bitor(code, 2);%按位或运算,第二位编码为1 diff = diff - tempstep; end
tempstep = bitshift(tempstep, -1); if (diff >= tempstep)
code = bitor(code, 1);%第一位编码为1 end
diffq = bitshift(step, -3);
if (bitand(code, 4))%按位与运算,提取第三位 diffq = diffq + step; end
if (bitand(code, 2))
diffq = diffq + bitshift(step, -1); end
if (bitand(code, 1))
diffq = diffq + bitshift(step, -2); end
if (bitand(code, 8))
predsample = predsample - diffq; else
predsample = predsample + diffq; end
if (predsample > 32767) predsample = 32767;
elseif (predsample < -32768)
predsample = -32768; end
index = index + IndexTable(code+1);
if (index < 1) index = 1; end
if (index > 89) index = 89; end
prevsample = predsample; previndex = index;
adpcm_y(n) = bitand(code, 15); -pcm_y(n) = code; n = n + 1; end ADPCM解码:
function raw_y = adpcm_decoder(adpcm_y)
IndexTable = [-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8];
StepSizeTable = [7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289,
16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767];
prevsample = 0; previndex = 1;
Ns = length(adpcm_y); n = 1;
while (n <= Ns)
predsample = prevsample; index = previndex;
step = StepSizeTable(index); code = adpcm_y(n);
diffq = bitshift(step, -3); if (bitand(code, 4)) diffq = diffq + step; end
if (bitand(code, 2))
diffq = diffq + bitshift(step, -1); end
if (bitand(code, 1))
diffq = diffq + bitshift(step, -2); end
if (bitand(code, 8))
predsample = predsample - diffq; else
predsample = predsample + diffq; end