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

Revision 799, 14.8 kB (checked in by murphpo, 10 months ago)

fixing typos in warplab workshop exercises

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Using Warplab to Estimate the Amplitude and Phase of a Narrowband Flat
3% Fading Wireless Channel
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5% In this lab exercise you will write a matlab script that transmits and
6% receives data using Warplab and computes an estimate of the amplitude and
7% phase of the channel by comparing the transmitted and received data.
8
9% The specific steps to be implemented are the following:
10
11% 0. Transmit a narrowband signal using Warplab
12% 1. Remove from the received vector the samples that do not correspond to
13% transmitted data.
14% 2. Compute the amplitude and the phase of the transmitted and received
15% sammples
16% 3. Compute the channel amplitude and channel phase per sample
17
18% NOTE 1 : The amplitude and phase computed in this exercise correspond to
19% the amplitude and phase of the channel together with the amplitude and
20% phase of the hardware. In other words, the effect of the radios is also
21% part of the channel.
22
23% You will write a matlab script that implements the four steps above.
24% Part of the code is provided, some part of the code you will write. Read
25% the code below and fill in with your code wherever you are asked to do
26% so.
27
28% NOTE 2 : To avoid conflict with other groups using the boards, please
29% test the code you write in this script in any of the following three
30% ways:
31%
32% Option 1. Run this script from matlab's Command Window by entering the
33% name of the script (enter warplab_example_ChannelEstimation_WorkshopExercise
34% in matlab's Command Window).
35% Option 2. In the menu bar go to Debug and select Run. If there
36% are errors in the code, error messages will appear in the Command Window.
37% Option 3. Press F5. If the are errors in the code, error messages will
38% appear in the Command Window.
39%
40% DO NOT USE the Evaluate selection option and DO NOT run the script by
41% sections. To test any change, always run the whole script by following
42% any of the three options above.
43
44try,
45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46% Code to avoid conflict between users, only needed for the workshop, go to
47% step 0 below to transmit a narrowband signal using Warplab
48%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49fid = fopen('c:\boards_lock.txt');
50
51if(fid > -1)
52    fclose('all');
53        errordlg('Boards already in use - Please try again!');
54        return;
55end
56
57!echo > c:\boards_lock.txt
58
59%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60% 0. Transmit a narrowband signal using Warplab
61%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62% Follow the steps for transmission and reception of data using Warplab.
63% These are the steps implemented in the previous lab exercise, the
64% following sections (0.0 to 0.5) guide you through the steps.
65
66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67% 0.0. Initializaton and definition of parameters
68%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69%-------------------------------------------------------------------------%
70% USER CODE HERE
71
72% Follow the steps for Initializaton and definition of parameters.
73% You can copy the code corresponding to step 0 in the previous lab
74% exercise and then paste it here.
75
76% Remember to set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the
77% same values you used in the warplab_siso_GUI.
78
79% For the code below to work, make sure the 'CaptOffset' variable exists
80% in the matlab workspace.
81
82%Load some global definitions (packet types, etc.)
83warplab_siso_defines
84
85% Create Socket handles and intialize nodes
86[socketHandles, packetNum] = warplab_initialize;
87
88%Separate the socket handles for easier access
89% The first socket handle is always the magic SYNC
90% The rest can be arranged in any combination of Tx and Rx
91udp_Sync = socketHandles(1);
92udp_Tx = socketHandles(2);
93udp_RxA = socketHandles(3);
94
95% Define the warplab options (parameters)
96CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14]
97TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset]
98TxGainBB = 3; %Tx Baseband Gain in [0:3]
99TxGainRF = 40; %Tx RF Gain in [0:63]
100RxGainBB = 12; %Rx Baseband Gain in [0:31]
101RxGainRF = 1; %Rx RF Gain in [1:3]
102CarrierChannel = 11; % Channel in the 2.4 GHz band. In [1:14]
103
104% Define the options vector; the order of options is set by the FPGA's code
105% (C code)
106optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel];
107% Send options vector to the nodes
108warplab_setOptions(socketHandles,optionsVector);
109
110%-------------------------------------------------------------------------%
111
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113% 0.1. Generate a vector of samples to transmit and send the samples to the
114% Warp board (Sample Frequency is 40MHz)
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116%-------------------------------------------------------------------------%
117% USER CODE HERE
118
119% Follow the steps to Generate a vector of samples to transmit and send
120% the samples to the Warp board.
121% You can copy the code corresponding to step 1 in the previous lab
122% exercise and then paste it here.
123% You can use the following two lines of code to generate the narrowband
124% signal:
125% t = 0:(1/40e6):TxLength/40e6 - 1/40e6;
126% TxData = exp(t*j*2*pi*1e6);
127
128% For the code below to work, make sure the transmit vector is
129% stored in a variable called 'TxData'. Make sure 'TxData' exists
130% in the matlab workspace.
131
132%Create time vector 't'
133t = 0:(1/40e6):TxLength/40e6 - 1/40e6;
134TxData = exp(t*j*2*pi*1e6); % Create a signal to transmit. Signal must be a
135% row vector. The signal can be real or complex, the only constraint is
136% that the amplitude of the real part must be in [-1:1] and the amplitude
137% of the imaginary part must be in [-1:1]
138
139% Download the samples to be transmitted
140warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA);
141%-------------------------------------------------------------------------%
142
143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144% 0.2. Prepare boards for transmission and reception and send trigger to
145% start transmission and reception (trigger is the SYNC packet)
146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147%-------------------------------------------------------------------------%
148% USER CODE HERE
149% Follow the steps to prepare boards for transmission and reception and
150% send trigger to start transmission and reception.
151% You can copy the code corresponding to step 2 in the previous lab
152% exercise and then paste it here.
153
154% Enable transmission
155warplab_enableTx(udp_Tx);
156
157% Enable reception
158warplab_enableRx(udp_RxA);
159
160% Send the SYNC packet
161warplab_sendSync(udp_Sync);
162%-------------------------------------------------------------------------%
163
164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165% 0.3. Read the received samples from the Warp board
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167%-------------------------------------------------------------------------%
168% USER CODE HERE
169% Follow the steps to read the received samples from the Warp board.
170% You can copy the code corresponding to step 3 in the previous lab
171% exercise and then paste it here. There is no need to read the RSSI, but
172% you can do so.
173
174% For the code below to work, make sure the received vector is
175% stored in a variable called 'RxData'. Make sure 'RxData' exists
176% in the matlab workspace.
177
178% Read back the received samples
179[RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset);
180% Process the received samples to obtain meaningful data
181[RxData,RxOTR] = warplab_processRawRxData(RawRxData);
182% Read stored RSSI data OPTIONAL
183[RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
184% Procecss Raw RSSI data to obtain meningful RSSI values OPTIONAL
185[RxRSSI] = warplab_processRawRSSIData(RawRSSIData);
186
187%-------------------------------------------------------------------------%
188
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% 0.4. Reset and disable the boards
191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
192%-------------------------------------------------------------------------%
193% USER CODE HERE
194% Follow the steps to reset and disable the boards.
195% You can copy the code corresponding to step 4 in the previous lab
196% exercise and then paste it here.
197
198% Reset the receiver
199warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum);
200
201% Disable the receiver
202warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum);
203
204% Disable the transmitter
205warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum);
206
207% Close sockets
208pnet('closeall');
209
210%-------------------------------------------------------------------------%
211
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213% 0.5. Plot the transmitted and received data
214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215%-------------------------------------------------------------------------%
216% USER CODE HERE
217% If you are interested in looking at the received and transmitted data you
218% can copy the code corresponding to step 5 in the previous lab exercise
219% and paste it here
220
221figure;
222subplot(2,2,1);
223plot(real(TxData));
224title('Tx I');
225xlabel('n (samples)'); ylabel('Amplitude');
226axis([0 2^14 -1 1]); % Set axis ranges.
227subplot(2,2,2);
228plot(imag(TxData));
229title('Tx Q');
230xlabel('n (samples)'); ylabel('Amplitude');
231axis([0 2^14 -1 1]); % Set axis ranges.
232subplot(2,2,3);
233plot(real(RxData));
234title('Rx I');
235xlabel('n (samples)'); ylabel('Amplitude');
236axis([0 2^14 -1 1]); % Set axis ranges.
237subplot(2,2,4);
238plot(imag(RxData));
239title('Rx Q');
240xlabel('n (samples)'); ylabel('Amplitude');
241axis([0 2^14 -1 1]); % Set axis ranges.
242
243%-------------------------------------------------------------------------%
244
245%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246% 1. Remove from the received vector the samples that do not correspond to
247% transmitted data. In other words, remove from the received vector samples
248% 1 to CaptOffset. This step will remove samples that correspond to measured
249% noise and make the RxData vector the same length as the TxData vector
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251RxData = RxData(CaptOffset+1:end);
252
253%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254% 2. Compute the amplitude and the phase of the transmitted and received
255% sammples
256%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257
258%-------------------------------------------------------------------------%
259% USER CODE HERE
260% Compute the magnitude per sample of the transmitted and received
261% data. Store the magnitude of the transmitted data in a variable named
262% 'mag_TxData'. Store the magnitude of the received data in a variable
263% named 'mag_RxData'.
264% Hint: You can use Matlab's 'abs' function
265
266mag_TxData = abs(TxData);
267mag_RxData = abs(RxData);
268
269%-------------------------------------------------------------------------%
270
271
272%-------------------------------------------------------------------------%
273% USER CODE HERE
274% Compute the phase per sample of the transmitted and received
275% data. Store the phase (in radians) of the transmitted data in a variable
276% named 'phase_TxData'. Store the phase (in radians) of the received data
277% in a variable named 'phase_RxData'.
278% Hint: You can use Matlab's 'angle' function
279
280phase_TxData = angle(TxData);
281phase_RxData = angle(RxData);
282
283%-------------------------------------------------------------------------%
284
285phase_TxData_unw = unwrap(phase_TxData); % Unwrap phase
286phase_TxData = phase_TxData *180/pi; % Convert phase to degrees
287phase_TxData_unw = phase_TxData_unw *180/pi; % Convert unwraped phase to degrees
288
289phase_RxData_unw = unwrap(phase_RxData); % Unwrap phase
290phase_RxData = phase_RxData *180/pi; % Convert phase to degrees
291phase_RxData_unw = phase_RxData_unw *180/pi; % Convert unwraped phase to degrees
292
293% Plot magnitude and phase of transmitted and received samples
294figure;
295subplot(2,3,1);
296plot(mag_TxData);
297title('Tx Magnitude');
298xlabel('n (samples)'); ylabel('Amplitude');
299subplot(2,3,2);
300plot(phase_TxData);
301title('Tx Phase');
302xlabel('n (samples)'); ylabel('Degrees');
303subplot(2,3,3);
304plot(phase_TxData_unw);
305title('Tx Phase unwrapped');
306xlabel('n (samples)'); ylabel('Degrees');
307subplot(2,3,4);
308plot(mag_RxData);
309title('Rx Magnitude');
310xlabel('n (samples)'); ylabel('Amplitude');
311subplot(2,3,5);
312plot(phase_RxData);
313title('Rx Phase');
314xlabel('n (samples)'); ylabel('Degrees');
315subplot(2,3,6);
316plot(phase_RxData_unw);
317title('Rx Phase unwrapped');
318xlabel('n (samples)'); ylabel('Degrees');
319
320
321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322% 3. Compute the channel amplitude and channel phase per sample
323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324%-------------------------------------------------------------------------%
325% USER CODE HERE
326% Compute the channel amplitude per sample. Store the result in a variable
327% named 'channel_amplitude'
328% Hint 1:
329% Channel amplitude = Magnitude of transmitted samples / Magnitude of received samples
330% Hint 2:
331% You can use Matlab's './' function to implement division of vetors entry
332% by entry. To learn more about this function enter 'help ./' in the Matlab
333% command window
334
335channel_amplitude = mag_RxData./mag_TxData;
336%-------------------------------------------------------------------------%
337
338%-------------------------------------------------------------------------%
339% USER CODE HERE
340% Compute the channel phase per sample. Store the result in a variable
341% named 'channel_phase'
342% Hint 1:
343% Channel Phase = Phase of transmitted samples - Phase of received samples
344
345channel_phase = phase_RxData_unw - phase_TxData_unw;
346%-------------------------------------------------------------------------%
347
348% Plot channel amplitude and phase
349figure
350subplot(2,1,1)
351plot(channel_amplitude)
352title('Channel Amplitude per sample');
353xlabel('n (samples)'); ylabel('Amplitude');
354subplot(2,1,2)
355plot(channel_phase)
356title('Channel Phase per sample');
357xlabel('n (samples)'); ylabel('Degrees');
358
359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360% Code to avoid conflict between users, only needed for the workshop
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362pnet('closeall');
363!del c:\boards_lock.txt
364catch,
365% Close sockets
366pnet('closeall');
367!del c:\boards_lock.txt
368lasterr
369end
Note: See TracBrowser for help on using the browser.