Aim:
To Simulate the operation of Binary Phase Shift Keying using MATLAB.

Algorithm:
1. Clear command window
2. Clear all variables in workspace window
3. Length of PCM modulated binary is determined
4. Generate the sinusoidal expression (sig1) with frequency (f) to
200/300/400/500/600 Hz and time (t) from 0 to 10/f in interval of 10−4 sec
5. Generate the expression for sampled signal (sig) with time (t2) from 0 to 10/f
in interval of sampling frequency (Fs) of 2500/3500/4500/5500/6500 Hz
6. Generate the PCM signal
7. Generate the PSK signal
8. Calculate the SNRdB , SNR and BER for the generated PSK signal
9. Calculate the power spectral density using pwelch function.
10. plot the constellation diagram.

Program:
clc;
clear all;
close all;
f=150;
t=0:0.0001:4/f;
sig1=sin(2*3.14*f*t);
subplot(4,2,1);
plot(t,sig1);
title('message signal');
xlabel('time');
ylabel('amplitude');
fs1=10*f;
t=1/fs1;
Ts=0:t:4/f;
s2=sin(2*3.14*f*Ts);
subplot(4,2,2);
stem(Ts,s2);
title('sampled signal');
xlabel('samples');
ylabel('amplitude');
vh=max(s2);
vl=min(s2);
N=4;
M=2^N;
s=(vh-vl)/M;
partition=vl+s:s:vh-s;
codebook=vl+s/2:s:vh-s/2;
[index,quants]=quantiz(s2,partition,codebook);
subplot(4,2,3);
stairs(quants);
title('quantized signal');
xlabel('time');
ylabel('amplitude');
codesig=de2bi(index,'left-msb');
codesig=codesig(:);
tt=0:N*length(s2)-1;
subplot(4,2,4);
stairs(tt,codesig);
xlabel('time');
ylabel('amplitude');
title('PCM signal');
n = length(codesig);
t3 = 0:.01:n;
for i = 1:n
if (codesig(i) == 0)
b_p(i) = -1;
else
b_p(i) = 1;
end
for j = i:.1:i+1
bw((i*100:(i+1)*100)) = b_p(i);
end
end
bw = bw(100:end);
sint = sin(2*pi*1*t3);
psksig = bw.*sint;
subplot(4,2,5);
plot(t3,sint);
title('BPSK carrier signal');
xlabel('time');
ylabel('amplitude');
axis([1 30 -2 +2]);
subplot(4,2,7);
plot(t3,psksig);
title('BPSK signal');
xlabel('time');
ylabel('amplitude');
axis([1 30 -2 +2]);
subplot(4,2,6);
plot(t3,bw);
title('BPSK Wave form');
xlabel('time');
ylabel('amplitude');
axis([1 30 -2 +2]);
num_bit=length(psksig);
SNRdB=0:20;
SNR=10.^(SNRdB/10);
for(k=1:length(SNRdB))
y=awgn(complex(psksig),SNRdB(k));
error=0;
R=0;
M=[];
for(c=1:1:num_bit)
if (y(c)>0&&psksig(c)==-1)||(y(c)<0&&psksig(c)==1)
error=error+1;
end
end
error=error/num_bit;
m(k)=error;
end
figure
semilogy(SNRdB,m,'o','linewidth',2.5);
grid on;
hold on;
BER_th=(1/2)*erfc(sqrt(SNR));
semilogy(SNRdB,BER_th,'r','linewidth',2.5);
grid on;hold on;
title(' curve for Bit Error Rate verses SNR for Binary BPSK modulation');
xlabel(' SNR(dB)');
ylabel('BER');
legend('simulation','theorytical');
axis([0 20 10^-5 1]);

%PSD
figure
subplot(2,1,1);
pwelch(psksig,'psd');
title('Power spectral Density');
xlabel('Time');
ylabel('Amplitude')
subplot(2,1,2);
pwelch(y,'psd');
title('PSD of signal with noise');
xlabel('Time');
ylabel('Amplitude')

%CONSTELLATION
figure
pskcons(psksig<=0)=-1;
pskcons(psksig>=0)=1;
plot_lims=[-2 2];
axis([-2 2 -2 2])
plot(real(pskcons),imag(pskcons),'ored','linewidth',3);
grid on;
xlim(plot_lims);
ylim(plot_lims);
title(['BPSK constellation at an SNR of' num2str(length(SNRdB)) 'dB'])
xlabel('Real part');
ylabel('Imaginary part')