clc
clear all;
nb = 25;
c = 2*(randn(nb,1) > 0)-1;
Fs = 5e3;
Ts = 1/Fs;
T = 1e-2;
fc = 1e2;
phi = 0;
t = 0:Ts: nb*T - Ts;
[x,xc,btilda] = BPSK_MOD(c, fc, phi, T, Ts);
figure;
subplot 311; plot(t, btilda); title('Information bits');
ylabel('amplitude'); xlabel('time');
subplot 312; plot(t, xc); title('Carrier Signal');
ylabel('amplitude'); xlabel('time');
subplot 313; plot(t, x); title('BPSK modulated Signal');
ylabel('amplitude'); xlabel('time');
SNR = 10;
sigma = 10^(-SNR/20);
noise = sigma*randn(size(x));
x = x + noise;
bhat = BPSK_DMOD(x, fc, phi, Ts);
bRcvd = receivedSignalBitEst(bhat, T, Ts);
figure;
subplot 311; plot(x); title('Received data signal');grid on;
subplot 312; plot(noise); title('Noise level ');grid on;
subplot 313; plot(bhat);title('Received data signal'); grid on;
bErr = length(find((c(1:end-1) - bRcvd)~= 0));
fprintf('Total bits are in Error: %d\n', bErr);