⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quanjima.txt

📁 卷积编码程序,Viterbi译码程序.很好啊
💻 TXT
字号:
卷积编码和Viterbi译码的Matlab程序

  

  卷积编码程序:

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);

  Viterbi译码程序:

function [decoder_output, survivor_state, cumulated_metric] = viterbi(channel_output, decodetext)
tic
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];
k = 1;

frr = fopen(channel_output, 'r');
[msg, len] = fread(frr, 'ubit1');
channel_output = msg';

n = size(G,1);

% check the sizes
if rem(size(G, 2), k) ~= 0
    error('channel_output not of the right size');
end

L = size(G, 2)/k;
number_of_states = 2^((L-1)*k);

% generate state transition matrix, output matrix, and input matrix
for j = 0:number_of_states - 1
    for i = 0:2^k-1
        [next_state, memory_contents] = nxt_stat(j,i, L, k);
        input(j+1, next_state + 1) = i;
        branch_output = rem(memory_contents*G', 2);
        nextstate(j+1, i+1) = next_state;
        output(j+1, i+1) = bin2deci(branch_output);
    end
end

% add the extra zero, ensure the length of channel_output is integral
% times to n.
if rem(len, n)>0
    channel_output = [channel_output, zeros(size(n-rem(len, n):-1:1))];
end

state_metric = zeros(number_of_states, 2);
depth_of_trellis = length(channel_output)/n;
channel_output_matrix = reshape(channel_output, n, depth_of_trellis);
survivor_state = zeros(number_of_states, depth_of_trellis + 1);

% start decoding of non-tail channel outputs
for i = 1:depth_of_trellis-L+1
    flag = zeros(1, number_of_states);
    if i <= L
        step = 2^((L-i)*k);
    else
        step = 1;
    end
    for j = 0:step:number_of_states - 1
        for l = 0:2^k - 1
            branch_metric = 0;
            binary_output = deci2bin(output(j+1, l+1), n);
            for r = 1:n
                branch_metric = branch_metric + metric(channel_output_matrix(r, i), binary_output(r));
            end
            if((state_metric(nextstate(j+1, l+1) + 1, 2)>state_metric(j+1,1)...
                    + branch_metric) | flag(nextstate(j+1, l+1) + 1) == 0)
                state_metric(nextstate(j+1,l+1)+1, 2) = state_metric(j+1, 1) + branch_metric;
                suvivor_state(nextstate(j+1,l+1) + 1, i+1) = j;
                flag(nextstate(j+1, l+1) + 1) = 1;
            end
        end
    end
    state_metric = state_metric(:, 2:-1:1);
end

% start decoding of the tail channel-outputs
for i = depth_of_trellis - L + 2:depth_of_trellis
    flag = zeros(1, number_of_states);
    last_stop = number_of_states/(2^((i - depth_of_trellis+L-2)*k));
    for j = 0:last_stop - 1
        branch_metric = 0;
        binary_output = deci2bin(output(j + 1, 1), n);
        for r = 1:n
                branch_metric = branch_metric + metric(channel_output_matrix(r, i), binary_output(r));
        end
        if((state_metric(nextstate(j+1, l+1) + 1, 2)>state_metric(j+1,1)...
                    + branch_metric) | flag(nextstate(j+1, 1) + 1) == 0)
                state_metric(nextstate(j+1,1)+1, 2) = state_metric(j+1, 1) + branch_metric;
                suvivor_state(nextstate(j+1,1) + 1, i+1) = j;
                flag(nextstate(j+1, 1) + 1) = 1;
        end
    end
    state_metric = state_metric(:, 2:-1:1);
end

% generate the decode output from the optimal path
state_sequence = zeros(1, depth_of_trellis + 1);
state_sequence(1, depth_of_trellis) = survivor_state(1, depth_of_trellis+1);
for i = 1:depth_of_trellis
    state_sequence(1, depth_of_trellis-i+1) = suvivor_state((state_sequence(1, depth_of_trellis+2-i)...
        +1), depth_of_trellis - i+2);
end

decoder_output_matrix = zeros(k, depth_of_trellis -L+1);
for i = 1:depth_of_trellis - L + 1
    dec_output_deci = input(state_sequence(1, i)+1, state_sequence(1, i+1)+1);
    dec_output_bin = deci2bin(dec_output_deci, k);
    decoder_output_matrix(:,i) = dec_output_bin(k:-1:1)';
end

decoder_output = reshape(decoder_output_matrix, 1, k*(depth_of_trellis-L+1));
cumulated_metric = state_metric(1, 1);

% write the output to the encodetext
result = fopen(decodetext, 'w');
for i = 1:k*(depth_of_trellis-L+1)
    fwrite(result, decoder_output(i), 'bit1');
end
fclose(result);
toc

%*************************************************************************%
function distance = metric(x,y)
if x == y
    distance = 0;
else
    distance = 1;
end

%************************************************************************%
function [next_state, memory_contents] = nxt_stat(current_state, input, L, k)
binary_state = deci2bin(current_state, k*(L-1));
binary_input = deci2bin(input, k);
next_state_binary = [binary_input, binary_state(1:(L-2)*k)];
next_state = bin2deci(next_state_binary);
memory_contents = [binary_input, binary_state];

%************************************************************************%
function y = bin2deci(x)
l = length(x);
y  = (l-1:-1:0);
y = 2.^y;
y = x*y';

%************************************************************************%
function y = deci2bin(x, L)
y = zeros(1,L);
i = 1;
while x >= 0 & i<= L
    y(i) = rem(x, 2);
    x = (x - y(i))/2;
    i = i + 1;
end
y = y(L:-1:1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -