% function [ber_lin] = linear(N_T,N_R,snr_db,nSymbols)
% Spatial multiplexing MIMO communication system with non-linear MMSE
%-------------------------------------------------------------------
clear all
close all
% % Configuration
N_T = 4;%4个输出天线
N_R = 4;%4个输入天线
number_frame = 100;%传输信号数量
frame_length = 100;
%snr_db=15;%信噪比
snr_db=0:2:40;%信噪比
% N=nSymbols; % Number of Symbols transmitted
sigma_s = 1;%信号的能量
snr = exp(snr_db*log(10)/10);
ne_lin=0;
sigma_c = 1;
const=[-1-sqrt(-1),-1+sqrt(-1),1+sqrt(-1),1-sqrt(-1)]/sqrt(2);
%Q= (sigma_c*randn(N_R,N_T)+sqrt(-1)*(sigma_c*randn(N_R,N_T)))/sqrt(2);
%ber_lin=[];
for i=1:length(snr_db)
sigma_n = sqrt( N_T/(snr(i)) )*sigma_s;
% Channel Matrix
for l = 1:number_frame
H = (sigma_c*randn(N_R,N_T)+sqrt(-1)*(sigma_c*randn(N_R,N_T)))/sqrt(2);
G = (H*H' + (sigma_n^2/sigma_s^2)*eye(N_R))\H;
for j = 1:frame_length
s = complex(sign(randn(N_T,1)),sign(randn(N_T,1)))/sqrt(2);
n = (sigma_n/sqrt(2))*complex(randn(N_R,1),randn(N_R,1));
r = H*s + n;
%% pro detection
%linear
s_hat=G'*r;
%% Error counting()
[x1] = find(sign(real(s_hat))~= sign(real(s)));
ne_lin = ne_lin + length(x1);
[x2] = find(sign(imag(s_hat))~= sign(imag(s)));
ne_lin = ne_lin + length(x2);
end
end
%display(ne_lin)
ber_lin(i)=ne_lin/(number_frame*N_T*frame_length)
ne_lin=0;
end
% semilogy (snr_db,ber_lin,'r');
semilogy(snr_db,ber_lin,'-r^','LineWidth',2);
hold on; grid on;
xlabel('snr(dB)');
ylabel('ber');