求救~~MATLAB实现语音信号量化
- 提问者网友:欺烟
- 2021-05-22 00:18
- 五星知识达人网友:woshuo
- 2021-05-22 01:40
以前做个这个
二维矢量量化的
给你一个
%文件名VQ.m
%采用K法算法
%[码,失真]=VQ(原始信源,维数,码率,理想失真)
function [codebook,distortion]=VQ(training_seq,dimension,codebook_size,tolerance)
if (nargin==3)
tolerance=0.001;
end
m=round(length(training_seq)/dimension);
if (m*dimension-length(training_seq)<0)
error('')
end
%编码初始化
initial=training_seq(1:dimension*codebook_size);
initialcodebook=(reshape(initial,dimension,codebook_size))';
updated_codebook=initialcodebook;
%第一次训练矢量
newdistortion=0;
distortion=0;
for i=1:m;
training_seq_block=training_seq((i-1)*dimension+1:i*dimension);
training_matrix(i,:)=training_seq_block;
distortion_block=[ ];
for j=1:codebook_size;
distort=sum((training_seq_block-updated_codebook(j,:)).^2);
distortion_block=[distortion_block distort];
end
[distortion_min,ind]=min(distortion_block);
newdistortion=newdistortion+distortion_min;
index(i)=ind;
end
for l=1:codebook_size;
partition=(index==l);
if sum(partition)>0
updated_codebook(l,:)=partition*training_matrix/sum(partition);
end
end
newdistortion=newdistortion/m;
%继续训练矢量,直到失真没有显著变化
while(abs(distortion-newdistortion)/newdistortion>tolerance)
distortion=newdistortion;
newdistortion=0;
for i=1:m;
training_seq_block=training_seq((i-1)*dimension+1:i*dimension);
training_matrix(i,:)=training_seq_block;
distortion_block=[ ];
for j=1:codebook_size;
distort=sum((training_seq_block-updated_codebook(j,:)).^2);
distortion_block=[distortion_block distort];
end
[distortion_min,ind]=min(distortion_block);
newdistortion=newdistortion+distortion_min;
index(i)=ind;
end
for l=1:codebook_size;
partition=(index==l);
if sum(partition)>0
updated_codebook(l,:)=partition*training_matrix/sum(partition);
end
end
newdistortion=newdistortion/m;
end
codebook=updated_codebook;
distortion=newdistortion/dimension;
附上效果图: