Aim:
To simulate and analyze the performance of M-ary PSK by plotting its signal waveform, BER, PSD and
constellation diagram.

Program:
clc;
clear all;
close all
f=100;
t=0:10^-4:1/f;
x=sin (2*pi*f*t);
subplot (3,2,1);
plot (t,x);
xlabel('time period');
ylabel('Amplitude ');
title('original signal');
fs=20*f;
ts=1/fs;
t1=0:ts:1/f;
y=sin(2*pi*f*t1);
subplot(3,2,2);
stem(t1,y);
xlabel('time period');
ylabel('Amplitude ');
title('sampled signal');
n=3;m=2^n;
vh=max(y);
vl=min(y);
s=(vh-vl)/(2^n);
partition=[(vl+s):s:(vh-s)];
codbook=[vl+s/2 : s :vh-s/2] ;
[index,quant]=quantiz(y,partition,codbook);
subplot(3,2,3)
stairs(t1,quant)
xlabel('time period(sec)')
ylabel('Amplitude (volts)')
title('quantized signal')
y1=de2bi(index,'left-msb');
y2=y1';
y3=y2(:);
t2=0:n*length(t1)-1;
subplot(3,2,4);
stairs(t2,y3);
axis([0 30 -2 3]);
xlabel('time period');
ylabel('Amplitude (volts)');
title('PCM signal');
n=length(y3);
t3=0:0.01:n;
for i = 1:n
if (y3(i) == 0)
pi(i) = -1;
else
pi(i) = 1;
end
bw((i*100:(i+1)*100)) = pi(i);
for j = i:0.1:i+1
if (mod(i,2) == 0)
bw_e((i*100:(i+1)*100)) = pi(i);
bw_e(((i+1)*100:(i+2)*100)) = pi(i);
else
bw_o((i*100:(i+1)*100)) = pi(i);
bw_o(((i+1)*100:(i+2)*100)) = pi(i);
end
if (mod(n,2)~= 0)
bw_e((n*100:(n+1)*100)) = -1;
bw_e(((n+1)*100:(n+2)*100)) = -1;
end
end
end
bw = bw(100:end);
figure
subplot(3,2,1);
plot(t3,bw);
axis([0 n -2 +2]);
xlabel('time period');
ylabel('Amplitude');
title('Input Bit Stream');
bw_o = bw_o(100:(n+1)*100);
bw_e = bw_e(200:(n+2)*100);
subplot(3,2,2);
plot(t3,bw_o);
axis([0 n -2 +2]);
xlabel('time period');
ylabel('Amplitude');
title('Odd Sequence');
subplot(3,2,3);
plot(t3,bw_e);
xlabel('time period');
ylabel('Amplitude');
title('Even Sequence');
cost = cos(2*3.14*t3);
sint = sin(2*3.14*t3);
x = bw_o.*cost;
y = bw_e.*sint;
z = x+y;
subplot(3,2,4);
plot(t3,x);
axis([0 n -2 +2]);
xlabel('time period');
ylabel('Amplitude');
title('Odd Sequence BPSK Modulated Wave');
subplot(3,2,5);
plot(t3,y);
axis([0 n -2 +2]);
xlabel('time period');
ylabel('Amplitude');
title('Even Sequence BPSK Modulated Wave');
subplot(3,2,6);
axis([0 n -2 +2]);
plot(t3,z);
xlabel('time period');
ylabel('Amplitude');
title('QPSK Modulated Wave');
num_bit=length(z);%number of bit
snrdb=1:1:10;
snrlin=10.^(snrdb/10);
for snrdb=1:1:10
w=awgn(z,snrdb);
r=w;
bw_o1=sign(real(r));
bw_e1=sign(imag(r));
ber1=(num_bit-sum(bw_o==bw_o1))/num_bit;
ber2=(num_bit-sum( bw_e== bw_e1))/num_bit;
ber(snrdb)=mean([ber1 ber2]);
end
snrdb=1:1:10;
snrlin=10.^(snrdb./10);
tber=0.5.*erfc(sqrt(snrlin));
figure
semilogy(snrdb,ber,'-*'),grid on,hold on;
semilogy(snrdb,tber,'r'),grid on,hold on;
title(' error comparison');
xlabel(' SNR(dB)');
ylabel('Bit error rate');
legend('simulation','theorytical');
axis([0 10 10^-5 1]);
figure
pwelch(z);
num_sym = length(z);
int_sym = randi([1, 4], 1, num_sym);
qpsk_sym = zeros(size(int_sym));
qpsk_sym(int_sym == 1) = 1 + 1i;
qpsk_sym(int_sym == 2) = 1 - 1i;
qpsk_sym(int_sym == 3) = - 1 + 1i;
qpsk_sym(int_sym == 4) = - 1 - 1i;
tx_sig = qpsk_sym;
figure;
plot_lims = [-3 3];
plot(real(qpsk_sym), imag(qpsk_sym), 'ored');
xlim(plot_lims);
ylim(plot_lims);
line(xlim,[0,0]);
line([0,0],ylim);
title('QPSK constellation');
xlabel('real part');
ylabel('imaginary part')

Algorithm:
1. Clear command window.
2. Clear all variables in work space.
3. Generate the sinusoidal signal.
4. Generate the sampled signal for the original signal.
5. Generate the quantized signal.
6. Generate the pulse code modulated signal.
7. Take the length of pcm signal.
8. Divide the pcm signal into odd sequence and even sequence.
9. Increase transmitted pulse width and store in a variable.
10. Plot the input sequence , odd sequence and even sequence.
11. Generate two carrier signals.
12. Generate and plot odd bpsk sequence and even bpsk sequence.
13. Combine the odd bpsk sequence and even bpsk sequence to generate qpsk signal.
14. Plot the qpsk signal.
15. Take the snr in dB.
16. Add additive white Gaussian noise to the modulated signal.
17. Compare symbol error rate or BER between theoretical and simulated.
18. Plot the power spectral density.
19. Plot the constellation diagram.