annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirentropy.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function varargout = mirentropy(x,varargin)
wolffd@0 2 % h = mirentropy(a) calculates the relative entropy of a.
wolffd@0 3 % (Cf. User's Manual.)
wolffd@0 4 % mirentropy(..., ?Center?) centers the input data before
wolffd@0 5 % transforming it into a probability distribution.
wolffd@0 6
wolffd@0 7 center.key = 'Center';
wolffd@0 8 center.type = 'Boolean';
wolffd@0 9 center.default = 0;
wolffd@0 10 option.center = center;
wolffd@0 11
wolffd@0 12 specif.option = option;
wolffd@0 13
wolffd@0 14 varargout = mirfunction(@mirentropy,x,varargin,nargout,specif,@init,@main);
wolffd@0 15
wolffd@0 16
wolffd@0 17 function [x type] = init(x,option)
wolffd@0 18 if isamir(x,'miraudio')
wolffd@0 19 x = mirspectrum(x);
wolffd@0 20 end
wolffd@0 21 type = 'mirscalar';
wolffd@0 22
wolffd@0 23
wolffd@0 24 function h = main(x,option,postoption)
wolffd@0 25 if iscell(x)
wolffd@0 26 x = x{1};
wolffd@0 27 end
wolffd@0 28 m = get(x,'Data');
wolffd@0 29 v = cell(1,length(m));
wolffd@0 30 for h = 1:length(m)
wolffd@0 31 v{h} = cell(1,length(m{h}));
wolffd@0 32 for k = 1:length(m{h})
wolffd@0 33 mk = m{h}{k};
wolffd@0 34 mn = mk;
wolffd@0 35 if isa(x,'mirhisto') || isa(x,'mirscalar')
wolffd@0 36 mn = mn';
wolffd@0 37 end
wolffd@0 38
wolffd@0 39 if option.center
wolffd@0 40 mn = center(mn);
wolffd@0 41 end
wolffd@0 42
wolffd@0 43 % Negative data is trimmed:
wolffd@0 44 mn(mn<0) = 0;
wolffd@0 45
wolffd@0 46 % Data is normalized such that the sum is equal to 1.
wolffd@0 47 mn = mn./repmat(sum(mn)+repmat(1e-12,...
wolffd@0 48 [1 size(mn,2) size(mn,3) size(mn,4)]),...
wolffd@0 49 [size(mn,1) 1 1 1]);
wolffd@0 50
wolffd@0 51 % Actual computation of entropy
wolffd@0 52 v{h}{k} = -sum(mn.*log(mn + 1e-12))./log(size(mn,1));
wolffd@0 53
wolffd@0 54 if isa(x,'mirhisto') || isa(x,'mirscalar')
wolffd@0 55 v{h}{k} = v{h}{k}';
wolffd@0 56 end
wolffd@0 57 end
wolffd@0 58 end
wolffd@0 59 t = ['Entropy of ',get(x,'Title')];
wolffd@0 60 h = mirscalar(x,'Data',v,'Title',t);