📄 voicecoding.m
字号:
function voicecoding
[origin,fs]=wavread('D:\graduate courses\编码理论\原始数据.wav');
% x_sample=resample(x,4,11);
% y=single(x_sample');
% global f;
preemp = .9378;
x = filter([1 -preemp], 1, origin)'; % Preemphasize speech
f=enframe(x,1000);
[row,col]=size(f);
order=100;
coeff=zeros(row,order+1);
%calculate the coeffient and save it in 'code',the formal 10 columns are
%the lpc coefficients,while the last column represent the error.
for i=1:row
[result,E(i)]=lpc1(f(i,:),order);
coeff(i,:)=result;
end
wavwrite(coeff,'D:\graduate courses\编码理论\100阶编码数据.wav');
%reconstruct the original voice signal
for i=1:row
% f_aft(i,:)=reconstruct(code(i,:),col,f(i,:));
v=rand(1,col);
v=v*sqrt(E(i));
f_aft(i,:)=filter(1,coeff(i,:),v);
end
f_aft=real(f_aft);
% f_recon = filter(1, [1 -preemp], f_aft);
f_result=[];
for i=1:row
f_result=[f_result,f_aft(i,:)];
end
wavwrite(f_result,16000,'D:\graduate courses\编码理论\100阶解码数据.wav');
%save 'D:\graduate courses\编码理论\data';
h=figure(1);
subplot(2,1,1);
plot(origin);
title('原始数据');
grid;
subplot(2,1,2);
plot(f_result);
title('解码数据');
grid;
saveas(h,'D:\graduate courses\编码理论\100阶编码前后数据比较.jpg');
disp('Press a key to play the original sound!');
pause;
soundsc(origin, fs);
disp('Press a key to play the LPC reconstructed sound!');
pause;
soundsc(f_result, fs);
function [result,E]=lpc1(f,p)
% calulate the lpc coefficients and error energy
% r--self correlation ; E--error energy ; p--order ; a--lpc coeffient,
% k--reflection coefficient; maxlags--center autocorrelation ;
[row,maxlags]=size(f);
r=xcorr(f);
a=zeros(p,p);
a(1,1)=r(maxlags+1)/r(maxlags);
k(1)=a(1,1);
E(1)=(1-k(1)^2).*r(maxlags);
for j=2:p
k(j)=(r(maxlags+j)-sum(a(j-1,1:(j-1)).*r(maxlags+j-1:-1:maxlags+1)))/E(j-1);
a(j,j)=k(j);
a(j,1:j-1)=a(j-1,1:j-1)-k(j).*a(j-1,j-1:-1:1);
E(j)=(1-k(j)^2)*E(j-1);
end
result=[1,-a(p,:)];
E=abs(E(p));
% function f_aft=reconstruct(code,recon_num,f)
% %the coeffients and error are saved in 'code',which will be used to
% %reconstruct the original signal of 'recon_num ' numbers
% [row,col]=size(code);
% v=rand(1,recon_num);
% % f_aft(1)=sqrt(code(col))*v(1);
% for i=1:col-1
% f_aft(i)=f(i);
% % f_aft(i)=sum(code(1:i-1).*f_aft(i-1:-1:1))+sqrt(code(col))*v(i);
% end
% for i=col:recon_num
% f_aft(i)=sum(code(1:col-1).*f_aft(i-1:-1:i-col+1))+sqrt(code(col))*v(i);
% end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -