root/ResearchApps/PHY/WARPLAB/WARPLab_SISO_MIMO2x2/M_Code/warplab_siso_example_ChannelEstim.m
| Revision 839, 9.3 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: 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 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 23 | % 0. Transmit a narrowband signal using warplab |
| 24 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 25 | % Follow the steps for transmission and reception of data using Warplab. |
| 26 | % These are the steps in the matlab script warplab_example_TxRx.m |
| 27 | |
| 28 | % This example transmits the following narrowband signal |
| 29 | % t = 0:(1/40e6):TxLength/40e6 - 1/40e6; |
| 30 | % TxData = exp(t*j*2*pi*1e6); |
| 31 | |
| 32 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 33 | % 0.0. Initializaton and definition of parameters |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | %Load some global definitions (packet types, etc.) |
| 36 | warplab_defines |
| 37 | |
| 38 | % Create Socket handles and intialize nodes |
| 39 | [socketHandles, packetNum] = warplab_initialize; |
| 40 | |
| 41 | %Separate the socket handles for easier access |
| 42 | % The first socket handle is always the magic SYNC |
| 43 | % The rest can be arranged in any combination of Tx and Rx |
| 44 | udp_Sync = socketHandles(1); |
| 45 | udp_Tx = socketHandles(2); |
| 46 | udp_RxA = socketHandles(3); |
| 47 | |
| 48 | % Define the warplab options (parameters) |
| 49 | CaptOffset = 1000; %Number of noise samples per Rx capture. In [0:2^14] |
| 50 | TxLength = 2^14-1000; %Length of transmission. In [0:2^14-CaptOffset] |
| 51 | TransMode = 0; %Transmission mode. In [0:1] |
| 52 | % 0: Single Transmission |
| 53 | % 1: Continuous Transmission. Tx board will continue |
| 54 | % transmitting the vector of samples until the user manually |
| 55 | % disables the transmitter. |
| 56 | CarrierChannel = 8; % Channel in the 2.4 GHz band. In [1:14] |
| 57 | TxGainBB = 3; %Tx Baseband Gain. In [0:3] |
| 58 | TxGainRF = 40; %Tx RF Gain. In [0:63] |
| 59 | RxGainBB = 15; %Rx Baseband Gain. In [0:31] |
| 60 | RxGainRF = 1; %Rx RF Gain. In [1:3] |
| 61 | |
| 62 | % Define the options vector; the order of options is set by the FPGA's code |
| 63 | % (C code) |
| 64 | optionsVector = [CaptOffset TxLength-1 TransMode CarrierChannel (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16)]; |
| 65 | % Send options vector to the nodes |
| 66 | warplab_setOptions(socketHandles,optionsVector); |
| 67 | |
| 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 69 | % 0.1. Generate a vector of samples to transmit and send the samples to the |
| 70 | % Warp board (Sample Frequency is 40MHz) |
| 71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | % Prepare some data to be transmitted |
| 73 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; %Create time vector. |
| 74 | TxData = exp(t*j*2*pi*1e6); % Create a signal to transmit. Signal must be a |
| 75 | % row vector. The signal can be real or complex, the only constraint is |
| 76 | % that the amplitude of the real part must be in [-1:1] and the amplitude |
| 77 | % of the imaginary part must be in [-1:1] |
| 78 | |
| 79 | % Download the samples to be transmitted |
| 80 | warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA); |
| 81 | |
| 82 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 83 | % 0.2. Prepare boards for transmission and reception and send trigger to |
| 84 | % start transmission and reception (trigger is the SYNC packet) |
| 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 | % Enable transmitter radio path in transmitter node |
| 87 | warplab_sendCmd(udp_Tx, RADIO2_TXEN, packetNum); |
| 88 | |
| 89 | % Enable receiver radio path in receiver node |
| 90 | warplab_sendCmd(udp_RxA, RADIO2_RXEN, packetNum); |
| 91 | |
| 92 | % Prime transmitter state machine in transmitter node. Transmitter will be |
| 93 | % waiting for the SYNC packet. Transmission will be triggered when the |
| 94 | % transmitter node receives the SYNC packet. |
| 95 | warplab_sendCmd(udp_Tx, TX_START, packetNum); |
| 96 | |
| 97 | % Prime receiver state machine in receiver node. Receiver will be waiting |
| 98 | % for the SYNC packet. Capture will be triggered when the receiver |
| 99 | % node receives the SYNC packet. |
| 100 | warplab_sendCmd(udp_RxA, RX_START, packetNum); |
| 101 | |
| 102 | % Send the SYNC packet |
| 103 | warplab_sendSync(udp_Sync); |
| 104 | |
| 105 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 106 | % 0.3. Read the received smaples from the Warp board |
| 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 108 | % Read back the received samples |
| 109 | [RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset); |
| 110 | % Process the received samples to obtain meaningful data |
| 111 | [RxData,RxOTR] = warplab_processRawRxData(RawRxData); |
| 112 | % Read stored RSSI data |
| 113 | [RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8); |
| 114 | % Procecss Raw RSSI data to obtain meningful RSSI values |
| 115 | [RxRSSI] = warplab_processRawRSSIData(RawRSSIData); |
| 116 | |
| 117 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 | % 0.4. Reset and disable the boards |
| 119 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 120 | % Reset the receiver |
| 121 | warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum); |
| 122 | |
| 123 | % Disable the receiver radio |
| 124 | warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum); |
| 125 | |
| 126 | % Disable the transmitter radio |
| 127 | warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum); |
| 128 | |
| 129 | % Close sockets |
| 130 | pnet('closeall'); |
| 131 | |
| 132 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 133 | % 0.5. Plot the transmitted and received data |
| 134 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 135 | figure; |
| 136 | subplot(2,2,1); |
| 137 | plot(real(TxData)); |
| 138 | title('Tx I'); |
| 139 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 140 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 141 | subplot(2,2,2); |
| 142 | plot(imag(TxData)); |
| 143 | title('Tx Q'); |
| 144 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 145 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 146 | subplot(2,2,3); |
| 147 | plot(real(RxData)); |
| 148 | title('Rx I'); |
| 149 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 150 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 151 | subplot(2,2,4); |
| 152 | plot(imag(RxData)); |
| 153 | title('Rx Q'); |
| 154 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 155 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 156 | |
| 157 | |
| 158 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 159 | % 1. Remove from the received vector the samples that do not correspond to |
| 160 | % transmitted data. In other words, remove from the received vector samples |
| 161 | % 1 to CaptOffset. This step will remove samples that correspond to measured |
| 162 | % noise and make the RxData vector the same length as the TxData vector |
| 163 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 164 | RxData = RxData(CaptOffset+1:end); |
| 165 | |
| 166 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 167 | % 2. Compute the amplitude and the phase of the transmitted and received |
| 168 | % sammples |
| 169 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 170 | % Compute the magnitude per sample of the transmitted and received |
| 171 | % data |
| 172 | mag_TxData = abs(TxData); |
| 173 | mag_RxData = abs(RxData); |
| 174 | |
| 175 | % Compute the phase per sample of the transmitted and received data |
| 176 | phase_TxData = angle(TxData); |
| 177 | phase_TxData_unw = unwrap(phase_TxData); |
| 178 | phase_TxData = phase_TxData *180/pi; %Convert to degrees |
| 179 | phase_TxData_unw = phase_TxData_unw *180/pi; %Convert to degrees |
| 180 | phase_RxData = angle(RxData); |
| 181 | phase_RxData_unw = unwrap(phase_RxData); |
| 182 | phase_RxData = phase_RxData *180/pi; %Convert to degrees |
| 183 | phase_RxData_unw = phase_RxData_unw *180/pi; %Convert to degrees |
| 184 | |
| 185 | % Plot magnitude and phase of transmitted and received samples |
| 186 | figure; |
| 187 | subplot(2,3,1); |
| 188 | plot(mag_TxData); |
| 189 | title('Tx Magnitude'); |
| 190 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 191 | subplot(2,3,2); |
| 192 | plot(phase_TxData); |
| 193 | title('Tx Phase'); |
| 194 | xlabel('n (samples)'); ylabel('Degrees'); |
| 195 | subplot(2,3,3); |
| 196 | plot(phase_TxData_unw); |
| 197 | title('Tx Phase unwrapped'); |
| 198 | xlabel('n (samples)'); ylabel('Degrees'); |
| 199 | subplot(2,3,4); |
| 200 | plot(mag_RxData); |
| 201 | title('Rx Magnitude'); |
| 202 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 203 | subplot(2,3,5); |
| 204 | plot(phase_RxData); |
| 205 | title('Rx Phase'); |
| 206 | xlabel('n (samples)'); ylabel('Degrees'); |
| 207 | subplot(2,3,6); |
| 208 | plot(phase_RxData_unw); |
| 209 | title('Rx Phase unwrapped'); |
| 210 | xlabel('n (samples)'); ylabel('Degrees'); |
| 211 | |
| 212 | |
| 213 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 214 | % 3. Compute the channel amplitude and channel phase per sample |
| 215 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 216 | % Compute the channel amplitude |
| 217 | channel_amplitude = mag_RxData./mag_TxData; |
| 218 | |
| 219 | % Compute the channel phase |
| 220 | channel_phase = phase_RxData_unw - phase_TxData_unw; |
| 221 | |
| 222 | % Plot channel amplitude and phase |
| 223 | figure |
| 224 | subplot(2,1,1) |
| 225 | plot(channel_amplitude) |
| 226 | title('Channel Amplitude per sample'); |
| 227 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 228 | subplot(2,1,2) |
| 229 | plot(channel_phase) |
| 230 | title('Channel Phase per sample'); |
| 231 | xlabel('n (samples)'); ylabel('Degrees'); |
Note: See TracBrowser
for help on using the browser.