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

//function bpskber1

printf('***** BPSK Bit error rate simulation *****\n');
//generate 10000 bpsk data
rand("uniform");
tx = round(rand(1,10000));
x=1-2*tx;
sigpower=mean(x^2);

for(sn=0:10)
  rand("normal");
  awgn=rand(1,10000);
  awgnpower=mean(awgn^2);
  awgn = awgn/sqrt(awgnpower)*10^(-sn/20)*sqrt(sigpower);
  y = x + awgn;
  scf();  histplot(100,y); 
  rx= 0.5 - 0.5*sign(y);
  err = sum(abs(rx-tx));
  ber = err / length(tx);
  printf('SN =%6.2f BER =%7.4f \n' ,sn, ber)
end;

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

//endfunction;
