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

📄 entropy_code.m

📁 SPIHT的一个Matlab程序
💻 M
字号:
function stream_out=entropy_code(stream_in,pass)

% stream_in : stream of input symbols
% stream_out: stream of bits
% pass : 0  : Dominant pass
%        1  : Subordinate pass 

clear global
format long
global no_of_chars no_of_symbols cum_freq low high top_value code_value_bits...
    bits_to_follow first_qtr half third_qtr max_freq freq symbols stream_out

stream_in=[stream_in '!']; % End of stream , a bad logic
if pass==0
    pedestral=7;
    symbols=['ripn!'];
    no_of_chars=5;
elseif pass==1
    pedestral=5;
    symbols=['01!'];
    no_of_chars=3;
end
% ---------------------------------------------------------------------------------------

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);
low=0;
high=top_value;
bits_to_follow=0;
% ---------------------------------------------------------------------------------------
start_model_adaptive;
index=1;

while stream_in(index)~='!' 
    symbol=find(symbols==stream_in(index));
    encode_symbol(pedestral-symbol,cum_freq);
    update_model_adaptive(symbol);    
    index=index+1;
end

encode_symbol(2,cum_freq);
done_encoding;

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

function start_model_adaptive

format long

global no_of_chars cum_freq no_of_symbols freq stream_out

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, bad programming

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

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

function encode_symbol(symbol,cum_freq)

global top_value first_qtr half third_qtr high low bits_to_follow stream_out

format long ;

range=fix(high-low)+1;
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
    if(high<half)
        bit_plus_follow(0);
    elseif (low>=half)
        bit_plus_follow(1);
        low=low-half;
        high=high-half;
    elseif (low>=first_qtr & high<third_qtr)
        bits_to_follow=bits_to_follow+1;
        low=low-first_qtr;
        high=high-first_qtr;
    else
        break;
    end
    low=fix(2*low);
    high=fix(2*high)+1;    
end

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

function update_model_adaptive(symbol)

format long

global cum_freq no_of_symbols freq
max_frequency=256;
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, bad programming

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

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

function bit_plus_follow(bit)
format long
global bits_to_follow stream_out

stream_out=[stream_out bit];

while(bits_to_follow>0)
    stream_out=[stream_out ~bit];
    bits_to_follow=bits_to_follow-1;
end

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

function done_encoding

format long
global bits_to_follow low first_qtr

bits_to_follow=bits_to_follow+1;
if(low<first_qtr),bit_plus_follow(0);
else bit_plus_follow(1);
end

⌨️ 快捷键说明

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