source: ResearchApps/PHY/WARPLAB/WARPLab_v06_1/M_Code_Examples/warplab_simo_1x2_example_TxRxAGC_80211aPreamble.m

Last change on this file was 1311, checked in by mduarte, 15 years ago

Adding files for WARPLab release 05

File size: 18.2 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmitting and Receiving Data using WARPLab with Automatic Gain Control
3% (SISO Configuration)
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5% Before looking at this code we recommend getting familiar with the
6% warplab_siso_example_TxRx.m code which doesn't use AGC hence it is easier
7% to understand.
8
9% The specific steps implemented in this script are the following
10
11% 0. Initializaton and definition of parameters
12% 1. Generate a vector of samples to transmit and send the samples to the
13% WARP board (Sample Frequency is 40MHz)
14% 2. Prepare WARP boards for transmission and reception and send trigger to
15% start transmission and reception (trigger is the SYNC packet)
16% 3. Read the received samples from the WARP board
17% 4. Read values related to AGC
18% 5. Reset and disable the boards
19% 6. 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.
38% For this experiment node 1 will be set as the transmitter and node
39% 2 will be set as the receiver (this is done later in the code), hence,
40% there is no need to define receive gains for node 1 and there is no
41% need to define transmitter gains for node 2.
42TxDelay = 0; % Number of noise samples per Rx capture. In [0:2^14]
43TxLength = 2^14-1; % Length of transmission. In [0:2^14-1-TxDelay]
44CarrierChannel = 13; % Channel in the 2.4 GHz band. In [1:14]
45Node1_Radio2_TxGain_BB = 1; % Tx Baseband Gain. In [0:3]
46Node1_Radio2_TxGain_RF = 25; % Tx RF Gain. In [0:63]
47TxMode = 0; % Transmission mode. In [0:1]
48            % 0: Single Transmission
49            % 1: Continuous Transmission. Tx board will continue
50            % transmitting the vector of samples until the user manually
51            % disables the transmitter.
52Node2_MGC_AGC_Select = 1;   % Set MGC_AGC_Select=1 to enable Automatic Gain Control (AGC).
53                            % Set MGC_AGC_Select=0 to enable Manual Gain Control (MGC).
54                            % By default, the nodes are set to MGC.
55Node2_TargetdBmAGC = -10; % AGC Target dBm. A larger target value will
56                          % result in larger Rx gains set by AGC. This is
57                          % the value we tune if AGC is not setting gains
58                          % correctly.
59Node2_NoiseEstdBmAGC = -95; % Noise power in dBm. -95dBm is a reasonable
60            % value for wireless. If AGC is not setting gains correctly
61            % this value may need to be modified. Usually we first try to
62            % change the TargetdBmAGC  before changing the NoiseEstdBmAGC
63Node2_Thresh1 = -90; 
64Node2_Thresh2 = -53;
65Node2_Thresh3 = -43;
66% Change format of Thresholds so they can be correctly understood by
67% the FPGA:
68Node_2Thresholds = uint32(Node2_Thresh3+2^8)*2^16+uint32(Node2_Thresh2+2^8)*2^8+uint32(Node2_Thresh1+2^8);
69% The three thresholds above are used to set the Rx RF gain. If the RSSI in
70% dBm of the received signal is less than -90 then the AGC declares the
71% signal to be too low and quits. If the RSSI in dBm of the received signal
72% is between -53 and -90 then the AGC selects the largest RF gain : 3. If
73% the RSSI dBm is between -43 and -53 then the AGC sets medium RF gain : 2. If
74% the RSSI dBm is larger than -43 then the AGC sets low RF gain :1.
75% If AGC is no setting gains correctly then these three thresholds may need
76% to be modified. Usually we first try to change the TargetdBmAGC  before
77% changing the Thresholds.
78% Remember there are 3 possible Rx RF gains: 1,2,3. Each step corresponds
79% to 15dB: Changing the gain from 2 to 3 increases the Rx gain by 15 dB.
80Node2_AGCTrigger_nsamps_delay = 50; % The AGC core should not be started before the
81% signal arrives. If TxDelay=0 then Tx and Rx should start at exactly the
82% same time (upon reception of magic sync) however, because of jitter in
83% reception of the magic sync, it may happed that the Rx gets the magic
84% sync before the Tx. If this is the case then the AGC will set wrong gains
85% because AGC will use RSSI values that are measured before reception of the signal.
86% To avoid this we can delay the trigger of the AGC core by
87% Node2_AGCTrigger_nsamps_delay samples relative to the reception of the
88% magic sync. We recommend to set this value between 0 and 50 samples. We
89% have not observed magic sync jitters greater than 50 samples.
90Node2_Enable_DCOffset_Correction = 1; % Enable/disable correction of DC Offsets (DCO).
91% Node2_Enable_DCOffset_Correction = 0; Disable correction of DC Offsets at
92% AGC
93% Node2_Enable_DCOffset_Correction = 1; Enable correction of DC Offsets at
94% AGC
95% Change of Rx gains by AGC may result in DC offsets. The AGC can correct
96% these offsets but the user must be very careful on the choice of preamble
97% used for AGC. For DCO correction at AGC to work properly the first 320
98% samples must correspond to a periodic signal with an average (DC) of zero
99% over 32 consecutive samples, this will generate the right signal required
100% by AGC DCO correction for a sampling frequency of 40 MHz. AGC DCO
101% correction can be disabled by setting Node2_Enable_DCOffset_Correction=0,
102% in this case there is no requirement for the periodicity of the
103% preamble used for AGC.
104
105% NOTES ON AGC:
106% 1.    As soon as AGC is triggered, it takes the AGC core
107% approximately 250 samples (at 40MHz sampling frequency) to set gains. If
108% DCO correction at AGC is enabled it takes the AGC an extra 32 samples to
109% filter DCO. This means that the first 250 samples received (282 when using DCO
110% correction) may not contain useful data because during reception of these
111% samples Rx gains (and DCO correction) were not set correctly.
112% 2.     The first 250 samples must be representative of the rest of signal
113% being transmitted (similar bandwidth and amplitude), otherwise the gains
114% set by the AGC will work for the first 250 samples but will be wrong
115% (causing saturation or underflow) for the rest of the signal.
116
117% Download the WARPLab parameters to the WARP nodes.
118% The nodes store the TxDelay, TxLength, and TxMode parameters in
119% registers defined in the WARPLab sysgen model. The nodes set radio
120% related parameters CarrierChannel, TxGains, and RxGains, using the
121% radio controller functions.
122
123% The TxDelay, TxLength, and TxMode parameters need to be known at the transmitter;
124% the receiver doesn't require knowledge of these parameters (the receiver
125% will always capture 2^14 samples). For this exercise node 1 will be set as
126% the transmitter (this is done later in the code). Since TxDelay, TxLength and
127% TxMode are only required at the transmitter we download the TxDelay, TxLength and
128% TxMode parameters only to the transmitter node (node 1).
129warplab_writeRegister(udp_node1,TX_DELAY,TxDelay);
130warplab_writeRegister(udp_node1,TX_LENGTH,TxLength);
131warplab_writeRegister(udp_node1,TX_MODE,TxMode);
132% The CarrierChannel parameter must be downloaded to all nodes 
133warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,CarrierChannel);
134warplab_setRadioParameter(udp_node2,CARRIER_CHANNEL,CarrierChannel);
135% Node 1 will be set as the transmitter so download Tx gains to node 1.
136warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16));
137% Node 2 will be set as the receiver so download AGC related parameters to node 2.
138warplab_setAGCParameter(udp_node2,MGC_AGC_SEL, Node2_MGC_AGC_Select); % AGC mode is enabled when
139% Node2_MGC_AGC_Select = 1. THIS COMMAND RESETS AND INITIALIZES THE AGC. THIS COMMAND INITIALIZES
140% AGC PARAMETER TO DEFAULTS. Default values are hard coded in warplab C code.
141% The default values can be changed as is done in the 5 lines below.
142warplab_setAGCParameter(udp_node2,SET_AGC_TARGET_dBm, Node2_TargetdBmAGC);
143warplab_setAGCParameter(udp_node2,SET_AGC_NOISEEST_dBm, Node2_NoiseEstdBmAGC);
144warplab_setAGCParameter(udp_node2,SET_AGC_THRESHOLDS, Node_2Thresholds);
145warplab_setAGCParameter(udp_node2,SET_AGC_TRIG_DELAY, Node2_AGCTrigger_nsamps_delay);
146warplab_setAGCParameter(udp_node2,SET_AGC_DCO_EN_DIS, Node2_Enable_DCOffset_Correction);
147
148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149% 1. Generate a vector of samples to transmit and send the samples to the
150% WARP board (Sample Frequency is 40MHz)
151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152% First generate the preamble for AGC. The preamble corresponds to the
153% short symbols borrowed from the 802.11a PHY standard
154shortSymbol_freq = [0 0 0 0 0 0 0 0 1+i 0 0 0 -1+i 0 0 0 -1-i 0 0 0 1-i 0 0 0 -1-i 0 0 0 1-i 0 0 0 0 0 0 0 1-i 0 0 0 -1-i 0 0 0 1-i 0 0 0 -1-i 0 0 0 -1+i 0 0 0 1+i 0 0 0 0 0 0 0].';
155shortSymbol_time = ifft(fftshift(shortSymbol_freq));
156shortSymbol_time = shortSymbol_time(1:16).';
157shortsyms_10 = repmat(shortSymbol_time,1,10);
158preamble_I = real(shortsyms_10);
159preamble_Q = imag(shortsyms_10);
160% Upsample by 2 so the standard preamble occupies a bandwith of +-10MHz (computed
161% for a sampling frequency of 40 MHz)
162[preamble_I_up2] = interp(preamble_I, 2);
163[preamble_Q_up2] = interp(preamble_Q, 2);
164% Scale to span -1,1 range of DAC
165scale_ShortSyms = max([ max(abs(preamble_I_up2)), max(abs(preamble_Q_up2)) ]);
166[preamble_I_up2] = (1/scale_ShortSyms)*preamble_I_up2;
167[preamble_Q_up2] = (1/scale_ShortSyms)*preamble_Q_up2;
168ShortTrainingSyms_up2 = (preamble_I_up2 + sqrt(-1)*preamble_Q_up2);
169% Notice that ShortTrainingSyms_up2 meets periodicity requirement for
170% correct function of AGC DC Offset correction
171
172
173% Create a signal to transmit after AGC preamble
174
175% The signal must meet the following requirements:
176% - Signal to transmit must be a row vector.
177% - The amplitude of the real part must be in [-1:1] and the amplitude
178% of the imaginary part must be in [-1:1].
179% - Highest frequency component is limited to 9.5 MHz (signal bandwidth
180% is limited to 19 MHz)
181% - Lowest frequency component is limited to 30 kHz
182
183% We will send 1000 zeros after AGC preamble, then we will send one
184% sequence of long training symbols borrowed from the 802.11a PHY standard,
185% then we will anlternate bewteen zeros and long training symbols and
186% short training symbols
187
188% Generate zero vector
189zero_vector = zeros(1,1000);
190
191% Generate long 802.11a long training symbols.
192% Generate one long training symbol
193LongSymbol_freq_bot = [0 0 0 0 0 0 1 1 -1 -1 1 1 -1 1 -1 1 1 1 1 1 1 -1 -1 1 1 -1 1 -1 1 1 1 1]';
194LongSymbol_freq_top = [1 -1 -1 1 1 -1 1 -1 1 -1 -1 -1 -1 -1 1 1 -1 -1 1 -1 1 -1 1 1 1 1 0 0 0 0 0]';
195LongSymbol_freq = [LongSymbol_freq_bot ; 0 ; LongSymbol_freq_top];
196LongSymbol_time = ifft(fftshift(LongSymbol_freq)).';
197LongSymbol_time_up2 = interp(LongSymbol_time,2); % Upsample by 2 so that
198% the final LongTrainingSyms_up2 signal will have a bandwidth of +-10MHz
199% (computed for a sampling frequency of 40 MHz)
200scale = 1/max([ max(abs(real(LongSymbol_time_up2))), max(abs(imag(LongSymbol_time_up2))) ]);
201LongSymbol_time_up2 = scale * LongSymbol_time_up2; % Scale to span -1,1 range of DAC
202% Concatenate two long training symbols and add cyclic prefix
203%longsyms_2_cp = [longSymbol_time(33:64) repmat(longSymbol_time,1,2)];
204%longsyms_2_cp_up2 = interp(longsyms_2_cp,2); % Upsample by 2
205LongTrainingSyms_up2 = [LongSymbol_time_up2(65:128) repmat(LongSymbol_time_up2,1,2)];
206
207Node1_Radio2_TxData = [ShortTrainingSyms_up2, zero_vector, LongTrainingSyms_up2, zero_vector];
208Node1_Radio2_TxData = repmat(Node1_Radio2_TxData,1,5);
209Node1_Radio2_TxData = [Node1_Radio2_TxData zeros(1,TxLength-length(Node1_Radio2_TxData))];
210
211% Download the samples to be transmitted
212warplab_writeSMWO(udp_node1, RADIO2_TXDATA, Node1_Radio2_TxData);
213
214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215% 2. Prepare WARP boards for transmission and reception and send trigger to
216% start transmission and reception (trigger is the SYNC packet)
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218% The following lines of code set node 1 as transmitter and node 2 as
219% receiver; transmission and capture are triggered by sending the SYNC
220% packet.
221
222% Enable transmitter radio path in radio 2 in node 1 (enable radio 2 in
223% node 1 as transmitter)
224warplab_sendCmd(udp_node1, RADIO2_TXEN, packetNum);
225
226% Enable transmission of node1's radio 2 Tx buffer (enable transmission
227% of samples stored in radio 2 Tx Buffer in node 1)
228warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXEN, packetNum);
229
230% Enable receiver radio path in radios 2 and 3 in node 2 (enable radio 2
231% and radio 3 in node 2 as receiver)
232warplab_sendCmd(udp_node2, [RADIO2_RXEN RADIO3_RXEN], packetNum);
233
234% Enable capture in node2's radio 2 and radio 3 Rx Buffer (enable radio 2 rx buffer
235% and radio 3 rx buffer in node 2 for storage of samples)
236warplab_sendCmd(udp_node2, [RADIO2RXBUFF_RXEN RADIO3RXBUFF_RXEN], packetNum);
237
238% Prime transmitter state machine in node 1. Node 1 will be
239% waiting for the SYNC packet. Transmission from node 1 will be triggered
240% when node 1 receives the SYNC packet.
241warplab_sendCmd(udp_node1, TX_START, packetNum);
242
243% Prime receiver state machine in node 2. Node 2 will be waiting
244% for the SYNC packet. Capture at node 2 will be triggered when node 2
245% receives the SYNC packet.
246warplab_sendCmd(udp_node2, RX_START, packetNum);
247
248% Send the SYNC packet
249warplab_sendSync(udp_Sync);
250
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252% 3. Read the received samples from the WARP board
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% Read back the received samples
255[Node2_Radio2_RawRxData] = warplab_readSMRO(udp_node2, RADIO2_RXDATA, TxLength+TxDelay);
256[Node2_Radio3_RawRxData] = warplab_readSMRO(udp_node2, RADIO3_RXDATA, TxLength+TxDelay);
257% Process the received samples to obtain meaningful data
258[Node2_Radio2_RxData,Node2_Radio2_RxOTR] = warplab_processRawRxData(Node2_Radio2_RawRxData);
259[Node2_Radio3_RxData,Node2_Radio3_RxOTR] = warplab_processRawRxData(Node2_Radio3_RawRxData);
260
261%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262% 4. Read values related to AGC
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264% Read the sample number that corresponds to AGC being done setting gains
265[Node2_AGC_Set_Addr] = warplab_readAGCValue(udp_node2, READ_AGC_DONE_ADDR);
266% Received samples are stored in Received buffer, when AGC is done the
267% address of the sample being written at that moment is stored, this
268% address is Node2_AGC_Set_Addr. This means that samples after
269% Node2_AGC_Set_Addr sample are applied the Rx Gains computed by AGC. From
270% sample zero the Node2_AGC_Set_Addr the amplitude of the received signal
271% may vary a lot because during this time the AGC was not done setting
272% gains.
273
274% Read the value of the RSSI that corresponds to AGC being done setting
275% gains. When AGC is done the currrent RSSI value measured by node 2 radio2
276% and radio 3 is stored in registers which can be read as shown below.
277[Node2_Radio2_AGC_Set_RSSI] = warplab_readAGCValue(udp_node2, READ_RADIO2AGCDONERSSI);
278[Node2_Radio3_AGC_Set_RSSI] = warplab_readAGCValue(udp_node2, READ_RADIO3AGCDONERSSI);
279
280% Read the gains set by AGC
281[Node2_Raw_AGC_Set_Gains] = warplab_readAGCValue(udp_node2, READ_AGC_GAINS);
282[Node2_GainsRF,Node2_GainsBB] = warplab_processRawAGCGainsData(Node2_Raw_AGC_Set_Gains);
283% Since only radio 2 and 3 were used then we only care about the second and third entries in
284% Node2_GainsRF and Node2_GainsBB vectors
285Node2_Radio2_GainsRF = Node2_GainsRF(2);
286Node2_Radio2_GainsBB = Node2_GainsBB(2);
287Node2_Radio3_GainsRF = Node2_GainsRF(3);
288Node2_Radio3_GainsBB = Node2_GainsBB(3);
289
290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291% 5. Reset and disable the boards
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293% Set radio 2 Tx buffer in node 1 back to Tx disabled mode
294warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXDIS, packetNum);
295
296% Disable the transmitter radio
297warplab_sendCmd(udp_node1, RADIO2_TXDIS, packetNum);
298
299% Set radio 2 and 3 Rx buffer in node 2 back to Rx disabled mode
300warplab_sendCmd(udp_node2, [RADIO2RXBUFF_RXDIS RADIO3RXBUFF_RXDIS], packetNum);
301
302% Disable the receiver radios
303warplab_sendCmd(udp_node2, [RADIO2_RXDIS RADIO3_RXDIS], packetNum);
304
305% Resets Rx gains to default values of RF Gain of 3 and Baseband gain of
306% 26. Sets AGC ready for a new capture.
307warplab_sendCmd(udp_node2, AGC_RESET, packetNum);
308
309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310% 6. Plot the transmitted and received data and close sockets
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312figure;
313subplot(3,2,1);
314plot(real(Node1_Radio2_TxData));
315title('Tx Node 1 Radio 2 I');
316xlabel('n (samples)'); ylabel('Amplitude');
317axis([0 2^14 -1 1]); % Set axis ranges.
318subplot(3,2,2);
319plot(imag(Node1_Radio2_TxData));
320title('Tx Node 1 Radio 2 Q');
321xlabel('n (samples)'); ylabel('Amplitude');
322axis([0 2^14 -1 1]); % Set axis ranges.
323subplot(3,2,3);
324plot(real(Node2_Radio2_RxData));
325title('Rx Node 2 Radio 2 I');
326xlabel('n (samples)'); ylabel('Amplitude');
327axis([0 2^14 -1 1]); % Set axis ranges.
328subplot(3,2,4);
329plot(imag(Node2_Radio2_RxData));
330title('Rx Node 2 Radio 2 Q');
331xlabel('n (samples)'); ylabel('Amplitude');
332axis([0 2^14 -1 1]); % Set axis ranges.
333subplot(3,2,5);
334plot(real(Node2_Radio3_RxData));
335title('Rx Node 2 Radio 3 I');
336xlabel('n (samples)'); ylabel('Amplitude');
337axis([0 2^14 -1 1]); % Set axis ranges.
338subplot(3,2,6);
339plot(imag(Node2_Radio3_RxData));
340title('Rx Node 2 Radio 3 Q');
341xlabel('n (samples)'); ylabel('Amplitude');
342axis([0 2^14 -1 1]); % Set axis ranges.
343
344% Close sockets
345pnet('closeall');
Note: See TracBrowser for help on using the repository browser.