root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_Comm.m

Revision 797, 14.9 kB (checked in by MelissaDuarte, 12 months ago)

Removing %% to avoid the cell display in matlab

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Using Warplab to Transmit Bits Over a Wireless Channel
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4% This matlab srcipt generates a bitstream, modulates the bitstream using
5% DQPSK, transmits the modulated symbols over a wireless channel using
6% Warplab, and demodulates the received signal to obtain the
7% transmitted bits. Bit error rate (BER) is computed by comparing the
8% transmitted bitstream with the bitstream recovered at the receiver
9
10% The specific steps implemented in this script are the following:
11
12% 0. Initialization, define paramters, create pulse shaping filter, and
13% create reference matrix for detection of preamble
14% 1. Generate a random bit stream and map it to symbols
15% 2. Modulate the symbols (map symbols to constellation points) and append
16% preamble symbols
17% 3. Upsample the modulated symbols with the appended preamble and filter
18% using a pulse shaping filter
19% 4. Transmit the signal over a wireless channel using Warplab
20% 5. Filter the received signal with a Matched Filter (matched to the pulse
21% shaping filter), detect preamble, and downsample output of Matched Filter
22% 6. Demodulate and recover the transmitted bitstream
23% 7. Compute the Bit Error Rate (BER)
24
25% Part of this code was adapted from Matlab's commdoc_mod and commdoc_rrc
26% examples.
27
28
29%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30% 0. Initialization, define paramters, create pulse shaping filter, and
31% create reference matrix for detection of preamble
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33% Define basic parameters
34M = 4; % Size of signal constellation
35k = log2(M); % Number of bits per symbol
36nsamp = 8; % Oversampling rate or Number of samples per symbol
37
38% Define parameters related to the pulse shaping filter and create the
39% pulse shaping filter
40% This pulse shaping filter is a Squared Root Raised Cosine (SRRC) filter
41filtorder = 64; % Filter order
42delay = filtorder/(nsamp*2); % Group delay (# of input samples). Group
43% delay is the time between the input to the filter and the filter's peak
44% response counted in number of input samples. In number of output samples
45% the delay would be equal to 'delay*nsam'.
46rolloff = 0.3; % Rolloff factor of filter
47rrcfilter = rcosine(1,nsamp,'fir/sqrt',rolloff,delay); % Create SRRC filter
48
49% Plot the filter's impulse response in a stem plot
50figure; % Create new figure window.
51stem(rrcfilter);
52title('Raised Cosine Impulse Response');
53xlabel('n (samples)'); ylabel('Amplitude');
54
55% Define number of symbols to process, number of bits to process, and the
56% preamble.
57% The Warplab transmit buffer can store a maximum of 2^14 samples, the
58% number of samples per symbol is equal 'nsam', and the SRRC filter delay
59% in number of samples is equal to 'delay*nsam'. Consequently, the total
60% number of symbols to be transmitted must be less than 2^14/nsam-2*delay.
61nsym = floor(2^14/nsamp-2*delay);  % Number or symbols to transmit
62preamble = [-1;-1;-1;1;-1;0;0;0;0;0;0;0;0]; % Preamble is a Barker sequence
63                                            % modulated with BPSK
64nsym_preamble = length(preamble); % number of symbols in preamble
65nsym_payload = nsym-nsym_preamble;
66nbits = floor(nsym_payload*k); % Number of bits to process
67
68% Create a reference matrix used for detection of the preamble in the
69% received signal. We will correlate the received signal with the reference
70% matrix
71preamble_upsamp = upsample(preamble,nsamp); % Upsample preamble
72length_preamble_upsamp = length(preamble_upsamp);
73corr_window = 150; % We expect to find the preamble within the first
74                   % 150 received samples
75reference_samples = zeros(corr_window,1); % Create reference vector.
76reference_samples(1:length_preamble_upsamp) = preamble_upsamp;
77                     % First samples of reference vector correspond to the
78                     % preamble upsampled
79reference_matrix = toeplitz(reference_samples,...
80circshift(reference_samples(corr_window:-1:1),1));
81         % Create reference matrix. The first column of the reference
82         % matrix is equal to the reference_samples vector. The i-th column
83         % of the reference matrix is equal to circular shift of the
84         % reference samples vector, it is a shift down by i samples.
85
86
87%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88% 1. Generate a random bit stream and map it to symbols
89%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90% Create a random binary data stream as a column vector.
91x = randint(nbits,1);
92
93% Map bits in vector x into k-bit symbols
94xsym = bi2de(reshape(x,k,length(x)/k).','left-msb');
95
96% Stem plot of bits and symbols
97% Plot first 40 bits in a stem plot.
98figure;
99subplot(2,1,1)
100stem(x(1:40),'filled');
101title('Random Bits');
102xlabel('Bit Index'); ylabel('Binary Value');
103% Plot first 40/k symbols in a stem plot.
104subplot(2,1,2)
105stem(xsym(1:40/k),'filled');
106title('Random Bits Mapped to Symbols');
107xlabel('Symbol Index'); ylabel('Integer Value');
108
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110% 2. Modulate the symbols (map symbols to constellation points) and append
111% preamble symbols
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113% Modulate using DQPSK
114ytx_mod = dpskmod(xsym,M);
115
116% Append preamble
117ytx_mod = [preamble;ytx_mod];
118
119%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120% 3. Upsample the modulated symbols with the appended preamble and filter
121% using a pulse shaping filter
122%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123% Upsample and apply square root raised cosine filter.
124ytx_mod_filt = rcosflt(ytx_mod,1,nsamp,'filter',rrcfilter);
125
126% Stem Plot of modulated symbols before and after Squared Root Raised
127% Cosine (SRRC) filter
128% Plots first 30 symbols.
129% Plots I and Q in different windows
130figure; % Create new figure window.
131subplot(2,1,1)
132stem([1:nsamp:nsamp*30],real(ytx_mod(1:30)));
133hold
134stem(real(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r');
135title('I Signal');
136xlabel('n (sample)'); ylabel('Amplitude');
137legend('Before SRRC Filter','After SRRC Filter');
138subplot(2,1,2)
139stem([1:nsamp:nsamp*30],imag(ytx_mod(1:30)));
140hold
141stem(imag(ytx_mod_filt(1+delay*nsamp:1+30*nsamp+delay*nsamp)),'r');
142title('Q Signal');
143xlabel('n (sample)'); ylabel('Amplitude');
144
145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146% 4. Transmit the signal over a wireless channel using Warplab
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148% Follow the steps for transmission and reception of data using Warplab.
149% These are the steps in the matlab script warplab_example_TxRx.m
150
151% In this example the vector to transmit is the 'ytx_mod_filt' vector. The
152% capture offset is zero.
153
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155% 4.0. Initializaton and definition of parameters
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157%Load some global definitions (packet types, etc.)
158warplab_siso_defines
159
160% Create Socket handles and intialize nodes
161[socketHandles, packetNum] = warplab_initialize;
162
163%Separate the socket handles for easier access
164% The first socket handle is always the magic SYNC
165% The rest can be arranged in any combination of Tx and Rx
166udp_Sync = socketHandles(1);
167udp_Tx = socketHandles(2);
168udp_RxA = socketHandles(3);
169
170% Define the warplab options (parameters)
171CaptOffset = 0; %Number of noise samples per Rx capture; in [0:2^14]
172TxLength = length(ytx_mod_filt); %Length of transmission; in [0:2^14-CaptOffset]
173TxGainBB = 3; %Tx Baseband Gain in [0:3]
174TxGainRF = 40; %Tx RF Gain in [0:63]
175RxGainBB = 13; %Rx Baseband Gain in [0:31]
176RxGainRF = 1; %Rx RF Gain in [1:3]
177CarrierChannel = 11;
178
179% Define the options vector; the order of options is set by the FPGA's code
180% (C code)
181optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel];
182% Send options vector to the nodes
183warplab_setOptions(socketHandles,optionsVector);
184
185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186% 4.1. Generate a vector of samples to transmit and send the samples to the
187% Warp board (Sample Frequency is 40MHz)
188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189% Prepare some data to be transmitted
190
191% Scale signal to transmit so that it spans [-1,1] range. We do this to
192% use the full range of the DAC at the tranmitter
193scale = 1 / max( [ max(real(ytx_mod_filt)) , max(imag(ytx_mod_filt)) ] );
194ytx_mod_filt = scale*ytx_mod_filt;
195
196TxData = ytx_mod_filt.'; % Create a signal to transmit. Signal must be a
197% row vector
198
199% Download the samples to be transmitted
200warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA);
201
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203% 4.2. Prepare boards for transmission and reception and send trigger to
204% start transmission and reception (trigger is the SYNC packet)
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% Enable transmission
207warplab_enableTx(udp_Tx);
208
209% Enable reception
210warplab_enableRx(udp_RxA);
211
212% Send the SYNC packet
213warplab_sendSync(udp_Sync);
214
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216% 4.3. Read the received smaples from the Warp board
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218% Read back the received samples
219[RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset);
220% Process the received samples to obtain meaningful data
221[RxData,RxOTR] = warplab_processRawRxData(RawRxData);
222% Read stored RSSI data
223[RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
224% Procecss Raw RSSI data to obtain meningful RSSI values
225[RxRSSI] = warplab_processRawRSSIData(RawRSSIData);
226
227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228% 4.4. Reset and disable the boards
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230% Reset the receiver
231warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum);
232
233% Disable the receiver
234warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum);
235
236% Disable the transmitter
237warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum);
238
239% Close sockets
240pnet('closeall');
241
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% 4.5. Plot the transmitted and received data
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245figure;
246subplot(2,2,1);
247plot(real(TxData));
248title('Tx I');
249xlabel('n (samples)'); ylabel('Amplitude');
250axis([0 2^14 -1 1]); % Set axis ranges.
251subplot(2,2,2);
252plot(imag(TxData));
253title('Tx Q');
254xlabel('n (samples)'); ylabel('Amplitude');
255axis([0 2^14 -1 1]); % Set axis ranges.
256subplot(2,2,3);
257plot(real(RxData));
258title('Rx I');
259xlabel('n (samples)'); ylabel('Amplitude');
260axis([0 2^14 -1 1]); % Set axis ranges.
261subplot(2,2,4);
262plot(imag(RxData));
263title('Rx Q');
264xlabel('n (samples)'); ylabel('Amplitude');
265axis([0 2^14 -1 1]); % Set axis ranges.
266
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268% 5. Filter the received signal with a Matched Filter (matched to the pulse
269% shaping filter), detect preamble, and downsample output of Matched Filter
270%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271% Store received samples as a column vector
272yrx_bb = RxData.';
273
274% Matched filter: Filter received signal using the SRRC filter
275yrx_bb_mf = rcosflt(yrx_bb,1,nsamp,'Fs/filter',rrcfilter);
276
277% Correlate with the reference matrix to find preamble sequence
278correlation = abs( (yrx_bb_mf(1:corr_window).') * reference_matrix );
279preamble_start = find(correlation == max(correlation)); % Start of preamble
280first_sample_index = preamble_start+length_preamble_upsamp; % Start of
281                                         % first symbol after preamble
282
283% Downsample output of Matched Filter
284yrx_bb_mf_ds = yrx_bb_mf(first_sample_index:end);
285yrx_bb_mf_ds = downsample(yrx_bb_mf_ds,nsamp);
286% Account for delay of filter
287yrx_bb_mf_ds = yrx_bb_mf_ds(1:end-2*delay); % Twice delay because signal
288                                       % goes through 2 filtering stages
289
290% Stem Plot of signal before Matched Filter, after Matched Filter, and
291% after downsampling
292% Plots first 30 symbols.
293% Plots real and imaginary parts in different windows
294figure; % Create new figure window.
295subplot(2,1,1)
296stem(real(yrx_bb(1+2*delay*nsamp+first_sample_index:1+2*delay*nsamp+...
297                                       first_sample_index+30*nsamp)),'b');
298hold
299stem(real(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r');
300stem([1:nsamp:nsamp*30],real(yrx_bb_mf_ds(1:30)),'k');
301title('I Symbols');
302xlabel('n (sample)'); ylabel('Amplitude');
303legend('Before Matched Filter','After Matched Filter','After Downsample');
304subplot(2,1,2)
305stem(imag(yrx_bb(first_sample_index:first_sample_index+30*nsamp)),'b');
306hold
307stem(imag(yrx_bb_mf(first_sample_index:first_sample_index+30*nsamp)),'r');
308stem([1:nsamp:nsamp*30],imag(yrx_bb_mf_ds(1:30)),'k');
309title('Q Symbols');
310xlabel('n (sample)'); ylabel('Amplitude');
311
312% Scatter Plot of received and transmitted constellation points
313h = scatterplot(yrx_bb_mf_ds(nsym_preamble+1:end),1,0,'g.');
314hold on;
315scatterplot(ytx_mod(nsym_preamble+1:end),1,0,'k*',h);
316title('Constellations');
317legend('Received','Transmitted');
318axis([-2 2 -2 2]); % Set axis ranges.
319hold off;
320
321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322% 6. Demodulate and recover the transmitted bitstream
323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324% Demodulate signal using DQPSK
325zsym = dpskdemod(yrx_bb_mf_ds,M);
326
327% Map Symbols to Bits
328z = de2bi(zsym,'left-msb'); % Convert integers to bits.
329% Convert z from a matrix to a vector.
330z = reshape(z.',prod(size(z)),1);
331
332% Plot first 80 transmitted bits and first 80 received bits in a stem plot
333figure;
334subplot(2,1,1)
335stem(x(1:80),'filled');
336title('Transmitted Bits');
337xlabel('Bit Index'); ylabel('Binary Value');
338subplot(2,1,2)
339stem(z(1:80),'filled');
340title('Received Bits');
341xlabel('Bit Index'); ylabel('Binary Value');
342
343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344% 7. Compute the Bit Error Rate (BER)
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346% Compare x and z to obtain the number of errors and the bit error rate
347[number_of_errors,bit_error_rate] = biterr(x(3:length(z)),z(3:length(z)))
348% We start comparing at three because the first two bits are the are always
349% lost in DQPSK. We compare until length(z) because z may be shorter than
350% x due to the fact that some bits (approx 1 to 5) may be lost fue to the
351% jitter of the synch pulse.
Note: See TracBrowser for help on using the browser.