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

📄 entropy_decode.m

📁 分层树分割算法的Matlab实现
💻 M
字号:
function [stream_out,iii]=entropy_decode(stream_in,pass)

clear global
format long
global no_of_chars no_of_symbols cum_freq top_value code_value_bits...
    first_qtr half third_qtr max_frequency stream_in iii freq
% -----------------------------------------------------------------------------

if pass==0
    pedestral=7;
    symbols=['ripn!'];
    no_of_chars=5;
elseif pass==1
    pedestral=5;
    symbols=['01!'];
    no_of_chars=3;
end
% -----------------------------------------------------------------------------
iii=0;
code_value_bits=16;
no_of_symbols=no_of_chars+1;
max_frequency=256;
top_value=2^code_value_bits-1;
first_qtr=fix(top_value / 4) +1;
half=fix(2*first_qtr);
third_qtr=fix(3*first_qtr);
% -------------------------------------------------------------------------------
start_model_adaptive;
start_decoding;
while 1
    symbol=decode_symbol(cum_freq);
    if((pedestral-symbol)==pedestral-2),break;end
    stream_out=[stream_out symbols(pedestral-symbol)];
    update_model_adaptive(pedestral-symbol);
end

% ------------------------------------------------------------------------------

function start_model_adaptive

format long

global no_of_chars cum_freq no_of_symbols freq

for ii=1:no_of_chars
    freq(ii)=1;
end

freq=freq(2:end);
freq=[freq 1];

cum_freq(1)=0;
for ii=1:no_of_symbols-2
    cum_freq(ii+1)=cum_freq(ii)+freq(ii);
end

cum_freq(ii+2)=sum(freq);

% reverse the cum_freq  % This logic has to be change later

for ii=1:length(cum_freq)
    cumfreq(length(cum_freq)-ii+1)=cum_freq(ii);
end
cum_freq=cumfreq;

%------------------------------------------------------------------------------------

function start_decoding

format long

global top_value high low code_value_bits value stream_in

value=0;
for ii=1:code_value_bits
    value=2*value+stream_in(ii);
end
low=0;
high=top_value;

%------------------------------------------------------------------------------------

function symbol=decode_symbol(cum_freq)

global high low half first_qtr third_qtr value

range=fix(high-low+1);
cum=fix((((value-low)+1)*cum_freq(1)-1)/range);
symbol=1;
while cum_freq(symbol)>cum
    symbol=symbol+1;  
end
xy=cum_freq(symbol-1);
yx=cum_freq(symbol);
high=low+fix(((range*xy)/cum_freq(1)))-1;
low=low+fix(((range*yx)/cum_freq(1)));
while(1~=0)
    if(high<half)
        % Nothing to be done        
    elseif (low>=half)
        value=value-half;
        low=low-half;
        high=high-half;
    elseif (low>=first_qtr & high<third_qtr)
        value=value-first_qtr;
        low=low-first_qtr;
        high=high-first_qtr;
    else
        break;
    end
    low=fix(2*low);
    high=fix(2*high)+1;
    h=input_bit;
    value=2*value+h;
end   


% -----------------------------------------------------------------------------------------

function update_model_adaptive(symbol)

format long

global cum_freq no_of_symbols freq max_frequency

if(cum_freq(1)==max_frequency)
    cum=0;
    for ii=no_of_symbols-1:-1:1
        freq(ii)=fix((freq(ii)+1)/2);
        cum_freq(ii)=cum;
        cum=cum+freq(ii);
    end
end

freq(symbol)=freq(symbol)+1;

% Now find cumulative frequency
cum_freq(1)=0;
for ii=1:no_of_symbols-2
    cum_freq(ii+1)=cum_freq(ii)+freq(ii);
end

cum_freq(ii+2)=sum(freq);

% reverse the cum_freq  % This logic has to be change later

for ii=1:length(cum_freq)
    cumfreq(length(cum_freq)-ii+1)=cum_freq(ii);
end
cum_freq=cumfreq;

% -----------------------------------------------------------------------------

function t=input_bit

format long
global code_value_bits stream_in iii
if(iii==0),iii=code_value_bits;end
iii=iii+1;
if (iii>length(stream_in)),t=0;return;end % random bit , a check is NOT performed for exceed of limit
t=stream_in(iii);

⌨️ 快捷键说明

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