annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mircentroid.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 = mircentroid(x,varargin)
Daniel@0 2 % c = mircentroid(x) calculates the centroid (or center of gravity) of x.
Daniel@0 3 % x can be either:
Daniel@0 4 % - a spectrum (spectral centroid),
Daniel@0 5 % - an envelope (temporal centroid)
Daniel@0 6 % - a histogram,
Daniel@0 7 % - or any data. Only the positive ordinates of the data are taken
Daniel@0 8 % into consideration.
Daniel@0 9 % c = mircentroid(x,'Peaks') calculates the centroid of the peaks only.
Daniel@0 10
Daniel@0 11 % Beauchamp 1982 version?
Daniel@0 12
Daniel@0 13 peaks.key = 'Peaks';
Daniel@0 14 peaks.type = 'String';
Daniel@0 15 peaks.choice = {0,'NoInterpol','Interpol'};
Daniel@0 16 peaks.default = 0;
Daniel@0 17 peaks.keydefault = 'NoInterpol';
Daniel@0 18 option.peaks = peaks;
Daniel@0 19
Daniel@0 20 specif.option = option;
Daniel@0 21
Daniel@0 22 varargout = mirfunction(@mircentroid,x,varargin,nargout,specif,@init,@main);
Daniel@0 23
Daniel@0 24
Daniel@0 25 function [x type] = init(x,option)
Daniel@0 26 if not(isamir(x,'mirdata')) || isamir(x,'miraudio')
Daniel@0 27 x = mirspectrum(x);
Daniel@0 28 end
Daniel@0 29 type = 'mirscalar';
Daniel@0 30
Daniel@0 31
Daniel@0 32 function c = main(x,option,postoption)
Daniel@0 33 if iscell(x)
Daniel@0 34 x = x{1};
Daniel@0 35 end
Daniel@0 36 if option.peaks
Daniel@0 37 if strcmpi(option.peaks,'Interpol')
Daniel@0 38 pt = get(x,'PeakPrecisePos');
Daniel@0 39 pv = get(x,'PeakPreciseVal');
Daniel@0 40 else
Daniel@0 41 pt = get(x,'PeakPos');
Daniel@0 42 pv = get(x,'PeakVal');
Daniel@0 43 end
Daniel@0 44 cx = cell(1,length(pt));
Daniel@0 45 for h = 1:length(pt)
Daniel@0 46 cx{h} = cell(1,length(pt{h}));
Daniel@0 47 for i = 1:length(pt{h})
Daniel@0 48 pti = pt{h}{i};
Daniel@0 49 pvi = pv{h}{i};
Daniel@0 50 %if isempty(pti)
Daniel@0 51
Daniel@0 52 nfr = size(pti,2);
Daniel@0 53 nbd = size(pti,3);
Daniel@0 54 ci = zeros(1,nfr,nbd);
Daniel@0 55 for j = 1:nfr
Daniel@0 56 for k = 1:nbd
Daniel@0 57 ptk = pti{1,j,k};
Daniel@0 58 pvk = pvi{1,j,k};
Daniel@0 59 sk = sum(pvk);
Daniel@0 60 ci(1,j,k) = sum(ptk.*pvk) ./ sk;
Daniel@0 61 end
Daniel@0 62 end
Daniel@0 63 cx{h}{i} = ci;
Daniel@0 64 end
Daniel@0 65 end
Daniel@0 66 else
Daniel@0 67 cx = peaksegments(@centroid,get(x,'Data'),get(x,'Pos'));
Daniel@0 68 end
Daniel@0 69 if isa(x,'mirspectrum')
Daniel@0 70 t = 'Spectral centroid';
Daniel@0 71 elseif isa(x,'mirenvelope')
Daniel@0 72 t = 'Temporal centroid';
Daniel@0 73 else
Daniel@0 74 t = ['centroid of ',get(x,'Title')];
Daniel@0 75 end
Daniel@0 76 c = mirscalar(x,'Data',cx,'Title',t);
Daniel@0 77
Daniel@0 78
Daniel@0 79 function c = centroid(d,p)
Daniel@0 80 c = (p'*d) ./ sum(d);