code.m

来自「一个matlab写的golomb coder」· M 代码 · 共 64 行

M
64
字号
%a golomb coder write by linghan liu 2008
%first to encode the code to numbers count by nomuber of 0's
clear
a = input('code');
y=length(a);
count0=0;
count1=0;

z(1)=0;
c=2;
for i=1:y
    if (a(i)==0)
        count0=count0+1;
        count1=0;
    else
        count1=count1+1;

    end
    if (count1 ==1)&&(count0>0)
        z(c)=count0;
        c=c+1;
        count0=0;
    end
end
finalcode=[];
%the second part to encode the rise code
for d= 1:length(z);
    x=z(d);
    m=3;
    b=ceil(log2(m));
    q=floor((x-1)/m);
    r=rem(x,m);   %find out the q and the remander

    if q>=1
        for i=1:q

            v(i)=1;      %write q onesand 
        end
        v(i+1)=0;       %write one zero

    else
        v=0;
    end
    if r < 2b - m
        bits=b-1;
    else
        bits=b;
    end
    c=bits;
    for i=1:bits
        if r>=1
            k=rem(r,2);
            r=floor(r/2);
        else k=0;
        end
        re(c)=k;
        c=c-1;
    end
    encode =[v re];
    
    finalcode=[finalcode,encode];
end
finalcode1=num2str(finalcode);
finalcodes=strrep(finalcode1,' ','')

⌨️ 快捷键说明

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