标签:简介 编解码 matlab audio sum slot water fun loading
基于matlab PCM编解码
clear all;
close all;
[x,fs]= audioread(‘C6_1_y.wav‘);
v=1;
xx=x/v;
sxx=floor(xx*4096);
y=pcm_encode(sxx);
yy=pcm_decode(y,v)‘;
nq=sum((x-yy).*(x-yy))/length(x);
sq=mean(yy.^2);
snr=(sq/nq);
t=(1:length(x))/fs;
subplot(211)
plot(t,x/max(abs(x)))
axis tight
title(‘(a)编码前语音‘)
xlabel(‘时间/s‘)
ylabel(‘幅度‘)
subplot(212)
plot(t,yy/max(abs(yy)))
axis tight
title(‘(b)解码后语音‘)
xlabel(‘时间/s‘)
ylabel(‘幅度‘)
%PCM解码函数
function[out]=pcm_decode(ins,v)
n=length(ins); %输入为8位PCM采样信号
in=reshape(ins‘,8,n/8)‘; %调整矩阵行列数
slot(1)=0; %量化幅值
slot(2)=32;
slot(3)=64;
slot(4)=128;
slot(5)=256;
slot(6)=512;
slot(7)=1024;
slot(8)=2048;
step(1)=2; %步长
step(2)=2;
step(3)=4;
step(4)=8;
step(5)=16;
step(6)=32;
step(7)=64;
step(8)=128;
function[out]=pcm_encode(x)
n=length(x); %-4096<x<4096
for i=1:n
if x(i)>0
out(i,1)=1; %根据符号输出第1位量化结果
else
out(i,1)=0;
end
if abs(x(i))>=0 & abs(x(i))<32 %根据输入范围输出后2-4位
out(i,2)=0; out(i,3)=0; out(i,4)=0; step=2;st=0;
elseif 32<=abs(x(i)) & abs(x(i))<64
out(i,2)=0; out(i,3)=0; out(i,4)=1; step=2;st=32;
elseif 64<=abs(x(i)) & abs(x(i))<128
out(i,2)=0; out(i,3)=1; out(i,4)=0; step=4;st=64;
elseif 128<=abs(x(i)) & abs(x(i))<256
out(i,2)=0; out(i,3)=1; out(i,4)=1; step=8;st=128;
elseif 256<=abs(x(i)) & abs(x(i))<512
out(i,2)=1; out(i,3)=0; out(i,4)=0; step=16;st=256;
elseif 512<=abs(x(i)) & abs(x(i))<1024
out(i,2)=1; out(i,3)=0; out(i,4)=1; step=32;st=512;
elseif 1024<=abs(x(i)) & abs(x(i))<2048
out(i,2)=1; out(i,3)=1; out(i,4)=0; step=64;st=1024;
elseif 2048<=abs(x(i)) & abs(x(i))<4096
out(i,2)=1; out(i,3)=1; out(i,4)=1; step=128;st=2048;
else
out(i,2)=1; out(i,3)=1; out(i,4)=1; step=128;st=2048;
end
if(abs(x(i))>=4096) %超出最大幅值的量化结果
out(i,2:8)=[1 1 1 1 1 1 1];
制字符串
out(i,5:8)=t(1:4);
end
版本:2014a
完整代码或代写加1564658423
【语音编码】基于matlab PCM编解码【含Matlab源码 555期】
标签:简介 编解码 matlab audio sum slot water fun loading
原文地址:https://www.cnblogs.com/homeofmatlab/p/14941309.html