root/ResearchApps/PHY/WARPLAB/WARPLab_SISO_MIMO2x2/M_Code/warplab_siso_example_ChannelEstim_WorkshopExercise_Solution.m
| Revision 839, 15.6 kB (checked in by MelissaDuarte, 4 months ago) |
|---|
| Line | |
|---|---|
| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | % Using Warplab (SISO configuration) to Estimate the Amplitude and Phase of |
| 3 | % a Narrowband Flat Fading Wireless Channel |
| 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. Transmit a narrowband signal using Warplab |
| 11 | % 1. Remove from the received vector the samples that do not correspond to |
| 12 | % transmitted data. |
| 13 | % 2. Compute the amplitude and the phase of the transmitted and received |
| 14 | % sammples |
| 15 | % 3. Compute the channel amplitude and channel phase per sample |
| 16 | |
| 17 | % NOTE 1: The amplitude and phase computed in this exercise correspond to the |
| 18 | % amplitude and phase of the channel together with the amplitude and phase |
| 19 | % of the hardware. In other words, the effect of the radios (like gains and |
| 20 | % carrier frequency offset)is also part of the channel. |
| 21 | |
| 22 | % You will write a matlab script that implements the four steps above. |
| 23 | % Part of the code is provided, some part of the code you will write. Read |
| 24 | % the code below and fill in with your code wherever you are asked to do |
| 25 | % so. |
| 26 | |
| 27 | % NOTE 2 : To avoid conflict with other groups using the boards, please |
| 28 | % test the code you write in this script in any of the following three |
| 29 | % ways: |
| 30 | % |
| 31 | % Option 1. Run this script from matlab's Command Window by entering the |
| 32 | % name of the script (enter warplab_example_ChannelEstimation_WorkshopExercise |
| 33 | % in matlab's Command Window). |
| 34 | % Option 2. In the menu bar go to Debug and select Run. If there |
| 35 | % are errors in the code, error messages will appear in the Command Window. |
| 36 | % Option 3. Press F5. If the are errors in the code, error messages will |
| 37 | % appear in the Command Window. |
| 38 | % |
| 39 | % DO NOT USE the Evaluate selection option and DO NOT run the script by |
| 40 | % sections. To test any change, always run the whole script by following |
| 41 | % any of the three options above. |
| 42 | |
| 43 | try, |
| 44 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 45 | % Code to avoid conflict between users, only needed for the workshop, go to |
| 46 | % step 0 below to transmit a narrowband signal using Warplab |
| 47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 48 | fid = fopen('c:\boards_lock.txt'); |
| 49 | |
| 50 | if(fid > -1) |
| 51 | fclose('all'); |
| 52 | errordlg('Boards already in use - Please try again!'); |
| 53 | return; |
| 54 | end |
| 55 | |
| 56 | !echo > c:\boards_lock.txt |
| 57 | |
| 58 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 59 | % 0. Transmit a narrowband signal using warplab |
| 60 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 61 | % Follow the steps for transmission and reception of data using Warplab. |
| 62 | % These are the steps implemented in the previous lab exercise, the |
| 63 | % following sections (0.0 to 0.5) guide you through the steps. |
| 64 | |
| 65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 | % 0.0. Initializaton and definition of parameters |
| 67 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 68 | %-------------------------------------------------------------------------% |
| 69 | % USER CODE HERE |
| 70 | |
| 71 | % Follow the steps for Initializaton and definition of parameters. |
| 72 | % You can copy the code corresponding to step 0 in the previous lab |
| 73 | % exercise and then paste it here. |
| 74 | |
| 75 | % % IMPORTANT NOTE: For this exercise set TransMode = 0; |
| 76 | |
| 77 | % Remember to set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the |
| 78 | % same values you used in the warplab_siso_GUI. |
| 79 | |
| 80 | %Load some global definitions (packet types, etc.) |
| 81 | warplab_defines |
| 82 | |
| 83 | % Create Socket handles and intialize nodes |
| 84 | [socketHandles, packetNum] = warplab_initialize; |
| 85 | |
| 86 | %Separate the socket handles for easier access |
| 87 | % The first socket handle is always the magic SYNC |
| 88 | % The rest can be arranged in any combination of Tx and Rx |
| 89 | udp_Sync = socketHandles(1); |
| 90 | udp_Tx = socketHandles(2); |
| 91 | udp_RxA = socketHandles(3); |
| 92 | |
| 93 | % Define the warplab options (parameters) |
| 94 | CaptOffset = 1000; %Number of noise samples per Rx capture. In [0:2^14] |
| 95 | TxLength = 2^14-1000; %Length of transmission. In [0:2^14-CaptOffset] |
| 96 | TransMode = 0; %Transmission mode. In [0:1] |
| 97 | % 0: Single Transmission |
| 98 | % 1: Continuous Transmission. Tx board will continue |
| 99 | % transmitting the vector of samples until the user manually |
| 100 | % disables the transmitter. |
| 101 | CarrierChannel = 8; % Channel in the 2.4 GHz band. In [1:14] |
| 102 | TxGainBB = 3; %Tx Baseband Gain. In [0:3] |
| 103 | TxGainRF = 40; %Tx RF Gain. In [0:63] |
| 104 | RxGainBB = 15; %Rx Baseband Gain. In [0:31] |
| 105 | RxGainRF = 1; %Rx RF Gain. In [1:3] |
| 106 | |
| 107 | % Define the options vector; the order of options is set by the FPGA's code |
| 108 | % (C code) |
| 109 | optionsVector = [CaptOffset TxLength-1 TransMode CarrierChannel (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16)]; |
| 110 | % Send options vector to the nodes |
| 111 | warplab_setOptions(socketHandles,optionsVector); |
| 112 | %-------------------------------------------------------------------------% |
| 113 | |
| 114 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 115 | % 0.1. Generate a vector of samples to transmit and send the samples to the |
| 116 | % Warp board (Sample Frequency is 40MHz) |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | |
| 119 | %-------------------------------------------------------------------------% |
| 120 | % USER CODE HERE |
| 121 | |
| 122 | % Follow the steps to Generate a vector of samples to transmit and send |
| 123 | % the samples to the Warp board. |
| 124 | % You can copy the code corresponding to step 1 in the previous lab |
| 125 | % exercise and then paste it here. |
| 126 | % You can use the following two lines of code to generate the narrowband |
| 127 | % signal: |
| 128 | % t = 0:(1/40e6):TxLength/40e6 - 1/40e6; |
| 129 | % TxData = exp(t*j*2*pi*1e6); |
| 130 | |
| 131 | % For the rest of the code to work, make sure the transmit vector is |
| 132 | % stored in a variable called 'TxData'. |
| 133 | |
| 134 | % Prepare some data to be transmitted |
| 135 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; %Create time vector. |
| 136 | TxData = exp(t*j*2*pi*1e6); % Create a signal to transmit. Signal must be a |
| 137 | % row vector. The signal can be real or complex, the only constraint is |
| 138 | % that the amplitude of the real part must be in [-1:1] and the amplitude |
| 139 | % of the imaginary part must be in [-1:1] |
| 140 | |
| 141 | % Download the samples to be transmitted |
| 142 | warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA); |
| 143 | %-------------------------------------------------------------------------% |
| 144 | |
| 145 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 146 | % 0.2. Prepare boards for transmission and reception and send trigger to |
| 147 | % start transmission and reception (trigger is the SYNC packet) |
| 148 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 149 | %-------------------------------------------------------------------------% |
| 150 | % USER CODE HERE |
| 151 | % Follow the steps to prepare boards for transmission and reception and |
| 152 | % send trigger to start transmission and reception. |
| 153 | % You can copy the code corresponding to step 2 in the previous lab |
| 154 | % exercise and then paste it here. |
| 155 | |
| 156 | % Enable transmitter radio path in transmitter node |
| 157 | warplab_sendCmd(udp_Tx, RADIO2_TXEN, packetNum); |
| 158 | |
| 159 | % Enable receiver radio path in receiver node |
| 160 | warplab_sendCmd(udp_RxA, RADIO2_RXEN, packetNum); |
| 161 | |
| 162 | % Prime transmitter state machine in transmitter node. Transmitter will be |
| 163 | % waiting for the SYNC packet. Transmission will be triggered when the |
| 164 | % transmitter node receives the SYNC packet. |
| 165 | warplab_sendCmd(udp_Tx, TX_START, packetNum); |
| 166 | |
| 167 | % Prime receiver state machine in receiver node. Receiver will be waiting |
| 168 | % for the SYNC packet. Capture will be triggered when the receiver |
| 169 | % node receives the SYNC packet. |
| 170 | warplab_sendCmd(udp_RxA, RX_START, packetNum); |
| 171 | |
| 172 | % Send the SYNC packet |
| 173 | warplab_sendSync(udp_Sync); |
| 174 | %-------------------------------------------------------------------------% |
| 175 | |
| 176 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 177 | % 0.3. Read the received smaples from the Warp board |
| 178 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 179 | |
| 180 | %-------------------------------------------------------------------------% |
| 181 | % USER CODE HERE |
| 182 | % Follow the steps to read the received samples from the Warp board. |
| 183 | % You can copy the code corresponding to step 3 in the previous lab |
| 184 | % exercise and then paste it here. There is no need to read the RSSI, but |
| 185 | % you can do so. |
| 186 | |
| 187 | % For the rest of the code to work, make sure the received vector is |
| 188 | % stored in a variable called 'RxData'. |
| 189 | |
| 190 | % Read back the received samples |
| 191 | [RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset); |
| 192 | % Process the received samples to obtain meaningful data |
| 193 | [RxData,RxOTR] = warplab_processRawRxData(RawRxData); |
| 194 | % Read stored RSSI data |
| 195 | [RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8); |
| 196 | % Procecss Raw RSSI data to obtain meningful RSSI values |
| 197 | [RxRSSI] = warplab_processRawRSSIData(RawRSSIData); |
| 198 | %-------------------------------------------------------------------------% |
| 199 | |
| 200 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 201 | % 0.4. Reset and disable the boards |
| 202 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 203 | %-------------------------------------------------------------------------% |
| 204 | % USER CODE HERE |
| 205 | % Follow the steps to reset and disable the boards. |
| 206 | % You can copy the code corresponding to step 4 in the previous lab |
| 207 | % exercise and then paste it here. |
| 208 | |
| 209 | % Reset the receiver |
| 210 | warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum); |
| 211 | |
| 212 | % Disable the receiver radio |
| 213 | warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum); |
| 214 | |
| 215 | % Disable the transmitter radio |
| 216 | warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum); |
| 217 | |
| 218 | % Close sockets |
| 219 | pnet('closeall'); |
| 220 | %-------------------------------------------------------------------------% |
| 221 | |
| 222 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 223 | % 0.5. Plot the transmitted and received data |
| 224 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 225 | |
| 226 | %-------------------------------------------------------------------------% |
| 227 | % USER CODE HERE |
| 228 | % If you are interested in looking at the received and transmitted data you |
| 229 | % can copy the code corresponding to step 5 in the previous lab exercise |
| 230 | % and paste it here |
| 231 | |
| 232 | figure; |
| 233 | subplot(2,2,1); |
| 234 | plot(real(TxData)); |
| 235 | title('Tx I'); |
| 236 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 237 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 238 | subplot(2,2,2); |
| 239 | plot(imag(TxData)); |
| 240 | title('Tx Q'); |
| 241 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 242 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 243 | subplot(2,2,3); |
| 244 | plot(real(RxData)); |
| 245 | title('Rx I'); |
| 246 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 247 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 248 | subplot(2,2,4); |
| 249 | plot(imag(RxData)); |
| 250 | title('Rx Q'); |
| 251 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 252 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 253 | %-------------------------------------------------------------------------% |
| 254 | |
| 255 | |
| 256 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 257 | % 1. Remove from the received vector the samples that do not correspond to |
| 258 | % transmitted data. In other words, remove from the received vector samples |
| 259 | % 1 to CaptOffset. This step will remove samples that correspond to measured |
| 260 | % noise and make the RxData vector the same length as the TxData vector |
| 261 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 262 | RxData = RxData(CaptOffset+1:end); |
| 263 | |
| 264 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 265 | % 2. Compute the amplitude and the phase of the transmitted and received |
| 266 | % sammples |
| 267 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 268 | |
| 269 | %-------------------------------------------------------------------------% |
| 270 | % USER CODE HERE |
| 271 | % Compute the magnitude per sample of the transmitted and received |
| 272 | % data. Store the magnitude of the transmitted data in a variable named |
| 273 | % 'mag_TxData'. Store the magnitude of the received data in a variable |
| 274 | % named 'mag_RxData'. |
| 275 | % Hint: You can use Matlab's 'abs' function |
| 276 | |
| 277 | % Compute the magnitude per sample of the transmitted and received |
| 278 | % data |
| 279 | mag_TxData = abs(TxData); |
| 280 | mag_RxData = abs(RxData); |
| 281 | %-------------------------------------------------------------------------% |
| 282 | |
| 283 | %-------------------------------------------------------------------------% |
| 284 | % USER CODE HERE |
| 285 | % Compute the phase per sample of the transmitted and received |
| 286 | % data. Store the phase (in radians) of the transmitted data in a variable |
| 287 | % named 'phase_TxData'. Store the phase (in radians) of the received data |
| 288 | % in a variable named 'phase_RxData'. |
| 289 | % Hint: You can use Matlab's 'angle' function |
| 290 | |
| 291 | % Compute the phase per sample of the transmitted and received data |
| 292 | phase_TxData = angle(TxData); |
| 293 | phase_RxData = angle(RxData); |
| 294 | %-------------------------------------------------------------------------% |
| 295 | |
| 296 | phase_TxData_unw = unwrap(phase_TxData); |
| 297 | phase_TxData = phase_TxData *180/pi; %Convert to degrees |
| 298 | phase_TxData_unw = phase_TxData_unw *180/pi; %Convert to degrees |
| 299 | phase_RxData_unw = unwrap(phase_RxData); |
| 300 | phase_RxData = phase_RxData *180/pi; %Convert to degrees |
| 301 | phase_RxData_unw = phase_RxData_unw *180/pi; %Convert to degrees |
| 302 | |
| 303 | |
| 304 | % Plot magnitude and phase of transmitted and received samples |
| 305 | figure; |
| 306 | subplot(2,3,1); |
| 307 | plot(mag_TxData); |
| 308 | title('Tx Magnitude'); |
| 309 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 310 | subplot(2,3,2); |
| 311 | plot(phase_TxData); |
| 312 | title('Tx Phase'); |
| 313 | xlabel('n (samples)'); ylabel('Degrees'); |
| 314 | subplot(2,3,3); |
| 315 | plot(phase_TxData_unw); |
| 316 | title('Tx Phase unwrapped'); |
| 317 | xlabel('n (samples)'); ylabel('Degrees'); |
| 318 | subplot(2,3,4); |
| 319 | plot(mag_RxData); |
| 320 | title('Rx Magnitude'); |
| 321 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 322 | subplot(2,3,5); |
| 323 | plot(phase_RxData); |
| 324 | title('Rx Phase'); |
| 325 | xlabel('n (samples)'); ylabel('Degrees'); |
| 326 | subplot(2,3,6); |
| 327 | plot(phase_RxData_unw); |
| 328 | title('Rx Phase unwrapped'); |
| 329 | xlabel('n (samples)'); ylabel('Degrees'); |
| 330 | |
| 331 | |
| 332 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | % 3. Compute the channel amplitude and channel phase per sample |
| 334 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 335 | |
| 336 | %-------------------------------------------------------------------------% |
| 337 | % USER CODE HERE |
| 338 | % Compute the channel amplitude per sample. Store the result in a variable |
| 339 | % named 'channel_amplitude' |
| 340 | % Hint 1: |
| 341 | % Channel amplitude = Magnitude of received samples / Magnitude of transmited samples |
| 342 | % Hint 2: |
| 343 | % You can use Matlab's './' function to implement division of vetors entry |
| 344 | % by entry. To learn more about this function enter 'help ./' in the Matlab |
| 345 | % command window |
| 346 | |
| 347 | % Compute the channel amplitude |
| 348 | channel_amplitude = mag_RxData./mag_TxData; |
| 349 | %-------------------------------------------------------------------------% |
| 350 | |
| 351 | %-------------------------------------------------------------------------% |
| 352 | % USER CODE HERE |
| 353 | % Compute the channel phase per sample. Store the result in a variable |
| 354 | % named 'channel_phase' |
| 355 | % Hint: |
| 356 | % Channel Phase = Phase of received samples - Phase of transmitted samples |
| 357 | |
| 358 | % Compute the channel phase |
| 359 | channel_phase = phase_RxData_unw - phase_TxData_unw; |
| 360 | %-------------------------------------------------------------------------% |
| 361 | |
| 362 | % Plot channel amplitude and phase |
| 363 | figure |
| 364 | subplot(2,1,1) |
| 365 | plot(channel_amplitude) |
| 366 | title('Channel Amplitude per sample'); |
| 367 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 368 | subplot(2,1,2) |
| 369 | plot(channel_phase) |
| 370 | title('Channel Phase per sample'); |
| 371 | xlabel('n (samples)'); ylabel('Degrees'); |
| 372 | |
| 373 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 374 | % Code to avoid conflict between users, only needed for the workshop |
| 375 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 376 | pnet('closeall'); |
| 377 | !del c:\boards_lock.txt |
| 378 | catch, |
| 379 | % Close sockets |
| 380 | pnet('closeall'); |
| 381 | !del c:\boards_lock.txt |
| 382 | lasterr |
| 383 | end |
Note: See TracBrowser
for help on using the browser.