wolffd@0: function varargout = mircentroid(x,varargin) wolffd@0: % c = mircentroid(x) calculates the centroid (or center of gravity) of x. wolffd@0: % x can be either: wolffd@0: % - a spectrum (spectral centroid), wolffd@0: % - an envelope (temporal centroid) wolffd@0: % - a histogram, wolffd@0: % - or any data. Only the positive ordinates of the data are taken wolffd@0: % into consideration. wolffd@0: % c = mircentroid(x,'Peaks') calculates the centroid of the peaks only. wolffd@0: wolffd@0: % Beauchamp 1982 version? wolffd@0: wolffd@0: peaks.key = 'Peaks'; wolffd@0: peaks.type = 'String'; wolffd@0: peaks.choice = {0,'NoInterpol','Interpol'}; wolffd@0: peaks.default = 0; wolffd@0: peaks.keydefault = 'NoInterpol'; wolffd@0: option.peaks = peaks; wolffd@0: wolffd@0: specif.option = option; wolffd@0: wolffd@0: varargout = mirfunction(@mircentroid,x,varargin,nargout,specif,@init,@main); wolffd@0: wolffd@0: wolffd@0: function [x type] = init(x,option) wolffd@0: if not(isamir(x,'mirdata')) || isamir(x,'miraudio') wolffd@0: x = mirspectrum(x); wolffd@0: end wolffd@0: type = 'mirscalar'; wolffd@0: wolffd@0: wolffd@0: function c = main(x,option,postoption) wolffd@0: if iscell(x) wolffd@0: x = x{1}; wolffd@0: end wolffd@0: if option.peaks wolffd@0: if strcmpi(option.peaks,'Interpol') wolffd@0: pt = get(x,'PeakPrecisePos'); wolffd@0: pv = get(x,'PeakPreciseVal'); wolffd@0: else wolffd@0: pt = get(x,'PeakPos'); wolffd@0: pv = get(x,'PeakVal'); wolffd@0: end wolffd@0: cx = cell(1,length(pt)); wolffd@0: for h = 1:length(pt) wolffd@0: cx{h} = cell(1,length(pt{h})); wolffd@0: for i = 1:length(pt{h}) wolffd@0: pti = pt{h}{i}; wolffd@0: pvi = pv{h}{i}; wolffd@0: %if isempty(pti) wolffd@0: wolffd@0: nfr = size(pti,2); wolffd@0: nbd = size(pti,3); wolffd@0: ci = zeros(1,nfr,nbd); wolffd@0: for j = 1:nfr wolffd@0: for k = 1:nbd wolffd@0: ptk = pti{1,j,k}; wolffd@0: pvk = pvi{1,j,k}; wolffd@0: sk = sum(pvk); wolffd@0: ci(1,j,k) = sum(ptk.*pvk) ./ sk; wolffd@0: end wolffd@0: end wolffd@0: cx{h}{i} = ci; wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: cx = peaksegments(@centroid,get(x,'Data'),get(x,'Pos')); wolffd@0: end wolffd@0: if isa(x,'mirspectrum') wolffd@0: t = 'Spectral centroid'; wolffd@0: elseif isa(x,'mirenvelope') wolffd@0: t = 'Temporal centroid'; wolffd@0: else wolffd@0: t = ['centroid of ',get(x,'Title')]; wolffd@0: end wolffd@0: c = mirscalar(x,'Data',cx,'Title',t); wolffd@0: wolffd@0: wolffd@0: function c = centroid(d,p) wolffd@0: c = (p'*d) ./ sum(d);