source: ResearchApps/PHY/WARPLAB/WARPLab_v05_2/WorkshopExercises/warplab_SpectrumSensing_WorkshopExercise_Solution.m

Last change on this file was 1671, checked in by murphpo, 11 years ago

adding Melissa's code from the Dyspan workshop

File size: 13.1 KB
RevLine 
[1671]1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Spectrum sensing using WARPLab
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4
5% The specific steps implemented in this script are the following:
6
7% 0. Initialization and definition of parameters
8% 1. Prepare the WARP node for reception (sensing the medium) and send trigger to
9% start reception (trigger is the SYNC packet)
10% 2. Read the received samples from the WARP node
11% 3. Reset and disable the WARP node
12% 4. Compute and plot the fft of the received data
13% 5. Plot the received waveform
14% 6. Compute the Received Signal Strength Indicator (RSSI in dBm) of the received signal
15% 7. Close sockets
16
17% In this lab exercise you will write a matlab script that implements the
18% steps above. Part of the code is provided, some part of the code you
19% will write. Read the code below and fill in with your code wherever you
20% are asked to do so.
21
22% WARPLab documentation can be found online at
23% http://warp.rice.edu/trac/wiki/WARPLab
24
25%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26
27%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28% 0. Initializaton and definition of parameters
29%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30
31%Load some global definitions (packet types, etc.)
32warplab_defines
33
34% Create Socket handles and intialize nodes
35[socketHandles, packetNum] = warplab_initialize(1);
36
37% Separate the socket handles for easier access
38% The first socket handle is always the magic SYNC
39% The rest of the handles are the handles to the WARP nodes
40udp_Sync = socketHandles(1); % SYNC
41udp_node1 = socketHandles(2); % Handle for node 1: Receiver node
42
43% Define WARPLab parameters for this workshop exercise.
44
45%-------------------------------------------------------------------------%
46% USER CODE HERE
47
48% - Create a variable named 'Node1_SensingChannel' and assign a value to
49% this variable .
50% - Variable 'Node1_SensingChannel' can be any integer value in [1:14] range.
51% - The value of Node1_SensingChannel specifies a channel in the 2.4 GHz band
52% and this is the channel at which sensing wll be centered
53
54Node1_SensingChannel = 1;
55%-------------------------------------------------------------------------%                           
56
57%-------------------------------------------------------------------------%
58% USER CODE HERE
59
60% - Set the sensing channel of node 1 by using the 'warplab_setRadioParameter'
61% function and the 'Node1_SensingChannel' variable just defined
62
63% The 'warplab_setRadioParameter' function has three arguments which are specified below:
64%(The arguments in the 'warplab_setRadioParameter' function do not use the quotes '')
65
66% 1. The first argument of the 'warplab_setRadioParameter' function identifies the
67% node where the radio parameter will be set. The id or handle to node 1 is
68% 'udp_node1'.
69
70% 2. The second argument of the 'warplab_setRadioParameter' function identifies the
71% radio parameter that will be set. The sensing channel is the receiver center
72% frequency or carrier channel. To set the sensing channel, the radio
73% parameter that needs to be set is the parameter identified as
74% 'CARRIER_CHANNEL'.
75
76% 3. The third argument of the 'warplab_setRadioParameter' function is the channel
77% number to be set. Use as third argument the variable 'Node1_SensingChannel'
78% that you have previously defined
79
80warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,Node1_SensingChannel);
81%-------------------------------------------------------------------------%
82
83%-------------------------------------------------------------------------%
84% USER CODE HERE
85
86% - Create a variable named 'Node1_Radio2_RxGain_BB' and assign a value to
87% this variable .
88% - Node1_Radio2_RxGain_BB can be any integer value in [0:31] range.
89% - Node1_Radio2_RxGain_BB is the baseband gain applied by the receiver.
90% - Each unit step increase in the value of Node1_Radio2_RxGain_BB
91% corresponds to a 2 dB increase of gain applied to the received signal.
92                           
93Node1_Radio2_RxGain_BB = 13; 
94%-------------------------------------------------------------------------%
95
96
97%-------------------------------------------------------------------------%
98% USER CODE HERE
99
100% - Create a variable named 'Node1_Radio2_RxGain_RF' and assign a value to
101% this variable .
102% - Node1_Radio2_RxGain_RF can be any integer value in [1:3] range.
103% - Node1_Radio2_RxGain_RF is the RF gain applied by the receiver.
104% - Each unit step increase in the value of Node1_Radio2_RxGain_RF
105% corresponds to a 15 dB increase of gain applied to the received signal.
106                           
107Node1_Radio2_RxGain_RF = 3; 
108%-------------------------------------------------------------------------%
109
110% Set the baseband and RF receiver gains of the radio by using the 'warplab_setRadioParameter'
111% function and the 'Node1_Radio2_RxGain_BB' and 'Node1_Radio2_RxGain_RF' variables just defined
112warplab_setRadioParameter(udp_node1,RADIO2_RXGAINS,(Node1_Radio2_RxGain_BB + Node1_Radio2_RxGain_RF*2^16));
113
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115% 1. Prepare the WARP node for reception (sensing the medium) and send trigger to
116% start reception (trigger is the SYNC packet)
117%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118
119%-------------------------------------------------------------------------%
120% USER CODE HERE
121
122% Enable radio 2 in node 1 as receiver by sending the 'RADIO2_RXEN' command
123% to node 1 using the 'warplab_sendCmd' function.
124
125% The 'warplab_sendCmd' function has three arguments which are specified below:
126%(The arguments in the 'warplab_setRadioParameter' function do not use the quotes '')
127
128% 1. The first argument of the 'warplab_sendCmd' function identifies the
129% node to which the command will be sent to. The id or handle to node 1 is
130% 'udp_node1'.
131
132% 2. The second argument of the 'warplab_sendCmd' function identifies the
133% command that will be sent.
134
135% 3. The third argument of the 'warplab_sendCmd' command is a field that is
136% not used at the moment, it may be used in future versions of WARPLab to
137% keep track of packets. Use 'packetNum' as the third argument of the
138% 'warplab_sendCmd' function.
139
140warplab_sendCmd(udp_node1, RADIO2_RXEN, packetNum);
141%-------------------------------------------------------------------------%
142
143%-------------------------------------------------------------------------%
144% USER CODE HERE
145
146% Enable storage of samples in the receive buffer that is connected to
147% radio 2 in node 1 by sending the RADIO2RXBUFF_RXEN command to
148% node 1 using the 'warplab_sendCmd' function.
149
150% The 'warplab_sendCmd' function has been described above
151
152warplab_sendCmd(udp_node1, RADIO2RXBUFF_RXEN, packetNum);
153%-------------------------------------------------------------------------%
154
155% Prime receiver state machine in node 1. Node 1 will be waiting
156% for the SYNC packet. Capture at node 1 will be triggered when node 1
157% receives the SYNC packet.
158warplab_sendCmd(udp_node1, RX_START, packetNum);
159
160% Send the SYNC packet
161warplab_sendSync(udp_Sync);
162
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164% 2. Read the received samples from the WARP node
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166BufferSize = 2^14;
167
168%-------------------------------------------------------------------------%
169% USER CODE HERE
170
171% Read the received samples from the WARP node using the
172% 'warplab_readSMRO' function. Store the samples output by the warplab_readSMRO
173% function samples in a variable named 'Node1_Radio2_RawRxData'.
174
175% The arguments of the 'warplab_readSMRO' function are the following:
176%(The arguments in the 'warplab_setRadioParameter' function do not use the quotes '')
177
178% 1. The first argument of the 'warplab_readSMRO' function identifies the
179% node from which samples will be read. In this exercise there is only one
180% node and the id or handle to node 1 is 'udp_node1'.
181
182% 2. The second argument of the 'warplab_readSMRO' function identifies the
183% receive buffer from which samples will be read. For this exercise samples
184% were captured in node 1 radio 2, hence, samples must be read from radio 2
185% Rx buffer, the id for this buffer is 'RADIO2_RXDATA'.
186
187% 3. The third argument of the 'warplab_readSMRO' function is the number of
188% samples to read; reading of samples always starts from address zero in
189% the receive buffer. For this exercise set the third argument of the 'warplab_readSMRO'
190% function equal to 'BufferSize' which has been defined in the code to be
191% equal to 2^14 which is the maximum number of samples that can be stored
192% in the receive buffer, hence you will read the entire receive buffer
193
194[Node1_Radio2_RawRxData] = warplab_readSMRO(udp_node1, RADIO2_RXDATA, BufferSize);
195%-------------------------------------------------------------------------%
196
197% Process the received samples to obtain meaningful data
198[Node1_Radio2_RxData,Node1_Radio2_RxOTR] = warplab_processRawRxData(Node1_Radio2_RawRxData);
199
200% Read stored RSSI data
201[Node1_Radio2_RawRSSIData] = warplab_readSMRO(udp_node1, RADIO2_RSSIDATA, ceil(BufferSize/8));
202
203% Process Raw RSSI data to obtain meningful RSSI values
204[Node1_Radio2_RSSIData] = warplab_processRawRSSIData(Node1_Radio2_RawRSSIData);
205
206%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207% 3. Reset and disable the WARP node
208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209%-------------------------------------------------------------------------%
210% USER CODE HERE
211
212% Set radio 2 in node 1 back to Rx disabled mode by sending the
213% 'RADIO2_RXDIS' command to node 1 using the 'warplab_sendCmd' function.
214
215% The 'warplab_sendCmd' function has been described above
216
217warplab_sendCmd(udp_node1, RADIO2_RXDIS, packetNum);
218%-------------------------------------------------------------------------%
219
220%-------------------------------------------------------------------------%
221% USER CODE HERE
222
223% Set storage of samples in the receive buffer that is connected to
224% radio 2 in node 1 back to disabled mode by sending the RADIO2RXBUFF_RXDIS
225% command to node 1 using the 'warplab_sendCmd' function.
226
227warplab_sendCmd(udp_node1, RADIO2RXBUFF_RXDIS, packetNum);
228%-------------------------------------------------------------------------%
229
230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231% 4. Compute and plot the fft of the received data
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% Computation of fft is based on the example in MATLAB's fft
234% documentation, see help fft for more information on MATLAB's fft function
235
236% Compute and plot the fft of the received signal
237% Compute fft
238L=length(Node1_Radio2_RxData); % Get length of transmitted vector
239NFFT = 2^nextpow2(L); % Next power of 2 from length of y
240Y = fftshift(fft(Node1_Radio2_RxData,NFFT)/L); % Compute fft
241Fs=40e6; % Sampling frequency is equal to 40e6
242f = Fs/2*linspace(0-1,1,NFFT);
243
244% Plot fft
245figure
246plot(f/10^6,abs(Y)) 
247title('Spectrum of received signal in current carrier channel')
248xlabel('Frequency (MHz)')
249ylabel('Magnitude')
250xlim([-10 10])
251
252
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% Attendees code:
255% 5. Plot the received waveform
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257figure
258subplot(2,2,1);
259plot(real(Node1_Radio2_RxData));
260title('Rx Node 2 Radio 2 I');
261xlabel('n (samples)'); ylabel('Amplitude');
262axis([0 2^14 -1 1]); % Set axis ranges.
263subplot(2,2,2);
264plot(imag(Node1_Radio2_RxData));
265title('Rx Node 2 Radio 2 Q');
266xlabel('n (samples)'); ylabel('Amplitude');
267axis([0 2^14 -1 1]); % Set axis ranges.
268
269% Plot amplitude versus time
270subplot(2,2,3);
271plot([0:1:length(Node1_Radio2_RxData)-1]/40e6,real(Node1_Radio2_RxData));
272title('Rx Node 2 Radio 2 I');
273xlabel('time (s)'); ylabel('Amplitude');
274axis([0 (length(Node1_Radio2_RxData)-1)/40e6 -1 1]); % Set axis ranges.
275subplot(2,2,4);
276plot([0:1:length(Node1_Radio2_RxData)-1]/40e6,imag(Node1_Radio2_RxData));
277title('Rx Node 2 Radio 2 Q');
278xlabel('time (s)'); ylabel('Amplitude');
279axis([0 (length(Node1_Radio2_RxData)-1)/40e6 -1 1]); % Set axis ranges.
280
281%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282% 6. Compute the Received Signal Strength Indicator (RSSI in dBm) of the received signal
283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284% RSSI measurements were stored in 'Node1_Radio2_RSSIData' variable.
285% Average over all measurements of RSSI
286RSSI_Avg = mean(Node1_Radio2_RSSIData);
287
288% Convert RSSIAvg to dBm.
289% The conversion is based on the following radio and RSSI vaue specifications:
290% For high receiver gain (Node1_Radio2_RxGain_RF = 3), RSSI_Avg=0 is -100dBm; RSSI_Avg=1023 is -30dBm.
291% For medium receiver gain (Node1_Radio2_RxGain_RF = 2), RSSI_Avg=0 is -85dBm; RSSI_Avg=1023 is -15dBm.
292% For low receiver gain (Node1_Radio2_RxGain_RF = 1), RSSI_Avg=0 is -70dBm; RSSI_Avg=1023 is 0dBm.
293
294RSSI_dBm = (70/1023)*RSSI_Avg - 70 - (Node1_Radio2_RxGain_RF-1)*15;
295fprintf('\nRSSI dBm = %5.2f\n',RSSI_dBm)
296
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298% 7. Close sockets
299%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300pnet('closeall');
Note: See TracBrowser for help on using the repository browser.