wolffd@0: function varargout = mirentropy(x,varargin) wolffd@0: % h = mirentropy(a) calculates the relative entropy of a. wolffd@0: % (Cf. User's Manual.) wolffd@0: % mirentropy(..., ?Center?) centers the input data before wolffd@0: % transforming it into a probability distribution. wolffd@0: wolffd@0: center.key = 'Center'; wolffd@0: center.type = 'Boolean'; wolffd@0: center.default = 0; wolffd@0: option.center = center; wolffd@0: wolffd@0: specif.option = option; wolffd@0: wolffd@0: varargout = mirfunction(@mirentropy,x,varargin,nargout,specif,@init,@main); wolffd@0: wolffd@0: wolffd@0: function [x type] = init(x,option) wolffd@0: if isamir(x,'miraudio') wolffd@0: x = mirspectrum(x); wolffd@0: end wolffd@0: type = 'mirscalar'; wolffd@0: wolffd@0: wolffd@0: function h = main(x,option,postoption) wolffd@0: if iscell(x) wolffd@0: x = x{1}; wolffd@0: end wolffd@0: m = get(x,'Data'); wolffd@0: v = cell(1,length(m)); wolffd@0: for h = 1:length(m) wolffd@0: v{h} = cell(1,length(m{h})); wolffd@0: for k = 1:length(m{h}) wolffd@0: mk = m{h}{k}; wolffd@0: mn = mk; wolffd@0: if isa(x,'mirhisto') || isa(x,'mirscalar') wolffd@0: mn = mn'; wolffd@0: end wolffd@0: wolffd@0: if option.center wolffd@0: mn = center(mn); wolffd@0: end wolffd@0: wolffd@0: % Negative data is trimmed: wolffd@0: mn(mn<0) = 0; wolffd@0: wolffd@0: % Data is normalized such that the sum is equal to 1. wolffd@0: mn = mn./repmat(sum(mn)+repmat(1e-12,... wolffd@0: [1 size(mn,2) size(mn,3) size(mn,4)]),... wolffd@0: [size(mn,1) 1 1 1]); wolffd@0: wolffd@0: % Actual computation of entropy wolffd@0: v{h}{k} = -sum(mn.*log(mn + 1e-12))./log(size(mn,1)); wolffd@0: wolffd@0: if isa(x,'mirhisto') || isa(x,'mirscalar') wolffd@0: v{h}{k} = v{h}{k}'; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: t = ['Entropy of ',get(x,'Title')]; wolffd@0: h = mirscalar(x,'Data',v,'Title',t);