root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_Comm_WorkshopExercise_Solution.m
| Revision 799, 20.1 kB (checked in by murphpo, 11 months ago) |
|---|
| Line | |
|---|---|
| 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | % Using Warplab to Transmit Bits Over a Wireless Channel |
| 3 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4 | % In this lab exercise you will write a matlab script that generates a |
| 5 | % bitstream, modulates the bitstream using DQPSK, transmits the modulated |
| 6 | % symbols over a wireless channel using Warplab, and demodulates the |
| 7 | % received signal to obtain the transmitted bits. Bit error rate (BER) is |
| 8 | % computed by comparing the transmitted bitstream with the bitstream |
| 9 | % recovered at the receiver |
| 10 | |
| 11 | % The specific steps implemented in this script are the following: |
| 12 | |
| 13 | % 0. Initialization, define paramters, create pulse shaping filter, and |
| 14 | % create reference matrix for detection of preamble |
| 15 | % 1. Generate a random bit stream and map it to symbols |
| 16 | % 2. Modulate the symbols (map symbols to constellation points) and append |
| 17 | % preamble symbols |
| 18 | % 3. Upsample the modulated symbols with the appended preamble and filter |
| 19 | % using a pulse shaping filter |
| 20 | % 4. Transmit the signal over a wireless channel using Warplab |
| 21 | % 5. Filter the received signal with a Matched Filter (matched to the pulse |
| 22 | % shaping filter), detect preamble, and downsample output of Matched Filter |
| 23 | % 6. Demodulate and recover the transmitted bitstream |
| 24 | % 7. Compute the Bit Error Rate (BER) |
| 25 | |
| 26 | % You will write a matlab script that implements the seven steps above. |
| 27 | % Part of the code is provided, some part of the code you will write. Read |
| 28 | % the code below and fill in with your code wherever you are asked to do |
| 29 | % so. |
| 30 | |
| 31 | % Part of this code was adapted from Matlab's commdoc_mod and commdoc_rrc |
| 32 | % examples. |
| 33 | |
| 34 | % NOTE : To avoid conflict with other groups using the boards, please |
| 35 | % test the code you write in this script in any of the following three |
| 36 | % ways: |
| 37 | % |
| 38 | % Option 1. Run this script from matlab's Command Window by entering the |
| 39 | % name of the script (enter warplab_example_Comm_WorkshopExercise |
| 40 | % in matlab's Command Window). |
| 41 | % Option 2. In the menu bar go to Debug and select Run. If there |
| 42 | % are errors in the code, error messages will appear in the Command Window. |
| 43 | % Option 3. Press F5. If the are errors in the code, error messages will |
| 44 | % appear in the Command Window. |
| 45 | % |
| 46 | % DO NOT USE the Evaluate selection option and DO NOT run the script by |
| 47 | % sections. To test any change, always run the whole script by following |
| 48 | % any of the three options above. |
| 49 | |
| 50 | try, |
| 51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 52 | % Code to avoid conflict between users, only needed for the workshop, go to |
| 53 | % step 0 below to start the initialization and definition of parameters |
| 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 55 | fid = fopen('c:\boards_lock.txt'); |
| 56 | |
| 57 | if(fid > -1) |
| 58 | fclose('all'); |
| 59 | errordlg('Boards already in use - Please try again!'); |
| 60 | return; |
| 61 | end |
| 62 | |
| 63 | !echo > c:\boards_lock.txt |
| 64 | |
| 65 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 | % 0. Initialization, define paramters, create pulse shaping filter, and |
| 67 | % create reference matrix for detection of preamble |
| 68 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 69 | % Define basic parameters |
| 70 | M = 4; % Size of signal constellation |
| 71 | k = log2(M); % Number of bits per symbol |
| 72 | nsamp = 8; % Oversampling rate or Number of samples per symbol |
| 73 | |
| 74 | % Define parameters related to the pulse shaping filter and create the |
| 75 | % pulse shaping filter |
| 76 | % This pulse shaping filter is a Squared Root Raised Cosine (SRRC) filter |
| 77 | filtorder = 64; % Filter order |
| 78 | delay = filtorder/(nsamp*2); % Group delay (# of input samples). Group |
| 79 | % delay is the time between the input to the filter and the filter's peak |
| 80 | % response counted in number of input samples. In number of output samples |
| 81 | % the delay would be equal to 'delay*nsam'. |
| 82 | rolloff = 0.3; % Rolloff factor of filter |
| 83 | rrcfilter = rcosine(1,nsamp,'fir/sqrt',rolloff,delay); % Create SRRC filter |
| 84 | |
| 85 | % Plot the filter's impulse response in a stem plot |
| 86 | figure; % Create new figure window. |
| 87 | stem(rrcfilter); |
| 88 | title('Raised Cosine Impulse Response'); |
| 89 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 90 | |
| 91 | % Define number of symbols to process, number of bits to process, and the |
| 92 | % preamble. |
| 93 | % The Warplab transmit buffer can store a maximum of 2^14 samples, the |
| 94 | % number of samples per symbol is equal 'nsam', and the SRRC filter delay |
| 95 | % in number of samples is equal to 'delay*nsam'. Consequently, the total |
| 96 | % number of symbols to be transmitted must be less than 2^14/nsam-2*delay. |
| 97 | nsym = floor(2^14/nsamp-2*delay); % Number or symbols to transmit |
| 98 | preamble = [-1;-1;-1;1;-1;0;0;0;0;0;0;0;0]; % Preamble is a Barker sequence |
| 99 | % modulated with BPSK |
| 100 | nsym_preamble = length(preamble); % number of symbols in preamble |
| 101 | nsym_payload = nsym-nsym_preamble; |
| 102 | nbits = floor(nsym_payload*k); % Number of bits to process |
| 103 | |
| 104 | % Create a reference matrix used for detection of the preamble in the |
| 105 | % received signal. We will correlate the received signal with the reference |
| 106 | % matrix |
| 107 | preamble_upsamp = upsample(preamble,nsamp); % Upsample preamble |
| 108 | length_preamble_upsamp = length(preamble_upsamp); |
| 109 | corr_window = 150; % We expect to find the preamble within the first |
| 110 | % 150 received samples |
| 111 | reference_samples = zeros(corr_window,1); % Create reference vector. |
| 112 | reference_samples(1:length_preamble_upsamp) = preamble_upsamp; |
| 113 | % First samples of reference vector correspond to the |
| 114 | % preamble upsampled |
| 115 | reference_matrix = toeplitz(reference_samples,... |
| 116 | circshift(reference_samples(corr_window:-1:1),1)); |
| 117 | % Create reference matrix. The first column of the reference |
| 118 | % matrix is equal to the reference_samples vector. The i-th column |
| 119 | % of the reference matrix is equal to circular shift of the |
| 120 | % reference samples vector, it is a shift down by i samples. |
| 121 | |
| 122 | |
| 123 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 124 | % 1. Generate a random bit stream and map it to symbols |
| 125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 126 | %-------------------------------------------------------------------------% |
| 127 | % USER CODE HERE |
| 128 | |
| 129 | % Create a random binary data stream as a column vector. The number of |
| 130 | % elements is equal to 'nbits'. You can use Matlab's 'randint' function. |
| 131 | % Store the vector in a variable named 'x' |
| 132 | |
| 133 | x = randint(nbits,1); |
| 134 | |
| 135 | %-------------------------------------------------------------------------% |
| 136 | |
| 137 | % Map bits in vector x into k-bit symbols |
| 138 | xsym = bi2de(reshape(x,k,length(x)/k).','left-msb'); |
| 139 | |
| 140 | % Stem plot of bits and symbols |
| 141 | % Plot first 40 bits in a stem plot. |
| 142 | figure; |
| 143 | subplot(2,1,1) |
| 144 | stem(x(1:40),'filled'); |
| 145 | title('Random Bits'); |
| 146 | xlabel('Bit Index'); ylabel('Binary Value'); |
| 147 | % Plot first 40/k symbols in a stem plot. |
| 148 | subplot(2,1,2) |
| 149 | stem(xsym(1:40/k),'filled'); |
| 150 | title('Random Bits Mapped to Symbols'); |
| 151 | xlabel('Symbol Index'); ylabel('Integer Value'); |
| 152 | |
| 153 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 154 | % 2. Modulate the symbols (map symbols to constellation points) and append |
| 155 | % preamble symbols |
| 156 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 157 | %-------------------------------------------------------------------------% |
| 158 | % USER CODE HERE |
| 159 | |
| 160 | % Modulate the symbols in vector 'xsym' using DQPSK. You can use Matlab's |
| 161 | % 'dpskmod' function. The alphabet or constellation size 'M' was set in |
| 162 | % step 0 above as 'M=4'. Store the modulated symbols in a variable named |
| 163 | % 'ytx_mod'. |
| 164 | |
| 165 | ytx_mod = dpskmod(xsym,M); |
| 166 | |
| 167 | %-------------------------------------------------------------------------% |
| 168 | |
| 169 | % Append preamble |
| 170 | ytx_mod = [preamble;ytx_mod]; |
| 171 | |
| 172 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 173 | % 3. Upsample the modulated symbols with the appended preamble and filter |
| 174 | % using a pulse shaping filter |
| 175 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 176 | % Upsample and apply square root raised cosine filter. |
| 177 | ytx_mod_filt = rcosflt(ytx_mod,1,nsamp,'filter',rrcfilter); |
| 178 | |
| 179 | % Stem Plot of modulated symbols before and after Squared Root Raised |
| 180 | % Cosine (SRRC) filter |
| 181 | % Plots first 30 symbols. |
| 182 | % Plots I and Q in different windows |
| 183 | figure; % Create new figure window. |
| 184 | subplot(2,1,1) |
| 185 | stem([1:nsamp:nsamp*30],real(ytx_mod(1:30))); |
| 186 | hold |
| 187 | stem(real(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r'); |
| 188 | title('I Signal'); |
| 189 | xlabel('n (sample)'); ylabel('Amplitude'); |
| 190 | legend('Before SRRC Filter','After SRRC Filter'); |
| 191 | subplot(2,1,2) |
| 192 | stem([1:nsamp:nsamp*30],imag(ytx_mod(1:30))); |
| 193 | hold |
| 194 | stem(imag(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r'); |
| 195 | title('Q Signal'); |
| 196 | xlabel('n (sample)'); ylabel('Amplitude'); |
| 197 | |
| 198 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 199 | % 4. Transmit the signal over a wireless channel using Warplab |
| 200 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 201 | % Follow the steps for transmission and reception of data using Warplab. |
| 202 | |
| 203 | % In this exercise the vector to transmit is the 'ytx_mod_filt' vector. The |
| 204 | % capture offset is zero. |
| 205 | |
| 206 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 207 | % 4.0. Initializaton and definition of parameters |
| 208 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 209 | %Load some global definitions (packet types, etc.) |
| 210 | warplab_siso_defines |
| 211 | |
| 212 | % Create Socket handles and intialize nodes |
| 213 | [socketHandles, packetNum] = warplab_initialize; |
| 214 | |
| 215 | %Separate the socket handles for easier access |
| 216 | % The first socket handle is always the magic SYNC |
| 217 | % The rest can be arranged in any combination of Tx and Rx |
| 218 | udp_Sync = socketHandles(1); |
| 219 | udp_Tx = socketHandles(2); |
| 220 | udp_RxA = socketHandles(3); |
| 221 | |
| 222 | %-------------------------------------------------------------------------% |
| 223 | % USER CODE HERE |
| 224 | |
| 225 | % Create the following variables and assign them valid values: |
| 226 | % CaptOffset: Value of the Capture offset. For this exercise set CaptOffset |
| 227 | % equal to zero |
| 228 | % TxLength: Number of samples to transmit. For this exercise the vector to |
| 229 | % transmit is the 'ytx_mod_filt' vector. Set TxLength equal to |
| 230 | % the length of the 'ytx_mod_filt' vector. You can use Matlab's |
| 231 | % 'length' function |
| 232 | % TxGainBB: Transmitter BaseBand gain. In [0:3] |
| 233 | % TxGainRF: Transmitter RF gain. In [0:63] |
| 234 | % RxGainBB: Receiver Baseband gain. In[0:31] |
| 235 | % RxGainRF: Receiver RF gain. In [1:3] |
| 236 | % CarrierChannel: Channel in the 2.4 GHz band. In [1:14] |
| 237 | |
| 238 | % Note: Set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the same values |
| 239 | % you used in the warplab_siso_GUI. |
| 240 | |
| 241 | % Define the warplab options (parameters) |
| 242 | CaptOffset = 0; %Number of noise samples per Rx capture; in [0:2^14] |
| 243 | TxLength = length(ytx_mod_filt); %Length of transmission; in [0:2^14-CaptOffset] |
| 244 | TxGainBB = 3; %Tx Baseband Gain in [0:3] |
| 245 | TxGainRF = 40; %Tx RF Gain in [0:63] |
| 246 | RxGainBB = 13; %Rx Baseband Gain in [0:31] |
| 247 | RxGainRF = 1; %Rx RF Gain in [1:3] |
| 248 | CarrierChannel = 11; |
| 249 | %-------------------------------------------------------------------------% |
| 250 | |
| 251 | % Define the options vector; the order of options is set by the FPGA's code |
| 252 | % (C code) |
| 253 | optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel]; |
| 254 | % Send options vector to the nodes |
| 255 | warplab_setOptions(socketHandles,optionsVector); |
| 256 | |
| 257 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 258 | % 4.1. Generate a vector of samples to transmit and send the samples to the |
| 259 | % Warp board (Sample Frequency is 40MHz) |
| 260 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | % Prepare some data to be transmitted |
| 262 | |
| 263 | % Scale signal to transmit so that it spans [-1,1] range. We do this to |
| 264 | % use the full range of the DAC at the tranmitter |
| 265 | scale = 1 / max( [ max(real(ytx_mod_filt)) , max(imag(ytx_mod_filt)) ] ); |
| 266 | ytx_mod_filt = scale*ytx_mod_filt; |
| 267 | |
| 268 | %-------------------------------------------------------------------------% |
| 269 | % USER CODE HERE |
| 270 | |
| 271 | % Download the 'ytx_mod_filt' vector to the Warp board using the |
| 272 | % 'warplab_writeSMWO' function. This function was used in the two |
| 273 | % previous exercises. |
| 274 | |
| 275 | % Hint 1: The second argument of the 'warplab_writeSMWO' function is the |
| 276 | % vector to be downloaded and it must be a row vector. Notice that |
| 277 | % 'ytx_mod_filt' is a column vector. To make 'ytx_mod_filt' a row vector |
| 278 | % simply take transpose (only transpose, NOT conjugate transpose). The |
| 279 | % transpose can be obtained using Matlab's 'transpose' function |
| 280 | |
| 281 | % Hint 2: The third argument of 'warplab_writeSMWO' is RADIO2_TXDATA, |
| 282 | % RADIO2_TXDATA is defined in 'warplab_siso_defines'. RADIO2_TXDATA can be |
| 283 | % understood as an id that identifies the transmitter buffer. |
| 284 | |
| 285 | TxData = ytx_mod_filt.'; % Create a signal to transmit. Signal must be a |
| 286 | % row vector |
| 287 | |
| 288 | % Download the samples to be transmitted |
| 289 | warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA); |
| 290 | %-------------------------------------------------------------------------% |
| 291 | |
| 292 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 293 | % 4.2. Prepare boards for transmission and reception and send trigger to |
| 294 | % start transmission and reception (trigger is the SYNC packet) |
| 295 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 296 | % Enable transmission |
| 297 | warplab_enableTx(udp_Tx); |
| 298 | |
| 299 | % Enable reception |
| 300 | warplab_enableRx(udp_RxA); |
| 301 | |
| 302 | % Send the SYNC packet |
| 303 | warplab_sendSync(udp_Sync); |
| 304 | |
| 305 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 306 | % 4.3. Read the received smaples from the Warp board |
| 307 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 308 | %-------------------------------------------------------------------------% |
| 309 | % USER CODE HERE |
| 310 | |
| 311 | % Read the received samples from the Warp board using the |
| 312 | % 'warplab_readSMRO' function. This function was used in the two |
| 313 | % previous exercises. Store the samples in a variable named 'RawRxData' |
| 314 | |
| 315 | % Hint 1: For this exercise, the third argument of the 'warplab_readSMRO' |
| 316 | % function is equal to 'TxLength'(in this exercise CaptOffset is equal to |
| 317 | % zero) |
| 318 | |
| 319 | % Hint 2: The second argument of 'warplab_readSMRO' is RADIO2_RXDATA, |
| 320 | % RADIO2_RXDATA is defined in 'warplab_siso_defines'. RADIO2_RXDATA can be |
| 321 | % understood as an id that identifies the receiver buffer. |
| 322 | |
| 323 | % Read back the received samples |
| 324 | [RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength); |
| 325 | |
| 326 | %-------------------------------------------------------------------------% |
| 327 | |
| 328 | % Process the received samples to obtain meaningful data |
| 329 | [RxData,RxOTR] = warplab_processRawRxData(RawRxData); |
| 330 | |
| 331 | |
| 332 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 333 | % 4.4. Reset and disable the boards |
| 334 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 335 | % Reset the receiver |
| 336 | warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum); |
| 337 | |
| 338 | % Disable the receiver |
| 339 | warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum); |
| 340 | |
| 341 | % Disable the transmitter |
| 342 | warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum); |
| 343 | |
| 344 | % Close sockets |
| 345 | pnet('closeall'); |
| 346 | |
| 347 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 348 | % 4.5. Plot the transmitted and received data |
| 349 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 350 | figure; |
| 351 | subplot(2,2,1); |
| 352 | plot(real(ytx_mod_filt)); |
| 353 | title('Tx I'); |
| 354 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 355 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 356 | subplot(2,2,2); |
| 357 | plot(imag(ytx_mod_filt)); |
| 358 | title('Tx Q'); |
| 359 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 360 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 361 | subplot(2,2,3); |
| 362 | plot(real(RxData)); |
| 363 | title('Rx I'); |
| 364 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 365 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 366 | subplot(2,2,4); |
| 367 | plot(imag(RxData)); |
| 368 | title('Rx Q'); |
| 369 | xlabel('n (samples)'); ylabel('Amplitude'); |
| 370 | axis([0 2^14 -1 1]); % Set axis ranges. |
| 371 | |
| 372 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 373 | % 5. Filter the received signal with a Matched Filter (matched to the pulse |
| 374 | % shaping filter), detect preamble, and downsample output of Matched Filter |
| 375 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 376 | % Store received samples as a column vector |
| 377 | yrx_bb = RxData.'; |
| 378 | |
| 379 | % Matched filter: Filter received signal using the SRRC filter |
| 380 | yrx_bb_mf = rcosflt(yrx_bb,1,nsamp,'Fs/filter',rrcfilter); |
| 381 | |
| 382 | % Correlate with the reference matrix to find preamble sequence |
| 383 | correlation = abs( (yrx_bb_mf(1:corr_window).') * reference_matrix ); |
| 384 | preamble_start = find(correlation == max(correlation)); % Start of preamble |
| 385 | first_sample_index = preamble_start+length_preamble_upsamp; % Start of |
| 386 | % first symbol after preamble |
| 387 | |
| 388 | % Downsample output of Matched Filter |
| 389 | yrx_bb_mf_ds = yrx_bb_mf(first_sample_index:end); |
| 390 | yrx_bb_mf_ds = downsample(yrx_bb_mf_ds,nsamp); |
| 391 | % Account for delay of filter |
| 392 | yrx_bb_mf_ds = yrx_bb_mf_ds(1:end-2*delay); % Twice delay because signal |
| 393 | % goes through 2 filtering stages (one at the Tx and the other one at the Rx) |
| 394 | |
| 395 | % Stem Plot of signal before Matched Filter, after Matched Filter, and |
| 396 | % after downsampling |
| 397 | % Plots first 30 symbols. |
| 398 | % Plots real and imaginary parts in different windows |
| 399 | figure; % Create new figure window. |
| 400 | subplot(2,1,1) |
| 401 | stem(real(yrx_bb(1+2*delay*nsamp+first_sample_index:1+2*delay*nsamp+... |
| 402 | first_sample_index+30*nsamp)),'b'); |
| 403 | hold |
| 404 | stem(real(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r'); |
| 405 | stem([1:nsamp:nsamp*30],real(yrx_bb_mf_ds(1:30)),'k'); |
| 406 | title('I Symbols'); |
| 407 | xlabel('n (sample)'); ylabel('Amplitude'); |
| 408 | legend('Before Matched Filter','After Matched Filter','After Downsample'); |
| 409 | subplot(2,1,2) |
| 410 | stem(imag(yrx_bb(first_sample_index:first_sample_index+30*nsamp)),'b'); |
| 411 | hold |
| 412 | stem(imag(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r'); |
| 413 | stem([1:nsamp:nsamp*30],imag(yrx_bb_mf_ds(1:30)),'k'); |
| 414 | title('Q Symbols'); |
| 415 | xlabel('n (sample)'); ylabel('Amplitude'); |
| 416 | |
| 417 | % Scatter Plot of received and transmitted constellation points |
| 418 | h = scatterplot(yrx_bb_mf_ds(nsym_preamble+1:end),1,0,'g.'); |
| 419 | hold on; |
| 420 | scatterplot(ytx_mod(nsym_preamble+1:end),1,0,'k*',h); |
| 421 | title('Constellations'); |
| 422 | legend('Received','Transmitted'); |
| 423 | axis([-2 2 -2 2]); % Set axis ranges. |
| 424 | hold off; |
| 425 | |
| 426 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 427 | % 6. Demodulate and recover the transmitted bitstream |
| 428 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 429 | %-------------------------------------------------------------------------% |
| 430 | % USER CODE HERE |
| 431 | |
| 432 | % Demodulate the 'yrx_bb_mf_ds' vector. Remember modulation is DQPSK. |
| 433 | % You can use Matlab's 'dpskdemod' function. The alphabet or constellation |
| 434 | % size 'M' was set in step 0 above as 'M=4'. Store the modulated symbols |
| 435 | % in a variable named 'zsym'. |
| 436 | zsym = dpskdemod(yrx_bb_mf_ds,M); |
| 437 | |
| 438 | %-------------------------------------------------------------------------% |
| 439 | |
| 440 | % Map Symbols to Bits |
| 441 | z = de2bi(zsym,'left-msb'); % Convert integers to bits. |
| 442 | % Convert z from a matrix to a vector. |
| 443 | z = reshape(z.',prod(size(z)),1); |
| 444 | |
| 445 | % Plot first 80 transmitted bits and first 80 received bits in a stem plot |
| 446 | figure; |
| 447 | subplot(2,1,1) |
| 448 | stem(x(1:80),'filled'); |
| 449 | title('Transmitted Bits'); |
| 450 | xlabel('Bit Index'); ylabel('Binary Value'); |
| 451 | subplot(2,1,2) |
| 452 | stem(z(1:80),'filled'); |
| 453 | title('Received Bits'); |
| 454 | xlabel('Bit Index'); ylabel('Binary Value'); |
| 455 | |
| 456 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 457 | % 7. Compute the Bit Error Rate (BER) |
| 458 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 459 | % Compare x and z to obtain the number of errors and the bit error rate |
| 460 | [number_of_errors,bit_error_rate] = biterr(x(3:length(z)),z(3:length(z))) |
| 461 | % We start comparing at three because the first two bits are the are always |
| 462 | % lost in DQPSK. We compare until length(z) because z may be shorter than |
| 463 | % x due to the fact that some bits (approx 1 to 5) may be lost fue to the |
| 464 | % jitter of the synch pulse. |
| 465 | |
| 466 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 467 | % Code to avoid conflict between users, only needed for the workshop |
| 468 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 469 | pnet('closeall'); |
| 470 | !del c:\boards_lock.txt |
| 471 | catch, |
| 472 | % Close sockets |
| 473 | pnet('closeall'); |
| 474 | !del c:\boards_lock.txt |
| 475 | lasterr |
| 476 | end |
Note: See TracBrowser
for help on using the browser.