comparison m-toolbox/classes/@ao/bicohere.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % BICOHERE computes the bicoherence of two input time-series
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: BICOHERE computes the bicoherence of two input time-series.
5 %
6 % CALL: bs = bicohere(a1,a2,pl)
7 %
8 % INPUTS: aN - input analysis objects
9 % a1,a2 - input analysis objects array
10 % pl - input parameter list
11 %
12 % OUTPUTS: bs - xyz data analysis object
13 %
14 % <a href="matlab:utils.helper.displayMethodInfo('bicohere', 'psd')">Parameters Description</a>
15 %
16 % VERSION: $Id: psd.m,v 1.59 2010/12/17 17:45:08 ingo Exp $
17 %
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19
20
21 function varargout = bicohere(varargin)
22
23 % Check if this is a call for parameters
24 if utils.helper.isinfocall(varargin{:})
25 varargout{1} = getInfo(varargin{3});
26 return
27 end
28
29 import utils.const.*
30 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
31
32 % Collect input variable names
33 in_names = cell(size(varargin));
34 for ii = 1:nargin,in_names{ii} = inputname(ii);end
35
36 % Collect all AOs and plists
37 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
38 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
39
40 if numel(as) ~= 2
41 error('bicohere only works with 2 time-series at the moment.');
42 end
43
44 % Get data
45 a = as(1);
46 b = as(2);
47
48 % same fs?
49 if a.data.fs ~= b.data.fs
50 error('### Two time-series have different sample rates.');
51 end
52
53 % Same length vectors?
54 if a.len ~= b.len
55 error('### Two time-series must have same length');
56 end
57
58 % Combine plists
59 pl = combine(pl, getDefaultPlist);
60 usepl = utils.helper.process_spectral_options(pl, 'lin', a.len, a.fs);
61
62 win = find(usepl, 'Win');
63 nfft = find(usepl, 'Nfft');
64 olap = find(usepl, 'Olap')/100;
65 Xolap = round(olap*nfft);
66 order = find(usepl, 'order');
67
68 fs = a.data.fs;
69 x = {a.data.y; b.data.y};
70
71 [x,M,isreal_x,y,Ly,win,winName,winParam,noverlap,k,L,options] = ...
72 ao.welchparse(x,'',win.win, Xolap, nfft, fs);
73
74 select = 1:(nfft+1)/2;
75
76 % loop over segments
77 LminusOverlap = L-noverlap;
78 xStart = 1:LminusOverlap:k*LminusOverlap;
79 xEnd = xStart+L-1;
80
81 m = zeros(length(select), length(select));
82
83 for i = 1:k
84 if order < 0
85 Xseg = x(xStart(i):xEnd(i));
86 Yseg = x(xStart(i):xEnd(i));
87 else
88 [Xseg,coeffs] = ltpda_polyreg(x(xStart(i):xEnd(i)), order);
89 [Yseg,coeffs] = ltpda_polyreg(y(xStart(i):xEnd(i)), order);
90 end
91
92 % window
93 xw = Xseg.*win.';
94 yw = Yseg.*win.';
95
96 % FFT
97 xx2s = fft(xw);
98 xx = xx2s(select);
99 yy2s = fft(yw);
100 yy = yy2s(select);
101
102 scalex = abs(xx);
103 scaley = abs(yy);
104 sc = scalex * scaley';
105 m = m + (xx * yy')./sc;
106
107 end
108
109 m = m./k;
110
111 f = psdfreqvec('npts',nfft,'Fs',fs);
112 f = f(select);
113 do = xyzdata(f, f, m);
114 do.setXunits('Hz');
115 do.setYunits('Hz');
116 do.setZunits(a.yunits*b.yunits);
117
118 ma = ao(do);
119 ma.setName(sprintf('%s, %s', a.name, b.name));
120 ma.addHistory(getInfo('None'), usepl, ao_invars, [a.hist b.hist]);
121
122
123 % Set output
124 varargout{1} = ma;
125 end
126
127 %--------------------------------------------------------------------------
128 % Get Info Object
129 %--------------------------------------------------------------------------
130 function ii = getInfo(varargin)
131 if nargin == 1 && strcmpi(varargin{1}, 'None')
132 sets = {};
133 pl = [];
134 else
135 sets = {'Default'};
136 pl = getDefaultPlist;
137 end
138 % Build info object
139 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: psd.m,v 1.59 2010/12/17 17:45:08 ingo Exp $', sets, pl);
140 end
141
142 %--------------------------------------------------------------------------
143 % Get Default Plist
144 %--------------------------------------------------------------------------
145 function plout = getDefaultPlist()
146 persistent pl;
147 if exist('pl', 'var')==0 || isempty(pl)
148 pl = buildplist();
149 end
150 plout = pl;
151 end
152
153 function pl = buildplist()
154
155 % General plist for Welch-based, linearly spaced spectral estimators
156 pl = plist.WELCH_PLIST;
157
158
159 end
160 % END
161