function [x,Nret]=decreg2(x,N,L,noi)
%noise is the string that has 
%generate QAM constellation, for now 4 is the default
%x is column vector containing the constellation
%N is the number of spaces on each axis
%L is the square area that we look at
%noise a column vector with the  name of the noise file: needs x and y coord.
%       and the number of noises must = number of x's s.t. the first 
%	noise that you input corresponds to the first constellation point
%	all the rows of this matrix must have same length strings


d=1;
noise_spacex=linspace(-L*d,L*d,N);
noise_spacey=linspace(-L*d,L*d,N);

for q=1:N
for s=1:N
noise_space(q,s)=noise_spacex(q)+noise_spacey(s)*j;
end
end
NS=zeros(length(x),N,N);
PE=zeros(length(x),N,N);

size(NS(1,:,:))
size(noise_space)
pause

for r=1:length(x)
r
 NS(r,:,:)=noise_space-x(r);
end


for r=1:length(x)
PE(r,:,:)=feval(noi(r,:),real(NS(r,:, :)),imag(NS(r,:, :)));
end





for q=1:N
for s=1:N
rr=[];
for r=1:length(x)
rr=[rr PE(r,q,s)];
end
ind=find(abs(rr-max(rr))<10^-8);
if(length(ind)==1)
  Dec_reg(q,s)=ind(1); %takes first argument indiscriminately
else
  Dec_reg(q,s)=length(x)+1;
end
end
end

for k=1:length(x)+1
  eval(['[I' num2str(k) ',J'num2str(k) ']=find(Dec_reg==k);']); 
end

pi
a=['yo';'mo';'co';'ro';'go';'bo';'wo';'ko']

clf
hold on
for k=1:length(x)+1
n=rem(k,length(a)-1)+1;
eval(['plot(noise_spacex(I' num2str(k) '),noise_spacey(J' num2str(k) '),a(n,:))']);
if(k==length(x)+1)
eval(['plot(noise_spacex(I' num2str(k) '),noise_spacey(J' num2str(k) '),a(8,:))']);
end
end 







