function [BER] = MMSE(Nr)
rho_dB = -5:5:20;
%generate sequence
X1 = (2*(rand(2,1e5)>0.5)-1) + sqrt(-1)*(2*(rand(2,1e5)>0.5)-1); %
X = (1./sqrt(2))*X1;
%generate channel H K noise
H = (randn(Nr,2)+sqrt(-1)*randn(Nr,2))./sqrt(2);
for p = 1:length(rho_dB)
rho = 10.^((p-6)./10);
Kmmse = (H'*H+(2./rho)*eye(2,2))\H';
W = sqrt(2./rho)*(randn(Nr,1e5)+sqrt(-1)*randn(Nr,1e5))./sqrt(2);
%pass through the channel
Y = zeros(Nr,1e5);
Z = zeros(2,1e5);
for i = 1:1e5
Y(:,i) = H*X(:,i)+W(:,i);
Z(:,i) = Kmmse*Y(:,i);
end
%slicer
z_re = real(Z);
z_im = imag(Z);
Z(z_re < 0 & z_im < 0) = -1-1i;
Z(z_re >= 0 & z_im > 0) = 1+1i;
Z(z_re < 0 & z_im >= 0) = -1+1i;
Z(z_re >= 0 & z_im < 0) = 1-1i;
%calculate BER
M = zeros(2,1e5);
for i = 1:2
for j = 1:1e5
M(i,j) = (abs(Z(i,j)-X1(i,j))).^2;
end
end
a1 = numel(find(M(1,:)==8));
a2 = numel(find(M(2,:)==8));
b1 = numel(find(M(1,:)==4));
b2 = numel(find(M(2,:)==4));
a = a1+a2;
b = b1+b2;
BER(p) = (a.*2+b)./400000;
end