Code Rle Rpl
-
Upload
thanh-vu-nguyen -
Category
Documents
-
view
219 -
download
0
Transcript of Code Rle Rpl
-
8/4/2019 Code Rle Rpl
1/15
function R=AutoCorrelate(y,lenth1,Max)
% Function: AutoCorrelate
% This subprogram is for the autocorrelate function
% y: sample point of speech
R=zeros(1,lenth1);
[R(1),V,X]=MiddleShap(y,lenth1,Max); % middleshap
for j1=21:880
for i1=j1:lenth1
if (i1+j1)
-
8/4/2019 Code Rle Rpl
2/15
6,206,188,172,158,144,132,120,110,102,92,84,78,70,64,60,54,50,46,42,38,34,32,30,2
6,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,1664
0,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,11
7,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)
-
8/4/2019 Code Rle Rpl
3/15
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
-
8/4/2019 Code Rle Rpl
4/15
-
8/4/2019 Code Rle Rpl
5/15
irc(n1)= drc(pres,n1);
end
if ipit==1
dpit(pres) = dpit(past);
endif 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);
endend
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
-
8/4/2019 Code Rle Rpl
6/15
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(i215)
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);
-
8/4/2019 Code Rle Rpl
7/15
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); %16,384 = 2^14
end
function [ipitch,irms,irc]=encode1(pitcha,rms,rc)
% Parameters Encoding
% Quantize LPC parameters for transmission
% INPUTS:% VOICE - Half frame voicing decisions
% PITCH - Pitch
% RMS - Energy
% RC - Reflection coefficients
% CORRP - Error Correction: TRUE = yes, FALSE = none
% OUTPUTS:
% IPITCH - Coded pitch and voicing
% IRMS - Quantized energy
% IRC - Quantized reflection coefficients
enctab=[0,7,11,12,13,10,6,1,14,9,5,2,3,4,8,15];
entau=[19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,42,43,41,45,37,53,49,51,50,
54,52,60,56,58,26,90,88,92,84,86,82,83,81,85,60,77,73,75,74,78,70,71,67,99,97,113,
112,114,98,106,104,108,100,101,76];
enadd=[1920,-768,2432,1280,3584,1536,2816,-1152];
enscl=[0.0204,0.0167,0.0145,0.0147,0.0143,0.0135,0.0125,0.0112];
enbits=[6,5,4,4,4,4,3,3];
entab6=[0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,
5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,9,10,10,11,11,12,13,14,15];rmst=[1024,936,856,784,718,656,600,550,502,460,420,384,352,328,294,270,24
-
8/4/2019 Code Rle Rpl
8/15
6,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];
% Scale RMS and RC's to integers
rcn=length(rc);
irms=fix(rms);for n1=1:rcn
irc(n1)=fix(rc(n1)*32768);
end
% Unvoice, voice and pitch period encoding:
% Unvoice is 1and it will change to 0 after encoding.
% Voice is the value of the pitch periodusing Gray code to encode.
if pitcha~=1
ipitch=pitcha;
else
ipitch=0;
end
% RMS encoding
n2=32;
idel=16;
irms=min(irms,1023);
while idel>0
if irms>rmst(n2)n2=n2-idel;
end
if irmsrmst(n2)
n2=n2-1;
end
irms=fix(31-n2*0.5);
% Reflection coefficient encoding
% Use RC(1),RC(2) as an area of logarithm to encode
for n1=1:2
i2=irc(n1);
mrk=0;
if i2
-
8/4/2019 Code Rle Rpl
9/15
end
i2=fix(bitshift(i2,-9));
i2=min(i2,63);
i2=entab6(i2+1);
if mrk~=0;i2=-i2;
end
irc(n1)=i2;
end
% RC(3)-RC(10) linear encoding
for n1=3:rcn
i2=bitshift(irc(n1),-1);
if enadd(rcn-n1+1)
-
8/4/2019 Code Rle Rpl
10/15
% replacing RC(5) - RC(10).
if length(ipitch)==0|length(ipitch)==127
irc(5)=enctab(bitshift(bitand(irc(1),30),-1));
irc(6)=enctab(bitshift(bitand(irc(2),30),-1));
irc(7)=enctab(bitshift(bitand(irc(3),30),-1));irc(8)=enctab(bitshift(bitand(irms,30),-1));
irc(9)=bitshift(enctab(bitshift(bitand(irc(4),30),-1)),-1);
irc(10)=enctab(bitshift(bitand(irc(4),30),-1));
end
function [a,e]=lpc(x,N);
%LPC Linear Predictor Coefficients.
% A = LPC(X,N) finds the coefficients, A=[ 1 A(2) ... A(N+1) ],
% of an Nth order forward linear predictor
%
% Xp(n) = -A(2)*X(n-1) - A(3)*X(n-2) - ... - A(N+1)*X(n-N)
%
% such that the sum of the squares of the errors
%
% err(n) = X(n) - Xp(n)
%
% is minimized. X can be a vector or a matrix. If X is a matrix
% containing a separate signal in each column, LPC returns a model% estimate for each column in the rows of A. N specifies the order
% of the polynomial A(z).
%
% If you do not specify a value for N, LPC uses a default N = length(X)-1.
%
% [A,E] = LPC(X,N) returns the variance (power) of the prediction error.
%
% LPC uses the Levinson-Durbin recursion to solve the normal equations
% that arise from the least-squares formulation. This computation
% of the linear prediction coefficients is often referred to as the
% autocorrelation method.
%
% See also LEVINSON, ARYULE, PRONY, STMCB.
% Author(s): T. Krauss, 9-21-93
% Modified: T. Bryan 11-14-97
% Copyright 1988-2002 The MathWorks, Inc.
% $Revision: 1.11 $ $Date: 2002/03/28 17:28:47 $
error(nargchk(1,2,nargin))
-
8/4/2019 Code Rle Rpl
11/15
[m,n] = size(x);
if (n>1)&(m==1)
x = x(:);
[m,n] = size(x);end
if nargin < 2, N = m-1; end
if (N > m-1),
% disp('Warning: zero-padding short input sequence')
x(N+1,:)=zeros(1,n);
end
% Compute autocorrelation vector or matrix
X = fft(x,2^nextpow2(2*size(x,1)-1));
R = ifft(abs(X).^2);
R = R./(m-1); % Biased autocorrelation estimate
[a,e] = levinson(R,N);
%-----------------------------------------------------------------
% FINAL PROJECT%
% Digital Signal Processing of Audio Signals for Speech Coding
%
%
% Date: April 2004
%-----------------------------------------------------------------
% Original Speech Analysis
[wavebuf,fs,nbits]=wavread('speech'); % read speech file
wavplay(wavebuf,fs) % produce speech
plot(wavebuf); % plot waveform
lenth=length(wavebuf); % time domain analysis
t=(0:lenth-1)/fs;
figure,
plot(t,wavebuf); % produce waveform with correct scale in
time domain
title('time domain analysis');
xlabel('time (s)');ylabel('amplitude');
-
8/4/2019 Code Rle Rpl
12/15
a=fft(wavebuf,2*lenth); % frequency domain analysis
b=20*log10(abs(a));
f=(0:lenth-1)/(lenth-1)*(fs/2);
figure,plot(f,b(1:lenth)); % produce waveform with correct scale in
frequency domain
title('frequency domain analysis');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');
w=kaiser(256,5); % Mixed time-frequency domain analysis
figure,
specgram(wavebuf,512,fs,w,220); % produce the specgram
% Pitch Detect
pitch=pitchesdetect(wavebuf,fs);
% Linear Predictive System (10 orders)
pitchk=length(pitch);
for k1=1:pitchk
x=wavebuf((k1-1)*300+1:k1*300+600);
x=filter(1,[1 1/2 1/3 1/4 1/5, 1/6 1/7 1/8 1/9 1/10],x); %filter
rms=0; % calculate root mean squared
for k2=1:130
temp=1-0.9375*(x(k2+385+1))^(-1);
rms=rms+(temp^2);
end
rmsn(k1)=(rms/130)^(1/2);
lpcn(k1,1:11)=lpc(x,10); % linear predictive coefficients
end
lpcn=real(lpcn);
% Parameters encoding
pitchk=length(pitch);
for k1=1:pitchk
[kk,r0]=ac2rc(lpcn(k1,1:11));
pitcha=pitch(k1);
[ipitch,irms,irc]=encode1(pitcha,rms,kk);
% Parameters decodingrcn=length(kk);
-
8/4/2019 Code Rle Rpl
13/15
[voice,pitcha,rms,kk]=decode1(ipitch,irms,irc,rcn);
pitch(k1)=pitcha;
lpca=rc2ac(kk,r0);
lpcn(k1,1:11)=lpca';
rms(k1)=rms;end
waveoutbuf=filter(1,[1 1/2 1/3 1/4 1/5, 1/6 1/7 1/8 1/9 1/10],wavebuf); % filter
wavwrite(waveoutbuf,fs,nbits,'newspeech');
[wavebuf,fs,nbits]=wavread('newspeech'); % read speech file
wavplay(wavebuf); % produce speech
plot(wavebuf); % plot waveform
function [R0,V,X]=MiddleShap(y,lenth2,Max);
% Function: MiddleShap
% This subprogram is used to middleshap.
RO=0;
for i1=1:lenth2
if abs(y(i1))0
V(i1)=1;
X(i1)=y(i1)-Max;
RO=RO+X(i1);
else
V(i1)=-1;
X(i1)=y(i1)+Max;
RO=RO-X(i1);
end
end
function y=myFilter(wavebuf,i1,lngth)
% Filter Function Design
% This subprogram is to design the filter function.
% wavebuf: sample point of speech
-
8/4/2019 Code Rle Rpl
14/15
y=zeros(1,900); % produce a matrix
H=[0.0035270585,-0.0075853243,-0.022130724,-0.037701912,-0.040792551,-
0.017618544,0.037134223,0.1139423,0.18955371,0.23657782,0.23657782,0.189537
1,0.1139423,0.037134223,-0.017618544,-0.040792551,-0.037701912,-0.022130724,-
0.0075853243,0.0035270585]; % filter parametersfor ii1=10:(lngth-11)
for j1=1:20
y(ii1)=y(ii1)+wavebuf(ii1+i1-j1+11)*H(j1); % filter
end
end
function pitchgray=pitchcode(pitcha)
% Gray code
% This program is for Gray code.
for aa=1:20
pitchm(aa)=aa+19;
end
for aa=21:40
pitchm(aa)=(aa-1)*2;
end
for aa=41:60pitchm(aa)=(aa-41)*4+80;
end
graym=[19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,42,43,41,45,37,53,49,51,5
0,54,52,60,56,58,26,90,88,92,84,86,82,83,81,85,69,77,73,75,74,78,70,71,67,99,97,11
3,112,114,98,106,104,108,100,101,76];
for aa=1:60
if pitcha==pitchm(aa);
pitchgray=graym(aa);
else pitchgray=0;
end
end
function n=PitchDetect(y)
% Function: PitchDetect
% This subprogram is also for detecting the pitch.% y: sample point of speech
-
8/4/2019 Code Rle Rpl
15/15
% Determined the threshold of middleshap
Maxhead=max(abs(y(1:300)));
Maxtail=max(abs(y(301:900)));
Maxy=0.68*min(Maxhead,Maxtail);
% Autocorrelate function
R=AutoCorrelate(y,900,Maxy);
[Maxy,n]=max(R(20:880)); % calculate the maximum and subscription of
array
n=n+20;
if Maxy