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