% showrfs.m - function to show receptive fields
%
% function showrfs(A,bg)
%
% A = rf matrix
% bg = 'black' or 'white' (default='black')

function showrfs(A,bg)
  
if ~exist('bg','var')
  bg='black';
end

[L M]=size(A);

sz=sqrt(L);

buf=1;

if floor(sqrt(M))^2 ~= M
  m=sqrt(M/2);
  n=M/m;
else
  m=sqrt(M);
  n=m;
end

if bg=='black'
  array=-ones(buf+m*(sz+buf),buf+n*(sz+buf));
else
  array=ones(buf+m*(sz+buf),buf+n*(sz+buf));
end

k=1;

for j=1:m
  for i=1:n

    clim=max(abs(A(:,k)));
    array(buf+(i-1)*(sz+buf)+[1:sz],buf+(j-1)*(sz+buf)+[1:sz])=...
	reshape(A(:,k),sz,sz)/clim;
    k=k+1;
    
  end
end

imagesc(array,[-1 1]), axis image off
