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

📄 turbo_encode.m

📁 It is turbo encoder and decoder qritten for communication module has viterbi
💻 M
字号:
function [seqout, alpha] = turbo_encode(seqin, len, key, puncture, gnum)
% [seqout, alpha] = turbo_encode(seqin, len, key [, puncture, gnum])
%
% Encode binary sequence using turbo codes.
%
% Input:
%   'seqin'   : input binary sequence, in {0,1} form; if set to the string "length" however,
%               the function just returns the codeword length as 'seqout'.
%   'len'     : length of sequence (input beeing padded or truncated).
%   'key'     : seed to use for pseudo-random interleaving during encoding.
%   'puncture': 1 to use the puncturer, giving rate 1/2; 0 otherwise, giving rate 1/3 (default).
%   'gnum'    : generator polynom, 1 (default), 2 or 3.
%
% Output:
%   'seqout'   : output sequence, in {-1,1} form;
%               note: {0,1} is mapped to {-1,1}.
%   'alpha'    : used pseudo-random interleaving.

if exist('key') ~= 1 | isempty(key)
    key = 1;
end

if exist('puncture') ~= 1 | isempty(puncture)
   puncture = 0;
end

if exist('gnum') ~= 1 | isempty(gnum)
   gnum = 1;
end

if gnum == 1
    g = [ 1 1 1; 1 0 1 ];
elseif gnum == 2
    g = [1 1 0 1; 1 1 1 1];
elseif gnum == 3
    g = [1 1 1 1 1; 1 0 0 0 1];
else
    error(sprintf('invalid generator polynom selection: %d, should be 1 to 3', gnum));
end

% Input sequence full length and sub-sequences length
%
full_len = len(1);
if length(len) > 1
    sub_len = len(2);
else
    sub_len = full_len;
end

full_len = max(full_len, 1);
if sub_len <= 0
    sub_len = full_len;
end
sub_len  = min(sub_len, full_len);

% Case where only the sequence length should be returned
%
if ischar(seqin) & strcmp(seqin, 'length')
    n_sub   = floor(full_len/sub_len);
    rem_len = full_len - n_sub*sub_len;
    if puncture
        seqout = n_sub*(2*sub_len + 4) + (rem_len > 0)*(2*rem_len + 4);
    else
        seqout = n_sub*(3*sub_len + 6) + (rem_len > 0)*(3*rem_len + 6);
    end
    alpha = [];
    return;
end

% Adjust input sequence length
%
seqin = seqin(:)';
if length(seqin) ~= full_len
    seqin = [seqin(1:min(full_len,length(seqin))) zeros(1, max(full_len-length(seqin),0))];
end

% Otherwise Turbo encode
%
old_state = rand('state');

if sub_len < full_len
    fprintf('turbo encoding .');
    nl = 0;
end

[n, K] = size(g);
m      = K - 1;
% rate = 1/(2+(~puncture));

seqout = [];
alpha  = [];
for p = 1:sub_len:full_len
    currlen = min(full_len-p+1, sub_len);
    if p == 1 | currlen < sub_len
        L_total = currlen + m;
    end
    
    rand('state', key);
    [temp, al] = sort(rand(1,L_total)); % random interleaver
    seqout = [seqout encoderm(seqin(p:p+currlen-1), g, al, ~puncture)]; % encoder output {+1, -1}
    alpha  = [alpha; [al zeros(1,size(alpha,2)-length(al))]];
    
    if sub_len < full_len
        fprintf('.');
        if nl >= 80
            fprintf('\n');
            nl = 0;
        else
            nl = nl + 1;
        end
    end
end

if sub_len < full_len
    fprintf('\n');
end

rand('state', old_state);

⌨️ 快捷键说明

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