PLUS implements addition operator for two analysis objects. M Hewitson 01-02-07
0001 function b = plus(varargin) 0002 0003 % PLUS implements addition operator for two analysis objects. 0004 % 0005 % M Hewitson 01-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: plus.html,v 1.1 2007/06/08 14:15:03 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 0046 % make output analysis object 0047 b = ao(do, h); 0048 b = set(b, 'name', sprintf('%s%s%s', n1, op, n2)); 0049 0050 end 0051 0052 %% BELOW HERE IS THE OLD VERSION BEFORE WE HAD CDATA CLASS 0053 % 0054 % % We can force these to be two analysis objects here so 0055 % % that a=b+1 works. 0056 % % 0057 % % If b or c is a double of length 1, then we grow a new vector the same 0058 % % length as the data in the other. The other possibility is that one of the 0059 % % inputs is a vector the same length as the data vector in the other 0060 % % analysis object. If both are analysis objects, we deal with them below. 0061 % 0062 % if ~isa(b, 'ao') 0063 % cd = c.data; 0064 % if isa(cd, 'tsdata') 0065 % x = tsdata(ones(size(cd.x))*b, cd.fs); 0066 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 't0', cd.t0, 'name', num2str(b)); 0067 % bval = b; 0068 % b = ao(x, history('ao', '', plist(param('const', b)))); 0069 % b = set(b, 'name', num2str(bval)); 0070 % elseif isa(cd, 'fsdata') 0071 % x = fsdata(ones(size(cd.x))*b, cd.fs); 0072 % x = set(x, 'xunits', cd.xunits, 'yunits', cd.yunits, 'name', num2str(b)); 0073 % bval = b; 0074 % b = ao(x, history('const', '', plist(param('const', b)))); 0075 % b = set(b, 'name', num2str(bval)); 0076 % else 0077 % error('### analysis object has an unknown data type.'); 0078 % end 0079 % clear cd; 0080 % end 0081 % if ~isa(c, 'ao') 0082 % bd = b.data; 0083 % if isa(bd, 'tsdata') 0084 % % x = tsdata(ones(size(bd.x))*c, bd.fs); 0085 % % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 't0', bd.t0, 'name', num2str(c)); 0086 % % cval = c; 0087 % % c = ao(x, history('ao', '', plist([param('N', length(bd.x)) param('Val', c)]))); 0088 % % c = set(c, 'name', num2str(cval)); 0089 % c = ao(plist([param('N', length(bd.x)) param('Val', c)])); 0090 % elseif isa(bd, 'fsdata') 0091 % x = fsdata(ones(size(bd.x))*c, bd.fs) 0092 % x = set(x, 'xunits', bd.xunits, 'yunits', bd.yunits, 'name', num2str(c)); 0093 % cval = c; 0094 % c = ao(x, history('const', '', plist(param('const', c)))); 0095 % c = set(c, 'name', num2str(cval)); 0096 % else 0097 % error('### analysis object has an unknown data type.'); 0098 % end 0099 % clear bd; 0100 % end 0101 % 0102 % % Check both analysis objects have the same data type 0103 % d1 = get(b, 'data'); 0104 % d2 = get(c, 'data'); 0105 % d1info = whos('d1') 0106 % d2info = whos('d2') 0107 % 0108 % if ~strcmp(d1info.class,d2info.class) 0109 % error('### two analysis objects have different data types. They can not be added'); 0110 % end 0111 % 0112 % % Check both data types have the same Y units 0113 % if ~strcmp(d1.xunits, d2.xunits) 0114 % warning('!!! adding two time/freq-series objects with different X units'); 0115 % xunits = [d1.xunits '/' d2.xunits]; 0116 % else 0117 % xunits = d1.xunits; 0118 % end 0119 % 0120 % % Check both data types have the same Y units 0121 % if ~strcmp(d1.yunits, d2.yunits) 0122 % warning('!!! adding two time/freq-series objects with different Y units'); 0123 % yunits = [d1.yunits '/' d2.yunits]; 0124 % else 0125 % yunits = d1.yunits; 0126 % end 0127 % 0128 % % Which data type do we have 0129 % dtype = d1info.class; 0130 % switch dtype 0131 % case 'tsdata' 0132 % disp('* adding two tsdata objects'); 0133 % 0134 % % for now we ignore the time vectors 0135 % if length(d1.x) ~= length(d2.x) 0136 % error('### two data vectors are different lengths. They can not be added.'); 0137 % end 0138 % if length(d1.fs) ~= length(d2.fs) 0139 % error('### two data vectors have different sample rates. They can not be added.'); 0140 % end 0141 % 0142 % % Check both data types have the same time origin 0143 % if ~strcmp(d1.t0, d2.t0) 0144 % warning('!!! adding two time-series objects with different time origins'); 0145 % t0 = [d1.t0 '/' d2.t0]; 0146 % else 0147 % t0 = d1.t0; 0148 % end 0149 % 0150 % % make a new tsdata object 0151 % ts = tsdata(d1.x+d2.x, d1.fs); 0152 % ts = set(ts, 'name', sprintf('(%s)+(%s)', d1.name, d2.name)); 0153 % ts = set(ts, 'xunits', xunits); 0154 % ts = set(ts, 'yunits', yunits); 0155 % ts = set(ts, 't0', t0); 0156 % 0157 % % make a new history object 0158 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0159 % h = set(h, 'invars', invars); 0160 % 0161 % % make output analysis object 0162 % a = ao(ts, h); 0163 % a = set(a, 'name', sprintf('%s+%s', char(invars{1}), char(invars{2}))); 0164 % % a = set(a, 'name', sprintf('(%s)+(%s)', b.name, c.name)); 0165 % 0166 % case 'fsdata' 0167 % disp('* adding two fsdata objects'); 0168 % 0169 % % for now we ignore the freq vectors 0170 % if length(d1.xx) ~= length(d2.xx) 0171 % error('### two frequency vectors are different lengths. They can not be added.'); 0172 % end 0173 % if length(d1.fs) ~= length(d2.fs) 0174 % error('### two original data vectors have different sample rates. They can not be added.'); 0175 % end 0176 % if d1.enbw ~= d2.enbw 0177 % warning('!!! Equivalent noise bandwidth of the two frequency series does not match.'); 0178 % end 0179 % % make a new tsdata object 0180 % fs = fsdata(d1.f,d1.xx+d2.xx, d1.fs); 0181 % fs = set(fs, 'name', sprintf('(%s)+(%s)', d1.name, d2.name)); 0182 % fs = set(fs, 'xunits', xunits); 0183 % fs = set(fs, 'yunits', yunits); 0184 % fs = set(fs, 'enbw', d1.enbw); 0185 % 0186 % % make a new history object 0187 % h = history(ALGONAME, VERSION, [], [b.hist c.hist]); 0188 % h = set(h, 'invars', invars); 0189 % 0190 % % make output analysis object 0191 % a = ao(fs, h); 0192 % a = set(a, 'name', sprintf('%s+%s', char(invars{1}), char(invars{2}))); 0193 % 0194 % otherwise 0195 % error('### unknown data type. They can not be addded.') 0196 % end 0197 0198 % END