function [out,f] = simroommix( in, sp, mp)
% Return a synthetic room recording simulation
%
%  Usage:
%    [out,f] = simroommix( in, p, m);
%
%  Inputs:
%    in: NxM matrix, containing one source in every row
%    sp: Nx3 matrix, nth row containing the position of the nth source
%    mp: Nx3 matrix, nth row containing the position of the nth microphone
%
%  Outputs:
%    out: If there is only one output it returns the mixing
%         filters in an NxN FIR matrix structure,
%         otherwise an NxM matrix containing the mixtures
%    f: Contains the mixing filters in an NxN FIR matrix structure
%
%  Notes:
%    The positions of each mike and the source are encoded in 3 elements
%    denoting the 3 coordinates in the room.  The room size is 10x10x10.  So
%    for example the position in the middle of the floor is [5 5 0]

% Get size of problem
[r,c] = size( in);

% Default Mic and source room positions
if nargin == 1
  sp = [ 0  5 5;
         10 5 5];
  mp = [ 4 6 5;
         6 5 5];
end

% Show positions in plot
clf
	hold on
for i = 1:r
	of = .3;
	plot3( sp(i,1), sp(i,2), sp(i,3), 'bx')
	text( sp(i,1)+of, sp(i,2)+of, sp(i,3)+of, sprintf( 'source %d', i))
	plot3( mp(i,1), mp(i,2), mp(i,3), 'ro')
	text( mp(i,1)+of, mp(i,2)+of, mp(i,3)+of, sprintf( 'mike %d', i))
end
axis( [0 10 0 10 0 10])
grid on
hold off
view( [-32 42])

% Make room filters
for i = 1:r
	for j = 1:r
		f{i,j} = simod( sp(j,:), mp(i,:), 5, .5, 10, 0);
	end
end

% Convolve and mix if needed
if nargout == 2
  out = zeros( size( in));
	for i = 1:r
    for j = 1:r
			out(i,:) = out(i,:) + fftfilt( f{i,j}, in(j,:));
		end
	end
end

% Just return the filters
if nargout == 1
  out = f;  
end
