-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_3o_d.m
130 lines (119 loc) · 3.67 KB
/
code_3o_d.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
clear all
close all
%% d
%% i)
A=fopen('C:\Users\Alex3\Desktop\ÔÇËÅÐÉÊÏÉÍÙÍÉÅÓ\christmas_carol_odd.txt','r');
binV=fscanf(A,'%c');
fclose(A); %äéáâÜæïõìå ôï êáôÜëëçëï áñ÷åßï êåéìÝíïõ
%binV= reshape(dec2bin('C:\Users\Alex3\Desktop\ÔÇËÅÐÉÊÏÉÍÙÍÉÅÓ\christmas_carol_odd.txt', 8).'-'0',1,[]);
%binS = dec2bin('C:\Users\Alex3\Desktop\ÔÇËÅÐÉÊÏÉÍÙÍÉÅÓ\christmas_carol_odd.txt',8);
%binS = binS';
%binS = binS(:)';
%binV = binS-48;
binV = reshape(dec2bin(binV, 8).'-'0',1,[]); %ôï ìåôáôñÝðïõìå óå binary ãéá íá ìðïñïýìå íá ôï äéáìïñöþíïõìå(áíôßóôïé÷ç åíôïëç ôçò binascii-Python)
takeastext2=binV;
B= dec2bin(binV, 8);
%% iii)
symbols_qpsk=zeros(size(length(takeastext2)/2));
allo4=zeros(size(100*length(takeastext2)/2));
fc=2; %fc=2Hz áöïý Á ðåñéôôü.
j=1;
for i=1:2:length(takeastext2)
if (takeastext2(i)==1) && (takeastext2(i+1)==1)
symbols_qpsk(j)=45;
elseif (takeastext2(i)==1) && (takeastext2(i+1)==0)
symbols_qpsk(j)=315;
elseif (takeastext2(i)==0) && (takeastext2(i+1)==0)
symbols_qpsk(j)=225;
else
symbols_qpsk(j)=135;
end
j=j+1;
end
t=0:1/100: 1-1/100;
x2=cos(4*pi*fc*t);
x3=sin(4*pi*fc*t);
x4=cos(4*pi*fc*t + pi/4);
x5=sin(4*pi*fc*t + pi/4);
for i=1:length(takeastext2)/2
for j=1:100
if (symbols_qpsk(i)==45)
allo4(j +(i-1)*100)=x2(j)*(-1);
elseif (symbols_qpsk(i)==135)
allo4(j +(i-1)*100)=x3(j)*1;
elseif (symbols_qpsk(i)==225)
allo4(j +(i-1)*100)=x2(j)*1;
elseif (symbols_qpsk(i)==315)
allo4(j +(i-1)*100)=x3(j)*(-1);
end
end
end
t=0:1/100:length(takeastext2)/2-1/100;
subplot(1,1,1);
plot(t,allo4);
xlabel ('t (sec)');
ylabel ('A (V)');
title ('QPSK Modulation');
%üìïéá äéáäéêáóßá ìå ðñïçãïýìåíï åñþôçìá.
%% iv
%ðáñÜãïõìå èüñõâï awgn êáé ôïí ðñïóèåôïõìå óôï óÞìá ãéá äýï ôéìÝò Eb/N0, 5 êáé 15dB
qpskModulator = comm.QPSKModulator;
qpskDemodulator = comm.QPSKDemodulator;
channel5 = comm.AWGNChannel('EbNo',5,'BitsPerSymbol',2);
channel15 = comm.AWGNChannel('EbNo',15,'BitsPerSymbol',2);%bits per symbol =2 ëüãù qpsk modulator
txSig= step(qpskModulator,takeastext2');
% ðñïóèÝôù èüñõâï awgn óôï óÞìá
rxSig5 = step(channel5,txSig);
rxSig15 = step(channel15,txSig);
%% v)
% Demodulate ôï èïñõâþäåò óÞìá
rxData5 = step(qpskDemodulator,rxSig5);
rxData15 = step(qpskDemodulator,rxSig15);
%ôá äéÜãñáììáôá ôùí áóôåñéóìþí:
%constellationDiagram=comm.ConstellationDiagram;
%r1=step(constellationDiagram,rxData15);
c= [-1-1i 1-1i -1+1i 1+1i ];
modData1 = genqammod(rxData5,c);
h = scatterplot(modData1);
hold on
scatterplot(c,[],[],'r*',h)
grid
hold off
modData2 = genqammod(rxData15,c);
h = scatterplot(modData2);
hold on
scatterplot(c,[],[],'r*',h)
grid
hold off
%% vi
errorRate = comm.ErrorRate;
errorStats = step(errorRate,takeastext2',rxData5);
ber5=errorStats(1);
errorStats = step(errorRate,takeastext2',rxData15);
ber15=errorStats(1);
display (ber5);
display (ber15);
%õðïëïãßæïõìå ber êáé ôï äåß÷íïõìå óôçí ïèüíç.
%% vii
%ïðïõ õðÜñ÷åé 2 Þ 3 âÜæïõìå ìçäåíéêÜ þóôå íá ìðïñÝóåé íá ãßíåé ç ìåôáôñïðÞ
%áðï binary.Tá 2 êáé ôá 3 ôá ðñïóÝèåóå ï èüñõâïò ðïõ âÜëáìå
for i=1:length(rxData5)
if(rxData5(i)~=0)&&(rxData5(i)~=1)
rxData5(i)=0;
end
end
for i=1:length(rxData5)
if(rxData15(i)~=0)&&(rxData15(i)~=1)
rxData15(i)=0;
end
end
str5 = char(bin2dec(reshape(char(rxData5+'0'), 8,[]).'))';
str15 = char(bin2dec(reshape(char(rxData15+'0'), 8,[]).'))';
%ìåôáôñïðÞ áðï binary se decimal êáé áðï êåé óå ÷áñáêôÞñåò
%Ýðåéôá åããñáöÞ óå áñ÷åßï êåéìÝíïõ .txt
AA = fopen('newtext5.txt','w');
fprintf(AA,'%s',str5);
fclose(AA);
AA = fopen('newtext15.txt','w');
fprintf(AA,'%c',str15);
fclose(AA);