source: ResearchApps/PHY/WARPLAB/WARPLab_v05_2/M_Code_Examples/warplab_mimo_2x2_example_TxRx.m

Last change on this file was 1394, checked in by sgupta, 14 years ago

M-code note update

File size: 18.4 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmitting and Receiving Data using WARPLab (2x2 MIMO configuration)
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4% To run this M-code the boards must be programmed with the
5% 2x2 MIMO 5.x version of WARPLab bitstream (because this bitstream provides
6% storage of RSSI values and this M-code reads RSSI values). This M-code
7% will work with warplab_mimo_4x4_v05.bit bitstream when read of RSSI
8% values is deleted from the M-code.
9
10% The specific steps implemented in this script are the following
11
12% 0. Initializaton and definition of parameters
13% 1. Generate a vector of samples to transmit and send the samples to the
14% WARP board (Sample Frequency is 40MHz)
15% 2. Prepare WARP boards for transmission and reception and send trigger to
16% start transmission and reception (trigger is the SYNC packet)
17% 3. Read the received samples from the Warp board
18% 4. Reset and disable the boards
19% 5. Plot the transmitted and received data and close sockets
20
21%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22% 0. Initializaton and definition of parameters
23%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24%Load some global definitions (packet types, etc.)
25warplab_defines
26
27% Create Socket handles and intialize nodes
28[socketHandles, packetNum] = warplab_initialize;
29
30% Separate the socket handles for easier access
31% The first socket handle is always the magic SYNC
32% The rest of the handles are the handles to the WARP nodes
33udp_Sync = socketHandles(1);
34udp_node1 = socketHandles(2);
35udp_node2 = socketHandles(3);
36
37% Define WARPLab parameters.
38TxDelay = 2000; % Number of noise samples per Rx capture. In [0:2^14]
39TxLength = 2^14-1-2000; % Length of transmission. In [0:2^14-1-TxDelay]
40TxMode = 0; % Transmission mode. In [0:1]
41            % 0: Single Transmission
42            % 1: Continuous Transmission. Tx board will continue
43            % transmitting the vector of samples until the user manually
44            % disables the transmitter.
45CarrierChannel = 12; % Channel in the 2.4 GHz band. In [1:14]
46Node1_Radio2_TxGain_BB = 3; % Tx Baseband Gain. In [0:3]
47Node1_Radio2_TxGain_RF = 40; % Tx RF Gain. In [0:63]
48Node1_Radio3_TxGain_BB = 3; % Tx Baseband Gain. In [0:3]
49Node1_Radio3_TxGain_RF = 40; % Tx RF Gain. In [0:63]
50Node2_Radio2_RxGain_BB = 15; % Rx Baseband Gain. In [0:31]
51Node2_Radio2_RxGain_RF = 1; % Rx RF Gain. In [1:3] 
52Node2_Radio3_RxGain_BB = 15; % Rx Baseband Gain. In [0:31]
53Node2_Radio3_RxGain_RF = 1; % Rx RF Gain. In [1:3] 
54Node2_MGC_AGC_Select = 0;   % Set MGC_AGC_Select=1 to enable Automatic Gain Control (AGC).
55                            % Set MGC_AGC_Select=0 to enable Manual Gain Control (MGC).
56                            % By default, the nodes are set to MGC.   
57
58% Download the WARPLab parameters to the WARP nodes.
59% The nodes store the TxDelay, TxLength, and TxMode parameters in
60% registers defined in the WARPLab sysgen model. The nodes set radio
61% related parameters CarrierChannel, TxGains, and RxGains, using the
62% radio controller functions.
63% The TxDelay, TxLength, and TxMode parameters need to be known at the transmitter;
64% the receiver doesn't require knowledge of these parameters (the receiver
65% will always capture 2^14 samples). For this exercise node 1 will be set as
66% the transmitter (this is done later in the code). Since TxDelay, TxLength and
67% TxMode are only required at the transmitter we download the TxDelay, TxLength and
68% TxMode parameters only to the transmitter node (node 1).
69warplab_writeRegister(udp_node1,TX_DELAY,TxDelay);
70warplab_writeRegister(udp_node1,TX_LENGTH,TxLength);
71warplab_writeRegister(udp_node1,TX_MODE,TxMode);
72% The CarrierChannel parameter must be downloaded to all nodes 
73warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,CarrierChannel);
74warplab_setRadioParameter(udp_node2,CARRIER_CHANNEL,CarrierChannel);
75
76% Download 'Node1_Radio2_TxGain_RF', 'Node1_Radio2_TxGain_BB',
77% 'Node1_Radio3_TxGain_RF', and 'Node1_Radio3_TxGain_BB' parameters
78% to node 1 using the 'warplab_setRadioParameter' function.
79% Hints:
80% 1. The first argument of the 'warplab_setRadioParameter' function
81% identifies the node to which the parameter will be downloaded to.
82% The id or handle to node 1 is 'udp_node1'.
83% 2. The second argument of the 'warplab_setRadioParameter' function
84% identifies the parameter that will be downloaded. The
85% 'Node1_Radio2_TxGain_RF' and 'Node1_Radio2_TxGain_BB' parameters are
86% downloaded in one call of the 'warplab_setRadioParameter' and the id to
87% download these parameters is 'RADIO2_TXGAINS'. The
88% 'Node1_Radio3_TxGain_RF' and 'Node1_Radio3_TxGain_BB' parameters are
89% downloaded in one call of the 'warplab_setRadioParameter' and the id to
90% download these parameters is 'RADIO3_TXGAINS'.
91% 3. The third argument of the 'warplab_setRadioParameter' function is the
92% value the parameter must be set to. The 'Node1_Radio2_TxGain_RF' and
93% 'Node1_Radio2_TxGain_BB' parameters are downloaded in one call of the
94% 'warplab_setRadioParameter', these two values must be combined for
95% download in the following way:
96% (Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16)
97% The 'Node1_Radio3_TxGain_RF' and
98% 'Node1_Radio3_TxGain_BB' parameters are downloaded in one call of the
99% 'warplab_setRadioParameter', these two values must be combined for
100% download in the following way:
101% (Node1_Radio3_TxGain_RF + Node1_Radio3_TxGain_BB*2^16)
102% 4. Call the 'warplab_setRadioParameter' function twice. One time to
103% download the 'Node1_Radio2_TxGain_RF' and 'Node1_Radio2_TxGain_BB'
104% parameters and one time to download the 'Node1_Radio3_TxGain_RF' and
105% 'Node1_Radio3_TxGain_BB' parameters.
106% Node 1 will be set as the transmitter so download Tx gains to node 1.
107warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16));
108warplab_setRadioParameter(udp_node1,RADIO3_TXGAINS,(Node1_Radio3_TxGain_RF + Node1_Radio3_TxGain_BB*2^16));
109
110% Download 'Node2_Radio2_RxGain_BB', 'Node2_Radio2_RxGain_RF',
111% 'Node2_Radio3_RxGain_BB', and 'Node2_Radio3_RxGain_RF' parameters
112% to node 2 using the 'warplab_setRadioParameter' function.
113% Hints:
114% 1. The first argument of the 'warplab_setRadioParameter' function
115% identifies the node to which the parameter will be downloaded to.
116% The id or handle to node 2 is 'udp_node2'.
117% 2. The second argument of the 'warplab_setRadioParameter' function
118% identifies the parameter that will be downloaded. The
119% 'Node2_Radio2_RxGain_BB' and 'Node2_Radio2_RxGain_RF' parameters are
120% downloaded in one call of the 'warplab_setRadioParameter' and the id to
121% download these parameters is 'RADIO2_RXGAINS'. The
122% 'Node2_Radio3_RxGain_BB' and 'Node2_Radio3_RxGain_RF' parameters are
123% downloaded in one call of the 'warplab_setRadioParameter' and the id to
124% download these parameters is 'RADIO3_RXGAINS'
125% 3. The third argument of the 'warplab_setRadioParameter' function is the
126% value the parameter must be set to. The 'Node2_Radio2_RxGain_BB' and
127% 'Node2_Radio2_RxGain_RF' parameters are downloaded in one call of the
128% 'warplab_setRadioParameter', these two values must be combined for
129% download in the following way:
130% (Node2_Radio2_RxGain_BB + Node2_Radio2_RxGain_RF*2^16)
131% The 'Node2_Radio3_RxGain_BB' and
132% 'Node2_Radio3_RxGain_RF' parameters are downloaded in one call of the
133% 'warplab_setRadioParameter', these two values must be combined for
134% download in the following way:
135% (Node2_Radio3_RxGain_BB + Node2_Radio3_RxGain_RF*2^16)
136% 4. Call the 'warplab_setRadioParameter' function twice. One time to
137% download the 'Node2_Radio2_RxGain_BB' and 'Node2_Radio2_RxGain_RF'
138% parameters and one time to download the 'Node2_Radio3_RxGain_BB' and
139% 'Node2_Radio3_RxGain_RF' parameters.
140% Node 2 will be set as the receiver so download Rx gains to node 2.
141warplab_setRadioParameter(udp_node2,RADIO2_RXGAINS,(Node2_Radio2_RxGain_BB + Node2_Radio2_RxGain_RF*2^16));
142warplab_setRadioParameter(udp_node2,RADIO3_RXGAINS,(Node2_Radio3_RxGain_BB + Node2_Radio3_RxGain_RF*2^16));
143
144% Set MGC mode in node 2 (receiver)
145warplab_setAGCParameter(udp_node2,MGC_AGC_SEL, Node2_MGC_AGC_Select);
146
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148% 1. Generate a vector of samples to transmit and send the samples to the
149% WARP board (Sample Frequency is 40MHz)
150%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151% Prepare some data to be transmitted
152t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector
153
154% Create a signal to transmit from radio 2, the signal can be real or complex.
155% The signal must meet the following requirements:
156% - Signal to transmit must be a row vector.
157% - The amplitude of the real part must be in [-1:1] and the amplitude
158% of the imaginary part must be in [-1:1].
159% - Highest frequency component is limited to 9.5 MHz (signal bandwidth
160% is limited to 19 MHz)
161% - Lowest frequency component is limited to 30 kHz
162Node1_Radio2_TxData = exp(t*j*2*pi*1e6);
163
164% Download the 'Node1_Radio2_TxData' vector to WARP node 1 radio 2 Tx
165% buffer using the 'warplab_writeSMWO' function. The 'Node1_Radio2_TxData'
166% vector is the vector of samples to be transmitted from node 1 radio 2.
167% The id for radio 2 Tx buffer is 'RADIO2_TXDATA'.
168warplab_writeSMWO(udp_node1, RADIO2_TXDATA, Node1_Radio2_TxData); % Download samples to
169% radio 2 Tx Buffer
170
171% Create a signal to transmit from radio 3, the signal can be real or complex.
172% The signal must meet the following requirements:
173% - Signal to transmit must be a row vector.
174% - The amplitude of the real part must be in [-1:1] and the amplitude
175% of the imaginary part must be in [-1:1].
176% - Highest frequency component is limited to 9.5 MHz (signal bandwidth
177% is limited to 19 MHz)
178% - Lowest frequency component is limited to 30 kHz
179Node1_Radio3_TxData = exp(t*j*2*pi*5e6); 
180
181% Download the 'Node1_Radio3_TxData' vector to WARP node 1 radio 3 Tx
182% buffer using the 'warplab_writeSMWO' function. The 'Node1_Radio3_TxData'
183% vector is the vector of samples to be transmitted from node 1 radio 3.
184% The id for radio 3 Tx buffer is 'RADIO3_TXDATA'.
185warplab_writeSMWO(udp_node1, RADIO3_TXDATA, Node1_Radio3_TxData); % Download samples to
186% radio 3 Tx Buffer
187
188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189% 2. Prepare WARP boards for transmission and reception and send trigger to
190% start transmission and reception (trigger is the SYNC packet)
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192% The following lines of code set node 1 as transmitter and node 2 as
193% receiver; transmission and capture are triggered by sending the SYNC
194% packet.
195
196% Enable transmitter radio path in radios 2 and 3 in node 1 (enable radio 2
197% and radio 3 in node 1 as transmitters) by sending the RADIO2_TXEN and
198% RADIO3_TXEN commands to node 1 using
199% the 'warplab_sendCmd' function. To send the RADIO2_TXEN
200% and RADIO3_TXEN commands in one call of the 'warplab_sendCmd' function
201% the second argument of the 'warplab_sendCmd' function can be a vector of
202% the commands: [RADIO2_TXEN, RADIO3_TXEN]
203warplab_sendCmd(udp_node1, [RADIO2_TXEN, RADIO3_TXEN], packetNum);
204
205% Enable transmission of node1's radio 2 and radio 3 Tx buffer (enable
206% transmission of samples stored in radio 2 Tx Buffer and in radio 3 Tx
207% Buffer in node 1) by sending the RADIO2TXBUFF_TXEN and
208% RADIO3TXBUFF_TXEN commands to node 1 using
209% the 'warplab_sendCmd' function. To send the
210% RADIO2TXBUFF_TXEN and RADIO3TXBUFF_TXEN commands in one call of the
211% 'warplab_sendCmd' function the second argument of the 'warplab_sendCmd'
212% function can be a vector of
213% the commands: [RADIO2TXBUFF_TXEN, RADIO3TXBUFF_TXEN]
214warplab_sendCmd(udp_node1, [RADIO2TXBUFF_TXEN, RADIO3TXBUFF_TXEN], packetNum);
215
216% Enable receiver radio path in radios 2 and 3 in node 2 (enable radios 2
217% and 3 in node 2 as receivers) by sending the 'RADIO2_RXEN' and
218% 'RADIO3_RXEN' ommands to node 2 using 'warplab_sendCmd' function.
219% To send the  RADIO2_RXEN and RADIO3_RXEN commands in one call of the
220% 'warplab_sendCmd' function the second argument of the 'warplab_sendCmd'
221% function can be a vector of
222% the commands: [RADIO2_RXEN, RADIO3_RXEN]
223warplab_sendCmd(udp_node2, [RADIO2_RXEN, RADIO3_RXEN], packetNum);
224
225% Enable capture in node2's radio 2 and radio 3 Rx Buffer (enable radio 2
226% Rx buffer and radio 3 Rx buffer in node 2 for storage of samples) by
227% sending the 'RADIO2RXBUFF_RXEN' and 'RADIO3RXBUFF_RXEN' commands to node 2
228% using 'warplab_sendCmd' function. To send the
229% RADIO2RXBUFF_RXEN and RADIO3RXBUFF_RXEN commands in one call of the
230% 'warplab_sendCmd' function the second argument of the 'warplab_sendCmd'
231% function can be a vector of
232% the commands: [RADIO2RXBUFF_RXEN, RADIO3RXBUFF_RXEN]
233warplab_sendCmd(udp_node2, [RADIO2RXBUFF_RXEN, RADIO3RXBUFF_RXEN], packetNum);
234
235
236% Prime transmitter state machine in node 1 by sending the TX_START command
237% to node 1 using the 'warplab_sendCmd' function.
238% Node 1 will start waiting for the SYNC packet as soon as it receives the
239% TX_START command. Transmission from node 1 will be triggered when node 1
240% receives the SYNC packet.
241warplab_sendCmd(udp_node1, TX_START, packetNum);
242
243% Prime receiver state machine in node 2 by sending the RX_START command
244% to node 2 using the 'warplab_sendCmd' function.
245% Node 2 will start waiting for the SYNC packet as soon as it receives the
246% RX_START command. Capture on node 2 will be triggered when node 2
247% receives the SYNC packet.
248warplab_sendCmd(udp_node2, RX_START, packetNum);
249
250% Send the SYNC packet
251warplab_sendSync(udp_Sync);
252
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% 3. Read the received samples from the Warp board
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256% Read the received samples from the WARP node 2 radio 2 Rx buffer using the
257% 'warplab_readSMRO' function.  Store the samples in a variable named
258% 'Node2_Radio2_RawRxData'. The id for radio 2 Rx buffer is 'RADIO2_RXDATA'.
259% For this exercise the third argument of the 'warplab_readSMRO'
260% function must be equal to 'TxLength+TxDelay', since TxLength is the
261% number of samples that were transmitted and the first TxDelay samples
262% that were captured correspond to noise samples captured before the data
263% was transmitted.
264[Node2_Radio2_RawRxData] = warplab_readSMRO(udp_node2, RADIO2_RXDATA, TxLength+TxDelay);
265
266
267% Read the received samples from the WARP node 2 radio 3 Rx buffer using the
268% 'warplab_readSMRO' function.  Store the samples in a variable named
269% 'Node2_Radio3_RawRxData'. The id for radio 3 Rx buffer is 'RADIO3_RXDATA'.
270% For this exercise the third argument of the 'warplab_readSMRO'
271% function must be equal to 'TxLength+TxDelay', since TxLength is the
272% number of samples that were transmitted and the first TxDelay samples
273% that were captured correspond to noise samples captured before the data
274% was transmitted.
275[Node2_Radio3_RawRxData] = warplab_readSMRO(udp_node2, RADIO3_RXDATA, TxLength+TxDelay);
276
277% Process the received samples to obtain meaningful data
278[Node2_Radio2_RxData,Node2_Radio2_RxOTR] = warplab_processRawRxData(Node2_Radio2_RawRxData);
279[Node2_Radio3_RxData,Node2_Radio3_RxOTR] = warplab_processRawRxData(Node2_Radio3_RawRxData);
280% Read stored RSSI data from radio 2
281[Node2_Radio2_RawRSSIData] = warplab_readSMRO(udp_node2, RADIO2_RSSIDATA, ceil((TxLength+TxDelay)/8));
282% Read stored RSSI data from radio 3
283[Node2_Radio3_RawRSSIData] = warplab_readSMRO(udp_node2, RADIO3_RSSIDATA, ceil((TxLength+TxDelay)/8));
284% Procecss Raw RSSI data to obtain meningful RSSI values
285[Node2_Radio2_RSSIData] = warplab_processRawRSSIData(Node2_Radio2_RawRSSIData);
286[Node2_Radio3_RSSIData] = warplab_processRawRSSIData(Node2_Radio3_RawRSSIData);
287% Note: If the four lines of code above (warplab_processRawRSSIData lines and
288% warplab_readSMRO(udp_node2, RADIO2_RSSIDATA, (TxLength+TxDelay)/8) line
289% and warplab_readSMRO(udp_node2, RADIO3_RSSIDATA, (TxLength+TxDelay)/8) line)
290% are deleted, then the code will work when the boards are programmed
291% with the warplab_mimo_4x4_v04.bit bitstream)
292
293%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294% 4. Reset and disable the boards
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296% Set radios 2 and 3 Tx buffer in node 1 back to Tx disabled mode
297warplab_sendCmd(udp_node1, [RADIO2TXBUFF_TXDIS RADIO3TXBUFF_TXDIS], packetNum);
298
299% Disable the transmitter radios
300warplab_sendCmd(udp_node1, [RADIO2_TXDIS, RADIO3_TXDIS], packetNum);
301
302% Set radios 2 and 3 Rx buffer in node 2 back to Rx disabled mode
303warplab_sendCmd(udp_node2, [RADIO2RXBUFF_RXDIS, RADIO3RXBUFF_RXDIS], packetNum);
304
305% Disable the receiver radios
306warplab_sendCmd(udp_node2, [RADIO2_RXDIS,RADIO3_RXDIS], packetNum);
307
308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309% 5. Plot the transmitted and received data and close sockets
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311figure;
312subplot(4,2,1);
313plot(real(Node1_Radio2_TxData));
314title('Tx Node 1 Radio 2 I');
315xlabel('n (samples)'); ylabel('Amplitude');
316axis([0 2^14 -1 1]); % Set axis ranges.
317subplot(4,2,2);
318plot(imag(Node1_Radio2_TxData));
319title('Tx Node 1 Radio 2 Q');
320xlabel('n (samples)'); ylabel('Amplitude');
321axis([0 2^14 -1 1]); % Set axis ranges.
322subplot(4,2,3);
323plot(real(Node1_Radio3_TxData));
324title('Tx Node 1 Radio 3 I');
325xlabel('n (samples)'); ylabel('Amplitude');
326axis([0 2^14 -1 1]); % Set axis ranges.
327subplot(4,2,4);
328plot(imag(Node1_Radio3_TxData));
329title('Tx Node 1 Radio 3 Q');
330xlabel('n (samples)'); ylabel('Amplitude');
331axis([0 2^14 -1 1]); % Set axis ranges.
332subplot(4,2,5);
333plot(real(Node2_Radio2_RxData));
334title('Rx Node 2 Radio 2 I');
335xlabel('n (samples)'); ylabel('Amplitude');
336axis([0 2^14 -1 1]); % Set axis ranges.
337subplot(4,2,6);
338plot(imag(Node2_Radio2_RxData));
339title('Rx Node 2 Radio 2 Q');
340xlabel('n (samples)'); ylabel('Amplitude');
341axis([0 2^14 -1 1]); % Set axis ranges.
342subplot(4,2,7);
343plot(real(Node2_Radio3_RxData));
344title('Rx Node 2 Radio 3 I');
345xlabel('n (samples)'); ylabel('Amplitude');
346axis([0 2^14 -1 1]); % Set axis ranges.
347subplot(4,2,8);
348plot(imag(Node2_Radio3_RxData));
349title('Rx Node 2 Radio 3 Q');
350xlabel('n (samples)'); ylabel('Amplitude');
351axis([0 2^14 -1 1]); % Set axis ranges.
352
353% Close sockets
354pnet('closeall');
Note: See TracBrowser for help on using the repository browser.