📄 cba1enco.m
字号:
function code = CBA1enco(seq)
% Check the incoming orientation and adjust if necessary
[row_s, col_s] = size(seq);
if (row_s > 1),
seq = seq.';
end
counts = [1,1];
cum_counts = [0, 1,2];
total_count = 2;
context0 =[1 1];%00 01 de pinshu
cum_ct0 = [0,cumsum(context0)];
total_ct0 = cum_ct0(end);
context1 =[1 1];%10 11 de pinshu
cum_ct1 = [0,cumsum(context1)];
total_ct1 = cum_ct1(end);
N = 16;
dec_low = 0;
dec_up = 2^N-1;
E3_count = 0;
% Obtain an over estimate for the length of CODE and initialize CODE
code_len = length(seq) * ( ceil(log2(length(counts))) + 2 ) + N;
code = zeros(1, code_len);
code_index = 1;
% Loop for each symbol in SEQ
for k = 1:length(seq)
symbol = seq(k);
if k==1
% Compute the new lower bound
dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_counts(symbol+1-1)/total_count );
% Compute the new upper bound
dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_counts(symbol+1)/total_count )-1;
% Update the lower bound
dec_low = dec_low_new;
end
if k>1
pre = seq(k-1);
if pre ==1
dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_ct0(symbol+1-1)/total_ct0 );
dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_ct0(symbol+1)/total_ct0 )-1;
dec_low = dec_low_new;
context0(symbol) = context0(symbol)+1;
cum_ct0 = [0, cumsum(context0)];
total_ct0 = cum_ct0(end);
end
if pre ==2
dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_ct1(symbol+1-1)/total_ct1 );
dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_ct1(symbol+1)/total_ct1 )-1;
dec_low = dec_low_new;
context1(symbol) = context1(symbol)+1;
cum_ct1 = [0, cumsum(context1)];
total_ct1 = cum_ct1(end);
end
% dec_low_new = dec_low + floor( (dec_up-dec_low+1)*cum_counts(symbol+1-1)/total_count );
% dec_up = dec_low + floor( (dec_up-dec_low+1)*cum_counts(symbol+1)/total_count )-1;
% dec_low = dec_low_new;
end
% Check for E1, E2 or E3 conditions and keep looping as long as they occur.
while( isequal(bitget(dec_low, N), bitget(dec_up, N)) || ...
(isequal(bitget(dec_low, N-1), 1) && isequal(bitget(dec_up, N-1), 0) ) ),
% If it is an E1 or E2 condition,
if isequal(bitget(dec_low, N), bitget(dec_up, N)),
% Get the MSB
b = bitget(dec_low, N);
code(code_index) = b;
code_index = code_index + 1;
% Left shifts
dec_low = bitshift(dec_low, 1) + 0;
dec_up = bitshift(dec_up, 1) + 1;
% Check if E3_count is non-zero and transmit appropriate bits
if (E3_count > 0),
% Have to transmit complement of b, E3_count times.
code(code_index:code_index+E3_count-1) = bitcmp(b, 1).*ones(1, E3_count);
code_index = code_index + E3_count;
E3_count = 0;
end
% Reduce to N for next loop
dec_low = bitset(dec_low, N+1, 0);
dec_up = bitset(dec_up, N+1, 0);
% Else if it is an E3 condition
elseif ( (isequal(bitget(dec_low, N-1), 1) && ...
isequal(bitget(dec_up, N-1), 0) ) ),
% Left shifts
dec_low = bitshift(dec_low, 1) + 0;
dec_up = bitshift(dec_up, 1) + 1;
% Reduce to N for next loop
dec_low = bitset(dec_low, N+1, 0);
dec_up = bitset(dec_up, N+1, 0);
% Complement the new MSB of dec_low and dec_up
dec_low = bitxor(dec_low, 2^(N-1) );
dec_up = bitxor(dec_up, 2^(N-1) );
% Increment E3_count to keep track of number of times E3 condition is hit.
E3_count = E3_count+1;
end
end
% counts(symbol) = counts(symbol)+1;
% cum_counts = [0, cumsum(counts)];
%
% % Compute the Word Length required.
% total_count = cum_counts(end);
end
% Terminate encoding
bin_low = de2bi(dec_low, N, 'left-msb');
if E3_count==0,
% Just transmit the final value of the lower bound bin_low
code(code_index:code_index + N - 1) = bin_low;
code_index = code_index + N;
else
% Transmit the MSB of bin_low.
b = bin_low(1);
code(code_index) = b;
code_index = code_index + 1;
% Then transmit complement of b (MSB of bin_low), E3_count times.
code(code_index:code_index+E3_count-1) = bitcmp(b, 1).*ones(1, E3_count);
code_index = code_index + E3_count;
% Then transmit the remaining bits of bin_low
code(code_index:code_index+N-2) = bin_low(2:N);
code_index = code_index + N - 1;
end
% Output only the filled values
code = code(1:code_index-1);
% Set the same output orientation as seq
if (row_s > 1)
code = code.';
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -