发布网友 发布时间:2022-04-24 13:26
共1个回答
热心网友 时间:2022-05-07 18:20
matlab里有,
用c实现太复杂了,
我现有的一个程序,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 = 2;
% 读入文本文件并计算文件长度
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);