-
Notifications
You must be signed in to change notification settings - Fork 3
/
OFDM_Simulation.m
215 lines (174 loc) · 6.92 KB
/
OFDM_Simulation.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
function OFDM_Simulation(~,~,handles)
% Initialise the global variables
global count;
global axesHandleArray;
global textHandleArray;
global qamOutput;
global userAllocation;
global imageWriteIndex;
global imageReadIndex;
global userStatus;
global rcvdImage;
global imageData;
% Assign values for number of users and number of input samples
numberOfUsers = 10;
numberOfInputSamples = 200;
% if count == 0 there is no active users
if(count == 0)
saveHandlesInArray(handles,numberOfUsers);
end
% schedule subcarriers for active users
if(mod(count,20) == 0)
userAllocation = UserScheduling(numberOfUsers,numberOfInputSamples,5);
end
count = count + 1;
msg = {int2str(count)};
set(handles.edit21,'String',msg);
% get data for the user according to the number of users
[inBuffer, transferDone] = getUserData(numberOfUsers,numberOfInputSamples);
% Do modulation and pad 56 zeros
qamOut=qamOutput(inBuffer+1);
qamOut=transpose(qamOut);
qamPad =[qamOut zeros(1, 256- numberOfInputSamples)];
% Apply IFFT
ifft_output=ifft_recursive(qamPad);
snr = get(handles.SNR_Slider, 'value');
set(handles.snrValue, 'String', strcat(int2str(snr), ' dB'));
% skip noise for header as it may corrupt the image
headerPresent = 0;
for i = 1 : numberOfUsers
if((userStatus(:,i) == 1) && (imageReadIndex(i) < 300))
headerPresent = 1;
end
end
% If not apply noise according to tha user given SNR
if(headerPresent == 0)
awgnOutput = awgn(ifft_output,snr);
else
awgnOutput = ifft_output;
end
% From here the receiver part starts
% FFT the received output
fft_output=fft_recursive(awgnOutput);
output=fft_output(1:1:200);
% Demodulate the result from FFT block
qamdeout=qamdemod(output);
% write the ouput image
writeOutputData(numberOfUsers,qamdeout);
% Show all the images in the axes in the GUI
for i = 1 : numberOfUsers
if(((imageWriteIndex(i) > 1000) && (mod(count,20) == 0)) || (transferDone(:,i) == 1))
I = imread(strcat('Receivedpicture_',int2str(i), '.bmp'));
imshow(I,'Parent',axesHandleArray(:,i));
end
% Compute the Data rate
dataRate = 0;
if(userStatus(:,i) == 1)
dataRate = (userAllocation(:,i) * 4)/(0.000067);
end
% Compute the BER
ber = 0;
imageSize = size(imageData, 1);
if((userStatus(:,i) == 1) && (imageWriteIndex(i) > 1) && (imageWriteIndex(i) < imageSize))
currentFrameStart = 1 + imageWriteIndex(i) - userAllocation(:, i);
currentFrameStop = imageWriteIndex(i);
diff = rcvdImage(i, currentFrameStart:currentFrameStop) - (imageData(currentFrameStart:currentFrameStop, 1))';
ber = (nnz(diff) / (currentFrameStop - currentFrameStart)) * 100.0;
end
set(textHandleArray(:,i), 'String' , strcat('DR: ', int2str(dataRate/1000), ' kbps', ' BER: ', int2str(ber), ' %'));
end
end
function [inBuffer, transferDone] = getUserData(numberOfUsers,numberOfInputSamples)
global userStatus;
global imageData;
global imageReadIndex;
global userHandleArray;
global imageWriteIndex;
global userAllocation;
inBuffer = zeros(numberOfInputSamples,1);
transferDone = zeros(1, numberOfUsers);
startCopyIndex = 1;
for i = 1 : numberOfUsers
% If the user is allocated subcarriers, get data for the users
if(userAllocation(:,i) > 0)
if (imageReadIndex(i) >= size(imageData,1))
userStatus(:,i) = 0;
set(userHandleArray(:,i),'String','Start');
imageReadIndex(:,i) = 0;
imageWriteIndex(:,i) = 0;
userAllocation(:,i) = 0;
transferDone(:,i) = 1;
else
readEndIndex = imageReadIndex(i) + userAllocation(:,i);
if(readEndIndex > size(imageData,1))
readEndIndex = size(imageData,1);
end
l = readEndIndex - imageReadIndex(i);
stopCopyIndex = startCopyIndex + l - 1;
inBuffer(startCopyIndex : stopCopyIndex , :) = imageData(imageReadIndex(i) + 1 : readEndIndex, 1);
imageReadIndex(i) = imageReadIndex(i) + l;
startCopyIndex = stopCopyIndex + 1;
end
end
end
end
function writeOutputData(numberOfUsers,qamdeout)
global imageWriteIndex;
global rcvdImage;
global userAllocation;
startCopyIndex = 1;
for i = 1 : numberOfUsers
% If the transmitter has transmitted data, write to the output
% accordingly
if(userAllocation(:,i) > 0)
writeEndIndex = imageWriteIndex(i) + userAllocation(:,i);
l = writeEndIndex - imageWriteIndex(i);
stopCopyIndex = startCopyIndex + l - 1;
rcvdImage(i,imageWriteIndex(i) + 1 :writeEndIndex) = qamdeout(:,startCopyIndex:stopCopyIndex);
imageWriteIndex(i) = writeEndIndex;
fid2=fopen(strcat('Receivedpicture_',int2str(i), '.bmp'),'w','b');
fwrite(fid2,rcvdImage(i, 1:imageWriteIndex(i)), 'ubit4');
fclose(fid2);
startCopyIndex = stopCopyIndex + 1;
end
end
end
function saveHandlesInArray(handles,numberOfUsers)
global axesHandleArray;
global userHandleArray;
global textHandleArray;
% GUI handles for axes for displaying images
axesHandleArray = zeros(1,numberOfUsers);
userHandleArray = zeros(1,numberOfUsers);
textHandleArray = zeros(1,numberOfUsers);
axesHandleArray(:,1) = handles.axes4;
axesHandleArray(:,2) = handles.axes12;
axesHandleArray(:,3) = handles.axes13;
axesHandleArray(:,4) = handles.axes14;
axesHandleArray(:,5) = handles.axes15;
axesHandleArray(:,6) = handles.axes16;
axesHandleArray(:,7) = handles.axes17;
axesHandleArray(:,8) = handles.axes18;
axesHandleArray(:,9) = handles.axes19;
axesHandleArray(:,10)= handles.axes20;
textHandleArray(:,1) = handles.edit4;
textHandleArray(:,2) = handles.edit12;
textHandleArray(:,3) = handles.edit13;
textHandleArray(:,4) = handles.edit14;
textHandleArray(:,5) = handles.edit15;
textHandleArray(:,6) = handles.edit16;
textHandleArray(:,7) = handles.edit17;
textHandleArray(:,8) = handles.edit18;
textHandleArray(:,9) = handles.edit19;
textHandleArray(:,10) = handles.edit20;
userHandleArray(:,1) = handles.User1;
userHandleArray(:,2) = handles.User2;
userHandleArray(:,3) = handles.User3;
userHandleArray(:,4) = handles.User4;
userHandleArray(:,5) = handles.User5;
userHandleArray(:,6) = handles.User6;
userHandleArray(:,7) = handles.User7;
userHandleArray(:,8) = handles.User8;
userHandleArray(:,9) = handles.User9;
userHandleArray(:,10) = handles.User10;
end