root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_ChannelEstimation_WorkshopExercise.m
| Revision 799, 11.7 kB (checked in by murphpo, 9 months ago) |
|---|
| Line | |
|---|---|
| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | % Using Warplab to Estimate the Amplitude and Phase of a Narrowband Flat |
| 3 | % Fading Wireless Channel |
| 4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5 | % In this lab exercise you will write a matlab script that transmits and |
| 6 | % receives data using Warplab and computes an estimate of the amplitude and |
| 7 | % phase of the channel by comparing the transmitted and received data. |
| 8 | |
| 9 | % The specific steps to be implemented are the following: |
| 10 | |
| 11 | % 0. Transmit a narrowband signal using Warplab |
| 12 | % 1. Remove from the received vector the samples that do not correspond to |
| 13 | % transmitted data. |
| 14 | % 2. Compute the amplitude and the phase of the transmitted and received |
| 15 | % sammples |
| 16 | % 3. Compute the channel amplitude and channel phase per sample |
| 17 | |
| 18 | % NOTE 1 : The amplitude and phase computed in this exercise correspond to |
| 19 | % the amplitude and phase of the channel together with the amplitude and |
| 20 | % phase of the hardware. In other words, the effect of the radios is also |
| 21 | % part of the channel. |
| 22 | |
| 23 | % You will write a matlab script that implements the four steps above. |
| 24 | % Part of the code is provided, some part of the code you will write. Read |
| 25 | % the code below and fill in with your code wherever you are asked to do |
| 26 | % so. |
| 27 | |
| 28 | % NOTE 2 : To avoid conflict with other groups using the boards, please |
| 29 | % test the code you write in this script in any of the following three |
| 30 | % ways: |
| 31 | % |
| 32 | % Option 1. Run this script from matlab's Command Window by entering the |
| 33 | % name of the script (enter warplab_example_ChannelEstimation_WorkshopExercise |
| 34 | % in 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 | |
| 44 | try, |
| 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 46 | % Code to avoid conflict between users, only needed for the workshop, go to |
| 47 | % step 0 below to transmit a narrowband signal using Warplab |
| 48 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 49 | fid = fopen('c:\boards_lock.txt'); |
| 50 | |
| 51 | if(fid > -1) |
| 52 | fclose('all'); |
| 53 | errordlg('Boards already in use - Please try again!'); |
| 54 | return; |
| 55 | end |
| 56 | |
| 57 | !echo > c:\boards_lock.txt |
| 58 | |
| 59 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | % 0. Transmit a narrowband signal using Warplab |
| 61 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 62 | % Follow the steps for transmission and reception of data using Warplab. |
| 63 | % These are the steps implemented in the previous lab exercise, the |
| 64 | % following sections (0.0 to 0.5) guide you through the steps. |
| 65 | |
| 66 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 67 | % 0.0. Initializaton and definition of parameters |
| 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 69 | %-------------------------------------------------------------------------% |
| 70 | % USER CODE HERE |
| 71 | |
| 72 | % Follow the steps for Initializaton and definition of parameters. |
| 73 | % You can copy the code corresponding to step 0 in the previous lab |
| 74 | % exercise and then paste it here. |
| 75 | |
| 76 | % Remember to set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the |
| 77 | % same values you used in the warplab_siso_GUI. |
| 78 | |
| 79 | % For the code below to work, make sure the 'CaptOffset' variable exists |
| 80 | % in the matlab workspace. |
| 81 | |
| 82 | %-------------------------------------------------------------------------% |
| 83 | |
| 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 85 | % 0.1. Generate a vector of samples to transmit and send the samples to the |
| 86 | % Warp board (Sample Frequency is 40MHz) |
| 87 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 88 | %-------------------------------------------------------------------------% |
| 89 | % USER CODE HERE |
| 90 | |
| 91 | % Follow the steps to Generate a vector of samples to transmit and send |
| 92 | % the samples to the Warp board. |
| 93 | % You can copy the code corresponding to step 1 in the previous lab |
| 94 | % exercise and then paste it here. |
| 95 | % You can use the following two lines of code to generate the narrowband |
| 96 | % signal: |
| 97 | % t = 0:(1/40e6):TxLength/40e6 - 1/40e6; |
| 98 | % TxData = exp(t*j*2*pi*1e6); |
| 99 | |
| 100 | % For the code below to work, make sure the transmit vector is |
| 101 | % stored in a variable called 'TxData'. Make sure 'TxData' exists |
| 102 | % in the matlab workspace. |
| 103 | |
| 104 | %-------------------------------------------------------------------------% |
| 105 | |
| 106 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 107 | % 0.2. Prepare boards for transmission and reception and send trigger to |
| 108 | % start transmission and reception (trigger is the SYNC packet) |
| 109 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 110 | %-------------------------------------------------------------------------% |
| 111 | % USER CODE HERE |
| 112 | % Follow the steps to prepare boards for transmission and reception and |
| 113 | % send trigger to start transmission and reception. |
| 114 | % You can copy the code corresponding to step 2 in the previous lab |
| 115 | % exercise and then paste it here. |
| 116 | |
| 117 | %-------------------------------------------------------------------------% |
| 118 | |
| 119 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 120 | % 0.3. Read the received samples from the Warp board |
| 121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 | %-------------------------------------------------------------------------% |
| 123 | % USER CODE HERE |
| 124 | % Follow the steps to read the received samples from the Warp board. |
| 125 | % You can copy the code corresponding to step 3 in the previous lab |
| 126 | % exercise and then paste it here. There is no need to read the RSSI, but |
| 127 | % you can do so. |
| 128 | |
| 129 | % For the code below to work, make sure the received vector is |
| 130 | % stored in a variable called 'RxData'. Make sure 'RxData' exists |
| 131 | % in the matlab workspace. |
| 132 | |
| 133 | %-------------------------------------------------------------------------% |
| 134 | |
| 135 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 136 | % 0.4. Reset and disable the boards |
| 137 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 | %-------------------------------------------------------------------------% |
| 139 | % USER CODE HERE |
| 140 | % Follow the steps to reset and disable the boards. |
| 141 | % You can copy the code corresponding to step 4 in the previous lab |
| 142 | % exercise and then paste it here. |
| 143 | |
| 144 | %-------------------------------------------------------------------------% |
| 145 | |
| 146 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 147 | % 0.5. Plot the transmitted and received data |
| 148 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 149 | %-------------------------------------------------------------------------% |
| 150 | % USER CODE HERE |
| 151 | % If you are interested in looking at the received and transmitted data you |
| 152 | % can copy the code corresponding to step 5 in the previous lab exercise |
| 153 | % and paste it here |
| 154 | |
| 155 | %-------------------------------------------------------------------------% |
| 156 | |
| 157 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 158 | % 1. Remove from the received vector the samples that do not correspond to |
| 159 | % transmitted data. In other words, remove from the received vector samples |
| 160 | % 1 to CaptOffset. This step will remove samples that correspond to measured |
| 161 | % noise and make the RxData vector the same length as the TxData vector |
| 162 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 163 | RxData = RxData(CaptOffset+1:end); |
| 164 | |
| 165 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 | % 2. Compute the amplitude and the phase of the transmitted and received |
| 167 | % sammples |
| 168 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 169 | |
| 170 | %-------------------------------------------------------------------------% |
| 171 | % USER CODE HERE |
| 172 | % Compute the magnitude per sample of the transmitted and received |
| 173 | % data. Store the magnitude of the transmitted data in a variable named |
| 174 | % 'mag_TxData'. Store the magnitude of the received data in a variable |
| 175 | % named 'mag_RxData'. |
| 176 | % Hint: You can use Matlab's 'abs' function |
| 177 | |
| 178 | |
| 179 | %-------------------------------------------------------------------------% |
| 180 | |
| 181 | |
| 182 | %-------------------------------------------------------------------------% |
| 183 | % USER CODE HERE |
| 184 | % Compute the phase per sample of the transmitted and received |
| 185 | % data. Store the phase (in radians) of the transmitted data in a variable |
| 186 | % named 'phase_TxData'. Store the phase (in radians) of the received data |
| 187 | % in a variable named 'phase_RxData'. |
| 188 | % Hint: You can use Matlab's 'angle' function |
| 189 | |
| 190 | |
| 191 | %-------------------------------------------------------------------------% |
| 192 | |
| 193 | phase_TxData_unw = unwrap(phase_TxData); % Unwrap phase |
| 194 | phase_TxData = phase_TxData *180/pi; % Convert phase to degrees |
| 195 | phase_TxData_unw = phase_TxData_unw *180/pi; % Convert unwraped phase to degrees |
| 196 | |
| 197 | phase_RxData_unw = unwrap(phase_RxData); % Unwrap phase |
| 198 | phase_RxData = phase_RxData *180/pi; % Convert phase to degrees |
| 199 | phase_RxData_unw = phase_RxData_unw *180/pi; % Convert unwraped phase to degrees |
| 200 | |
| 201 | % Plot magnitude and phase of transmitted and received samples |
| 202 | figure; |
| 203 | subplot(2,3,1); |
| 204 | plot(mag_TxData); |
| 205 | title('Tx Magnitude'); |
| 206 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 207 | subplot(2,3,2); |
| 208 | plot(phase_TxData); |
| 209 | title('Tx Phase'); |
| 210 | xlabel('n (samples)'); ylabel('Degrees'); |
| 211 | subplot(2,3,3); |
| 212 | plot(phase_TxData_unw); |
| 213 | title('Tx Phase unwrapped'); |
| 214 | xlabel('n (samples)'); ylabel('Degrees'); |
| 215 | subplot(2,3,4); |
| 216 | plot(mag_RxData); |
| 217 | title('Rx Magnitude'); |
| 218 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 219 | subplot(2,3,5); |
| 220 | plot(phase_RxData); |
| 221 | title('Rx Phase'); |
| 222 | xlabel('n (samples)'); ylabel('Degrees'); |
| 223 | subplot(2,3,6); |
| 224 | plot(phase_RxData_unw); |
| 225 | title('Rx Phase unwrapped'); |
| 226 | xlabel('n (samples)'); ylabel('Degrees'); |
| 227 | |
| 228 | |
| 229 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 230 | % 3. Compute the channel amplitude and channel phase per sample |
| 231 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 232 | %-------------------------------------------------------------------------% |
| 233 | % USER CODE HERE |
| 234 | % Compute the channel amplitude per sample. Store the result in a variable |
| 235 | % named 'channel_amplitude' |
| 236 | % Hint 1: |
| 237 | % Channel amplitude = Magnitude of received samples / Magnitude of transmitted samples |
| 238 | % Hint 2: |
| 239 | % You can use Matlab's './' function to implement division of vetors entry |
| 240 | % by entry. To learn more about this function enter 'help ./' in the Matlab |
| 241 | % command window |
| 242 | |
| 243 | %-------------------------------------------------------------------------% |
| 244 | |
| 245 | %-------------------------------------------------------------------------% |
| 246 | % USER CODE HERE |
| 247 | % Compute the channel phase per sample. Store the result in a variable |
| 248 | % named 'channel_phase' |
| 249 | % Hint 1: |
| 250 | % Channel Phase = Phase of received samples - Phase of transmitted samples |
| 251 | |
| 252 | %-------------------------------------------------------------------------% |
| 253 | |
| 254 | % Plot channel amplitude and phase |
| 255 | figure |
| 256 | subplot(2,1,1) |
| 257 | plot(channel_amplitude) |
| 258 | title('Channel Amplitude per sample'); |
| 259 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 260 | subplot(2,1,2) |
| 261 | plot(channel_phase) |
| 262 | title('Channel Phase per sample'); |
| 263 | xlabel('n (samples)'); ylabel('Degrees'); |
| 264 | |
| 265 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 266 | % Code to avoid conflict between users, only needed for the workshop |
| 267 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 268 | pnet('closeall'); |
| 269 | !del c:\boards_lock.txt |
| 270 | catch, |
| 271 | % Close sockets |
| 272 | pnet('closeall'); |
| 273 | !del c:\boards_lock.txt |
| 274 | lasterr |
| 275 | end |
Note: See TracBrowser
for help on using the browser.