mf2mf.m

Package [view]: fuzzy.rar
Upload User: hnchenxi
Upload Date: 2008-11-02
Package Size: 1083k
Code Size: 7k
Category: AI-NN-PR
Development Platform: Matlab
  1. function [outParams,errorStr]=mf2mf(inParams,inType,outType,tol)
  2. %MF2MF Translates parameters between membership functions.
  3. %   Synopsis
  4. %   outParams = mf2mf(inParams,inType,outType)
  5. %   
  6. %   Description
  7. %   This function translates any built-in membership function type into 
  8. %   another, in terms of its parameter set. mf2mf tries to mimic the symmetry 
  9. %   points for both the new and old membership functions. Occasionally this 
  10. %   translation results in lost information, so that if the output parameters 
  11. %   are translated back into the original membership function type, the 
  12. %   transformed membership function will not look the same as it did 
  13. %   originally. 
  14. %   The input arguments for mf2mf are as follows: 
  15. %   inParams: The parameters of the membership function you are transforming
  16. %   inType: a string name for the type of membership function you are 
  17. %   transforming
  18. %   outType: a string name for the new membership function you are transforming 
  19. %   to.
  20. %   Examples
  21. %   x=0:0.1:5;
  22. %   mfp1 = [1 2 3];
  23. %   mfp2 = mf2mf(mfp1,'gbellmf','trimf');
  24. %   plot(x,gbellmf(x,mfp1),x,trimf(x,mfp2))
  25. %
  26. %   See also DSIGMF, GAUSSMF, GAUSS2MF, GBELLMF, EVALMF, PIMF,
  27. %   PSIGMF, SIGMF, SMF, TRAPMF, TRIMF, ZMF.
  28. %   Ned Gulley, 6-17-94
  29. %   Copyright 1994-2002 The MathWorks, Inc. 
  30. %   $Revision: 1.24 $  $Date: 2002/04/02 21:25:21 $
  31. yWaist=0.5;
  32. yShoulder=0.90;
  33. outParams=[];
  34. errorStr=[];
  35. if strcmp(inType,'trimf'),
  36.     lftWaist=yWaist*(inParams(2)-inParams(1))+inParams(1);
  37.     lftShoulder=yShoulder*(inParams(2)-inParams(1))+inParams(1);
  38.     rtShoulder=(1-yShoulder)*(inParams(3)-inParams(2))+inParams(2);
  39.     rtWaist=(1-yWaist)*(inParams(3)-inParams(2))+inParams(2);
  40. elseif strcmp(inType,'trapmf') | strcmp(inType,'pimf'),
  41.     lftWaist=yWaist*(inParams(2)-inParams(1))+inParams(1);
  42.     lftShoulder=yShoulder*(inParams(2)-inParams(1))+inParams(1);
  43.     rtShoulder=(1-yShoulder)*(inParams(4)-inParams(3))+inParams(3);
  44.     rtWaist=(1-yWaist)*(inParams(4)-inParams(3))+inParams(3);
  45. elseif strcmp(inType,'gaussmf'),
  46.     lftWaist=-abs(inParams(1))*sqrt(-2*log(yWaist))+inParams(2);
  47.     lftShoulder=-abs(inParams(1))*sqrt(-2*log(yShoulder))+inParams(2);
  48.     rtShoulder=2*inParams(2)-lftShoulder;
  49.     rtWaist=2*inParams(2)-lftWaist;
  50. elseif strcmp(inType,'gauss2mf'),
  51.     lftWaist=-abs(inParams(1))*sqrt(-2*log(yWaist))+inParams(2);
  52.     lftShoulder=inParams(2);
  53.     rtShoulder=inParams(4);
  54.     rtWaist=abs(inParams(3))*sqrt(-2*log(yWaist))+inParams(4);
  55. elseif strcmp(inType,'gbellmf'),
  56.     lftWaist=-inParams(1)*((1/yWaist-1)^(1/(2*inParams(2))))+inParams(3);
  57.     lftShoulder=-inParams(1)*((1/yShoulder-1)^(1/(2*inParams(2))))+inParams(3);
  58.     rtShoulder=2*inParams(3)-lftShoulder;
  59.     rtWaist=2*inParams(3)-lftWaist;
  60. elseif strcmp(inType,'sigmf'),
  61.     if inParams(1)>0,
  62.         lftWaist=inParams(2);
  63.         lftShoulder=-1/inParams(1)*log(1/yShoulder-1)+inParams(2);
  64.         rtShoulder=2*lftShoulder-lftWaist;
  65.         rtWaist=2*rtShoulder-lftWaist;
  66.     else
  67.         rtWaist=inParams(2);
  68.         rtShoulder=-1/inParams(1)*log(1/yShoulder-1)+inParams(2);
  69.         lftShoulder=rtShoulder;
  70.         lftWaist=2*lftShoulder-rtWaist;
  71.     end
  72. elseif strcmp(inType,'dsigmf'),
  73.     lftWaist=inParams(2);
  74.     lftShoulder=-1/inParams(1)*log(1/yShoulder-1)+inParams(2);
  75.     rtWaist=inParams(4);
  76.     rtShoulder=1/inParams(3)*log(1/yShoulder-1)+inParams(4);
  77. elseif strcmp(inType,'psigmf'),
  78.     lftWaist=inParams(2);
  79.     lftShoulder=-1/inParams(1)*log(1/yShoulder-1)+inParams(2);
  80.     rtWaist=inParams(4);
  81.     rtShoulder=-1/inParams(3)*log(1/yShoulder-1)+inParams(4);
  82. elseif strcmp(inType,'smf'),
  83.     lftWaist=yWaist*(inParams(2)-inParams(1))+inParams(1);
  84.     lftShoulder=yShoulder*(inParams(2)-inParams(1))+inParams(1);
  85.     rtShoulder=inParams(2);
  86.     if inParams(1)<inParams(2),
  87.         lftWaist=inParams(1);
  88.         rtWaist=2*inParams(2)-inParams(1);
  89.     else
  90.         lftWaist=2*inParams(2)-inParams(1);
  91.         rtWaist=inParams(1);
  92.     end
  93. elseif strcmp(inType,'zmf'),
  94.     lftShoulder=inParams(2);
  95.     rtShoulder=inParams(2);
  96.     if inParams(1)<inParams(2),
  97.         lftWaist=inParams(1);
  98.         rtWaist=2*inParams(2)-inParams(1);
  99.     else
  100.         lftWaist=2*inParams(2)-inParams(1);
  101.         rtWaist=inParams(1);
  102.     end
  103. else
  104.     % Input MF type is unknown
  105.     outParams=[];
  106.     errorStr=['Cannot translate from input MF type ' inType];
  107.     if nargout<2, error(errorStr); end
  108.     return
  109. end
  110. % We've translated into generalized coordinates, now translate back into
  111. % MF specific parameters...
  112. if nargin<4,
  113.     tol=max(eps, 1e-3*(rtShoulder-lftShoulder));
  114. end
  115. if strcmp(outType,'trimf'),
  116.     center=(rtShoulder+lftShoulder)/2;
  117.     % Assumes yWaist=0.5
  118.     outParams=[2*lftWaist-center center 2*rtWaist-center];
  119. elseif strcmp(outType,'trapmf'),
  120.     % Assumes yWaist=0.5
  121.     outParams=[2*lftWaist-lftShoulder lftShoulder rtShoulder 2*rtWaist-rtShoulder];
  122.     
  123. elseif strcmp(outType,'pimf'),
  124.     % Assumes yWaist=0.5
  125.     outParams=[2*lftWaist-lftShoulder lftShoulder rtShoulder max(tol,2*rtWaist-rtShoulder)];
  126. elseif strcmp(outType,'gbellmf'),
  127.     center=(rtShoulder+lftShoulder)/2;
  128.     a=max(tol,center-lftWaist);
  129.     b=2*a/(max(tol,lftShoulder-lftWaist));
  130.     outParams=[a b center];
  131. elseif strcmp(outType,'gaussmf'),
  132.     center=(rtShoulder+lftShoulder)/2;
  133.     sigma=max(tol,(rtWaist-center)/sqrt(-2*log(yWaist)));
  134.     outParams=[sigma center];
  135. elseif strcmp(outType,'gauss2mf'),
  136.     lftSigma=max(tol,lftShoulder-lftWaist)/sqrt(-2*log(yWaist));
  137.     rtSigma=max(tol,rtWaist-rtShoulder)/sqrt(-2*log(yWaist));
  138.     outParams=[lftSigma lftShoulder rtSigma rtShoulder];
  139. elseif strcmp(outType,'sigmf'),
  140.     center=lftWaist;
  141.     a=-1/max(tol,lftShoulder-center)*log(1/yShoulder-1);
  142.     outParams=[a center];
  143. elseif strcmp(outType,'dsigmf'),
  144.     lftCenter=lftWaist;
  145.     lftA=-1/max(tol,lftShoulder-lftCenter)*log(1/yShoulder-1);
  146.     rtCenter=rtWaist;
  147.     rtA=-1/max(tol,rtCenter-rtShoulder)*log(1/yShoulder-1);
  148.     outParams=[lftA lftCenter rtA rtCenter];
  149. elseif strcmp(outType,'psigmf'),
  150.     lftCenter=lftWaist;
  151.     lftA=-1/max(tol,lftShoulder-lftCenter)*log(1/yShoulder-1);
  152.     rtCenter=rtWaist;
  153.     rtA=1/max(tol,rtCenter-rtShoulder)*log(1/yShoulder-1);
  154.     outParams=[lftA lftCenter rtA rtCenter];
  155. elseif strcmp(outType,'smf'),
  156.     % Assumes yWaist=0.5
  157.     outParams=[2*lftWaist-lftShoulder lftShoulder];
  158. elseif strcmp(outType,'zmf'),
  159.     % Assumes yWaist=0.5
  160.     outParams=[rtShoulder 2*rtWaist-rtShoulder];
  161. else
  162.     % Output MF type is unknown
  163.     outParams=[];
  164.     errorStr=['Cannot translate to output MF type ' outType];
  165.     if nargout<2, error(errorStr); end
  166.     return
  167. end
  168. outParams=eval(mat2str(outParams,4));