-
Notifications
You must be signed in to change notification settings - Fork 27
/
qamqpskbpsk.m
241 lines (188 loc) · 7 KB
/
qamqpskbpsk.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
Eb = 1;
SNR_dB = 0:2:13; % in dB
SNR = 10.^(SNR_dB./10);
%number of symbols for modulation schemes
sym_BPSK = 2;
sym_QPSK = 4;
sym_QAM = 16;
M = 4000;
%transmit-symbols for each modulation scheme
Es_BPSK = Eb*log2(sym_BPSK);
Es_QPSK = Eb*log2(sym_QPSK);
Es_QAM = Eb*log2(sym_QAM);
%transmit signal
signal_BPSK = randi([0,1],1,1000); %symbols transmitted
signal_QPSK = randi([0,3],1,2000);
signal_QAM = randi([0,15],1,1000); %16-QAM
symbol_QAM = [-3,-1,1,3];
totalerrorcount_BPSK = zeros(1,7);
totalerrorcount_QPSK = zeros(1,7);
totalerrorcount_QAM = zeros(1,7);
for MCruns = 1:15
txsignal_BPSK = zeros(1,1000);
y_BPSK = zeros(1,1000);
txsignal_QPSK = zeros(1,2000);
y_QPSK = zeros(1,1000);
txsignal_QAM = zeros(1,4000);
y_QAM = zeros(1,1000);
errorcount_BPSK = zeros(1,7);
errorcount_QPSK = zeros(1,7);
errorcount_QAM = zeros(1,7);
for n=1:1000
%BPSK
if(signal_BPSK(n) == 0)
txsignal_BPSK(n) = 1*exp(1i*(2*pi));
elseif(signal_BPSK(n) == 1)
txsignal_BPSK(n) = 1*exp(1i*(pi));
end
%QPSK
if (signal_QPSK(n) == 0)
txsignal_QPSK(n) = sqrt(Es_QPSK)*exp(1i*pi/4);
elseif(signal_QPSK(n) == 1)
txsignal_QPSK(n) = sqrt(Es_QPSK)*exp(1i*(3*pi/4));
elseif(signal_QPSK(n) == 2)
txsignal_QPSK(n) = sqrt(Es_QPSK)*exp(1i*(7*pi/4));
elseif(signal_QPSK(n) == 3)
txsignal_QPSK(n) = sqrt(Es_QPSK)*exp(1i*(5*pi/4));
end
end
%QAM generation
txsignal_QAM = (randsrc(1,M,symbol_QAM) + 1i*randsrc(1,M,symbol_QAM)) ;
BPSK_demod = zeros(1,1000);
QPSK_demod = zeros(1,2000);
QAM_demod = zeros(1,1000);
%received signal
for l=1:length(SNR_dB)
for n=1:1000
%BPSK
y_BPSK(n) = awgn(txsignal_BPSK(n),SNR_dB(l)); %received signal
BPSK_demod(n) = pskdemod(y_BPSK(n), sym_BPSK);
s=sign(y_BPSK);
k1 = abs(BPSK_demod(n)-signal_BPSK(n));
if (k1~= 0)
errorcount_BPSK(l) = errorcount_BPSK(l) + 1;
end
%QPSK
y_QPSK(n)= awgn(txsignal_QPSK(n),SNR_dB(l));
QPSK_demod(n) = pskdemod(y_QPSK(n), sym_QPSK,pi/4,'gray');
k2 = abs(QPSK_demod(n)-signal_QPSK(n));
if (k2~=0)
errorcount_QPSK(l) = errorcount_QPSK(l) + 1;
end
% QAM
y_QAM(n) = awgn(txsignal_QAM(n),SNR_dB(l));
QAM_demod(n) = qamdemod(y_QAM(n),sym_QAM,0,'gray');
k3 = abs(QAM_demod(n)-txsignal_QAM(n));
if (k3~=0)
errorcount_QAM(l) = errorcount_QAM(l) + 1;
end
end
end
totalerrorcount_BPSK = totalerrorcount_BPSK + errorcount_BPSK;
totalerrorcount_QPSK = totalerrorcount_QPSK + errorcount_QPSK;
totalerrorcount_QAM = totalerrorcount_QAM + errorcount_QAM;
end
%symbol errors
symerr_BPSK = totalerrorcount_BPSK/15000;
symerr_QPSK = totalerrorcount_QPSK/15000;
symerr_QAM = totalerrorcount_QAM/15000;
Pes_BPSK = zeros(1,7);
Pes_QPSK = zeros(1,7);
Pes_QAM = zeros(1,7);
% theoretical value
for j=1:7
Pes_BPSK(j) = qfunc(sqrt(2*SNR(j)));
Pes_QPSK(j) = 2*qfunc(sqrt(2*SNR(j)));
Pes_QAM(j) = 3*qfunc(sqrt(0.8*SNR(j)))/sqrt(sym_QAM); %dmin = 0.8
end
%BPSK
semilogy(SNR_dB,symerr_BPSK,'r',SNR_dB,Pes_BPSK);
legend('simulated graph', 'theoretical graph');
title('BPSK AWGN v/s Theoretical simulation');
xlabel('SNR in dB');
ylabel('probability of symbol error');
%QPSK
semilogy(SNR_dB,symerr_QPSK,'r',SNR_dB,Pes_QPSK);
legend('simulated graph', 'theoretical graph');
title('QPSK AWGN v/s Theoretical simulation');
xlabel('SNR in dB');
ylabel('probability of symbol error');
%QAM
scatterplot(txsignal_QAM);
title('16 QAM constellation');
semilogy(SNR_dB,Pes_QAM);% theoretical QAM graph
title('16-QAM theoretical symbol error');
xlabel('SNR in dB');
ylabel('probability of symbol error');
semilogy(SNR_dB,symerr_QAM); %simulated QAM
scatterplot(y_QAM); % constellation of noise added to QAM
%--------------------------------Rayleigh-----------------------------------------------------
RayleighSNRdb = 0:2:35;
RayleighSNR = 10.^(RayleighSNRdb./10);
Rayleigh_totalerrorcount_BPSK = zeros(1,18);
Rayleigh_totalerrorcount_QPSK = zeros(1,18);
for MCruns = 1:45
Raytxsignal_BPSK = zeros(1,1000);
Raytxsignal_QPSK = zeros(1,2000);
Rayy_BPSK = zeros(1,1000);
Rayy_QPSK = zeros(1,2000);
Rayerrorcount_BPSK = zeros(1,18);
Rayerrorcount_QPSK = zeros(1,18);
for p = 1:1000
alphac(p) = normrnd(0,0.5);
alphas(p) = normrnd(0,0.5);
alpha(p) = sqrt((alphac(p)).^2 + (alphas(p)).^2);
end
for m=1:1000
%BPSK
if(signal_BPSK(n) == 0)
Raytxsignal_BPSK(n) = 1;
elseif(signal_BPSK(n) == 1)
Raytxsignal_BPSK(n) = -1;
end
%QPSK
if (signal_QPSK(m) == 0)
Raytxsignal_QPSK(m) = sqrt(Es_QPSK)*exp(1i*pi/4);
elseif(signal_QPSK(m) == 1)
Raytxsignal_QPSK(m) = sqrt(Es_QPSK)*exp(1i*(3*pi/4));
elseif(signal_QPSK(m) == 2)
Raytxsignal_QPSK(m) = sqrt(Es_QPSK)*exp(1i*(7*pi/4));
elseif(signal_QPSK(m) == 3)
Raytxsignal_QPSK(m) = sqrt(Es_QPSK)*exp(1i*(5*pi/4));
end
end
for l=1:length(RayleighSNRdb)
for m=1:1000
%BPSK
y_BPSK(n) = awgn(alpha(n)*Raytxsignal_BPSK(n),RayleighSNRdb(l)); %received signal
BPSK_demod(n) = pskdemod(y_BPSK(n),sym_BPSK,0,'gray');
k1 = abs(BPSK_demod(n)-signal_BPSK(n));
if (k1~= 0)
Rayerrorcount_BPSK(l) = Rayerrorcount_BPSK(l) + 1;
end
%QPSK
y_QPSK(m) = awgn(alpha(m)*Raytxsignal_QPSK(m),RayleighSNRdb(l)); %received signal
QPSK_demod(m) = pskdemod(y_QPSK(m),sym_QPSK,pi/4,'gray');
s=sign(y_QPSK);
k1 = abs(QPSK_demod(m)-signal_QPSK(m));
if (k1~= 0)
Rayerrorcount_QPSK(l) = Rayerrorcount_QPSK(l) + 1;
end
end
end
Rayleigh_totalerrorcount_BPSK = Rayleigh_totalerrorcount_BPSK + Rayerrorcount_BPSK;
Rayleigh_totalerrorcount_QPSK = Rayleigh_totalerrorcount_QPSK + Rayerrorcount_QPSK;
end
%Rayleigh symbol error
Raysymerr_BPSK = Rayleigh_totalerrorcount_BPSK/45000;
Raysymerr_QPSK = Rayleigh_totalerrorcount_QPSK/45000;
semilogy(RayleighSNRdb,Raysymerr_BPSK,'b',SNR_dB,symerr_BPSK,'r');
legend('Rayleigh graph', 'AWGN graph');
title('BPSK Rayleigh v/s AWGN simulation');
xlabel('SNR in dB');
ylabel('probability of symbol error');
semilogy(RayleighSNRdb,Raysymerr_QPSK,'b',SNR_dB,symerr_QPSK,'r');
legend('Rayleigh graph', 'AWGN graph');
title('QPSK Rayleigh v/s AWGN simulation');
xlabel('SNR in dB');
ylabel('probability of symbol error');