% construct a ny * nx perturbation
%
% function z = mkbump(nx,ny,x,y,amp,wx,wy,shape);
%
% output variable
%   z      matrix perturbation has rows of y and columns of x
%
% input variables
%   nx     x-dimension of domain (default 64)
%   ny     y-dimension of domain (default nx)
%   x      x-coordinate of perturbation (default centered)
%   y      y-coordinate of perturbation (default centered)
%   amp    perturbation amplitude (default 1)
%   wx     x-width of perturbation (default nx)
%   wy     y-width of perturbation (default wx)
%          wy <= 0 -> circular perturbation with diameter wx
%   shape  perturbation shape (default 'hanning')

function z = mkbump(nx,ny,x,y,amp,wx,wy,shape);

% check input arguments

if nargin < 1, nx    = 64;        end
if nargin < 2, ny    = nx;        end
if nargin < 3, x     = (nx+1)/2;  end
if nargin < 4, y     = (ny+1)/2;  end
if nargin < 5, amp   = 1;         end
if nargin < 6, wx    = nx;        end
if nargin < 7, wy    = wx;        end
if nargin < 8, shape = 'hanning'; end
if ~isstr(shape), error('argument shape must be a string'); end
if nx<=0 | ny<=0 | wx<=0
  error('arguments nx, ny and wx cannot be negative or zero');
end

rflag=(wy > 0);        % rectangular/circular perturbation flag
if ~rflag, wy=wx; end  % for circular perturbation x-width = y-width

z=zeros(ny,nx);

% compute indices of perturbation

ix=ceil(x-wx/2):floor(x+wx/2);
wx=length(ix);
px=find(ix>0&ix<=nx);                   % retain x-indices inside domain
ix=ix(px);                         

iy=ceil(y-wy/2):floor(y+wy/2);
wy=length(iy);
py=find(iy>0&iy<=ny);                   % retain y-indices inside domain
iy=iy(py);                         

if isempty(ix) | isempty(iy)
  error('arguments do not define bump inside domain')
end

if rflag                                % rectangular perturbation
  
  eval(['tmp=amp*ones(wy,1)*' shape '(wx)'';']);
  eval(['tmp=tmp.*(' shape '(wy)*ones(1,wx));']);
  z(iy,ix)=tmp(py,px);

else                                    % circular perturbation

  if shape ~= 'hanning'
    error('at present circular bump only works with hanning window')
  else
    shape = ['my' shape];
  end
  [xx yy]=meshgrid(ix,iy);
  r=abs(x-xx+sqrt(-1)*(y-yy));
  eval(['tmp=amp*' shape '(wx,r);']);
  z(iy,ix)=tmp;

end
