function [voice,pitcha,rms,rc]=decode1(ipitch,irms,irc,rcn)
% This program provides error correction and decoding for all LPC parameters.
% INPUTS:
% ORDER - Number of RC's
% IPITV - Index value of pitch
% IRMS - Coded Energy
% IRC - Coded Reflection Coefficients
% CORRP - Error correction:
% If FALSE, parameters are decoded directly with no delay. If TRUE,
% most important parameter bits are protected by Hamming code and
% median smoothed. This requires an additional frame of delay.
% OUTPUTS:
% VOICE - Half frame voicing decisions
% PITCH - Decoded pitch
% RMS - Energy
% RC - Reflection coefficients
%
% NOTE: Zero RC's should be done more directly, but this would affect
% coded parameter printout.
rmst=[1024,936,856,784,718,656,600,550,502,460,420,384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0];
ivtab =[24960,24960,24960,24960,25480,25480,25483,25480,16640,1560,1560,1560,16640,1816,1563,1560,24960,24960,24859,24856,26001,25881,25915,25913,1560,1560,7800,3640,1561,1561,3643,3641];
corth=[32767.0, 32767.0, 32.0, 32.0, 32.0, 32.0, 16.0, 16.0;10.0, 8.0, 6.4, 6.4, 11.2, 11.2, 5.6, 5.6;5.0, 4.0, 3.2, 3.2, 6.4, 6.4, 3.2, 3.2;0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
detau=[0,0,0,3,0,3,3,31, 0,3,3,21,3,3,29,30,0,3,3,20,3,25,27,26, 3,23,58,22,3,24,28,3,0,3,3,3,3,39,33,32, 3,37,35,36,3,38,34,3,3,42,46,44,50,40,48,3, 54,3,56,3,52,3,3,1,0,3,3,108,3,78,100,104, 3,84,92,88,156,80,96,3,3,74,70,72,66,76,68,3, 62,3,60,3,64,3,3,1,3,116,132,112,148,152,3,3, 140,3,136,3,144,3,3,1,124,120,128,3,3,3,3,1, 3,3,3,1,3,1,1,1];
detab7=[4,11,18,25,32,39,46,53,60,66,72,77,82,87,92,96,101,104,108,111,114,115,117,119,121,122,123,124,125,126,127,127];
descl=[0.6953,0.6250,0.5781,0.5469,0.5312,0.5391,0.4688,0.3828];
deadd=[1152,-2816,-1536,-3584,-1280,-2432,768,-1920];
qb=[511,511,1023,1023,1023,1023,2047,4095];
nbit=[8,8,5,5,4,4,4,4,3,2];
zrc=[0,0,0,0,0,3,0,2,0,0];
abit=[2,4,8,16,32];
ivp2h=0;
erate=0;
iovoic=0;
iavgp=60;
ethrs=2048;
ethrs1=128;
ethrs2=1024;
ethrs3=2048;
fut=1;
pres=1;
past=2;
first=1;
rcn=length(irc);
pitcha=0;
% If no error correction, do pitch and voicing then jump to decode
% i4 = detau(ipitch);
i4=ipitch;
% Do error correction pitch and voicing
if(i4>4)
dpit(fut) = i4;
ivoic = 2;
iavgp =fix((15*iavgp+i4+8)*0.0625);
else
ivoic = i4;
dpit(fut) = iavgp;
end
drms(fut)=irms;
for n1=1:rcn
drc(fut,n1) = irc(n1);
end
% Determine index to IVTAB from V/UV decision
% If error rate is high then use alternate table
index = 16*ivp2h + 4*iovoic + ivoic + 1;
i1 = ivtab(index);
ipit = bitand(i1,3);
icorf = fix(i1*0.125);
if erate<ethrs
icorf =fix(icorf * 0.015625);
end
% Determine error rate: 4=high 1=low
ixcor = 4;
if(erate<ethrs3)
ixcor = 3;
end
if(erate<ethrs2)
ixcor = 2;
end
if(erate<ethrs1)
ixcor = 1;
end
% Voice/unvoice decision determined from bits 0 and 1 of IVTAB
voice(1) = bitand(fix(icorf*0.5),1);
voice(2) = bitand(icorf,1);
% Skip decoding on first frame because present data not yet available
if(first)
first = 0;
else
% If bit 4 of ICORF is set then correct RMS and RC(1) - RC(4).
% Determine error rate and correct errors using a Hamming 8,4 code
% during transition or unvoiced frame. If IOUT is negative,
% more than 1 error occurred, use previous frame's parameters.
if(bitand(icorf,abit(3))~=0)
errcnt = 0;
lsb = bitand(drms(pres),1);
index = fix(drc(pres,7)*16 + drms(pres) * 0.5);
% ham84(index,&iout,&errcnt);
drms(pres) = drms(past);
if(iout>=0)
drms(pres) = iout*2 + lsb;
end
for n1=1:4
if n1==1
i1= bitand(drc(pres,8),7)*2+bitand(drc(pres,9),1);
else
i1 = drc(pres,8-n1)&15;
end
i2 = bitand(drc(pres,4-n1),31);
lsb = bitand(i2,1);
index = fix(16*i1 + i2*0.5);
% ham84(index,&iout,&errcnt);
if iout>=0
iout = iout*2+lsb;
if(bitand(iout,16)==16)
iout = iout-32;
end
else
iout = drc(past,4-n2);
end
drc(pres,4-n2) = iout;
end
% Determine error rate
erate = fix(erate*0.96875 + errcnt*102);
end
% Get unsmoothed RMS, RC's, and PITCH
irms = drms(pres);
for n1=1;rcn
irc(n1)= drc(pres,n1);
end
if ipit==1
dpit(pres) = dpit(past);
end
if ipit==3
dpit(pres) = dpit(fut);
end
pitcha= dpit(pres);
% If bit 2 of ICORF is set then smooth RMS and RC's
if(bitand(icorf,abit(1))~=0)
if (abs(drms(pres)-drms(fut))>=corth(ixcor-1,1))&(abs(drms(res)-drms(past))>= corth(ixcor-1,1))
temp1=[drms(past),drms(pres),drms(fut)];
irms=median(temp1);
end
for n1=1:6
if( abs(drc(pres,n1-1)-drc(fut,n1-1))>=corth(ixcor-1,n1+1)&abs(drc(pres,n1-1)-drc(past,n1-1))>= corth(ixcor-1,n1+1))
temp1=[drc(past,n1-1),drc(pres,n1-1),drc(fut,n1-1)];
irc(n1)=median(temp1);
end
end
end
% If bit 3 of ICORF is set then smooth pitch
if(bitand(icorf,abit(2))~=0)
if(abs(dpit(pres)-dpit(fut)) >= corth(ixcor-1,0)&abs(dpit(pres)-dpit(past))>= corth(ixcor-1,0))
temp1=[dpit(past), dpit(pres), dpit(fut)];
pitch = median(temp1);
end
end
end
% If bit 5 of ICORF is set then RC(5) - RC(10) are loaded with
% values so that after quantization bias is removed in decode
% the values will be zero.
% end if first
if(bitand(icorf,abit(4))~=0)
for(n1=5:rcn)
irc(n1)=zrc(n1);
end
end
% House keeping - one frame delay
iovoic = ivoic;
ivp2h = voice(2);
dpit(past) = dpit(pres);
dpit(pres) = dpit(fut);
drms(past) = drms(pres);
drms(pres) = drms(fut);
for(n1=1:rcn)
drc(past,n1) = drc(pres,n1);
drc(pres,n1) = drc(fut,n1);
end
% Decode RMS
irms = rmst((31-irms)*2);
% Decode RC(1) and RC(2) from log-area-ratios
% Protect from illegal coded value (-16) caused by bit errors
for(n1=1:2)
i2 = irc(n1);
i1 = 0;
if(i2<0)
i1 = 1;
i2 = -i2;
if(i2>15)
i2 = 0;
end
end
i2 = detab7(2*i2+1);
if(i1==1)
i2 = -i2;
end
ishift = 15 - nbit(n1);
irc(n1) = i2*bitshift((ishift-1),2);
end
% Decode RC(3)-RC(10) to sign plus 14 bits
for(n1=3:rcn)
i2 = irc(n1);
ishift = 15 - nbit(n1-1);
i2 = i2*bitshift((ishift-1),2);
i2 = i2+qb(n1-2);
ftemp = i2*descl(n1-2) + deadd(n1-2); % rounding problem
if(ftemp < 0)
ftemp = - ftemp;
itemp = fix(ftemp);
irc(n1) = -itemp;
else
irc(n1) = fix(ftemp);
end
end
% Scale RMS and RC's to reals
rms=double(irms);
for(n1=1:rcn)
rc(n1)= double(irc(n1)* 6.103515625e-05); ,384 = 2^14
end