root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_TxRx_WorkshopExercise_Solution.m

Revision 794, 8.2 kB (checked in by MelissaDuarte, 10 months ago)

Added the contention resolution code to the workshop exercises

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmitting and Receiving Data using Warplab
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4% In this lab exercise you will write a matlab script to transmit and
5% receive data using Warplab.
6
7% Using Warplab, transmission and reception of a signal over a wireless
8% channel can be done by following the next six steps:
9
10% 0. Initializaton and definition of parameters
11% 1. Generate a vector of samples to transmit and send the samples to the
12% Warp board (Sample Frequency is 40MHz)
13% 2. Prepare boards for transmission and reception and send trigger to
14% start transmission and reception (trigger is the SYNC packet)
15% 3. Read the received samples from the Warp board
16% 4. Reset and disable the boards
17% 5. Plot the transmitted and received data (Optional)
18
19% In this lab exercise you will write a matlab script that implements the
20% six steps above. Part of the code is provided, some part of the code you
21% will write. Read the code below and fill in with your code wherever you
22% are asked to do so.
23
24% NOTE: To avoid conflict with other groups using the boards, please test
25% the code you write in this script in any of the following three ways:
26%
27% Option 1. Run this script from matlab's Command Window by entering the
28% name of the script (enter warplab_example_TxRx_WorkshopExercise in
29% matlab's Command Window).
30% Option 2. In the menu bar go to Debug and select Run. If there
31% are errors in the code, error messages will appear in the Command Window.
32% Option 3. Press F5. If the are errors in the code, error messages will
33% appear in the Command Window.
34%
35% DO NOT USE the Evaluate selection option and DO NOT run the script by
36% sections. To test any change, always run the whole script by following
37% any of the three options above.
38
39try,
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41% Code to avoid conflict between users, only needed for the workshop, go to
42% step 0 below to start the initialization and definition of parameters
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44fid = fopen('c:\boards_lock.txt');
45
46if(fid > -1)
47    fclose('all');
48        errordlg('Boards already in use - Please try again!');
49        return;
50end
51
52!echo > c:\boards_lock.txt
53
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55% 0. Initializaton and definition of parameters
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57%Load some global definitions (packet types, etc.)
58warplab_siso_defines
59
60% Create Socket handles and intialize nodes
61[socketHandles, packetNum] = warplab_initialize;
62
63%Separate the socket handles for easier access
64% The first socket handle is always the magic SYNC
65% The rest can be arranged in any combination of Tx and Rx
66udp_Sync = socketHandles(1);
67udp_Tx = socketHandles(2);
68udp_RxA = socketHandles(3);
69
70%-------------------------------------------------------------------------%
71% USER CODE HERE
72
73% Create the following variables and assign them valid values:
74% CaptOffset: Value of the Capture offset. In [0:2^14]
75% TxLength: Number of samples to transmit. In [0:2^14-CaptOffset]
76% TxGainBB: Transmitter BaseBand gain. In [0:3]
77% TxGainRF: Transmitter RF gain. In [0:63]
78% RxGainBB: Receiver Baseband gain. In[0:31]
79% RxGainRF: Receiver RF gain. In [1:3]
80% CarrierChannel: Channel in the 2.4 GHz band. In [1:14]
81
82% Note: Set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the same values
83% you used in the warplab_siso_GUI.
84
85% Define the warplab options (parameters)
86CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14]
87TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset]
88TxGainBB = 3; %Tx Baseband Gain in [0:3]
89TxGainRF = 40; %Tx RF Gain in [0:63]
90RxGainBB = 18; %Rx Baseband Gain in [0:31]
91RxGainRF = 1; %Rx RF Gain in [1:3]
92CarrierChannel = 11; % Channel in the 2.4 GHz band. In [1:14]
93
94%-------------------------------------------------------------------------%
95   
96% Define the options vector; the order of options is set by the FPGA's code
97% (C code)
98optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel];
99% Send options vector to the nodes
100warplab_setOptions(socketHandles,optionsVector);
101
102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103% 1. Generate a vector of samples to transmit and send the samples to the
104% Warp board (Sample Frequency is 40MHz)
105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106%Create time vector 't'
107t = 0:(1/40e6):TxLength/40e6 - 1/40e6;
108
109%-------------------------------------------------------------------------%
110% USER CODE HERE
111
112% Create a signal to transmit (a vector of samples to transmit).
113% The signal must be a row vector. The Signal is a function of the time
114% vector 't'. The signal can be real or complex, the only constraint is
115% that the amplitude of the real part must be in [-1:1] and the amplitude
116% of the imaginary part must be in [-1:1]. Store the signal to transmit in
117% a variable called TxData (TxData = your signal)
118
119TxData = exp(t*j*2*pi*1e6); % Create a signal to transmit. Signal must be a
120% row vector. The signal can be real or complex, the only constraint is
121% that the amplitude of the real part must be in [-1:1] and the amplitude
122% of the imaginary part must be in [-1:1]
123%-------------------------------------------------------------------------%
124
125% Download the samples to be transmitted
126warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA);
127
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129% 2. Prepare boards for transmission and reception and send trigger to
130% start transmission and reception (trigger is the SYNC packet)
131%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132% Enable transmission
133warplab_enableTx(udp_Tx);
134
135% Enable reception
136warplab_enableRx(udp_RxA);
137
138% Send the SYNC packet
139warplab_sendSync(udp_Sync);
140
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142% 3. Read the received samples from the Warp board
143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144% Read back the received samples
145[RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset);
146% Process the received samples to obtain meaningful data
147[RxData,RxOTR] = warplab_processRawRxData(RawRxData);
148% Read stored RSSI data
149[RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
150% Procecss Raw RSSI data to obtain meningful RSSI values
151[RxRSSI] = warplab_processRawRSSIData(RawRSSIData);
152
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% 4. Reset and disable the boards
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156% Reset the receiver
157warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum);
158
159% Disable the receiver
160warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum);
161
162% Disable the transmitter
163warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum);
164
165% Close sockets
166pnet('closeall');
167
168
169%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170% 5. Plot the transmitted and received data
171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172figure;
173subplot(2,2,1);
174plot(real(TxData));
175title('Tx I');
176xlabel('n (samples)'); ylabel('Amplitude');
177axis([0 2^14 -1 1]); % Set axis ranges.
178subplot(2,2,2);
179plot(imag(TxData));
180title('Tx Q');
181xlabel('n (samples)'); ylabel('Amplitude');
182axis([0 2^14 -1 1]); % Set axis ranges.
183subplot(2,2,3);
184plot(real(RxData));
185title('Rx I');
186xlabel('n (samples)'); ylabel('Amplitude');
187axis([0 2^14 -1 1]); % Set axis ranges.
188subplot(2,2,4);
189plot(imag(RxData));
190title('Rx Q');
191xlabel('n (samples)'); ylabel('Amplitude');
192axis([0 2^14 -1 1]); % Set axis ranges.
193
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195% Code to avoid conflict between users, only needed for the workshop
196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197pnet('closeall');
198!del c:\boards_lock.txt
199catch,
200% Close sockets
201pnet('closeall');
202!del c:\boards_lock.txt
203lasterr
204end
Note: See TracBrowser for help on using the browser.