%% 提取数据并做数值转换
clear all
% 读取wav文件
[y, fs] = audioread('audio.wav');
Y=y*10000;
% 将语音数据转换为二进制序列
xvlie = reshape(dec2bin(typecast(int16(Y(:)), 'uint16'))', 1, []) - '0';
bitcode=xvlie';
%% 通信信道模型部分
%数据初步处理完成,编码数据初始化
code=reshape(bitcode,[],4);%信息序列
ecode=hamming74_encode(code);%汉明编码完成
e=rand2(ecode);%噪音 rand1是每行增加0-1为错误,rand2是每行增加2或更多个错误,可以改函数参数
ercode=ecode+e;%噪声添加完成
ercode=mod(ercode,2);
%ecode=mod(ecode,2);
%解码开始
S=hamming74_decode(ercode);%计算伴随式
E=check(S);%根据伴随式找到错误图案
ER=ercode+E;
% S=hamming74_decode(ecode);%计算伴随式
% E=check(S);%根据伴随式找到错误图案
% ER=ecode+E;
ER=mod(ER,2);
result=ER(:, 1:4);
%% 解码恢复部分
% 将二进制序列转换回语音数据
numSamples = numel(result);
yout = zeros(numSamples/16, 1);
for i = 1:numSamples/16
startIndex = (i - 1) * 16 + 1;
endIndex = i * 16;
yout(i) = typecast(uint16(bin2dec(char(result(startIndex:endIndex) + '0'))), 'int16');
end
yout=yout/10000;
yout=reshape(yout,[],2);
% 将语音数据写入wav文件
%audiowrite('output.wav', yOut, fs);
% sound(yout, fs);
subplot(211);
plot(y,'b');
title('原始数据');
subplot(212);
plot(yout,'r');
title('经过信道后的数据');