Matlab:关于在示例

时间:2016-05-30 17:50:36

标签: matlab signal-processing entropy

Tent Map Prediction

图1。假设情节。 y轴:平均熵。 x轴:比特。

此问题继续提到前一个问Matlab : Plot of entropy vs digitized code length

我想计算随机变量的熵,该随机变量是连续随机变量x的离散化版本(0/1)。随机变量表示称为Tent Map的非线性动力系统的状态。 Tent Map的迭代产生一个长度为N的时间序列。

一旦离散时间序列的熵变得等于动力系统的熵,代码就应该退出。理论上已知系统的熵H is log_e(2) or ln(2) = 0.69约。代码的目标是找到产生与系统熵相同的熵所需的迭代次数jH

问题1:当我计算二进制时间序列的熵时,我的问题是信息消息,那么我应该在与H相同的基础上进行吗?或者我应该将H的值转换为位,因为信息消息是在0/1?两者都给出不同的结果,即j的不同值。

问题2:可能会发生0或1的概率可能变为零,因此与其对应的熵可能变为无穷大。为了防止这种情况,我想到了使用if-else进行检查。但是,循环

if entropy(:,j)==NaN
     entropy(:,j)=0;
 end

似乎没有用。应该为创意而努力,并帮助解决这个问题。谢谢

更新:我实施了建议和答案以更正代码。但是,我的解决逻辑早先不合适。在修改后的代码中,我想计算具有位2,8,16,32的时间序列长度的熵。对于每个代码长度,计算熵。对于动态系统的每个不同的初始条件,每个代码长度的熵计算重复N次。采用这个appraoch来检查熵变为1的代码长度。熵与比特的关系图的性质应该从零增加并逐渐接近1,之后它饱和 - 对于所有剩余的比特保持不变。我无法得到这条曲线(图1)。应该感谢帮助纠正我出错的地方。

clear all

 H = 1  %in bits
 Bits = [2,8,16,32,64];
threshold = 0.5;
N=100;  %Number of runs of the experiment


for r = 1:length(Bits)


t = Bits(r)

for Runs = 1:N
    x(1)            = rand;

    for j = 2:t


        % Iterating over the Tent Map


        if x(j - 1) < 0.5
            x(j) = 2 * x(j - 1);
        else
            x(j) = 2 * (1 - x(j - 1));
        end % if
    end
    %Binarizing the output of the Tent Map
    s  = (x >=threshold);
    p1 = sum(s == 1 ) / length(s);  %calculating probaility of number of 1's
    p0 = 1 - p1;  % calculating probability of number of 0'1

    entropy(t) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculating entropy in bits

    if isnan(entropy(t))
        entropy(t) = 0;
    end



    %disp(abs(lambda-H))



end


  Entropy_Run(Runs) =  entropy(t)
end
Entropy_Bits(r) = mean(Entropy_Run)
plot(Bits,Entropy_Bits)

2 个答案:

答案 0 :(得分:1)

对于问题1,Hentropy可以是nats或bits单位,只要它们都使用相同的单位计算。换句话说,您应该同时使用log或两者都使用log2。使用您提供的代码示例,使用一致的nats单位正确计算Hentropy。如果您希望以位为单位工作,则H的转换应该为您H = log(2)/log(2) = 1(或使用转换因子1/log(2) ~ 1.443H ~ 0.69 * 1.443 ~ 1)。

对于问题2,正如@noumenal已经指出你可以使用isnan检查NaN。或者,您可以使用以下内容检查p1是否在(0,1)范围内(不包括0和1)

if (p1 > 0 && p1 < 1)
    entropy(:,j) = -p1 * log(p1) - (1 - p1) * log(1 - p1); %calculating entropy  in natural base e
else
    entropy(:, j) = 0;
end

答案 1 :(得分:0)

首先你只是

function [mean_entropy, bits] = compute_entropy(bits, blocks, threshold, replicate)

    if replicate
        disp('Replication is ON');
    else
        disp('Replication is OFF');
    end

    %%
    % Populate random vector
    if replicate
        seed = 849;
        rng(seed);
    else
        rng('default');
    end

    rs = rand(blocks);


    %%
    % Get random
    trial_entropy = zeros(length(bits));

    for r = 1:length(rs)

        bit_entropy = zeros(length(bits), 1); % H

        % Traverse bit trials
        for b = 1:(length(bits)) % N

            tent_map = zeros(b, 1); %Preallocate for memory management

            %Initialize
            tent_map(1) = rs(r);

            for j = 2:b % j is the iterator, b is the current bit

                if tent_map(j - 1) < threshold
                    tent_map(j) = 2 * tent_map(j - 1);
                else
                    tent_map(j) = 2 * (1 - tent_map(j - 1));
                end % if
            end

            %Binarize the output of the Tent Map
            s  = find(tent_map >= threshold);
            p1 = sum(s == 1) / length(s);  %calculate probaility of number of 1's
            %p0 = 1 - p1;  % calculate probability of number of 0'1

            bit_entropy(b) = -p1 * log2(p1) - (1 - p1) * log2(1 - p1); %calculate entropy in bits

            if isnan(bit_entropy(b))
                bit_entropy(b) = 0;
            end

            %disp(abs(lambda-h))

        end

        trial_entropy(:, r) = bit_entropy;

        disp('Trial Statistics')
        data = get_summary(bit_entropy);
        disp('Mean')
        disp(data.mean);
        disp('SD')
        disp(data.sd);

    end

    % TO DO Compute the mean for each BIT index in trial_entropy
    mean_entropy = 0;

    disp('Overall Statistics')
    data = get_summary(trial_entropy);
    disp('Mean')
    disp(data.mean);
    disp('SD')
    disp(data.sd);

    %This is the wrong mean...
    mean_entropy = data.mean;

    function summary = get_summary(entropy)
        summary = struct('mean', mean(entropy), 'sd', std(entropy));
    end
end

然后你必须

% Entropy Script
clear all

%% Settings
replicate = false; % = false % Use true for debugging only.
%H = 1;  %in bits
Bits = 2.^(1:6);
Threshold = 0.5;
%Tolerance = 0.001;
Blocks = 100;  %Number of runs of the experiment

%% Run
[mean_entropy, bits] = compute_entropy(Bits, Blocks, Threshold, replicate);

%What we want
%plot(bits, mean_entropy);

%What we have
plot(1:length(mean_entropy), mean_entropy);