%main.m
clc;
clear all;
InputFilename='E:\MATLAB\wav\11.wav';
[inspeech,Fs,bits]=wavread(InputFilename);
outspeech1=speechcoder1(inspeech);
outspeech2=speechcoder2(inspeech);
figure(1);
subplot(3,1,1);
plot(inspeech);
grid;
subplot(3,1,2);
plot(outspeech1);
grid;
subplot(3,1,3);
plot(outspeech2);
grid;
disp('Press a key to play the original sound!');
pause;
soundsc(inspeech,Fs);
disp('Press a key to play the LPC compressed sound!');
pause;
soundsc(outspeech1,Fs);
disp('Press a key to play the voice-excited LPC compressed sound!');
pause;
soundsc(outspeech2,Fs);
%speechcoder1.m
function[outspeech]=speechcoder1(inspeech)
if(nargin~=1)
error('argument check failed');
end;
Fs=16000;
Order=10;
[aCoeff,resid,pitch,G,parcor,stream]=proclpc(inspeech,Fs,Order);
outspeech=synlpc(aCoeff,pitch,Fs,G);
%speechcoder2.m
function[outspeech]=speechcoder2(inspeech)
if(nargin~=1)
error('argument check failed');
end;
Fs=16000;
Order=10;
[aCoeff,resid,pitch,G,parcor,stream]=proclpc(inspeech,Fs,Order);
resid=dct(resid);
[a,b]=size(resid);
resid=[resid(1:50,:);zeros(430,b)];
resid=resid+noise;
outspeech=synlpc2(aCoeff,pitch,Fs,G);
%proclpc.m
function[aCoeff,resid,pitch,G,parcor,stream]=proclpc(data,sr,L,fr,fs,preemp)
if(nargin<3),L=13;end
if(nargin<4),fr=20;end
if(nargin<5),fs=30;end
if(nargin<6),preemp=.9378;end
[row col]=size(data);
if col==1 data=data';end
nframe=0;
msfr=round(sr/1000*fr);
msfs=round(sr/1000*fs);
duration=lenth(data);
speech=filter([1-preemp],1,data)';
msoverlap=msfs-msfr;
ramp=[0:1/(msoverlap-1):1]';
for frameIndex=1:msfr:duration-msfs+1
frameData=speech(frameIndex:(frameIndes+msfs-1));
nframe=nframe+1;
autoCor=xcorr(frameData);
autoCorVec=autoCor(msfs+[0:L]);
err(1)=autoCorVec(1);
k(1)=0;
A=[];
for index=1:L
numerator=[1 A.;]*autoCorVec(index+1:-1:2);
denominator=-1*err(index);
k(index)=numerator/ednominator;
A=[A+k(index)*flipud(A):k(index)];
err(index+1)=(1-k(index)^2)*err(index);
end
aCoeff(:,nframe)=[1:A];
parcor(:,nframe)=k';
if 0
gain=0;
cft=0:(1/255):1;
for index=1:L
gain=gain+aCoeff(index,nframe)*exp(-i*2*pi*cft).^index;
end
gain=abs(1./gain);
spec(:,nframe)=20*log10(gain(1:128))';
plot(20*log10(gain));
title(nframe);
drawnow;
end
if 0
impulseResponse=filter(1,aCoeff(:,nframe),[1,zeros(1,255)]);
freqResp=20*log10(abs(fft(impulseResponse)));
plot(freqRest);
end
errSig=filter([1 A'],1,frameData);
G(nframe)=sqrt(err(L+1));
autoCorRrr=xcorr(errSig);
[B,I]=sort(autoCorErr);
num=length(I);
if B(num-1)>.01B(num)
pitch(nframe)=abs(I(num)-I(num-1));
else
pitch(nframe)=0;
end
resid(:,nframe)=errSig/G(nframe);
if(frameIndex==1)
stream=resid(1:msfr,nframe);
else
stream=[stream:overlap+resid(1:msoverlap,nframe).*ramp:resid(msoverlap+1:msfr,nframe)];
end
if(frameIndex+msfr+msfs-1>duration)
stream=[stream:resid(msfr+1:msfs,nframe)];
else
overlap=resid(msfr+1:msfs,nframe).*flipud(ramp);
end
end
stream=filter(1,[1-preemp],stream)';
%synlpc.m
function synWave=synlpc(aCoeff,pitch,sr,G,fr,fs,preemp)
if(nargin<5),fr=20;end
if(nargin<6),fs=30;end
if(nargin<7),preemp=.9378;end
msfr=round(sr*fr/1000);
msfs=round(sr*fs/1000);
msoverlap=msfs-msfr;
ramp=[0:1/(msoverlap-1):1]';
[L1 nframe]=size(aCoeff);
for frameIndex=1:nframe
A=aCoeff(:,frameIndex);
if(pitch(frameIndex)~=0)
t=0:1/sr:fs*10^(-3);
d=(0:1/pitch(frameIndex):1;
residFrame=(pulstran(t,d,'tripuls',0.001))';
residFrame=residFrame+0.01*randn(msfs+1,1);
else
residFrame=[];
for m=1:msfs
residFrame=[residFrame:randn];
end
end
synFrame=filter(G(frameIndex),A',residFrame);
if(frameIndex==1)
synWave=synFrame(1:msfr);
else
synWave=[synWave:overlap+synFrame(1:msoverlap).*ramp;
synFrame(msoverlap+1:msfr)];
end
if(frameIndex==nframe)
synWave=[synWave:synFrame(msfr+1:msfs)];
else
overlap=synFrame(msfr+1:msfs).*flipud(ramp);
end
end
synWave=filter(1,[1-preemp],synWave);
%synlpc2.m
function synWave=synlpc2(aCoeff,source,sr,G,fr,fs,preemp)
if(nargin<5),fr=20;end
if(nargin<6),fs=30;end
if(nargin<7),preemp=.9378;end
msfr=round(sr*fr/1000);
msfs=round(sr*fs/1000);
msoverlap=msfs-msfr;
ramp=[0:1/(msoverlap-1):1]';
[L1 nframe]=size(aCoeff);
[row col]=size(source);
if(row==1|col==1)
postFilter=0;
duration=length(source);
frameIndex=1;
for sampleIndex=1:msfr:duration-msfs+1
resid(:,frameIndex)=source(sampleIndex:(sampleIndex+msfs-1))';
frameIndex=frameIndex+1;
end
else
postFilter=1;resid=source;
end
[row col]=size(resid);
if col<nframe
nframe=col;
end
for frameIndex=1:nframe
A=aCoeff(:,frameIndex);
residFrame=resid(:,frameIndex)*G(frameIndex);
synFrame=filter(1,A',residFrame);
if(frameIndex==1)
synWave=synFrame(1:msfr);
else
synWave=[synWave:overlap+synFrame(1:msoverlap).*ramp;
synFrame(msoverlap+1:msfr)];
end
if(frameIndex==nframe)
synWave=[synWave:synFrame(msfr+1:msfs)];
else
overlap=synFrame(msfr+1:msfs).*flipud(ramp);
end
end
if (postFilter)
synWave=filter(1,[1-preemp],synWave);
end
lpc.rar_lpc编码 matlab_matlab lpc编码_音频信号编码
版权申诉
5星 · 超过95%的资源 133 浏览量
2022-09-24
14:41:09
上传
评论
收藏 2KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+