Home > classes > @ao > private > aooperate.m

aooperate

PURPOSE ^

AOOPERATE implements specified operator overload for AOs.

SYNOPSIS ^

function [a1,a2,do] = aooperate(varargin)

DESCRIPTION ^

 AOOPERATE implements specified operator overload for AOs.
 
 M Hewitson 16-03-07
 
 $Id: aooperate.m,v 1.12 2007/06/12 06:17:27 hewitson Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [a1,a2,do] = aooperate(varargin)
0002 
0003 % AOOPERATE implements specified operator overload for AOs.
0004 %
0005 % M Hewitson 16-03-07
0006 %
0007 % $Id: aooperate.m,v 1.12 2007/06/12 06:17:27 hewitson Exp $
0008 %
0009 
0010 ALGONAME = mfilename;
0011 VERSION  = '$Id: aooperate.m,v 1.12 2007/06/12 06:17:27 hewitson Exp $';
0012 
0013 % should be possible to operate with
0014 %     cdata and tsdata - only need to be same length if length(cdata)>1
0015 % or  cdata and fsdata - only need to be same length if length(cdata)>1
0016 % or  cdata and cdata
0017 %
0018 % ** how do we deal with units for these different types?
0019 %  - only compare units when types are the same
0020 
0021 op       = varargin{end};
0022 varargin = varargin{1:end-1};
0023 nargin   = length(varargin);
0024 
0025 as = [];
0026 for j=1:nargin
0027   a = varargin{j};
0028   if isa(a, 'ao')
0029     as  = [as a];
0030   else
0031     if isnumeric(a)
0032       % create an ao from the values
0033       as = [as ao(a)];
0034     else
0035       error('### AO/plus inputs must be either numeric or AO.');
0036     end
0037   end
0038 end
0039 
0040 if length(as) > 2
0041   warning('!!! I only work with the first two input AOs. Ignoring all others.');
0042   warning('!!! Perhaps you have input an AO array?');
0043 end
0044 
0045 % Now we have two AOs
0046 a1 = as(1);
0047 a2 = as(2);
0048 clear as;
0049 d1 = a1.data;
0050 d2 = a2.data;
0051 % Check types
0052 if (isa(d1, 'fsdata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'fsdata'))
0053   error('### can''t add time-series data to frequency-series data.');
0054 end
0055 if (isa(d1, 'xydata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'xydata'))
0056   error('### can''t add XY data to time-series data.');
0057 end
0058 if (isa(d1, 'xydata') && isa(d2, 'fsdata')) || (isa(d1, 'fsdata') && isa(d2, 'xydata'))
0059   error('### can''t add XY data to frequency-series data.');
0060 end
0061 
0062 % get data
0063 x1 = getAOdata(a1);
0064 x2 = getAOdata(a2);
0065 nx1 = length(x1);
0066 nx2 = length(x2);
0067 
0068 % Some special cases
0069 % switch op
0070 %   case '*'
0071 %     if size(x1) == size(x2)
0072 %       x1 = x1.';
0073 %     end
0074 % end
0075 
0076 
0077 % check data lengths
0078 if isa(d1, 'cdata') && ~isa(d2, 'cdata')
0079   if nx1 > 1 && nx1~=nx2
0080     error('###1 cdata type must be same length as other AO, or single valued.');
0081   end
0082 elseif isa(d2, 'cdata') && ~isa(d1, 'cdata')
0083   if nx2 > 1 && nx1~=nx2
0084     error('###2 cdata type must be same length as other AO, or single valued.');
0085   end  
0086 end
0087 
0088 if nx1~=nx2
0089   if nx1~=1 && nx2~=1
0090     error('### data vectors must be the same length or one should be single valued.');
0091   end
0092 end
0093 
0094 % check units
0095 if isa(d1, 'fsdata') && isa(d2, 'fsdata')
0096   if ~strcmp(d1.xunits, d2.xunits)
0097     error('### xunits must be the same for same type of AOs.');
0098   end
0099   % check X-axis
0100   if d1.f ~= d2.f
0101     error('### frequency vectors are not the same.');
0102   end
0103 end
0104 if isa(d1, 'tsdata') && isa(d2, 'tsdata')
0105   if ~strcmp(d1.xunits, d2.xunits)
0106     error('### xunits must be the same for same type of AOs.');
0107   end
0108 end
0109 if isa(d1, 'xydata') && isa(d2, 'xydata')
0110   if ~strcmp(d1.xunits, d2.xunits)
0111     error('### xunits must be the same for same type of AOs.');
0112   end
0113 end
0114 
0115 % Add the data
0116 disp(sprintf('*** applying %s', op))
0117 eval(sprintf('x = x1%sx2;', op));
0118 
0119 % Create output data object
0120 
0121 if isa(d1, 'fsdata') || isa(d2, 'fsdata')
0122   % then we create an output fsdata
0123   % select which object gives properties to
0124   % the output object
0125   if isa(d1, 'fsdata')
0126     d = d1;
0127   else
0128     d = d2;
0129   end
0130   % then we create an output tsdata
0131   do = fsdata(d.f, x, d.fs);
0132   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0133   do = set(do, 'xunits', d.xunits);
0134   do = set(do, 'yunits', d.yunits);
0135   do = set(do, 'enbw', d.enbw);
0136 
0137 elseif isa(d1, 'tsdata') || isa(d2, 'tsdata')
0138   % select which object gives properties to
0139   % the output object
0140   if isa(d1, 'tsdata')
0141     d = d1;
0142   else
0143     d = d2;
0144   end
0145   % then we create an output tsdata
0146   do = d;
0147   do = set(do, 'x', x);
0148   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0149   
0150 elseif isa(d1, 'xydata') || isa(d2, 'xydata')
0151   % select which object gives properties to
0152   % the output object
0153   if isa(d1, 'xydata')
0154     d = d1;
0155   else
0156     d = d2;
0157   end
0158   % then we create an output xydata
0159   do = xydata(d.x, x);
0160   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0161   do = set(do, 'xunits', d.xunits);
0162   do = set(do, 'yunits', d.yunits);
0163   
0164 elseif isa(d1, 'cdata') || isa(d2, 'cdata')
0165   % then we create an output cdata
0166   % select which object gives properties to
0167   % the output object
0168   if isa(d1, 'cdata')
0169     d = d1;
0170   else
0171     d = d2;
0172   end
0173   % then we create an output cdata
0174   do = cdata(x);
0175   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0176   do = set(do, 'xunits', d.xunits);
0177   do = set(do, 'yunits', d.yunits);
0178   
0179 else
0180   error('### unknown data type - 2');
0181 end
0182 
0183   
0184 % END

Generated on Mon 03-Sep-2007 12:12:34 by m2html © 2003