-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdraft1.m
198 lines (169 loc) · 5.04 KB
/
draft1.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
clear all;
close all;
%clf;
%---------------- Initial Parameters ----------------
choice1 = menu ("Which number of bits in the bitstream do you want?", "12", "64", "102", "1020", "10200"); %number of bits in the FIFO
if choice1 == 1
n = 12;
elseif choice1 == 2
n = 64;
elseif choice1 == 3
n = 102;
elseif choice1 == 4
n = 1020;
elseif choice1 == 5
n = 10200;
endif
choice2 = menu ("Which QAM do you want to use?", "4", "16", "64"); %number of symbols in the constellation
if choice2 == 1
M = 4;
elseif choice2 == 2
M = 16;
elseif choice2 == 3
M = 64;
endif
k = log2(M); %number of bits in one symbol
w = 2*pi*10; % angular frequency
if mod(n, k) ~= 0
error('numberOfBits must be a multiple of log2(M).');
end
numSamplesPerSymbol = 1; % Oversampling factor
%----------------Creation of the bitstream ---------
dataIn = randi(2,n,1) - 1 % Generate vector of binary data
%stem(dataIn,"filled");
%title('Random Bits');
%xlabel('Bit Index');
%ylabel('Binary Value');
%----------------- QAM Mapper --------------------
numberOfSymbols = length(dataIn)/k; %number of symbols in the FIFO
% Define mapping table applying Gray mapping
if M == 4
mappingTable(1) = 1 + 1*j;
mappingTable(2) = -1 + 1*j;
mappingTable(3) = 1 - 1*j;
mappingTable(4) = -1 - 1*j;
elseif M == 16
mappingTable(1:4) = -3;
mappingTable(5:8) = -1;
mappingTable(9:12) = +3;
mappingTable(13:16) = +1;
for i = 0:15
if mod(i,4) == 0
mappingTable(i+1) = mappingTable(i+1) -3*j;
elseif mod(i+3,4) == 0
mappingTable(i+1) = mappingTable(i+1) -1*j;
elseif mod(i+1,4) == 0
mappingTable(i+1) = mappingTable(i+1) +1*j;
elseif mod(i+2,8) == 0
mappingTable(i+1) = mappingTable(i+1) +3*j;
endif
endfor
elseif M == 64
mappingTable(1:8) = + 7*j;
mappingTable(9:16) = + 5*j;
mappingTable(17:24) = + 1*j;
mappingTable(25:32) = + 3*j;
mappingTable(33:40) = - 7*j;
mappingTable(41:48) = - 5*j;
mappingTable(49:56) = - 1*j;
mappingTable(57:64) = - 3*j;
for i = 0:63
if mod(i+2,8) == 0
mappingTable(i+1) = mappingTable(i+1) +1;
elseif mod(i+1,8) == 0
mappingTable(i+1) = mappingTable(i+1) +3;
elseif mod(i+3,8) == 0
mappingTable(i+1) = mappingTable(i+1) +5;
elseif mod(i+4,8) == 0
mappingTable(i+1) = mappingTable(i+1) +7;
elseif mod(i+6,8) == 0
mappingTable(i+1) = mappingTable(i+1) -1;
elseif mod(i+5,8) == 0
mappingTable(i+1) = mappingTable(i+1) -3;
elseif mod(i+7,8) == 0
mappingTable(i+1) = mappingTable(i+1) -5;
elseif mod(i,8) == 0
mappingTable(i+1) = mappingTable(i+1) -7;
endif
endfor
endif
mappedSymbols = zeros(1, numberOfSymbols);
% Map bits to symbols
for i = 1:k:length(dataIn)
symbolBits = dataIn(i:i+k-1);
if M == 4
symbolIndex = 2^1 * symbolBits(1) + 2^0 * symbolBits(2);
elseif M == 16
symbolIndex = 2^3 * symbolBits(1) + 2^2 * symbolBits(2) + 2^1 * symbolBits(3) + 2^0 * symbolBits(4);
elseif M == 64
symbolIndex = 2^5 * symbolBits(1) + 2^4 * symbolBits(2) + 2^3 * symbolBits(3) + 2^2 * symbolBits(4) + 2^1 * symbolBits(5) + 2^0 * symbolBits(6);
endif
% Mapping
mappedSymbols((i - 1)/k + 1) = mappingTable( symbolIndex + 1);
endfor
%%-----------------------------Pulse shaping ----------------------
%
%
%
%%------------------------------Construction of the signal-----------
%
%signal = mappedSymbols;
%
%t = 0:1/k:length(dataIn)/(k*k)-1/k;
%modulation = real(signal).*cos(w.*t) + imag(signal).*sin(w.*t);
%
%%--------------------------------Channel with noise addition----------------
%
%EbNo = 10;
%snr = EbNo + 10*log10(k) - 10*log10(numSamplesPerSymbol);
%
%signalAfter = awgn(signal,snr,'measured');
%
%%----------------------------- Deconstruction of the signal--------------
%
%phi = 0;
%
%signalAfterDeconstructionI = signalAfter.*cos(w*t + phi);
%signalAfterDeconstructionQ = signalAfter.*sin(w*t + phi);
%
%%-------------------------------FIR-------------------------------------------
%
%% Low pass filtering with a Butterworth filter
%[b,a]=butter(2,0.3);
%Yi=filter(b,a,signalAfterDeconstructionI);
%Yk=filter(b,a,signalAfterDeconstructionQ);
%----------------------------- QAM demapper -------------------------------
receivedSymbols = mappedSymbols;
%sPlotFig = scatterplot(signal,1,0,'k*');
%hold on
%scatterplot(signalAfter,1,0,'g.', sPlotFig);
%hold on
%scatterplot(receivedSymbols,1,0,'r+', sPlotFig);
%grid;
%xlabel('I');
%ylabel('Q');
%if M == 4
% axis([-2 2 -2 2]);
%elseif M == 16
% axis([-4 4 -4 4]);
%elseif M == 64
% axis([-8 8 -8 8]);
%endif
%title('Signal before sending, after sending and after filtering');
%legend('Before Sending','After Sending', 'After Filtering');
for i = 1:length(receivedSymbols)
[mindiff minIndex] = min(receivedSymbols(i) - mappingTable);
symbolIndexAfter(i) = minIndex - 1;
endfor
for i=1:length(symbolIndexAfter)
bitString = dec2bin(symbolIndexAfter, k);
endfor
bitString
bitString(1)
bitString(2)
for i=1:length(bitString)
pack =bitString(i);
% for j=1:k
% dataOut((i*k)-k+j ) = str2double(pack(j));
% endfor
endfor