source: ResearchApps/PHY/WARPLAB/WARPLab_v05_2/WorkshopExercises/warplab_SpectrumSensing_WorkshopExercise_Instructors_ContTx.m

Last change on this file was 1671, checked in by murphpo, 13 years ago

adding Melissa's code from the Dyspan workshop

File size: 6.8 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Spectrum sensing using WARPLab
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4
5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6% Instructors code
7
8% 0. Initializaton and definition of parameters
9% 1. Generate a sum of two sinusoids to transmit
10% 2. Plot the transmitted data fft and waveform
11% 3. Prepare WARP node for transmission and send trigger to
12% start transmission (trigger is the SYNC packet)
13% 4. Stop continuous Tx Mode and disable the transmitter path (Commented out
14%    by default, run these lines of code for stopping continous Tx and disable Tx radios)
15
16%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
18%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
19% 0. Initializaton and definition of parameters
20%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22%Load some global definitions (packet types, etc.)
23warplab_defines
24
25% Create Socket handles and intialize nodes
26[socketHandles, packetNum] = warplab_initialize(1);
27
28% Separate the socket handles for easier access
29% The first socket handle is always the magic SYNC
30% The rest of the handles are the handles to the WARP nodes
31udp_Sync = socketHandles(1); % SYNC
32udp_node1 = socketHandles(2); % Handle for node 1. There is only one node
33
34% Define WARPLab parameters.
35TxDelay = 0; % Number of noise samples per Rx capture. In [0:2^14]
36TxLength = 2^14-1-TxDelay; % Length of transmission. In [0:2^14-1-TxDelay]
37Node1_CarrierChannel = 1; % Channel in the 2.4 GHz band. In [1:14]
38Node1_Radio2_TxGain_BB = 3; % Tx Baseband Gain. In [0:3]
39Node1_Radio2_TxGain_RF = 10; % Tx RF Gain. In [0:63]
40TxMode = 1; % Transmission mode. In [0:1]
41            % 0: Single Transmission
42            % 1: Continuous Transmission. Tx node will continue
43            % transmitting the vector of samples until the user manually
44            % disables the transmitter.
45           
46% Download the WARPLab parameters to the WARP nodes.
47warplab_writeRegister(udp_node1,TX_DELAY,TxDelay);
48warplab_writeRegister(udp_node1,TX_LENGTH,TxLength);
49warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,Node1_CarrierChannel);
50% Node 2 will be set as the transmitter so download Tx gains to node 2.
51warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16));
52warplab_writeRegister(udp_node1,TX_MODE,TxMode);           
53
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55% 1. Generate a sum of two sinusoids to transmit
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58% 1. Generate a sum of two sinusoids to transmit
59
60% Create a signal to transmit, the signal is a function of the time vector
61% 't' the signal can be real or complex.
62% The signal must meet the following requirements:
63% - Signal to transmit must be a row vector.
64% - The amplitude of the real part must be in [-1:1] and the amplitude
65% of the imaginary part must be in [-1:1].
66% - Highest frequency component is limited to 9.5 MHz (signal bandwidth
67% is limited to 19 MHz)
68% - Lowest frequency component is limited to 30 kHz
69
70t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector.
71f1 = 1e6;
72f2 = 4e6;
73Node1_Radio2_TxData = 0.45*exp(t*j*2*pi*f1)+0.45*exp(t*j*2*pi*f2);
74% Node1_Radio2_TxData = 0.45*sin(t*2*pi*f1)+0.45*exp(t*j*2*pi*f2);
75% Node1_Radio2_TxData = 0.45*cos(t*2*pi*f1)+0.45*exp(t*j*2*pi*f2);
76
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78% 2. Plot the transmitted data fft and waveform
79%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80
81% Compute and plot the fft of the transmitted signal centered at baseband
82% Computation of fft is based on the example in MATLAB's fft
83% documentation, see help fft for more information on MATLAB's fft function
84% Comppute fft
85L=length(Node1_Radio2_TxData); % Get length of transmitted vector
86NFFT = 2^nextpow2(L); % Next power of 2 from length of y
87Y = fftshift(fft(Node1_Radio2_TxData,NFFT)/L); % Compute fft
88Fs=40e6; % Sampling frequency is equal to 40e6
89f = Fs/2*linspace(-1,1,NFFT);
90
91% Plot plot fft.
92figure
93plot(f/10^6,abs(Y)) 
94title('Spectrum of transmitted signal in current carrier channel')
95xlabel('Frequency (MHz)')
96ylabel('Magnitude')
97xlim([-10, 10])
98
99% Plot amplitude versus sample
100figure;
101subplot(2,2,1);
102plot(real(Node1_Radio2_TxData));
103title('Tx Node 1 Radio 2 I');
104xlabel('n (samples)'); ylabel('Amplitude');
105axis([0 2^14 -1 1]); % Set axis ranges.
106
107subplot(2,2,2);
108plot(imag(Node1_Radio2_TxData));
109title('Tx Node 1 Radio 2 Q');
110xlabel('n (samples)'); ylabel('Amplitude');
111axis([0 2^14 -1 1]); % Set axis ranges.
112
113subplot(2,2,3);
114plot([0:1:length(Node1_Radio2_TxData)-1]/40e6,real(Node1_Radio2_TxData));
115title('Tx Node 1 Radio 2 I');
116xlabel('time (s)'); ylabel('Amplitude');
117axis([0 (length(Node1_Radio2_TxData)-1)/40e6 -1 1]); % Set axis ranges.
118
119subplot(2,2,4);
120plot([0:1:length(Node1_Radio2_TxData)-1]/40e6,imag(Node1_Radio2_TxData));
121title('Tx Node 1 Radio 2 Q');
122xlabel('time (s)'); ylabel('Amplitude');
123axis([0 (length(Node1_Radio2_TxData)-1)/40e6 -1 1]); % Set axis ranges.
124
125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126% 3. Prepare WARP node for transmission and send trigger to
127% start transmission (trigger is the SYNC packet)
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129% Download the samples to be transmitted
130warplab_writeSMWO(udp_node1, RADIO2_TXDATA, Node1_Radio2_TxData);
131
132% Enable transmitter radio path in radio 2 in node 2 (enable radio 2 in
133% node 2 as transmitter)
134warplab_sendCmd(udp_node1, RADIO2_TXEN, packetNum);
135
136% Enable transmission of Node1's radio 2 Tx buffer (enable transmission
137% of samples stored in radio 2 Tx Buffer in node 2)
138warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXEN, packetNum);
139
140% Prime transmitter state machine in node 2. Node 2 will be
141% waiting for the SYNC packet. Transmission from node 2 will be triggered
142% when node 2 receives the SYNC packet.
143warplab_sendCmd(udp_node1, TX_START, packetNum);
144
145% Send the SYNC packet
146warplab_sendSync(udp_Sync);
147
148
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150% 4. Stop continuous Tx Mode and disable the transmitter path
151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152% Stop continuous transmission. Resets the output and read address of the
153% transmitter buffer without disabling the transmitter radio.
154
155% Set radio 2 Tx buffer in node 2 back to Tx disabled mode
156
157% Disable the transmitter radio
158
159% warplab_sendCmd(udp_node1, TX_STOP, packetNum);
160% warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXDIS, packetNum);
161% warplab_sendCmd(udp_node1, RADIO2_TXDIS, packetNum);
162
163
164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165% Close sockets
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167% pnet('closeall');
Note: See TracBrowser for help on using the repository browser.