卷积码matlab程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
卷积编码程序:
function [output, len_tal] = cnv_encd(secrettext, encodetext)
g = [0 0 1 0 0 1 0 0; 0 0 0 0 0 0 0 1; 1 0 0 0 0 0 0 1; 0 1 0 0 1 1 0 1]; k0 = 1;
% 读入文本文件并计算文件长度
frr = fopen(secrettext, 'r');
[msg, len] = fread(frr, 'ubit1');
msg = msg';
% check to see if extra zero padding is necessary
if rem(length(msg), k0) > 0
msg = [msg, zeros(size(1:k0-rem(length(msg),k0)))];
end
n = length(msg)/k0; % 把输入比特按k0分组,n为所得的组数。
% check the size of matrix g
if rem(size(g, 2), k0) > 0
error('Error, g is not of the right size.');
end
% determine L and n0
L = size(g, 2)/k0;
n0 = size(g, 1);
% add extra zeros,以保证编码器是从全0开始,并回到全0状态。
u = [zeros(size(1:(L-1)*k0)), msg, zeros(size(1:(L-1)*k0))];
% generate uu, a matrix whose columns are the contents of conv. encoder at
% various clock cycles.
u1 = u(L*k0: -1 :1);
for i = 1:n+L-2
u1 = [u1, u((i+L)*k0:-1:i*k0+1)];
end
uu = reshape(u1, L*k0, n+L-1);
% determine the output
output = reshape(rem(g*uu, 2), 1, n0*(L+n-1));
len_tal = n0*(L + n - 1);
% write the output to the encodetext result = fopen(encodetext, 'w');
for i = 1:n0*(L+n -1)
fwrite(result, output(i), 'bit1'); end
fclose(result);