MRDIVIDE overloads / operator for analysis objects. M Hewitson 05-02-07
0001 function b = mrdivide(varargin) 0002 0003 % MRDIVIDE overloads / operator for analysis objects. 0004 % 0005 % M Hewitson 05-02-07 0006 % 0007 0008 % capture input variable names 0009 invars = {}; 0010 for j=1:nargin 0011 iname = inputname(j); 0012 if isempty(iname) && isnumeric(varargin{j}) 0013 iname = num2str(varargin{j}); 0014 elseif isempty(iname) && ischar(varargin{j}) 0015 iname = varargin{j}; 0016 end 0017 invars = [invars cellstr(iname)]; 0018 end 0019 0020 ALGONAME = mfilename; 0021 VERSION = '$Id: mrdivide.html,v 1.1 2007/06/08 14:15:02 hewitson Exp $'; 0022 0023 op = '/'; 0024 0025 [a1,a2,do] = aooperate(varargin, op); 0026 0027 %--------- create output AO 0028 0029 % make a new history object 0030 h = history(ALGONAME, VERSION, [], [a1.hist a2.hist]); 0031 h = set(h, 'invars', invars); 0032 0033 % get names for output 0034 if isempty(char(invars{1})) 0035 n1 = a1.name; 0036 else 0037 n1 = char(invars{1}); 0038 end 0039 if isempty(char(invars{2})) 0040 n2 = a2.name; 0041 else 0042 n2 = char(invars{2}); 0043 end 0044 0045 % make output analysis object 0046 % op = op(2:end); 0047 b = ao(do, h); 0048 b = set(b, 'name', sprintf('%s%s%s', n1, op, n2)); 0049 0050 end 0051 0052 % % capture input variable names 0053 % invars = {}; 0054 % for j=1:nargin 0055 % invars = [invars cellstr(inputname(j))]; 0056 % end 0057 % 0058 % ALGONAME = mfilename; 0059 % VERSION = '$Id: mrdivide.html,v 1.1 2007/06/08 14:15:02 hewitson Exp $'; 0060 % 0061 % % We can force these to be two analysis objects here so 0062 % % that a=b./1 works. 0063 % % 0064 % % If b or c is a double of length 1, then we grow a new vector the same 0065 % % length as the data in the other. The other possibility is that one of the 0066 % % inputs is a vector the same length as the data vector in the other 0067 % % analysis object. If both are analysis objects, we deal with them below. 0068 % 0069 % if ~isa(b, 'ao') 0070 % cd = c.data; 0071 % if isa(cd, 'tsdata') 0072 % x = tsdata(ones(size(cd.x))*b, cd.fs); 0073 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 't0', cd.t0, 'name', num2str(b)); 0074 % bval = b; 0075 % b = ao(x, history('const', '', plist(param('const', b)))); 0076 % b = set(b, 'name', num2str(bval)); 0077 % elseif isa(cd, 'fsdata') 0078 % x = fsdata(ones(size(cd.x))*b, cd.fs); 0079 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 't0', cd.t0, 'name', num2str(b)); 0080 % bval = b; 0081 % b = ao(x, history('const', '', plist(param('const', b)))); 0082 % b = set(b, 'name', num2str(bval)); 0083 % else 0084 % error('### analysis object has an unknown data type.'); 0085 % end 0086 % clear cd; 0087 % end 0088 % if ~isa(c, 'ao') 0089 % bd = b.data; 0090 % if isa(bd, 'tsdata') 0091 % x = tsdata(ones(size(bd.x))*c, bd.fs); 0092 % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 't0', bd.t0, 'name', num2str(c)); 0093 % cval = c; 0094 % c = ao(x, history('const', '', plist(param('const', c)))); 0095 % c = set(c, 'name', num2str(cval)); 0096 % elseif isa(bd, 'fsdata') 0097 % x = fsdata(ones(size(bd.x))*c, bd.fs) 0098 % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 't0', bd.t0, 'name', num2str(c)); 0099 % cval = c; 0100 % c = ao(x, history('const', '', plist(param('const', c)))); 0101 % c = set(c, 'name', num2str(cval)); 0102 % else 0103 % error('### analysis object has an unknown data type.'); 0104 % end 0105 % clear bd; 0106 % end 0107 % 0108 % % Check both analysis objects have the same data type 0109 % d1 = get(b, 'data'); 0110 % d2 = get(c, 'data'); 0111 % d1info = whos('d1'); 0112 % d2info = whos('d2'); 0113 % 0114 % if d1info.class ~= d2info.class 0115 % error('### two analysis objects have different data types. They can not be divided.'); 0116 % end 0117 % 0118 % % Check both data types have the same Y units 0119 % if ~strcmp(d1.xunits, d2.xunits) 0120 % warning('!!! dividing two time/freq-series objects with different X units'); 0121 % xunits = [d1.xunits '/' d2.xunits]; 0122 % else 0123 % xunits = d1.xunits; 0124 % end 0125 % 0126 % % Check both data types have the same Y units 0127 % if ~strcmp(d1.yunits, d2.yunits) 0128 % warning('!!! dividing two time/freq-series objects with different Y units'); 0129 % yunits = [d1.yunits '/' d2.yunits]; 0130 % else 0131 % yunits = ''; 0132 % end 0133 % 0134 % % Which data type do we have 0135 % dtype = d1info.class; 0136 % switch dtype 0137 % case 'tsdata' 0138 % disp('* dividing two tsdata objects'); 0139 % 0140 % % for now we ignore the time vectors 0141 % if length(d1.x) ~= length(d2.x) 0142 % error('### two data vectors are different lengths. They can not be divided.'); 0143 % end 0144 % if length(d1.fs) ~= length(d2.fs) 0145 % error('### two data vectors have different sample rates. They can not be divided.'); 0146 % end 0147 % 0148 % % Check both data types have the same time origin 0149 % if ~strcmp(d1.t0, d2.t0) 0150 % warning('!!! dividing two time-series objects with different time origins'); 0151 % t0 = [d1.t0 '/' d2.t0]; 0152 % else 0153 % t0 = d1.t0; 0154 % end 0155 % 0156 % % make a new tsdata object 0157 % ts = tsdata(d1.x./d2.x, d1.fs); 0158 % ts = set(ts, 'name', sprintf('(%s)./(%s)', d1.name, d2.name)); 0159 % ts = set(ts, 'xunits', xunits); 0160 % ts = set(ts, 'yunits', ''); 0161 % ts = set(ts, 't0', t0); 0162 % 0163 % % make a new history object 0164 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0165 % h = set(h, 'invars', invars); 0166 % 0167 % % make output analysis object 0168 % a = ao(ts, h); 0169 % % a = set(a, 'name', sprintf('(%s)./(%s)', b.name, c.name)); 0170 % a = set(a, 'name', sprintf('%s/%s', char(invars{1}), char(invars{2}))); 0171 % 0172 % case 'fsdata' 0173 % disp('* dividing two fsdata objects'); 0174 % 0175 % % for now we ignore the freq vectors 0176 % if length(d1.xx) ~= length(d2.xx) 0177 % error('### two frequency vectors are different lengths. They can not be divided.'); 0178 % end 0179 % if length(d1.fs) ~= length(d2.fs) 0180 % error('### two original data vectors have different sample rates. They can not be divided.'); 0181 % end 0182 % if d1.enbw ~= d2.enbw 0183 % warning('!!! Equivalent noise bandwidth of the two frequency series does not match.'); 0184 % end 0185 % % make a new tsdata object 0186 % fs = fsdata(d1.f,d1.xx./d2.xx, d1.fs); 0187 % fs = set(fs, 'name', sprintf('(%s)./(%s)', d1.name, d2.name)); 0188 % fs = set(fs, 'xunits', xunits); 0189 % fs = set(fs, 'yunits', yunits); 0190 % fs = set(fs, 'enbw', d1.enbw); 0191 % 0192 % % make a new history object 0193 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0194 % h = set(h, 'invars', invars); 0195 % 0196 % % make output analysis object 0197 % a = ao(fs, h); 0198 % % a = set(a, 'name', sprintf('(%s)./(%s)', b.name, c.name)); 0199 % a = set(a, 'name', sprintf('%s/%s', char(invars{1}), char(invars{2}))); 0200 % 0201 % otherwise 0202 % error('### unknown data type. They can not be addded.') 0203 % end 0204 0205 % END