source: ResearchApps/PHY/WARPLAB/WARPLab_v6p5/M_Code_Examples/warplab_siso_example_Comm.m

Last change on this file was 1311, checked in by mduarte, 15 years ago

Adding files for WARPLab release 05

File size: 22.4 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Using WARPLab (SISO configuration) to Transmit Bits Over a Wireless
3% Channel .
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6% This matlab srcipt generates a bitstream, modulates the bitstream using
7% DQPSK, transmits the modulated symbols over a wireless channel using
8% WARPLab, and demodulates the received signal to obtain the
9% transmitted bits. Bit error rate (BER) is computed by comparing the
10% transmitted bitstream with the bitstream recovered at the receiver
11
12% The specific steps implemented in this script are the following:
13
14% 0. Initialization, define paramters, create pulse shaping filter, and
15% create reference matrix for detection of preamble
16% 1. Generate a random bit stream and map it to symbols
17% 2. Modulate the symbols (map symbols to constellation points) and append
18% preamble symbols
19% 3. Upsample the modulated symbols with the appended preamble and filter
20% using a pulse shaping filter
21% 4. Upconvert from baseband to 5MHz to avoid radio DC attenuation
22% 5. Transmit the signal over a wireless channel using Warplab
23% 6. Downconvert from 5MHz to baseband
24% 7. Filter the received signal with a Matched Filter (matched to the pulse
25% shaping filter), detect preamble, and downsample output of Matched Filter
26% 8. Demodulate and recover the transmitted bitstream
27% 9. Compute the Bit Error Rate (BER) and close sockets
28
29% Part of this code was adapted from Matlab's commdoc_mod and commdoc_rrc
30% examples.
31
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33% 0. Initialization, define paramters, create pulse shaping filter, and
34% create reference matrix for detection of preamble
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36% Define basic parameters
37M = 4; % Size of signal constellation
38k = log2(M); % Number of bits per symbol
39nsamp = 8; % Oversampling rate or Number of samples per symbol
40
41% Define parameters related to the pulse shaping filter and create the
42% pulse shaping filter
43% This pulse shaping filter is a Squared Root Raised Cosine (SRRC) filter
44filtorder = 64; % Filter order
45delay = filtorder/(nsamp*2); % Group delay (# of input samples). Group
46% delay is the time between the input to the filter and the filter's peak
47% response counted in number of input samples. In number of output samples
48% the delay would be equal to 'delay*nsam'.
49rolloff = 0.3; % Rolloff factor of filter
50rrcfilter = rcosine(1,nsamp,'fir/sqrt',rolloff,delay); % Create SRRC filter
51
52% Plot the filter's impulse response in a stem plot
53figure; % Create new figure window.
54stem(rrcfilter);
55title('Raised Cosine Impulse Response');
56xlabel('n (samples)'); ylabel('Amplitude');
57
58% Define number of symbols to process, number of bits to process, and the
59% preamble.
60% The Warplab transmit buffer can store a maximum of 2^14 samples, the
61% number of samples per symbol is equal 'nsam', and the SRRC filter delay
62% in number of samples is equal to 'delay*nsam'. Consequently, the total
63% number of symbols to be transmitted must be less than
64% (2^14-200)/nsam-2*delay. We subtract extra 200 to account for jitter in
65% sync trigger.
66nsym = floor((2^14-200)/nsamp-2*delay);  % Number or symbols to transmit
67preamble = [-1;-1;-1;1;-1;0;0;0;0;0;0;0;0]; % Preamble is a Barker sequence
68                                            % modulated with BPSK
69nsym_preamble = length(preamble); % number of symbols in preamble
70nsym_payload = nsym-nsym_preamble;
71nbits = floor(nsym_payload*k); % Number of bits to process
72
73% Create a reference matrix used for detection of the preamble in the
74% received signal. We will correlate the received signal with the reference
75% matrix
76preamble_upsamp = upsample(preamble,nsamp); % Upsample preamble
77length_preamble_upsamp = length(preamble_upsamp);
78corr_window = 300; % We expect to find the preamble within the first
79                   % 300 received samples
80reference_samples = zeros(corr_window,1); % Create reference vector.
81reference_samples(1:length_preamble_upsamp) = preamble_upsamp; 
82                     % First samples of reference vector correspond to the
83                     % preamble upsampled
84reference_matrix = toeplitz(reference_samples,...
85circshift(reference_samples(corr_window:-1:1),1)); 
86         % Create reference matrix. The first column of the reference
87         % matrix is equal to the reference_samples vector. The i-th column
88         % of the reference matrix is equal to circular shift of the
89         % reference samples vector, it is a shift down by i samples.
90
91
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% 1. Generate a random bit stream and map it to symbols
94%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96% Create a random binary data stream as a column vector.
97x = randint(nbits,1); 
98
99% Map bits in vector x into k-bit symbols
100xsym = bi2de(reshape(x,k,length(x)/k).','left-msb');
101
102% Stem plot of bits and symbols
103% Plot first 40 bits in a stem plot.
104figure;
105subplot(2,1,1)
106stem(x(1:40),'filled');
107title('Random Bits');
108xlabel('Bit Index'); ylabel('Binary Value');
109% Plot first 40/k symbols in a stem plot.
110subplot(2,1,2)
111stem(xsym(1:40/k),'filled');
112title('Random Bits Mapped to Symbols');
113xlabel('Symbol Index'); ylabel('Integer Value');
114
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116% 2. Modulate the symbols (map symbols to constellation points) and append
117% preamble symbols
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120% Modulate using DQPSK
121ytx_mod = dpskmod(xsym,M);
122
123% Append preamble
124ytx_mod = [preamble;ytx_mod];
125
126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127% 3. Upsample the modulated symbols with the appended preamble and filter
128% using a pulse shaping filter
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130% Upsample and apply square root raised cosine filter.
131ytx_mod_filt = rcosflt(ytx_mod,1,nsamp,'filter',rrcfilter);
132
133% Stem Plot of modulated symbols before and after Squared Root Raised
134% Cosine (SRRC) filter
135% Plots first 30 symbols.
136% Plots I and Q in different windows
137figure; % Create new figure window.
138subplot(2,1,1)
139stem([1:nsamp:nsamp*30],real(ytx_mod(1:30)));
140hold
141stem(real(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r');
142title('I Signal');
143xlabel('n (sample)'); ylabel('Amplitude');
144legend('Before SRRC Filter','After SRRC Filter');
145subplot(2,1,2)
146stem([1:nsamp:nsamp*30],imag(ytx_mod(1:30)));
147hold
148stem(imag(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r');
149title('Q Signal');
150xlabel('n (sample)'); ylabel('Amplitude');
151
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153% 4. Upconvert from baseband to 5MHz to avoid radio DC attenuation
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155time = [0:1:length(ytx_mod_filt)-1]/40e6; % Sampling Freq. is 40MHz
156ytx_mod_filt_up = ytx_mod_filt .* exp(sqrt(-1)*2*pi*5e6*time).';
157
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159% 5. Transmit the signal over a wireless channel using Warplab
160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161% Follow the steps for transmission and reception of data using Warplab.
162% These are the steps in the matlab script warplab_example_TxRx.m
163
164% In this example the vector to transmit is the 'ytx_mod_filt' vector.
165
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167% 5.0. Initializaton and definition of parameters
168%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
169%Load some global definitions (packet types, etc.)
170warplab_defines
171
172% Create Socket handles and intialize nodes
173[socketHandles, packetNum] = warplab_initialize;
174
175% Separate the socket handles for easier access
176% The first socket handle is always the magic SYNC
177% The rest of the handles are the handles to the WARP nodes
178udp_Sync = socketHandles(1);
179udp_node1 = socketHandles(2);
180udp_node2 = socketHandles(3);
181
182% Define WARPLab parameters.
183% Note: For this experiment node 1 will be set as the transmitter and node
184% 2 will be set as the receiver (this is done later in the code), hence,
185% there is no need to define receive gains for node 1 and there is no
186% need to define transmitter gains for node 2.
187
188TxDelay = 100; % Number of noise samples per Rx capture. In [0:2^14]
189TxLength =length(ytx_mod_filt_up); % Length of transmission. In [0:2^14-1-TxDelay]
190CarrierChannel = 12; % Channel in the 2.4 GHz band. In [1:14]
191Node1_Radio2_TxGain_BB = 3; % Tx Baseband Gain. In [0:3]
192Node1_Radio2_TxGain_RF = 40; % Tx RF Gain. In [0:63]
193Node2_Radio2_RxGain_BB = 13; % Rx Baseband Gain. In [0:31]
194Node2_Radio2_RxGain_RF = 2; % Rx RF Gain. In [1:3] 
195TxMode = 0; % Transmission mode. In [0:1]
196            % 0: Single Transmission
197            % 1: Continuous Transmission. Tx board will continue
198            % transmitting the vector of samples until the user manually
199            % disables the transmitter.
200Node2_MGC_AGC_Select = 0;   % Set MGC_AGC_Select=1 to enable Automatic Gain Control (AGC).
201                            % Set MGC_AGC_Select=0 to enable Manual Gain Control (MGC).
202                            % By default, the nodes are set to MGC.             
203
204% Download the WARPLab parameters to the WARP nodes.
205% The nodes store the TxDelay, TxLength, and TxMode parameters in
206% registers defined in the WARPLab sysgen model. The nodes set radio
207% related parameters CarrierChannel, TxGains, and RxGains, using the
208% radio controller functions.
209% The TxDelay, TxLength, and TxMode parameters need to be known at the transmitter;
210% the receiver doesn't require knowledge of these parameters (the receiver
211% will always capture 2^14 samples). For this exercise node 1 will be set as
212% the transmitter (this is done later in the code). Since TxDelay, TxLength and
213% TxMode are only required at the transmitter we download the TxDelay, TxLength and
214% TxMode parameters only to the transmitter node (node 1).
215warplab_writeRegister(udp_node1,TX_DELAY,TxDelay);
216warplab_writeRegister(udp_node1,TX_LENGTH,TxLength);
217warplab_writeRegister(udp_node1,TX_MODE,TxMode);
218% The CarrierChannel parameter must be downloaded to all nodes 
219warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,CarrierChannel);
220warplab_setRadioParameter(udp_node2,CARRIER_CHANNEL,CarrierChannel);
221% Node 1 will be set as the transmitter so download Tx gains to node 1.
222warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16));
223% Node 2 will be set as the receiver so download Rx gains to node 2.
224warplab_setRadioParameter(udp_node2,RADIO2_RXGAINS,(Node2_Radio2_RxGain_BB + Node2_Radio2_RxGain_RF*2^16));
225% Set MGC mode in node 2 (receiver)
226warplab_setAGCParameter(udp_node2,MGC_AGC_SEL, Node2_MGC_AGC_Select);
227
228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229% 5.1. Generate a vector of samples to transmit and send the samples to the
230% WARP board (Sample Frequency is 40MHz)
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232% Prepare some data to be transmitted
233
234% Scale signal to transmit so that it spans [-1,1] range. We do this to
235% use the full range of the DAC at the tranmitter
236scale = 1 / max( [ max(real(ytx_mod_filt_up)) , max(imag(ytx_mod_filt_up)) ] );
237ytx_mod_filt_up = scale*ytx_mod_filt_up;
238
239Node1_Radio2_TxData = ytx_mod_filt_up.'; % Create a signal to transmit. Signal must be a
240% row vector
241
242% Download the samples to be transmitted
243% Hints:
244% 1. The first argument of the 'warplab_writeSMRO' function identifies the
245% node to which samples will be downloaded to. In this exercise we will set
246% node 1 as the transmitter node, the id or handle to node 1 is 'udp_node1'.
247% 2. The second argument of the 'warplab_writeSMRO' function identifies the
248% transmit buffer where the samples will be written. A node programmed with
249% the warplab_mimo_2x2_v04.bit bitstream has 2 transmit buffers and a node
250% programmed with the warplab_mimo_4x4_v04.bit bitstream has 4 transmit
251% buffers. For this exercise we will transmit from radio 2, hence, samples
252% must be downloaded to radio 2 Tx buffer, the id for this buffer is
253% 'RADIO2_TXDATA'.
254% 3. The third argument of the 'warplab_writeSMWO' function is the
255% vector of samples to download, it must be a row vector. For this
256% exercise the 'Node1_Radio2_TxData' vector is the vector of samples to be
257% transmitted, hence, this is the vector that must be downloaded to radio 2
258% Tx buffer.
259
260warplab_writeSMWO(udp_node1, RADIO2_TXDATA, Node1_Radio2_TxData);
261
262%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263% 5.2. Prepare WARP boards for transmission and reception and send trigger to
264% start transmission and reception (trigger is the SYNC packet)
265%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266% The following lines of code set node 1 as transmitter and node 2 as
267% receiver; transmission and capture are triggered by sending the SYNC
268% packet.
269
270% Enable transmitter radio path in radio 2 in node 1 (enable radio 2 in
271% node 1 as transmitter) by sending the RADIO2_TXEN command to node 1 using
272% the 'warplab_sendCmd' function.
273% Hints:
274% 1. The first argument of the 'warplab_sendCmd' function identifies the
275% node to which the command will be sent to. The id or handle to node 1 is
276% 'udp_node1'.
277% 2. The second argument of the 'warplab_sendCmd' function identifies the
278% command that will be sent.
279% 3. The third argument of the 'warplab_sendCmd' command is a field that is
280% not used at the moment, it may be used in future versions of WARPLab to
281% keep track of packets. Use 'packetNum' as the third argument of the
282% 'warplab_sendCmd' command.
283warplab_sendCmd(udp_node1, RADIO2_TXEN, packetNum);
284
285% Enable transmission of node1's radio 2 Tx buffer (enable transmission
286% of samples stored in radio 2 Tx Buffer in node 1) by sending the
287% RADIO2TXBUFF_TXEN command to node 1 using the 'warplab_sendCmd' function.
288warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXEN, packetNum);
289
290% Enable receiver radio path in radio 2 in node 2 (enable radio 2 in
291% node 2 as receiver) by sending the RADIO2_RXEN command to node 2 using
292% the 'warplab_sendCmd' function.
293% Hint: The id or handle to node 2 is 'udp_node2'.
294warplab_sendCmd(udp_node2, RADIO2_RXEN, packetNum);
295
296% Enable capture in node2's radio 2 Rx Buffer (enable radio 2 rx buffer in
297% node 2 for storage of samples) by sending the RADIO2RXBUFF_RXEN command to
298% node 2 using the 'warplab_sendCmd' function.
299warplab_sendCmd(udp_node2, RADIO2RXBUFF_RXEN, packetNum);
300
301% Prime transmitter state machine in node 1. Node 1 will be
302% waiting for the SYNC packet. Transmission from node 1 will be triggered
303% when node 1 receives the SYNC packet.
304warplab_sendCmd(udp_node1, TX_START, packetNum);
305
306% Prime receiver state machine in node 2. Node 2 will be waiting
307% for the SYNC packet. Capture at node 2 will be triggered when node 2
308% receives the SYNC packet.
309warplab_sendCmd(udp_node2, RX_START, packetNum);
310
311% Send the SYNC packet
312warplab_sendSync(udp_Sync);
313
314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315% 5.3. Read the received smaples from the Warp board
316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317% Read the received samples from the WARP board using the
318% 'warplab_readSMRO' function. (Read extra 100 samples to account for
319% jitter in sync trigger)
320% Hints:
321% 1. The first argument of the 'warplab_readSMRO' function identifies the
322% node from which samples will be read. In this exercise we set node 2 as
323% the receiver node, the id or handle to node 2 is 'udp_node2'.
324% 2. The second argument of the 'warplab_readSMRO' function identifies the
325% receive buffer from which samples will be read. A node programmed with
326% the warplab_mimo_2x2_v04.bit bitstream has 2 receive buffers and a node
327% programmed with the warplab_mimo_4x4_v04.bit bitstream has 4 receive
328% buffers. For this exercise samples were captured in node 2 radio 2,
329% hence, samples must be read from radio 2 Rx buffer, the id for this
330% buffer is 'RADIO2_RXDATA'.
331% 3. The third argument of the 'warplab_readSMRO' function is the number of
332% samples to read; reading of samples always starts from address zero.
333% For this exercise set third argument of the 'warplab_readSMRO'
334% function equal to 'TxLength+CaptOffset+100' (Read extra 100 samples to
335% account for jitter in sync trigger)
336[Node2_Radio2_RawRxData] = warplab_readSMRO(udp_node2, RADIO2_RXDATA, TxLength+TxDelay+100);
337
338% Process the received samples to obtain meaningful data
339[Node2_Radio2_RxData,Node2_Radio2_RxOTR] = warplab_processRawRxData(Node2_Radio2_RawRxData);
340
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342% 5.4. Reset and disable the boards
343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344% Set radio 2 Tx buffer in node 1 back to Tx disabled mode
345warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXDIS, packetNum);
346
347% Disable the transmitter radio
348warplab_sendCmd(udp_node1, RADIO2_TXDIS, packetNum);
349
350% Set radio 2 Rx buffer in node 2 back to Rx disabled mode
351warplab_sendCmd(udp_node2, RADIO2RXBUFF_RXDIS, packetNum);
352
353% Disable the receiver radio
354warplab_sendCmd(udp_node2, RADIO2_RXDIS, packetNum);
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356% 5.5. Plot the transmitted and received data
357%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358figure;
359subplot(2,2,1);
360plot(real(Node1_Radio2_TxData));
361title('Tx Node 1 Radio 2 I');
362xlabel('n (samples)'); ylabel('Amplitude');
363axis([0 2^14 -1 1]); % Set axis ranges.
364subplot(2,2,2);
365plot(imag(Node1_Radio2_TxData));
366title('Tx Node 1 Radio 2 Q');
367xlabel('n (samples)'); ylabel('Amplitude');
368axis([0 2^14 -1 1]); % Set axis ranges.
369subplot(2,2,3);
370plot(real(Node2_Radio2_RxData));
371title('Rx Node 2 Radio 2 I');
372xlabel('n (samples)'); ylabel('Amplitude');
373axis([0 2^14 -1 1]); % Set axis ranges.
374subplot(2,2,4);
375plot(imag(Node2_Radio2_RxData));
376title('Rx Node 2 Radio 2 Q');
377xlabel('n (samples)'); ylabel('Amplitude');
378axis([0 2^14 -1 1]); % Set axis ranges.
379
380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381% 6. Downconvert from 5MHz to baseband
382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383time = [0:1:length(Node2_Radio2_RxData)-1]/40e6; % Sampling Freq. is 40MHz
384yrx_bb = Node2_Radio2_RxData .* exp(-sqrt(-1)*2*pi*5e6*time);
385
386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387% 7. Filter the received signal with a Matched Filter (matched to the pulse
388% shaping filter), detect preamble, and downsample output of Matched Filter
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390% Store received samples as a column vector
391yrx_bb = yrx_bb.';
392
393% Matched filter: Filter received signal using the SRRC filter
394yrx_bb_mf = rcosflt(yrx_bb,1,nsamp,'Fs/filter',rrcfilter);
395
396% Correlate with the reference matrix to find preamble sequence
397correlation = abs( (yrx_bb_mf(1:corr_window).') * reference_matrix );
398preamble_start = find(correlation == max(correlation)); % Start of preamble
399first_sample_index = preamble_start+length_preamble_upsamp; % Start of
400                                         % first symbol after preamble
401
402% Downsample output of Matched Filter
403yrx_bb_mf_ds = yrx_bb_mf(first_sample_index:end);
404yrx_bb_mf_ds = downsample(yrx_bb_mf_ds,nsamp);
405
406% Slice symbols of interest (nsym_payload symbols were transmitted so if
407% yrx_bb_mf_ds has more than nsym_payload elements the extra elements of the
408% yrx_bb_mf_ds vector are due to the extra samples read from the Rx buffer
409% and the extra samples added by the delay of the filters)
410yrx_bb_mf_ds = yrx_bb_mf_ds(1:nsym_payload); 
411
412% Stem Plot of signal before Matched Filter, after Matched Filter, and
413% after downsampling
414% Plots first 30 symbols.
415% Plots real and imaginary parts in different windows
416figure; % Create new figure window.
417subplot(2,1,1)
418stem(real(yrx_bb(first_sample_index-(1+delay*nsamp):first_sample_index-(1+delay*nsamp)+30*nsamp)),'b');
419hold
420stem(real(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r');
421stem([1:nsamp:nsamp*30],real(yrx_bb_mf_ds(1:30)),'k');
422title('I Symbols');
423xlabel('n (sample)'); ylabel('Amplitude');
424legend('Before Matched Filter','After Matched Filter','After Downsample');
425subplot(2,1,2)
426stem(imag(yrx_bb(first_sample_index-(1+delay*nsamp):first_sample_index-(1+delay*nsamp)+30*nsamp)),'b');
427hold
428stem(imag(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r');
429stem([1:nsamp:nsamp*30],imag(yrx_bb_mf_ds(1:30)),'k');
430title('Q Symbols');
431xlabel('n (sample)'); ylabel('Amplitude');
432
433% Scatter Plot of received and transmitted constellation points
434h = scatterplot(yrx_bb_mf_ds(1:end),1,0,'g.');
435hold on;
436scatterplot(ytx_mod(nsym_preamble+1:end),1,0,'k*',h);
437title('Constellations');
438legend('Received','Transmitted');
439axis([-2 2 -2 2]); % Set axis ranges.
440hold off;
441
442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443% 8. Demodulate and recover the transmitted bitstream
444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445
446% Demodulate signal using DQPSK
447zsym = dpskdemod(yrx_bb_mf_ds,M);
448
449% Map Symbols to Bits
450z = de2bi(zsym,'left-msb'); % Convert integers to bits.
451% Convert z from a matrix to a vector.
452z = reshape(z.',prod(size(z)),1);
453
454% Plot first 80 transmitted bits and first 80 received bits in a stem plot
455figure;
456subplot(2,1,1)
457stem(x(1:80),'filled');
458title('Transmitted Bits');
459xlabel('Bit Index'); ylabel('Binary Value');
460subplot(2,1,2)
461stem(z(1:80),'filled');
462title('Received Bits');
463xlabel('Bit Index'); ylabel('Binary Value');
464
465%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466% 9. Compute the Bit Error Rate (BER) and close sockets
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468% Compare x and z to obtain the number of errors and the bit error rate
469[number_of_errors,bit_error_rate] = biterr(x(3:length(z)),z(3:length(z)))
470% We start comparing at three because the first two bits are are always
471% lost in DQPSK. We compare until minlen because z may be shorter
472% than x due to the jitter of the synch pulse.
473
474% Close sockets
475pnet('closeall');
476
Note: See TracBrowser for help on using the repository browser.