root/ResearchApps/PHY/WARPLAB/WARPLab_SISO_MIMO2x2/M_Code/warplab_siso_example_ContinuousTx_WorkshopExercise_Solution.m

Revision 839, 12.9 kB (checked in by MelissaDuarte, 4 months ago)

WARPLab Release 02 April 09 2008. Release for 2x2 MIMO and improved SISO. Matlab code is modified to support MIMO. SISO now supports continuous transmission. There is one bitstream for MIMO and one bitstream for SISO. The xps project for MIMO and SISO is different but the M code is the same (The 'warplab_' functions are the same, the argument input to the functions is the only thing that changes from SISO to MIMO).

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Generating a Frequency Sweep with Continous Transmission using Warplab
3% (SISO configuration)
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5% To run this code the boards must be programmed with the
6% warplab_siso_v02.bit bitstream
7
8% The specific steps implemented in this script are the following
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. Leave continuous transmitter on for n seconds and then stop continuous
16% transmission.
17% 4. Read the received samples from the Warp board.
18% 5. Reset and disable the boards.
19% 6. Plot the first 2^14 received samples.
20
21% You can observe the transmitted signal on the spectrum analyzer in the
22% lab
23
24% In this lab exercise you will write a matlab script that implements the
25% seven steps above. Part of the code is provided, some part of the code you
26% will write. Read the code below and fill in with your code wherever you
27% are asked to do so.
28
29% NOTE: To avoid conflict with other groups using the boards, please test
30% the code you write in this script in any of the following three ways:
31%
32% Option 1. Run this script from matlab's Command Window by entering the
33% name of the script (enter warplab_example_TxRx_WorkshopExercise in
34% matlab's Command Window).
35% Option 2. In the menu bar go to Debug and select Run. If there
36% are errors in the code, error messages will appear in the Command Window.
37% Option 3. Press F5. If the are errors in the code, error messages will
38% appear in the Command Window.
39%
40% DO NOT USE the Evaluate selection option and DO NOT run the script by
41% sections. To test any change, always run the whole script by following
42% any of the three options above.
43
44try,
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46% Code to avoid conflict between users, only needed for the workshop, go to
47% step 0 below to start the initialization and definition of parameters
48%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49fid = fopen('c:\boards_lock.txt');
50
51if(fid > -1)
52    fclose('all');
53        errordlg('Boards already in use - Please try again!');
54        return;
55end
56
57!echo > c:\boards_lock.txt
58
59%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60% 0. Initializaton and definition of parameters
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62%Load some global definitions (packet types, etc.)
63warplab_defines
64
65% Create Socket handles and intialize nodes
66[socketHandles, packetNum] = warplab_initialize;
67
68%Separate the socket handles for easier access
69% The first socket handle is always the magic SYNC
70% The rest can be arranged in any combination of Tx and Rx
71udp_Sync = socketHandles(1);
72udp_Tx = socketHandles(2);
73udp_RxA = socketHandles(3);
74
75%-------------------------------------------------------------------------%
76% USER CODE HERE
77
78% Create the following variables and assign them valid values:
79
80% CaptOffset: Number of noise samples per Rx capture. In [0:2^14]
81% TxLength : Length of transmission. In [0:2^14-CaptOffset]         
82% CarrierChannel : Channel in the 2.4 GHz band. In [1:14]
83% TransMode : Transmission mode; in [0:1]
84                % 0: Single Transmission
85                % 1: Continuous Transmission. Tx board will continue
86                % transmitting the vector of samples until the user manually
87                % disables the transmitter.
88            % For this exercise set TransMode = 0;
89% TxGainBB : Tx Baseband Gain. In [0:3]
90% TxGainRF : Tx RF Gain. In [0:63]
91% RxGainBB : Rx Baseband Gain. In [0:31]
92% RxGainRF : Rx RF Gain. In [1:3]
93
94% Note: Set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the same values
95% you used in the warplab_siso_GUI.
96
97% Define the warplab options (parameters)
98CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14]
99TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset]
100TransMode = 1; %Transmission mode; in [0:1]
101               % 0: Single Transmission
102               % 1: Continuous Transmission. Tx board will continue
103               % transmitting the vector of samples until the user manually
104               % disables the transmitter.
105CarrierChannel = 8; % Channel in the 2.4 GHz band. In [1:14]
106TxGainBB = 3; %Tx Baseband Gain in [0:3]
107TxGainRF = 40; %Tx RF Gain in [0:63]
108RxGainBB = 15; %Rx Baseband Gain in [0:31]
109RxGainRF = 1; %Rx RF Gain in [1:3]
110%-------------------------------------------------------------------------%
111
112% Define the options vector; the order of options is set by the FPGA's code
113% (C code)
114optionsVector = [CaptOffset TxLength-1 TransMode CarrierChannel (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16)];
115% Send options vector to the nodes
116warplab_setOptions(socketHandles,optionsVector);
117
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119% 1. Generate a vector of samples to transmit and send the samples to the
120% Warp board (Sample Frequency is 40MHz). 
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122% Prepare some data to be transmitted
123t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector.
124
125%-------------------------------------------------------------------------%
126% USER CODE HERE
127
128% Create a signal to transmit (a vector of samples to transmit).
129% The signal must be a row vector. The Signal is a function of the time
130% vector 't'. The signal can be real or complex, the only constraint is
131% that the amplitude of the real part must be in [-1:1] and the amplitude
132% of the imaginary part must be in [-1:1]. Store the signal to transmit in
133% a variable called TxData (TxData = your signal)
134
135% You will be able to observe the transmitted signal on the spectrum
136% analyzer in the lab. 
137
138% As a suggestion, you can transmit the sum of two sinusoids separated by
139% 5MHz. You can use the following lines of code to generate the signal
140
141% f1 = 1e6;
142% f2 = 6e6;
143% TxData = exp(t*j*2*pi*f1)+exp(t*j*2*pi*f2);
144% scale = 1 / max( [ max(real(TxData)) , max(imag(TxData)) ] );
145% TxData = scale*TxData;
146
147% TxData is scaled so that amplitude of the real and  imaginary part is
148% in [-1:1]. We want the signal to span [-1,1] range so it uses the full
149% range of the DAC at the tranmitter.
150
151f1 = 1e6;
152f2 = 6e6;
153TxData = exp(t*j*2*pi*f1)+exp(t*j*2*pi*f2); % Create a signal to transmit.
154% Signal is the sum of two sinusoids with frequencies f1 and f2.
155% Signal to transmit must be a row vector. The signal can be real or
156% complex, the only constraint is that the amplitude of the real part must
157% be in [-1:1] and the amplitude of the imaginary part must be in [-1:1]
158scale = 1 / max( [ max(real(TxData)) , max(imag(TxData)) ] );
159TxData = scale*TxData; % Scale signal so that amplitude of the real and
160% imaginary part is in [-1:1]. We want the signal to span [-1,1] range
161% so it uses the full range of the DAC at the tranmitter.
162
163%-------------------------------------------------------------------------%
164
165% Download the samples to be transmitted
166warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA);
167
168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169% 2. Prepare boards for transmission and reception and send trigger to
170% start transmission and reception (trigger is the SYNC packet)
171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172% Enable transmitter radio path in transmitter node
173warplab_sendCmd(udp_Tx, RADIO2_TXEN, packetNum);
174
175% Enable receiver radio path in receiver node
176warplab_sendCmd(udp_RxA, RADIO2_RXEN, packetNum);
177
178% Prime transmitter state machine in transmitter node. Transmitter will be
179% waiting for the SYNC packet. Transmission will be triggered when the
180% transmitter node receives the SYNC packet.
181warplab_sendCmd(udp_Tx, TX_START, packetNum);
182
183% Prime receiver state machine in receiver node. Receiver will be waiting
184% for the SYNC packet. Capture will be triggered when the receiver
185% node receives the SYNC packet.
186warplab_sendCmd(udp_RxA, RX_START, packetNum);
187
188% Send the SYNC packet
189warplab_sendSync(udp_Sync);
190
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192% 3. Leave continuous transmitter on for n seconds and then stop continuous
193% transmission
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195
196%-------------------------------------------------------------------------%
197% USER CODE HERE
198
199% Use matlab's pause command to pause execution for n seconds. Because you
200% are sharing the board with other users, please pause for only less than 5
201% seconds: n < 5
202
203% To learn more about the pause function enter 'help pause' in the Matlab
204% command window.
205
206% USE pause(n) (with an argument). If you just use pause it will pause
207% until you press a key, since you are sharing the boards with other users
208% it is better to use pause(n) to avoid one user retaining the boards for
209% too long.
210
211% Leave continuous transmitter on for nsec seconds
212nsec = 5;
213pause(nsec);
214%-------------------------------------------------------------------------%
215
216%-------------------------------------------------------------------------%
217% USER CODE HERE
218
219% Stop transmission by sending the TX_STOP command using the
220% 'warplab_sendCmd' function. This function has been used in all the
221% previous exercises.
222
223% Hints:
224
225% 1. The first argument of the 'warplab_sendCmd' function identifies the
226% node to which the command will be sent. The TX_STOP command must be sent
227% to the transmitter node so use udp_Tx as the first argument.
228
229% 2. The second argument of the 'warplab_sendCmd' function identifies the
230% instruction or command to be sent. In this case, the command to send is
231% the TX_STOP command defined in 'warplab_defines'.
232
233% 3. The third argument of the 'warplab_sendCmd' command is a field that is
234% not used at the moment, it may be used in future versions of WARPLab to
235% keep track of packets. Use 'packetNum' as the third argument of the
236% 'warplab_sendCmd' command.
237
238% Stop transmission
239warplab_sendCmd(udp_Tx, TX_STOP, packetNum); % Resets the output and read
240% address of the transmitter buffer without disabling the transmitter radio.
241%-------------------------------------------------------------------------%
242
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244% 4. Read the received samples from the Warp board
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246% In continuous transmitter mode the receiver stores CaptOffset samples of
247% noise and the first TxLength samples transmitted.
248
249% Read back the received samples
250[RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset);
251% Process the received samples to obtain meaningful data
252[RxData,RxOTR] = warplab_processRawRxData(RawRxData);
253% Read stored RSSI data
254[RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
255% Procecss Raw RSSI data to obtain meningful RSSI values
256[RxRSSI] = warplab_processRawRSSIData(RawRSSIData);
257
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259% 5. Reset and disable the boards
260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261% Reset the receiver
262warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum);
263
264% Disable the receiver radio
265warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum);
266
267% Disable the transmitter radio
268warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum);
269
270% Close sockets
271pnet('closeall');
272
273%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274% 6. Plot the transmitted and received data
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276figure;
277subplot(2,2,1);
278plot(real(TxData));
279title('Tx I');
280xlabel('n (samples)'); ylabel('Amplitude');
281axis([0 2^14 -1 1]); % Set axis ranges.
282subplot(2,2,2);
283plot(imag(TxData));
284title('Tx Q');
285xlabel('n (samples)'); ylabel('Amplitude');
286axis([0 2^14 -1 1]); % Set axis ranges.
287subplot(2,2,3);
288plot(real(RxData));
289title('Rx I first 2^14 received samples');
290xlabel('n (samples)'); ylabel('Amplitude');
291axis([0 2^14 -1 1]); % Set axis ranges.
292subplot(2,2,4);
293plot(imag(RxData));
294title('Rx Q first 2^14 received samples');
295xlabel('n (samples)'); ylabel('Amplitude');
296axis([0 2^14 -1 1]); % Set axis ranges.
297
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299% Code to avoid conflict between users, only needed for the workshop
300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301pnet('closeall');
302!del c:\boards_lock.txt
303catch,
304% Close sockets
305pnet('closeall');
306!del c:\boards_lock.txt
307lasterr
308end
Note: See TracBrowser for help on using the browser.