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