root/ResearchApps/PHY/WARPLAB/WARPLab_SISO_MIMO2x2/M_Code/warplab_mimo_example_ContinuousTx_WorkshopExercise_Solution.m
| Revision 839, 10.8 kB (checked in by MelissaDuarte, 4 months ago) |
|---|
| Line | |
|---|---|
| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | % Generating a Frequency Sweep with Continous Transmission using Warplab |
| 3 | % (2x2 MIMO configuration) |
| 4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5 | % To run this code the boards must be programmed with the |
| 6 | % warplab_mimo_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). Each transmitter radio transmits |
| 13 | % a sinusoid with different frequency. |
| 14 | % 2. Prepare boards for transmission and reception and send trigger to |
| 15 | % start transmission and reception (trigger is the SYNC packet) |
| 16 | % 3. Leave continuous transmitter on for n seconds and then stop continuous |
| 17 | % transmission. |
| 18 | % 4. Read the received samples from the Warp board. |
| 19 | % 5. Reset and disable the boards. |
| 20 | % 6. Plot the transmitted and received data. |
| 21 | |
| 22 | % In this lab exercise you will write a matlab script that implements the |
| 23 | % seven steps above. Part of the code is provided, some part of the code you |
| 24 | % will write. Read the code below and fill in with your code wherever you |
| 25 | % are asked to do so. |
| 26 | |
| 27 | % NOTE: To avoid conflict with other groups using the boards, please test |
| 28 | % the code you write in this script in any of the following three ways: |
| 29 | % |
| 30 | % Option 1. Run this script from matlab's Command Window by entering the |
| 31 | % name of the script (enter warplab_example_TxRx_WorkshopExercise in |
| 32 | % matlab's Command Window). |
| 33 | % Option 2. In the menu bar go to Debug and select Run. If there |
| 34 | % are errors in the code, error messages will appear in the Command Window. |
| 35 | % Option 3. Press F5. If the are errors in the code, error messages will |
| 36 | % appear in the Command Window. |
| 37 | % |
| 38 | % DO NOT USE the Evaluate selection option and DO NOT run the script by |
| 39 | % sections. To test any change, always run the whole script by following |
| 40 | % any of the three options above. |
| 41 | |
| 42 | try, |
| 43 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 44 | % Code to avoid conflict between users, only needed for the workshop, go to |
| 45 | % step 0 below to start the initialization and definition of parameters |
| 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 47 | fid = fopen('c:\boards_lock.txt'); |
| 48 | |
| 49 | if(fid > -1) |
| 50 | fclose('all'); |
| 51 | errordlg('Boards already in use - Please try again!'); |
| 52 | return; |
| 53 | end |
| 54 | |
| 55 | !echo > c:\boards_lock.txt |
| 56 | |
| 57 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 58 | % 0. Initializaton and definition of parameters |
| 59 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | %Load some global definitions (packet types, etc.) |
| 61 | warplab_defines |
| 62 | |
| 63 | % Create Socket handles and intialize nodes |
| 64 | [socketHandles, packetNum] = warplab_initialize; |
| 65 | |
| 66 | %Separate the socket handles for easier access |
| 67 | % The first socket handle is always the magic SYNC |
| 68 | % The rest can be arranged in any combination of Tx and Rx |
| 69 | udp_Sync = socketHandles(1); |
| 70 | udp_Tx = socketHandles(2); |
| 71 | udp_RxA = socketHandles(3); |
| 72 | |
| 73 | % Define the warplab options (parameters) |
| 74 | CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14] |
| 75 | TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset] |
| 76 | TransMode = 1; %Transmission mode; in [0:1] |
| 77 | % 0: Single Transmission |
| 78 | % 1: Continuous Transmission. Tx board will continue |
| 79 | % transmitting the vector of samples until the user manually |
| 80 | % disables the transmitter. |
| 81 | CarrierChannel = 8; % Channel in the 2.4 GHz band. In [1:14] |
| 82 | Tx2GainBB = 3; %Tx Baseband Gain in [0:3] |
| 83 | Tx2GainRF = 40; %Tx RF Gain in [0:63] |
| 84 | Rx2GainBB = 15; %Rx Baseband Gain in [0:31] |
| 85 | Rx2GainRF = 1; %Rx RF Gain in [1:3] |
| 86 | Tx3GainBB = 3; %Tx Baseband Gain in [0:3] |
| 87 | Tx3GainRF = 40; %Tx RF Gain in [0:63] |
| 88 | Rx3GainBB = 15; %Rx Baseband Gain in [0:31] |
| 89 | Rx3GainRF = 1; %Rx RF Gain in [1:3] |
| 90 | TxSelect = 2; % Select transmitter radio [0:2] 0:Radio2, 1:Radio3, 2: Both |
| 91 | RxSelect = 2; % Select transmitter radio [0:2] 0:Radio2, 1:Radio3, 2: Both |
| 92 | |
| 93 | % Define the options vector; the order of options is set by the FPGA's code |
| 94 | % (C code) |
| 95 | optionsVector = [CaptOffset TxLength-1 TransMode CarrierChannel (Rx2GainBB + Rx2GainRF*2^16) (Tx2GainRF + Tx2GainBB*2^16) (Rx3GainBB + Rx3GainRF*2^16) (Tx3GainRF + Tx3GainBB*2^16) TxSelect RxSelect]; |
| 96 | % Send options vector to the nodes |
| 97 | warplab_setOptions(socketHandles,optionsVector); |
| 98 | |
| 99 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 100 | % 1. Generate a vector of samples to transmit and send the samples to the |
| 101 | % Warp board (Sample Frequency is 40MHz). Vector represents a sinusoid |
| 102 | % with complex frequency linearly increasing in time. |
| 103 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 104 | % Prepare some data to be transmitted |
| 105 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector |
| 106 | |
| 107 | % Create a signal to transmit from radio 2 |
| 108 | f1 = 1e6; |
| 109 | TxData_2 = exp(t*j*2*pi*f1); %Signal must be a row vector. The signal can |
| 110 | % be real or complex, the only constraint is that the amplitude of the real |
| 111 | % part must be in [-1:1] and the amplitude of the imaginary part must be |
| 112 | % in [-1:1] |
| 113 | |
| 114 | % Download the samples to be transmitted |
| 115 | warplab_writeSMWO(udp_Tx, TxData_2, RADIO2_TXDATA); % Download samples to |
| 116 | % radio 2 Tx Buffer |
| 117 | |
| 118 | % Create a signal to transmit from radio 3 |
| 119 | f2 = 6e6; |
| 120 | TxData_3 = linspace(0,1,TxLength).*exp(t*j*2*pi*f2); |
| 121 | % Signal must be a row vector. The signal can be real or complex, |
| 122 | % the only constraint is that the amplitude of the real part must be in |
| 123 | % [-1:1] and the amplitude of the imaginary part must be in [-1:1] |
| 124 | |
| 125 | warplab_writeSMWO(udp_Tx, TxData_3, RADIO3_TXDATA); % Download samples to |
| 126 | % radio 3 Tx Buffer |
| 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 transmitter radio path in transmitter node (enable radio 2 and |
| 133 | % radio 3 in transmitter node as transmitters) |
| 134 | warplab_sendCmd(udp_Tx, [RADIO2_TXEN RADIO3_TXEN], packetNum); |
| 135 | |
| 136 | % Enable receiver radio path in receiver node (enable radio 2 and |
| 137 | % radio 3 in receiver node as receivers) |
| 138 | warplab_sendCmd(udp_RxA, [RADIO2_RXEN RADIO3_RXEN], packetNum); |
| 139 | |
| 140 | % Prime transmitter state machine in transmitter node. Transmitter will be |
| 141 | % waiting for the SYNC packet. Transmission will be triggered when the |
| 142 | % transmitter node receives the SYNC packet. |
| 143 | warplab_sendCmd(udp_Tx, TX_START, packetNum); |
| 144 | |
| 145 | % Prime receiver state machine in receiver node. Receiver will be waiting |
| 146 | % for the SYNC packet. Capture will be triggered when the receiver |
| 147 | % node receives the SYNC packet. |
| 148 | warplab_sendCmd(udp_RxA, RX_START, packetNum); |
| 149 | |
| 150 | % Send the SYNC packet |
| 151 | warplab_sendSync(udp_Sync); |
| 152 | |
| 153 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 154 | % 3. Leave continuous transmitter on for n seconds and then stop continuous |
| 155 | % transmission and disable transmitter radio |
| 156 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 157 | % Leave continuous transmitter on for nsec seconds |
| 158 | nsec = 5; |
| 159 | pause(nsec); |
| 160 | |
| 161 | % Stop transmission |
| 162 | warplab_sendCmd(udp_Tx, TX_STOP, packetNum); % Resets the output and read |
| 163 | % address of the transmitter buffer without disabling the transmitter radio. |
| 164 | |
| 165 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 | % 4. Read the received samples from the Warp board |
| 167 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 168 | % In continuous transmitter mode the receiver stores CaptOffset samples of |
| 169 | % noise and the first TxLength samples transmitted. |
| 170 | |
| 171 | % Read back the received samples from radio 2 |
| 172 | [RawRxData_2] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset); |
| 173 | % Read back the received samples from radio 3 |
| 174 | [RawRxData_3] = warplab_readSMRO(udp_RxA, RADIO3_RXDATA, TxLength+CaptOffset); |
| 175 | % Process the received samples to obtain meaningful data |
| 176 | [RxData_2,RxOTR_2] = warplab_processRawRxData(RawRxData_2); |
| 177 | [RxData_3,RxOTR_3] = warplab_processRawRxData(RawRxData_3); |
| 178 | % Read stored RSSI data from radio 2 |
| 179 | [RawRSSIData_2] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8); |
| 180 | % Read stored RSSI data from radio 3 |
| 181 | [RawRSSIData_3] = warplab_readSMRO(udp_RxA, RADIO3_RSSIDATA, (TxLength+CaptOffset)/8); |
| 182 | % Procecss Raw RSSI data to obtain meningful RSSI values |
| 183 | [RxRSSI_2] = warplab_processRawRSSIData(RawRSSIData_2); |
| 184 | [RxRSSI_3] = warplab_processRawRSSIData(RawRSSIData_3); |
| 185 | |
| 186 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 187 | % 5. Reset and disable the boards |
| 188 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 189 | % Reset the receiver |
| 190 | warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum); |
| 191 | |
| 192 | % Disable the receiver radio 2 radio 3 |
| 193 | warplab_sendCmd(udp_RxA, [RADIO2_RXDIS RADIO3_RXDIS], packetNum); |
| 194 | |
| 195 | % Disable the transmitter radio 2 and radio 3 |
| 196 | warplab_sendCmd(udp_Tx, [RADIO2_TXDIS RADIO3_TXDIS], packetNum); |
| 197 | |
| 198 | % Close sockets |
| 199 | pnet('closeall'); |
| 200 | |
| 201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 202 | % 6. Plot the transmitted and received data |
| 203 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 204 | figure; |
| 205 | subplot(4,2,1); |
| 206 | plot(real(TxData_2)); |
| 207 | title('Tx Radio 2 I'); |
| 208 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 209 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 210 | subplot(4,2,2); |
| 211 | plot(imag(TxData_2)); |
| 212 | title('Tx Radio 2 Q'); |
| 213 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 214 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 215 | subplot(4,2,3); |
| 216 | plot(real(TxData_3)); |
| 217 | title('Tx Radio 3 I'); |
| 218 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 219 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 220 | subplot(4,2,4); |
| 221 | plot(imag(TxData_3)); |
| 222 | title('Tx Radio 3 Q'); |
| 223 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 224 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 225 | subplot(4,2,5); |
| 226 | plot(real(RxData_2)); |
| 227 | title('Rx Radio 2 I'); |
| 228 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 229 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 230 | subplot(4,2,6); |
| 231 | plot(imag(RxData_2)); |
| 232 | title('Rx Radio 2 Q'); |
| 233 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 234 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 235 | subplot(4,2,7); |
| 236 | plot(real(RxData_3)); |
| 237 | title('Rx Radio 3 I'); |
| 238 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 239 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 240 | subplot(4,2,8); |
| 241 | plot(imag(RxData_3)); |
| 242 | title('Rx Radio 3 Q'); |
| 243 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 244 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 245 | |
| 246 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 247 | % Code to avoid conflict between users, only needed for the workshop |
| 248 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 249 | pnet('closeall'); |
| 250 | !del c:\boards_lock.txt |
| 251 | catch, |
| 252 | % Close sockets |
| 253 | pnet('closeall'); |
| 254 | !del c:\boards_lock.txt |
| 255 | lasterr |
| 256 | end |
Note: See TracBrowser
for help on using the browser.