-
Notifications
You must be signed in to change notification settings - Fork 23
/
Pluto_beamformer_Monopulse_youtube.py
213 lines (190 loc) · 9.6 KB
/
Pluto_beamformer_Monopulse_youtube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Jon Kraft, Nov 5 2022
https://github.com/jonkraft/Pluto_Beamformer
video walkthrough of this at: https://www.youtube.com/@jonkraft
"""
# Copyright (C) 2020 Analog Devices, Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# - Neither the name of Analog Devices, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# - The use of this software may or may not infringe the patent rights
# of one or more patent holders. This license does not release you
# from the requirement that you obtain separate licenses from these
# patent holders to use this software.
# - Use of the software either in source or binary form, must be run
# on or directly connected to an Analog Devices Inc. component.
#
# THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.
#
# IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
# RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Copyright (C) 2020 Analog Devices, Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# - Neither the name of Analog Devices, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# - The use of this software may or may not infringe the patent rights
# of one or more patent holders. This license does not release you
# from the requirement that you obtain separate licenses from these
# patent holders to use this software.
# - Use of the software either in source or binary form, must be run
# on or directly connected to an Analog Devices Inc. component.
#
# THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED.
#
# IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
# RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import adi
import matplotlib.pyplot as plt
import numpy as np
'''Setup'''
samp_rate = 2e6 # must be <=30.72 MHz if both channels are enabled
NumSamples = 2**12
rx_lo = 2.3e9
rx_mode = "manual" # can be "manual" or "slow_attack"
rx_gain0 = 40
rx_gain1 = 40
tx_lo = rx_lo
tx_gain = -3
fc0 = int(200e3)
phase_cal = 0
num_scans = 5
''' Set distance between Rx antennas '''
d_wavelength = 0.5 # distance between elements as a fraction of wavelength. This is normally 0.5
wavelength = 3E8/rx_lo # wavelength of the RF carrier
d = d_wavelength*wavelength # distance between elements in meters
print("Set distance between Rx Antennas to ", int(d*1000), "mm")
'''Create Radios'''
sdr = adi.ad9361(uri='ip:192.168.2.1')
'''Configure properties for the Rx Pluto'''
sdr.rx_enabled_channels = [0, 1]
sdr.sample_rate = int(samp_rate)
sdr.rx_rf_bandwidth = int(fc0*3)
sdr.rx_lo = int(rx_lo)
sdr.gain_control_mode = rx_mode
sdr.rx_hardwaregain_chan0 = int(rx_gain0)
sdr.rx_hardwaregain_chan1 = int(rx_gain1)
sdr.rx_buffer_size = int(NumSamples)
sdr._rxadc.set_kernel_buffers_count(1) # set buffers to 1 (instead of the default 4) to avoid stale data on Pluto
sdr.tx_rf_bandwidth = int(fc0*3)
sdr.tx_lo = int(rx_lo)
sdr.tx_cyclic_buffer = True
sdr.tx_hardwaregain_chan0 = int(tx_gain)
sdr.tx_hardwaregain_chan1 = int(-88)
sdr.tx_buffer_size = int(2**18)
'''Program Tx and Send Data'''
fs = int(sdr.sample_rate)
N = 2**16
ts = 1 / float(fs)
t = np.arange(0, N * ts, ts)
i0 = np.cos(2 * np.pi * t * fc0) * 2 ** 14
q0 = np.sin(2 * np.pi * t * fc0) * 2 ** 14
iq0 = i0 + 1j * q0
sdr.tx([iq0,iq0]) # Send Tx data.
# Assign frequency bins and "zoom in" to the fc0 signal on those frequency bins
xf = np.fft.fftfreq(NumSamples, ts)
xf = np.fft.fftshift(xf)/1e6
signal_start = int(NumSamples*(samp_rate/2+fc0/2)/samp_rate)
signal_end = int(NumSamples*(samp_rate/2+fc0*2)/samp_rate)
def calcTheta(phase):
# calculates the steering angle for a given phase delta (phase is in deg)
# steering angle is theta = arcsin(c*deltaphase/(2*pi*f*d)
arcsin_arg = np.deg2rad(phase)*3E8/(2*np.pi*rx_lo*d)
arcsin_arg = max(min(1, arcsin_arg), -1) # arcsin argument must be between 1 and -1, or numpy will throw a warning
calc_theta = np.rad2deg(np.arcsin(arcsin_arg))
return calc_theta
def dbfs(raw_data):
# function to convert IQ samples to FFT plot, scaled in dBFS
NumSamples = len(raw_data)
win = np.hamming(NumSamples)
y = raw_data * win
s_fft = np.fft.fft(y) / np.sum(win)
s_shift = np.fft.fftshift(s_fft)
s_dbfs = 20*np.log10(np.abs(s_shift)/(2**11)) # Pluto is a signed 12 bit ADC, so use 2^11 to convert to dBFS
return s_shift, s_dbfs
def monopulse_angle(array1, array2):
''' Correlate the sum and delta signals '''
# Since our signals are closely aligned in time, we can just return the 'valid' case where the signals completley overlap
# We can do correlation in the time domain (probably faster) or the freq domain
# In the time domain, it would just be this:
# sum_delta_correlation = np.correlate(delayed_sum, delayed_delta, 'valid')
# But I like the freq domain, because then I can focus just on the fc0 signal of interest
sum_delta_correlation = np.correlate(array1[signal_start:signal_end], array2[signal_start:signal_end], 'valid')
angle_diff = np.angle(sum_delta_correlation)
return angle_diff
def scan_for_DOA():
# go through all the possible phase shifts and find the peak, that will be the DOA (direction of arrival) aka steer_angle
data = sdr.rx()
Rx_0=data[0]
Rx_1=data[1]
peak_sum = []
peak_delta = []
monopulse_phase = []
delay_phases = np.arange(-180, 180, 2) # phase delay in degrees
for phase_delay in delay_phases:
delayed_Rx_1 = Rx_1 * np.exp(1j*np.deg2rad(phase_delay+phase_cal))
delayed_sum = Rx_0 + delayed_Rx_1
delayed_delta = Rx_0 - delayed_Rx_1
delayed_sum_fft, delayed_sum_dbfs = dbfs(delayed_sum)
delayed_delta_fft, delayed_delta_dbfs = dbfs(delayed_delta)
mono_angle = monopulse_angle(delayed_sum_fft, delayed_delta_fft)
peak_sum.append(np.max(delayed_sum_dbfs))
peak_delta.append(np.max(delayed_delta_dbfs))
monopulse_phase.append(np.sign(mono_angle))
peak_dbfs = np.max(peak_sum)
peak_delay_index = np.where(peak_sum==peak_dbfs)
peak_delay = delay_phases[peak_delay_index[0][0]]
steer_angle = int(calcTheta(peak_delay))
return delay_phases, peak_dbfs, peak_delay, steer_angle, peak_sum, peak_delta, monopulse_phase
'''Collect Data'''
for i in range(20):
# let Pluto run for a bit, to do all its calibrations
data = sdr.rx()
for i in range(num_scans):
delay_phases, peak_dbfs, peak_delay, steer_angle, peak_sum, peak_delta, monopulse_phase = scan_for_DOA()
plt.plot(delay_phases, peak_sum)
plt.plot(delay_phases, peak_delta)
plt.plot(delay_phases, monopulse_phase)
plt.axvline(x=peak_delay, color='r', linestyle=':')
plt.text(-180, -26, "Peak signal occurs with phase shift = {} deg".format(round(peak_delay,1)))
plt.text(-180, -28, "If d={}mm, then steering angle = {} deg".format(int(d*1000), steer_angle))
plt.ylim(top=5, bottom=-30)
plt.xlabel("phase shift [deg]")
plt.ylabel("Rx0 + Rx1 [dBfs]")
plt.draw()
plt.show()
sdr.tx_destroy_buffer()
if i>40: print('\a') # for a long capture, beep when the script is done