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