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.) |
---|
23 | warplab_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 |
---|
31 | udp_Sync = socketHandles(1); % SYNC |
---|
32 | udp_node1 = socketHandles(2); % Handle for node 1. There is only one node |
---|
33 | |
---|
34 | % Define WARPLab parameters. |
---|
35 | TxDelay = 0; % Number of noise samples per Rx capture. In [0:2^14] |
---|
36 | TxLength = 2^14-1-TxDelay; % Length of transmission. In [0:2^14-1-TxDelay] |
---|
37 | Node1_CarrierChannel = 1; % Channel in the 2.4 GHz band. In [1:14] |
---|
38 | Node1_Radio2_TxGain_BB = 3; % Tx Baseband Gain. In [0:3] |
---|
39 | Node1_Radio2_TxGain_RF = 10; % Tx RF Gain. In [0:63] |
---|
40 | TxMode = 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. |
---|
47 | warplab_writeRegister(udp_node1,TX_DELAY,TxDelay); |
---|
48 | warplab_writeRegister(udp_node1,TX_LENGTH,TxLength); |
---|
49 | warplab_setRadioParameter(udp_node1,CARRIER_CHANNEL,Node1_CarrierChannel); |
---|
50 | % Node 2 will be set as the transmitter so download Tx gains to node 2. |
---|
51 | warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16)); |
---|
52 | warplab_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 | |
---|
70 | t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector. |
---|
71 | f1 = 1e6; |
---|
72 | f2 = 4e6; |
---|
73 | Node1_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 |
---|
85 | L=length(Node1_Radio2_TxData); % Get length of transmitted vector |
---|
86 | NFFT = 2^nextpow2(L); % Next power of 2 from length of y |
---|
87 | Y = fftshift(fft(Node1_Radio2_TxData,NFFT)/L); % Compute fft |
---|
88 | Fs=40e6; % Sampling frequency is equal to 40e6 |
---|
89 | f = Fs/2*linspace(-1,1,NFFT); |
---|
90 | |
---|
91 | % Plot plot fft. |
---|
92 | figure |
---|
93 | plot(f/10^6,abs(Y)) |
---|
94 | title('Spectrum of transmitted signal in current carrier channel') |
---|
95 | xlabel('Frequency (MHz)') |
---|
96 | ylabel('Magnitude') |
---|
97 | xlim([-10, 10]) |
---|
98 | |
---|
99 | % Plot amplitude versus sample |
---|
100 | figure; |
---|
101 | subplot(2,2,1); |
---|
102 | plot(real(Node1_Radio2_TxData)); |
---|
103 | title('Tx Node 1 Radio 2 I'); |
---|
104 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
105 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
106 | |
---|
107 | subplot(2,2,2); |
---|
108 | plot(imag(Node1_Radio2_TxData)); |
---|
109 | title('Tx Node 1 Radio 2 Q'); |
---|
110 | xlabel('n (samples)'); ylabel('Amplitude'); |
---|
111 | axis([0 2^14 -1 1]); % Set axis ranges. |
---|
112 | |
---|
113 | subplot(2,2,3); |
---|
114 | plot([0:1:length(Node1_Radio2_TxData)-1]/40e6,real(Node1_Radio2_TxData)); |
---|
115 | title('Tx Node 1 Radio 2 I'); |
---|
116 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
117 | axis([0 (length(Node1_Radio2_TxData)-1)/40e6 -1 1]); % Set axis ranges. |
---|
118 | |
---|
119 | subplot(2,2,4); |
---|
120 | plot([0:1:length(Node1_Radio2_TxData)-1]/40e6,imag(Node1_Radio2_TxData)); |
---|
121 | title('Tx Node 1 Radio 2 Q'); |
---|
122 | xlabel('time (s)'); ylabel('Amplitude'); |
---|
123 | axis([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 |
---|
130 | warplab_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) |
---|
134 | warplab_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) |
---|
138 | warplab_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. |
---|
143 | warplab_sendCmd(udp_node1, TX_START, packetNum); |
---|
144 | |
---|
145 | % Send the SYNC packet |
---|
146 | warplab_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'); |
---|