//Program: berbpsk.sce
//Fire Wada
//
//Usage:
//Execute-> Load into Scilab (or Ctrl L)

//function bpskber1

printf('***** HW4 Bit error rate simulation *****\n');
//generate 100000 bit data
rand("uniform");
tx = round(rand(1,100000));
tx = matrix (tx, 50000,2);
x1 = 2*tx(:,1) + tx(:,2);
mod = [3/sqrt(10), 1/sqrt(10), -1/sqrt(10), -3/sqrt(10)];
x2 = mod(x1+1);
scf();  histplot(100,x2);
sigpower=mean(x2^2)

for(sn=0:5:20)
  rand("normal");
  awgn=rand(1,50000);
  awgnpower=mean(awgn^2);
  awgn = awgn/sqrt(awgnpower)*10^(-sn/20)*sqrt(sigpower);
  y2 = x2 + awgn;
  scf();  histplot(100,y2); 
  //
  // CALCULATE Bit error rate
  //
end;

printf('***** END *****\n\n');

//endfunction;
