home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / DATAFUN.DI$ / CONV2.M < prev    next >
Encoding:
Text File  |  1993-03-22  |  2.0 KB  |  70 lines

  1. function c = conv2(u,b,shape)
  2. %CONV2    Two dimensional convolution.
  3. %    C = CONV2(A, B) performs the 2-D convolution of matrices
  4. %    A and B.   If [ma,na] = size(A) and [mb,nb] = size(B), then 
  5. %    size(C) = [ma+mb-1,na+nb-1]. 
  6. %
  7. %    C = CONV2(A,B,'shape') returns a subsection of the 2-D
  8. %    convolution with size specified by 'shape':
  9. %      'full'  - (default) returns the full 2-D convolution, 
  10. %      'same'  - returns the central part of the convolution 
  11. %                that is the same size as A.
  12. %      'valid' - returns only those parts of the convolution
  13. %                that are computed without the zero-padded
  14. %                edges, size(C) = [ma-mb+1,na-nb+1] when
  15. %                size(A) > size(B).
  16. %    CONV2 is fastest when size(A) > size(B).
  17. %
  18. %    See also XCORR2, CONV, XCORR, DECONV, FILTER2.
  19.  
  20. %    Marc Ullman   2-6-86
  21. %    Revised 2-18-87 J.N. Little
  22. %    Revised 10-22-91 L. Shure, C. Thompson
  23. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  24.  
  25. error(nargchk(2,3,nargin));
  26. if nargin<3, shape = 'full'; end
  27.  
  28. code = [shape,' ']; code = code(1);
  29. if isempty(find(code=='svf')), error('Unknown shape parameter.'); end
  30.  
  31. if prod(size(u))<prod(size(b)), % Switch arguments for faster computation.
  32.   tmp = u; u = b; b = tmp;
  33.   switched = 1;
  34. else
  35.   switched = 0;
  36. end
  37.   
  38. [mu,nu] = size(u);
  39. sten = rot90(b,2);
  40. [ms,ns] = size(sten);
  41.  
  42. c = zeros(ms+mu-1,ns+nu-1);
  43. for i=1:ms
  44.     for j=1:ns
  45.         w = sten(ms-i+1,ns-j+1);
  46.         if w~=0,
  47.             c(i:i+mu-1,j:j+nu-1) = c(i:i+mu-1,j:j+nu-1) + w*u;
  48.         end
  49.     end
  50. end
  51.  
  52. if code=='s',
  53.   % Return the central part that is the same size as the original U.
  54.   if switched,
  55.     rows = floor((mu-1)/2) + (1:ms);
  56.     cols = floor((nu-1)/2) + (1:ns);
  57.     c = c(rows,cols);
  58.   else
  59.     rows = floor((ms-1)/2) + (1:mu);
  60.     cols = floor((ns-1)/2) + (1:nu);
  61.     c = c(rows,cols);
  62.   end
  63.  
  64. elseif code == 'v',
  65.   % Return the central part that is computed without the padded edges.
  66.   rows = ms-1 + (1:mu-ms+1);
  67.   cols = ns-1 + (1:nu-ns+1);
  68.   c = c(rows,cols);
  69. end
  70.