root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_TxRxBidirectional.m
| Revision 794, 8.4 kB (checked in by MelissaDuarte, 9 months ago) |
|---|
| Line | |
|---|---|
| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | % Transmitting and Receiving Data using Warplab to establish a |
| 3 | % bidirectional link |
| 4 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 5 | % This MATLAB script uses Warplab to establish a bidirectional link |
| 6 | % between two nodes. First node A will transmit to node B and then node B |
| 7 | % will transmit to node A. |
| 8 | |
| 9 | % The specific steps implemented in this script are the following |
| 10 | |
| 11 | % 0. Initializaton and definition of parameters |
| 12 | % 1. Generate the vector of samples that node A will transmit to node B and |
| 13 | % the vector of samples that node B will transmit to node A, then download |
| 14 | % the samples to the Warp boards (Sample Frequency is 40MHz) |
| 15 | % 2. Prepare boards for transmission and reception from node A to node B |
| 16 | % and send trigger to start transmission and reception (trigger is the SYNC |
| 17 | % packet) |
| 18 | % 3. Disable the radios |
| 19 | % 4. Prepare boards for transmission and reception from node B to node A |
| 20 | % and send trigger to start transmission and reception (trigger is the SYNC |
| 21 | % packet) |
| 22 | % 5. Disable the radios |
| 23 | % 6. Read the received samples from the Warp boards |
| 24 | % 7. Reset the boards and close sockets |
| 25 | % 8. Plot the transmitted and received data |
| 26 | |
| 27 | |
| 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 29 | % 0. Initializaton and definition of parameters |
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 31 | %Load some global definitions (packet types, etc.) |
| 32 | warplab_siso_defines |
| 33 | |
| 34 | % Create Socket handles and intialize nodes |
| 35 | [socketHandles, packetNum] = warplab_initialize; |
| 36 | |
| 37 | %Separate the socket handles for easier access |
| 38 | % The first socket handle is always the magic SYNC |
| 39 | % The rest can be arranged in any combination of Tx and Rx |
| 40 | udp_Sync = socketHandles(1); |
| 41 | udp_nodeA = socketHandles(2); |
| 42 | udp_nodeB = socketHandles(3); |
| 43 | |
| 44 | % Define the warplab options (parameters) |
| 45 | CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14] |
| 46 | TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset] |
| 47 | TxGainBB = 3; %Tx Baseband Gain in [0:3] |
| 48 | TxGainRF = 40; %Tx RF Gain in [0:63] |
| 49 | RxGainBB = 13; %Rx Baseband Gain in [0:31] |
| 50 | RxGainRF = 1; %Rx RF Gain in [1:3] |
| 51 | CarrierChannel = 11; % Channel in the 2.4 GHz band. In [1:14] |
| 52 | |
| 53 | % Define the options vector; the order of options is set by the FPGA's code |
| 54 | % (C code) |
| 55 | optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel]; |
| 56 | % Send options vector to the nodes |
| 57 | warplab_setOptions(socketHandles,optionsVector); |
| 58 | |
| 59 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 60 | % 1. Generate the vector of samples that node A will transmit to node B and |
| 61 | % the vector of samples that node B will transmit to node A, then download |
| 62 | % the samples to the Warp boards (Sample Frequency is 40MHz) |
| 63 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 64 | % Create time vector. |
| 65 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; |
| 66 | |
| 67 | % Create a signal to transmit from node A to node B |
| 68 | TxDataAB = exp(t*j*2*pi*1e6); %Signal must be a row vector. The signal can |
| 69 | % be real or complex, the only constraint is that the amplitude of the real |
| 70 | % part must be in [-1:1] and the amplitude of the imaginary part must be |
| 71 | % in [-1:1] |
| 72 | |
| 73 | % Download the samples to be transmitted |
| 74 | warplab_writeSMWO(udp_nodeA, TxDataAB, RADIO2_TXDATA); % Download samples to node A |
| 75 | |
| 76 | % Create a signal to transmit from node B to node A |
| 77 | TxDataBA = linspace(0,1,TxLength).*exp(t*j*2*pi*1e6); |
| 78 | % Signal must be a row vector. The signal can be real or complex, |
| 79 | % the only constraint is that the amplitude of the real part must be in |
| 80 | % [-1:1] and the amplitude of the imaginary part must be in [-1:1] |
| 81 | |
| 82 | warplab_writeSMWO(udp_nodeB, TxDataBA, RADIO2_TXDATA); % Download samples to node B |
| 83 | |
| 84 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 85 | % 2. Prepare boards for transmission and reception from node A to node B |
| 86 | % and send trigger to start transmission and reception (trigger is the SYNC |
| 87 | % packet) |
| 88 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 89 | % Enable transmission |
| 90 | warplab_enableTx(udp_nodeA); |
| 91 | |
| 92 | % Enable reception |
| 93 | warplab_enableRx(udp_nodeB); |
| 94 | |
| 95 | % Send the SYNC packet |
| 96 | warplab_sendSync(udp_Sync); |
| 97 | |
| 98 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 99 | % 3. Disable the radios |
| 100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 101 | % Disable the receiver |
| 102 | warplab_sendCmd(udp_nodeB, RADIO2_RXDIS, packetNum); |
| 103 | |
| 104 | % Disable the transmitter |
| 105 | warplab_sendCmd(udp_nodeA, RADIO2_TXDIS, packetNum); |
| 106 | |
| 107 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 108 | % 4. Prepare boards for transmission and reception from node B to node A |
| 109 | % and send trigger to start transmission and reception (trigger is the SYNC |
| 110 | % packet) |
| 111 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 112 | % Enable transmission |
| 113 | warplab_enableTx(udp_nodeB); |
| 114 | |
| 115 | % Enable reception |
| 116 | warplab_enableRx(udp_nodeA); |
| 117 | |
| 118 | % Send the SYNC packet |
| 119 | warplab_sendSync(udp_Sync); |
| 120 | |
| 121 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 122 | % 5. Reset and disable the boards |
| 123 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 124 | % Disable the receiver |
| 125 | warplab_sendCmd(udp_nodeA, RADIO2_RXDIS, packetNum); |
| 126 | |
| 127 | % Disable the transmitter |
| 128 | warplab_sendCmd(udp_nodeB, RADIO2_TXDIS, packetNum); |
| 129 | |
| 130 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 131 | % 6. Read the received samples from the Warp boards |
| 132 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 133 | % Read back the received samples sent from A to B |
| 134 | [RawRxDataAB] = warplab_readSMRO(udp_nodeB, RADIO2_RXDATA, TxLength+CaptOffset); |
| 135 | % Process the received samples to obtain meaningful data |
| 136 | [RxDataAB,RxOTRAB] = warplab_processRawRxData(RawRxDataAB); |
| 137 | % Read stored RSSI data corresponding to A to B transmission |
| 138 | [RawRSSIDataAB] = warplab_readSMRO(udp_nodeB, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8); |
| 139 | % Procecss Raw RSSI data to obtain meningful RSSI values |
| 140 | [RxRSSIAB] = warplab_processRawRSSIData(RawRSSIDataAB); |
| 141 | |
| 142 | % Read back the received samples sent from B to A |
| 143 | [RawRxDataBA] = warplab_readSMRO(udp_nodeA, RADIO2_RXDATA, TxLength+CaptOffset); |
| 144 | % Process the received samples to obtain meaningful data |
| 145 | [RxDataBA,RxOTRBA] = warplab_processRawRxData(RawRxDataBA); |
| 146 | % Read stored RSSI data corresponding to B to A transmission |
| 147 | [RawRSSIDataBA] = warplab_readSMRO(udp_nodeA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8); |
| 148 | % Procecss Raw RSSI data to obtain meningful RSSI values |
| 149 | [RxRSSIBA] = warplab_processRawRSSIData(RawRSSIDataBA); |
| 150 | |
| 151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 152 | % 7. Reset the boards and close sockets |
| 153 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 154 | % Reset node A |
| 155 | warplab_sendCmd(udp_nodeA, RX_DONEREADING, packetNum); |
| 156 | |
| 157 | % Reset node B |
| 158 | warplab_sendCmd(udp_nodeB, RX_DONEREADING, packetNum); |
| 159 | |
| 160 | % Close sockets |
| 161 | pnet('closeall'); |
| 162 | |
| 163 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 164 | % 5. Plot the transmitted and received data |
| 165 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 166 | % Plot data from A to B transmissio |
| 167 | figure; |
| 168 | subplot(2,2,1); |
| 169 | plot(real(TxDataAB)); |
| 170 | title('Tx I A to B'); |
| 171 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 172 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 173 | subplot(2,2,2); |
| 174 | plot(imag(TxDataAB)); |
| 175 | title('Tx Q A to B'); |
| 176 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 177 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 178 | subplot(2,2,3); |
| 179 | plot(real(RxDataAB)); |
| 180 | title('Rx I A to B'); |
| 181 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 182 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 183 | subplot(2,2,4); |
| 184 | plot(imag(RxDataAB)); |
| 185 | title('Rx Q A to B'); |
| 186 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 187 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 188 | |
| 189 | % Plot data from B to A transmission |
| 190 | figure; |
| 191 | subplot(2,2,1); |
| 192 | plot(real(TxDataBA)); |
| 193 | title('Tx I B to A'); |
| 194 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 195 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 196 | subplot(2,2,2); |
| 197 | plot(imag(TxDataBA)); |
| 198 | title('Tx Q B to A'); |
| 199 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 200 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 201 | subplot(2,2,3); |
| 202 | plot(real(RxDataBA)); |
| 203 | title('Rx I B to A'); |
| 204 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 205 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 206 | subplot(2,2,4); |
| 207 | plot(imag(RxDataBA)); |
| 208 | title('Rx Q B to A'); |
| 209 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 210 | axis([0 2^14 -1 1]); % Set axis ranges. |
Note: See TracBrowser
for help on using the browser.