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.15 2007/10/24 17:35:28 ingo 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.15 2007/10/24 17:35:28 ingo Exp $
0008 %
0009 
0010 ALGONAME = mfilename;
0011 VERSION  = '$Id: aooperate.m,v 1.15 2007/10/24 17:35:28 ingo 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     
0030     if any(size(a) > 1)
0031       error('### The operation works only with single analysis objects. Do not use vector or matrices of analysis objects.')
0032     end
0033     
0034     as  = [as a];
0035   else
0036     if isnumeric(a)
0037       % create an ao from the values
0038       as = [as ao(a)];
0039     else
0040       error('### AO/plus inputs must be either numeric or AO.');
0041     end
0042   end
0043 end
0044 
0045 if length(as) > 2
0046   warning('!!! I only work with the first two input AOs. Ignoring all others.');
0047   warning('!!! Perhaps you have input an AO array?');
0048 end
0049 
0050 %%%%%%%%%%   Now we have two AOs   %%%%%%%%%%
0051 a1 = as(1);
0052 a2 = as(2);
0053 clear as;
0054 d1 = a1.data;
0055 d2 = a2.data;
0056 
0057 
0058 %%%%%%%%%%   Check types   %%%%%%%%%%
0059 if (isa(d1, 'fsdata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'fsdata'))
0060   error('### can''t add time-series data to frequency-series data.');
0061 end
0062 if (isa(d1, 'xydata') && isa(d2, 'tsdata')) || (isa(d1, 'tsdata') && isa(d2, 'xydata'))
0063   error('### can''t add XY data to time-series data.');
0064 end
0065 if (isa(d1, 'xydata') && isa(d2, 'fsdata')) || (isa(d1, 'fsdata') && isa(d2, 'xydata'))
0066   error('### can''t add XY data to frequency-series data.');
0067 end
0068 
0069 %%%%%%%%%%   get data   %%%%%%%%%%
0070 [x1, y1] = get_xy_values(a1.data);
0071 [x2, y2] = get_xy_values(a2.data);
0072 ny1 = length(y1);
0073 ny2 = length(y2);
0074 
0075 % Some special cases
0076 % switch op
0077 %   case '*'
0078 %     if size(y1) == size(y2)
0079 %       y1 = y1.';
0080 %     end
0081 % end
0082 
0083 
0084 %%%%%%%%%%   check data lengths   %%%%%%%%%%
0085 if isa(d1, 'cdata') && ~isa(d2, 'cdata')
0086   if ny1 > 1 && ny1~=ny2
0087     error('###1 cdata type must be same length as other AO, or single valued.');
0088   end
0089 elseif isa(d2, 'cdata') && ~isa(d1, 'cdata')
0090   if ny2 > 1 && ny1~=ny2
0091     error('###2 cdata type must be same length as other AO, or single valued.');
0092   end  
0093 end
0094 
0095 if ny1~=ny2
0096   if ny1~=1 && ny2~=1
0097     error('### data vectors must be the same length or one should be single valued.');
0098   end
0099 end
0100 
0101 %%%%%%%%%%   check units   %%%%%%%%%%
0102 if isa(d1, 'fsdata') && isa(d2, 'fsdata')
0103   if ~strcmp(d1.xunits, d2.xunits)
0104     error('### xunits must be the same for same type of AOs.');
0105   end
0106   % check X-axis
0107   if d1.f ~= d2.f
0108     error('### frequency vectors are not the same.');
0109   end
0110 end
0111 if isa(d1, 'tsdata') && isa(d2, 'tsdata')
0112   if ~strcmp(d1.xunits, d2.xunits)
0113     error('### xunits must be the same for same type of AOs.');
0114   end
0115 end
0116 if isa(d1, 'xydata') && isa(d2, 'xydata')
0117   if ~strcmp(d1.xunits, d2.xunits)
0118     error('### xunits must be the same for same type of AOs.');
0119   end
0120 end
0121 
0122 %%%%%%%%%%   Add the data   %%%%%%%%%%
0123 disp(sprintf('*** applying %s', op))
0124 eval(sprintf('x = y1%sy2;', op));
0125 
0126 %%%%%%%%%%   Create output data object   %%%%%%%%%%
0127 
0128 if isa(d1, 'fsdata') || isa(d2, 'fsdata')
0129   % then we create an output fsdata
0130   % select which object gives properties to
0131   % the output object
0132   if isa(d1, 'fsdata')
0133     d = d1;
0134   else
0135     d = d2;
0136   end
0137   % then we create an output tsdata
0138   do = fsdata(d.f, x, d.fs);
0139   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0140   do = set(do, 'xunits', d.xunits);
0141   do = set(do, 'yunits', d.yunits);
0142   do = set(do, 'enbw', d.enbw);
0143 
0144 elseif isa(d1, 'tsdata') || isa(d2, 'tsdata')
0145   % select which object gives properties to
0146   % the output object
0147   if isa(d1, 'tsdata')
0148     d = d1;
0149   else
0150     d = d2;
0151   end
0152   % then we create an output tsdata
0153   do = d;
0154   do = set(do, 'x', x);
0155   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0156   
0157 elseif isa(d1, 'xydata') || isa(d2, 'xydata')
0158   % select which object gives properties to
0159   % the output object
0160   if isa(d1, 'xydata')
0161     d = d1;
0162   else
0163     d = d2;
0164   end
0165   % then we create an output xydata
0166   do = xydata(d.x, x);
0167   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0168   do = set(do, 'xunits', d.xunits);
0169   do = set(do, 'yunits', d.yunits);
0170   
0171 elseif isa(d1, 'cdata') || isa(d2, 'cdata')
0172   % then we create an output cdata
0173   % select which object gives properties to
0174   % the output object
0175   if isa(d1, 'cdata')
0176     d = d1;
0177   else
0178     d = d2;
0179   end
0180   % then we create an output cdata
0181   do = cdata(x);
0182   do = set(do, 'name', sprintf('(%s) %s (%s)', d1.name, op, d2.name));
0183   do = set(do, 'xunits', d.xunits);
0184   do = set(do, 'yunits', d.yunits);
0185   
0186 else
0187   error('### unknown data type - 2');
0188 end
0189 
0190   
0191 % END

Generated on Thu 01-Nov-2007 09:42:34 by m2html © 2003