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

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