1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
2 | % Spectrum sensing using WARPLab |
---|
3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
4 | % The specific steps implemented in this script are the following |
---|
5 | |
---|
6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
7 | % Instructors code |
---|
8 | |
---|
9 | % 0. Initializaton and definition of parameters |
---|
10 | % 1. Generate a sum of two sinusoids to transmit |
---|
11 | % 2. Plot the transmitted data fft and waveform |
---|
12 | % 3. Prepare WARP node for transmission |
---|
13 | % 4. Disable the transmitter path |
---|
14 | |
---|
15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
16 | |
---|
17 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
18 | % Attendees code |
---|
19 | |
---|
20 | % The specific steps implemented in this script are the following |
---|
21 | % 0. Initialization and definition of parameters |
---|
22 | % 1. Prepare the WARP node for reception (sensing the medium) and send trigger to |
---|
23 | % start reception (trigger is the SYNC packet) |
---|
24 | % 2. Read the received samples from the WARP node |
---|
25 | % 3. Reset and disable the WARP node |
---|
26 | % 4. Compute and plot the fft of the received data |
---|
27 | % 5. Plot the received waveform |
---|
28 | % 6. Compute the Received Signal Strength Indicator (RSSI in dBm) of the received signal |
---|
29 | % 7. Close sockets |
---|
30 | |
---|
31 | % In this lab exercise you will write a matlab script that implements the |
---|
32 | % steps above. Part of the code is provided, some part of the code you |
---|
33 | % will write. Read the code below and fill in with your code wherever you |
---|
34 | % are asked to do so. |
---|
35 | |
---|
36 | % WARPLab documentation can be found online at |
---|
37 | % http://warp.rice.edu/trac/wiki/WARPLab |
---|
38 | |
---|
39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
40 | |
---|
41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
42 | % 0. Initializaton and definition of parameters |
---|
43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
44 | |
---|
45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
46 | % Instructors and attendees code |
---|
47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
48 | |
---|
49 | %Load some global definitions (packet types, etc.) |
---|
50 | warplab_defines |
---|
51 | |
---|
52 | % Create Socket handles and intialize nodes |
---|
53 | [socketHandles, packetNum] = warplab_initialize(2); |
---|
54 | |
---|
55 | % Separate the socket handles for easier access |
---|
56 | % The first socket handle is always the magic SYNC |
---|
57 | % The rest of the handles are the handles to the WARP nodes |
---|
58 | udp_Sync = socketHandles(1); % SYNC |
---|
59 | udp_node1 = socketHandles(2); % Handle for node 1: Receiver node |
---|
60 | udp_node2 = socketHandles(3); % Handle for node 2: Transmitter node |
---|
61 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
62 | |
---|
63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
64 | % Instructors code |
---|
65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
66 | |
---|
67 | % Define WARPLab parameters. |
---|
68 | TxDelay = 0; % Number of noise samples per Rx capture. In [0:2^14] |
---|
69 | TxLength = 2^14-1-TxDelay; % Length of transmission. In [0:2^14-1-TxDelay] |
---|
70 | Node2_CarrierChannel = 1; % Channel in the 2.4 GHz band. In [1:14] |
---|
71 | Node2_Radio2_TxGain_BB = 3; % Tx Baseband Gain. In [0:3] |
---|
72 | Node2_Radio2_TxGain_RF = 40; % Tx RF Gain. In [0:63] |
---|
73 | TxMode = 0; % Transmission mode. In [0:1] |
---|
74 | % 0: Single Transmission |
---|
75 | % 1: Continuous Transmission. Tx node will continue |
---|
76 | % transmitting the vector of samples until the user manually |
---|
77 | % disables the transmitter. |
---|
78 | |
---|
79 | % Download the WARPLab parameters to the WARP nodes. |
---|
80 | warplab_writeRegister(udp_node2,TX_DELAY,TxDelay); |
---|
81 | warplab_writeRegister(udp_node2,TX_LENGTH,TxLength); |
---|
82 | warplab_setRadioParameter(udp_node2,CARRIER_CHANNEL,Node2_CarrierChannel); |
---|
83 | % Node 2 will be set as the transmitter so download Tx gains to node 2. |
---|
84 | warplab_setRadioParameter(udp_node2,RADIO2_TXGAINS,(Node2_Radio2_TxGain_RF + Node2_Radio2_TxGain_BB*2^16)); |
---|
85 | warplab_writeRegister(udp_node2,TX_MODE,TxMode); |
---|
86 | |
---|
87 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
88 | % Attendees code |
---|
89 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
90 | |
---|
91 | % Define WARPLab parameters for this workshop exercise. |
---|
92 | |
---|
93 | %-------------------------------------------------------------------------% |
---|
94 | % USER CODE HERE |
---|
95 | |
---|
96 | % - Create a variable named 'Node1_SensingChannel' and assign a value to |
---|
97 | % this variable . |
---|
98 | % - Variable 'Node1_SensingChannel' can be any integer value in [1:14] range. |
---|
99 | % - The value of Node1_SensingChannel specifies a channel in the 2.4 GHz band |
---|
100 | % and this is the channel at which sensing wll be centered |
---|
101 | |
---|
102 | Node1_SensingChannel = 1; |
---|
103 | %-------------------------------------------------------------------------% |
---|
104 | |
---|
105 | %-------------------------------------------------------------------------% |
---|
106 | % USER CODE HERE |
---|
107 | |
---|
108 | % - Set the sensing channel of node 1 by using the 'warplab_setRadioParameter' |
---|
109 | % function and the 'Node1_SensingChannel' variable just defined |
---|
110 | |
---|
111 | % The 'warplab_setRadioParameter' function has three arguments which are specified below: |
---|
112 | %(The arguments in the 'warplab_setRadioParameter' function do not use the quotes '') |
---|
113 | |
---|
114 | % 1. The first argument of the 'warplab_setRadioParameter' function identifies the |
---|
115 | % node where the radio parameter will be set. The id or handle to node 1 is |
---|
116 | % 'udp_node1'. |
---|
117 | |
---|
118 | % 2. The second argument of the 'warplab_setRadioParameter' function identifies the |
---|
119 | % radio parameter that will be set. The sensing channel is the receiver center |
---|
120 | % frequency or carrier channel. To set the sensing channel, the radio |
---|
121 | % parameter that needs to be set is the parameter identified as |
---|
122 | % 'CARRIER_CHANNEL'. |
---|
123 | |
---|
124 | % 3. The third argument of the 'warplab_setRadioParameter' function is the channel |
---|
125 | % number to be set. Use as third argument the variable 'Node1_SensingChannel' |
---|
126 | % that you have previously defined |
---|
127 | |
---|
128 | warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,Node1_SensingChannel); |
---|
129 | %-------------------------------------------------------------------------% |
---|
130 | |
---|
131 | %-------------------------------------------------------------------------% |
---|
132 | % USER CODE HERE |
---|
133 | |
---|
134 | % - Create a variable named 'Node1_Radio2_RxGain_BB' and assign a value to |
---|
135 | % this variable . |
---|
136 | % - Node1_Radio2_RxGain_BB can be any integer value in [0:31] range. |
---|
137 | % - Node1_Radio2_RxGain_BB is the baseband gain applied by the receiver. |
---|
138 | % - Each unit step increase in the value of Node1_Radio2_RxGain_BB |
---|
139 | % corresponds to a 2 dB increase of gain applied to the received signal. |
---|
140 | |
---|
141 | Node1_Radio2_RxGain_BB = 9; |
---|
142 | %-------------------------------------------------------------------------% |
---|
143 | |
---|
144 | |
---|
145 | %-------------------------------------------------------------------------% |
---|
146 | % USER CODE HERE |
---|
147 | |
---|
148 | % - Create a variable named 'Node1_Radio2_RxGain_RF' and assign a value to |
---|
149 | % this variable . |
---|
150 | % - Node1_Radio2_RxGain_RF can be any integer value in [1:3] range. |
---|
151 | % - Node1_Radio2_RxGain_RF is the RF gain applied by the receiver. |
---|
152 | % - Each unit step increase in the value of Node1_Radio2_RxGain_RF |
---|
153 | % corresponds to a 15 dB increase of gain applied to the received signal. |
---|
154 | |
---|
155 | Node1_Radio2_RxGain_RF = 2; |
---|
156 | %-------------------------------------------------------------------------% |
---|
157 | |
---|
158 | % Set the baseband and RF receiver gains of the radio by using the 'warplab_setRadioParameter' |
---|
159 | % function and the 'Node1_Radio2_RxGain_BB' and 'Node1_Radio2_RxGain_RF' variables just defined |
---|
160 | warplab_setRadioParameter(udp_node1,RADIO2_RXGAINS,(Node1_Radio2_RxGain_BB + Node1_Radio2_RxGain_RF*2^16)); |
---|
161 | |
---|
162 | Node1_MGC_AGC_Select = 0; % Set MGC_AGC_Select=1 to enable Automatic Gain Control (AGC). |
---|
163 | % Set MGC_AGC_Select=0 to enable Manual Gain Control (MGC). |
---|
164 | % By default, the nodes are set to MGC. |
---|
165 | % Set MGC mode in Rx node |
---|
166 | warplab_setAGCParameter(udp_node1,MGC_AGC_SEL,Node1_MGC_AGC_Select); |
---|
167 | |
---|
168 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
169 | % Instructors code : |
---|
170 | % 1. Generate a sum of two sinusoids to transmit |
---|
171 | % 2. Plot the transmitted data fft and waveform |
---|
172 | % 3. Prepare WARP node for transmission |
---|
173 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
174 | |
---|
175 | % 1. Generate a sum of two sinusoids to transmit |
---|
176 | |
---|
177 | % Create a signal to transmit, the signal is a function of the time vector |
---|
178 | % 't' the signal can be real or complex. |
---|
179 | % The signal must meet the following requirements: |
---|
180 | % - Signal to transmit must be a row vector. |
---|
181 | % - The amplitude of the real part must be in [-1:1] and the amplitude |
---|
182 | % of the imaginary part must be in [-1:1]. |
---|
183 | % - Highest frequency component is limited to 9.5 MHz (signal bandwidth |
---|
184 | % is limited to 19 MHz) |
---|
185 | % - Lowest frequency component is limited to 30 kHz |
---|
186 | |
---|
187 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector. |
---|
188 | f1 = 1e6; |
---|
189 | f2 = 4e6; |
---|
190 | Node2_Radio2_TxData = 0.45*exp(t*j*2*pi*f1)+0.45*exp(t*j*2*pi*f2); |
---|
191 | % Node2_Radio2_TxData = 0.45*sin(t*2*pi*f1)+0.45*exp(t*j*2*pi*f2); |
---|
192 | % Node2_Radio2_TxData = 0.45*cos(t*2*pi*f1)+0.45*exp(t*j*2*pi*f2); |
---|
193 | |
---|
194 | % 2. Plot the transmitted data fft and waveform |
---|
195 | |
---|
196 | % Compute and plot the fft of the transmitted signal centered at baseband |
---|
197 | % Computation of fft is based on the example in MATLAB's fft |
---|
198 | % documentation, see help fft for more information on MATLAB's fft function |
---|
199 | % Comppute fft |
---|
200 | L=length(Node2_Radio2_TxData); % Get length of transmitted vector |
---|
201 | NFFT = 2^nextpow2(L); % Next power of 2 from length of y |
---|
202 | Y = fftshift(fft(Node2_Radio2_TxData,NFFT)/L); % Compute fft |
---|
203 | Fs=40e6; % Sampling frequency is equal to 40e6 |
---|
204 | f = Fs/2*linspace(-1,1,NFFT); |
---|
205 | |
---|
206 | % Plot plot fft. |
---|
207 | figure |
---|
208 | plot(f/10^6,abs(Y)) |
---|
209 | title('Spectrum of transmitted signal in current carrier channel') |
---|
210 | xlabel('Frequency (MHz)') |
---|
211 | ylabel('Magnitude') |
---|
212 | xlim([-10, 10]) |
---|
213 | |
---|
214 | % Plot amplitude versus sample |
---|
215 | figure; |
---|
216 | subplot(2,2,1); |
---|
217 | plot(real(Node2_Radio2_TxData)); |
---|
218 | title('Tx Node 1 Radio 2 I'); |
---|
219 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
220 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
221 | |
---|
222 | subplot(2,2,2); |
---|
223 | plot(imag(Node2_Radio2_TxData)); |
---|
224 | title('Tx Node 1 Radio 2 Q'); |
---|
225 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
226 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
227 | |
---|
228 | subplot(2,2,3); |
---|
229 | plot([0:1:length(Node2_Radio2_TxData)-1]/40e6,real(Node2_Radio2_TxData)); |
---|
230 | title('Tx Node 1 Radio 2 I'); |
---|
231 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
232 | axis([0 (length(Node2_Radio2_TxData)-1)/40e6 -1 1]); % Set axis ranges. |
---|
233 | |
---|
234 | subplot(2,2,4); |
---|
235 | plot([0:1:length(Node2_Radio2_TxData)-1]/40e6,imag(Node2_Radio2_TxData)); |
---|
236 | title('Tx Node 1 Radio 2 Q'); |
---|
237 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
238 | axis([0 (length(Node2_Radio2_TxData)-1)/40e6 -1 1]); % Set axis ranges. |
---|
239 | |
---|
240 | % 3. Prepare WARP node for transmission |
---|
241 | % Download the samples to be transmitted |
---|
242 | warplab_writeSMWO(udp_node2, RADIO2_TXDATA, Node2_Radio2_TxData); |
---|
243 | |
---|
244 | % Enable transmitter radio path in radio 2 in node 2 (enable radio 2 in |
---|
245 | % node 2 as transmitter) |
---|
246 | warplab_sendCmd(udp_node2, RADIO2_TXEN, packetNum); |
---|
247 | |
---|
248 | % Enable transmission of Node2's radio 2 Tx buffer (enable transmission |
---|
249 | % of samples stored in radio 2 Tx Buffer in node 2) |
---|
250 | warplab_sendCmd(udp_node2, RADIO2TXBUFF_TXEN, packetNum); |
---|
251 | |
---|
252 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
253 | % Attendees code : |
---|
254 | % 1. Prepare the WARP node for reception (sensing the medium) and send trigger to |
---|
255 | % start reception (trigger is the SYNC packet) |
---|
256 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
257 | |
---|
258 | %-------------------------------------------------------------------------% |
---|
259 | % USER CODE HERE |
---|
260 | |
---|
261 | % Enable radio 2 in node 1 as receiver by sending the 'RADIO2_RXEN' command |
---|
262 | % to node 1 using the 'warplab_sendCmd' function. |
---|
263 | |
---|
264 | % The 'warplab_sendCmd' function has three arguments which are specified below: |
---|
265 | %(The arguments in the 'warplab_setRadioParameter' function do not use the quotes '') |
---|
266 | |
---|
267 | % 1. The first argument of the 'warplab_sendCmd' function identifies the |
---|
268 | % node to which the command will be sent to. The id or handle to node 1 is |
---|
269 | % 'udp_node1'. |
---|
270 | |
---|
271 | % 2. The second argument of the 'warplab_sendCmd' function identifies the |
---|
272 | % command that will be sent. |
---|
273 | |
---|
274 | % 3. The third argument of the 'warplab_sendCmd' command is a field that is |
---|
275 | % not used at the moment, it may be used in future versions of WARPLab to |
---|
276 | % keep track of packets. Use 'packetNum' as the third argument of the |
---|
277 | % 'warplab_sendCmd' function. |
---|
278 | |
---|
279 | warplab_sendCmd(udp_node1, RADIO2_RXEN, packetNum); |
---|
280 | %-------------------------------------------------------------------------% |
---|
281 | |
---|
282 | %-------------------------------------------------------------------------% |
---|
283 | % USER CODE HERE |
---|
284 | |
---|
285 | % Enable storage of samples in the receive buffer that is connected to |
---|
286 | % radio 2 in node 1 by sending the RADIO2RXBUFF_RXEN command to |
---|
287 | % node 1 using the 'warplab_sendCmd' function. |
---|
288 | |
---|
289 | % The 'warplab_sendCmd' function has been described above |
---|
290 | |
---|
291 | warplab_sendCmd(udp_node1, RADIO2RXBUFF_RXEN, packetNum); |
---|
292 | %-------------------------------------------------------------------------% |
---|
293 | |
---|
294 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
295 | % Instructors code |
---|
296 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
297 | % Prime transmitter state machine in node 2. Node 2 will be |
---|
298 | % waiting for the SYNC packet. Transmission from node 2 will be triggered |
---|
299 | % when node 2 receives the SYNC packet. |
---|
300 | warplab_sendCmd(udp_node2, TX_START, packetNum); |
---|
301 | |
---|
302 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
303 | % Attendees code |
---|
304 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
305 | |
---|
306 | % Prime receiver state machine in node 1. Node 1 will be waiting |
---|
307 | % for the SYNC packet. Capture at node 1 will be triggered when node 1 |
---|
308 | % receives the SYNC packet. |
---|
309 | warplab_sendCmd(udp_node1, RX_START, packetNum); |
---|
310 | |
---|
311 | % Send the SYNC packet |
---|
312 | warplab_sendSync(udp_Sync); |
---|
313 | |
---|
314 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
315 | % Attendees code: |
---|
316 | % 2. Read the received samples from the WARP node |
---|
317 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
318 | BufferSize = 2^14; |
---|
319 | |
---|
320 | %-------------------------------------------------------------------------% |
---|
321 | % USER CODE HERE |
---|
322 | |
---|
323 | % Read the received samples from the WARP node using the |
---|
324 | % 'warplab_readSMRO' function. Store the samples output by the warplab_readSMRO |
---|
325 | % function samples in a variable named 'Node1_Radio2_RawRxData'. |
---|
326 | |
---|
327 | % The arguments of the 'warplab_readSMRO' function are the following: |
---|
328 | |
---|
329 | % 1. The first argument of the 'warplab_readSMRO' function identifies the |
---|
330 | % node from which samples will be read. In this exercise there is only one |
---|
331 | % node and the id or handle to node 1 is 'udp_node1'. |
---|
332 | |
---|
333 | % 2. The second argument of the 'warplab_readSMRO' function identifies the |
---|
334 | % receive buffer from which samples will be read. For this exercise samples |
---|
335 | % were captured in node 1 radio 2, hence, samples must be read from radio 2 |
---|
336 | % Rx buffer, the id for this buffer is 'RADIO2_RXDATA'. |
---|
337 | |
---|
338 | % 3. The third argument of the 'warplab_readSMRO' function is the number of |
---|
339 | % samples to read; reading of samples always starts from address zero in |
---|
340 | % the receive buffer. For this exercise set the third argument of the 'warplab_readSMRO' |
---|
341 | % function equal to 'BufferSize' which has been defined in the code to be |
---|
342 | % equal to 2^14 which is the maximum number of samples that can be stored |
---|
343 | % in the receive buffer, hence you will read the entire receive buffer |
---|
344 | |
---|
345 | [Node1_Radio2_RawRxData] = warplab_readSMRO(udp_node1, RADIO2_RXDATA, BufferSize); |
---|
346 | %-------------------------------------------------------------------------% |
---|
347 | |
---|
348 | % Process the received samples to obtain meaningful data |
---|
349 | [Node1_Radio2_RxData,Node1_Radio2_RxOTR] = warplab_processRawRxData(Node1_Radio2_RawRxData); |
---|
350 | |
---|
351 | % Read stored RSSI data |
---|
352 | [Node1_Radio2_RawRSSIData] = warplab_readSMRO(udp_node1, RADIO2_RSSIDATA, ceil(BufferSize/8)); |
---|
353 | |
---|
354 | % Process Raw RSSI data to obtain meningful RSSI values |
---|
355 | [Node1_Radio2_RSSIData] = warplab_processRawRSSIData(Node1_Radio2_RawRSSIData); |
---|
356 | |
---|
357 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
358 | % Instructors code |
---|
359 | % 4. Disable te transmitter path |
---|
360 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
361 | |
---|
362 | % Set radio 2 Tx buffer in node 2 back to Tx disabled mode |
---|
363 | warplab_sendCmd(udp_node2, RADIO2TXBUFF_TXDIS, packetNum); |
---|
364 | |
---|
365 | % Disable the transmitter radio |
---|
366 | warplab_sendCmd(udp_node2, RADIO2_TXDIS, packetNum); |
---|
367 | |
---|
368 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
369 | % Attendees code: |
---|
370 | % 3. Reset and disable the WARP node |
---|
371 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
372 | %-------------------------------------------------------------------------% |
---|
373 | % USER CODE HERE |
---|
374 | |
---|
375 | % Set radio 2 in node 1 back to Rx disabled mode by sending the |
---|
376 | % 'RADIO2_RXDIS' command to node 1 using the 'warplab_sendCmd' function. |
---|
377 | |
---|
378 | % The 'warplab_sendCmd' function has been described above |
---|
379 | |
---|
380 | warplab_sendCmd(udp_node1, RADIO2_RXDIS, packetNum); |
---|
381 | %-------------------------------------------------------------------------% |
---|
382 | |
---|
383 | %-------------------------------------------------------------------------% |
---|
384 | % USER CODE HERE |
---|
385 | |
---|
386 | % Set storage of samples in the receive buffer that is connected to |
---|
387 | % radio 2 in node 1 back to disabled mode by sending the RADIO2RXBUFF_RXDIS |
---|
388 | % command to node 1 using the 'warplab_sendCmd' function. |
---|
389 | |
---|
390 | warplab_sendCmd(udp_node1, RADIO2RXBUFF_RXDIS, packetNum); |
---|
391 | %-------------------------------------------------------------------------% |
---|
392 | |
---|
393 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
394 | % Attendees code: |
---|
395 | % 4. Compute and plot the fft of the received data |
---|
396 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
397 | % Computation of fft is based on the example in MATLAB's fft |
---|
398 | % documentation, see help fft for more information on MATLAB's fft function |
---|
399 | |
---|
400 | % Compute and plot the fft of the received signal |
---|
401 | % Compute fft |
---|
402 | L=length(Node1_Radio2_RxData); % Get length of transmitted vector |
---|
403 | NFFT = 2^nextpow2(L); % Next power of 2 from length of y |
---|
404 | Y = fftshift(fft(Node1_Radio2_RxData,NFFT)/L); % Compute fft |
---|
405 | Fs=40e6; % Sampling frequency is equal to 40e6 |
---|
406 | f = Fs/2*linspace(0-1,1,NFFT); |
---|
407 | |
---|
408 | % Plot fft |
---|
409 | figure |
---|
410 | plot(f/10^6,abs(Y)) |
---|
411 | title('Spectrum of received signal in current carrier channel') |
---|
412 | xlabel('Frequency (MHz)') |
---|
413 | ylabel('Magnitude') |
---|
414 | xlim([-10 10]) |
---|
415 | |
---|
416 | |
---|
417 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
418 | % Attendees code: |
---|
419 | % 5. Plot the received waveform |
---|
420 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
421 | figure |
---|
422 | subplot(2,2,1); |
---|
423 | plot(real(Node1_Radio2_RxData)); |
---|
424 | title('Rx Node 2 Radio 2 I'); |
---|
425 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
426 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
427 | subplot(2,2,2); |
---|
428 | plot(imag(Node1_Radio2_RxData)); |
---|
429 | title('Rx Node 2 Radio 2 Q'); |
---|
430 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
431 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
432 | |
---|
433 | % Plot amplitude versus time |
---|
434 | subplot(2,2,3); |
---|
435 | plot([0:1:length(Node1_Radio2_RxData)-1]/40e6,real(Node1_Radio2_RxData)); |
---|
436 | title('Rx Node 2 Radio 2 I'); |
---|
437 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
438 | axis([0 (length(Node1_Radio2_RxData)-1)/40e6 -1 1]); % Set axis ranges. |
---|
439 | subplot(2,2,4); |
---|
440 | plot([0:1:length(Node1_Radio2_RxData)-1]/40e6,imag(Node1_Radio2_RxData)); |
---|
441 | title('Rx Node 2 Radio 2 Q'); |
---|
442 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
443 | axis([0 (length(Node1_Radio2_RxData)-1)/40e6 -1 1]); % Set axis ranges. |
---|
444 | |
---|
445 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
446 | % Attendees code: |
---|
447 | % 6. Compute the Received Signal Strength Indicator (RSSI in dBm) of the received signal |
---|
448 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
449 | % RSSI measurements were stored in 'Node1_Radio2_RSSIData' variable. |
---|
450 | % Average over all measurements of RSSI |
---|
451 | RSSI_Avg = mean(Node1_Radio2_RSSIData); |
---|
452 | |
---|
453 | % Convert RSSIAvg to dBm. |
---|
454 | % The conversion is based on the following radio and RSSI vaue specifications: |
---|
455 | % For high receiver gain (Node1_Radio2_RxGain_RF = 3), RSSI_Avg=0 is -100dBm; RSSI_Avg=1023 is -30dBm. |
---|
456 | % For medium receiver gain (Node1_Radio2_RxGain_RF = 2), RSSI_Avg=0 is -85dBm; RSSI_Avg=1023 is -15dBm. |
---|
457 | % For low receiver gain (Node1_Radio2_RxGain_RF = 1), RSSI_Avg=0 is -70dBm; RSSI_Avg=1023 is 0dBm. |
---|
458 | |
---|
459 | RSSI_dBm = (70/1023)*RSSI_Avg - 70 - (Node1_Radio2_RxGain_RF-1)*15; |
---|
460 | fprintf('\nRSSI dBm = %5.2f\n',RSSI_dBm) |
---|
461 | |
---|
462 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
463 | % 7. Close sockets |
---|
464 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
465 | pnet('closeall'); |
---|