y=-6x+10 X = np.linspace(-10, 10, SAMPLE_NUM)什么意思
时间: 2024-05-31 20:07:13 浏览: 156
这是Python代码,其中:
- y = -6x + 10 表示一个一次函数,y 是 x 的线性函数,系数为 -6,截距为 10。
- X = np.linspace(-10, 10, SAMPLE_NUM) 表示创建一个 numpy 数组 X,其中包含从 -10 到 10 的均匀间隔的 SAMPLE_NUM 个数。
相关问题
将这个代码修改为自适应序列采样的插值方法:import numpy as np import matplotlib.pyplot as plt def gen_data(x1, x2): y_sample = np.sin(np.pi * x1 / 2) + np.cos(np.pi * x1 / 3) y_all = np.sin(np.pi * x2 / 2) + np.cos(np.pi * x2 / 3) return y_sample, y_all def kernel_interpolation(y_sample, x1, sig): gaussian_kernel = lambda x, c, h: np.exp(-(x - x[c]) ** 2 / (2 * (h ** 2))) num = len(y_sample) w = np.zeros(num) int_matrix = np.asmatrix(np.zeros((num, num))) for i in range(num): int_matrix[i, :] = gaussian_kernel(x1, i, sig) w = int_matrix.I * np.asmatrix(y_sample).T return w def kernel_interpolation_rec(w, x1, x2, sig): gkernel = lambda x, xc, h: np.exp(-(x - xc) ** 2 / (2 * (h ** 2))) num = len(x2) y_rec = np.zeros(num) for i in range(num): for k in range(len(w)): y_rec[i] = y_rec[i] + w[k] * gkernel(x2[i], x1[k], sig) return y_rec if __name__ == '__main__': snum = 12 # control point数量 ratio =50 # 总数据点数量:snum*ratio sig = 2 # 核函数宽度 xs = -4 xe = 4 x1 = np.linspace(xs, xe, snum) x2 = np.linspace(xs, xe, (snum - 1) * ratio + 1) y_sample, y_all = gen_data(x1, x2) plt.figure(1) w = kernel_interpolation(y_sample, x1, sig) y_rec = kernel_interpolation_rec(w, x1, x2, sig) plt.plot(x2, y_rec, 'k') plt.plot(x2, y_all, 'r:') plt.ylabel('y') plt.xlabel('x') for i in range(len(x1)): plt.plot(x1[i], y_sample[i], 'go', markerfacecolor='none') plt.legend(labels=['reconstruction', 'original', 'control point'], loc='lower left') plt.title('kernel interpolation:$y=sin(\pi x/2)+cos(\pi x/3)$') plt.show()
以下是修改后的代码,其中使用了自适应序列采样的方法:
```
import numpy as np
import matplotlib.pyplot as plt
def gen_data(x1, x2):
y_sample = np.sin(np.pi * x1 / 2) + np.cos(np.pi * x1 / 3)
y_all = np.sin(np.pi * x2 / 2) + np.cos(np.pi * x2 / 3)
return y_sample, y_all
def kernel_interpolation(y_sample, x1, sig, x2=None):
if x2 is None:
x2 = x1
gaussian_kernel = lambda x, c, h: np.exp(-(x - x[c]) ** 2 / (2 * (h ** 2)))
num = len(y_sample)
w = np.zeros(num)
int_matrix = np.asmatrix(np.zeros((num, num)))
for i in range(num):
int_matrix[i, :] = gaussian_kernel(x1, i, sig)
w = int_matrix.I * np.asmatrix(y_sample).T
if len(x2) != len(x1):
y_rec = kernel_interpolation_rec(w, x1, x2, sig)
return w, y_rec
else:
return w
def adaptive_sampling(x1, ratio, sig):
x2 = []
num = len(x1)
for i in range(num-1):
step = (x1[i+1]-x1[i])/ratio
x2.append(x1[i])
y_sample = np.sin(np.pi * x1[i] / 2) + np.cos(np.pi * x1[i] / 3)
y_next = np.sin(np.pi * x1[i+1] / 2) + np.cos(np.pi * x1[i+1] / 3)
diff = y_next - y_sample
for j in range(ratio):
x_new = x1[i] + (j+1)*step
y_new = y_sample + diff*(j+1)/ratio
x2.append(x_new)
x2.append(x1[num-1])
y_sample, y_all = gen_data(x1, x2)
w, y_rec = kernel_interpolation(y_sample, x1, sig, x2)
return x2, y_all, y_rec
if __name__ == '__main__':
snum = 12 # control point数量
ratio = 50 # 总数据点数量:snum*ratio
sig = 2 # 核函数宽度
xs = -4
xe = 4
x1 = np.linspace(xs, xe, snum)
x2, y_all, y_rec = adaptive_sampling(x1, ratio, sig)
plt.figure(1)
plt.plot(x2, y_rec, 'k')
plt.plot(x2, y_all, 'r:')
plt.ylabel('y')
plt.xlabel('x')
for i in range(len(x1)):
plt.plot(x1[i], y_rec[i*ratio], 'go', markerfacecolor='none')
plt.legend(labels=['reconstruction', 'original', 'control point'], loc='lower left')
plt.title('kernel interpolation:$y=sin(\pi x/2)+cos(\pi x/3)$')
plt.show()
```
主要的修改如下:
1. 修改了 `kernel_interpolation` 函数,加入了一个可选参数 `x2`,用于指定插值的数据点序列。若 `x2` 未指定,则默认使用 `x1`。
2. 新增了一个 `adaptive_sampling` 函数,用于生成自适应序列采样的数据点序列 `x2`。该函数根据 `x1` 的间隔和 `ratio` 的值计算每个间隔内采样的点数,并在两个控制点之间均匀插值采样点。
3. 在 `main` 函数中,改用 `adaptive_sampling` 函数生成数据点序列 `x2` 和对应的采样数据 `y_all` 和 `y_rec`。并且在绘制图形时,使用 `y_rec[i*ratio]` 代替原来的 `y_sample[i]`,以便在图上标出控制点。
详细解释以下Python代码:import numpy as np import adi import matplotlib.pyplot as plt sample_rate = 1e6 # Hz center_freq = 915e6 # Hz num_samps = 100000 # number of samples per call to rx() sdr = adi.Pluto("ip:192.168.2.1") sdr.sample_rate = int(sample_rate) # Config Tx sdr.tx_rf_bandwidth = int(sample_rate) # filter cutoff, just set it to the same as sample rate sdr.tx_lo = int(center_freq) sdr.tx_hardwaregain_chan0 = -50 # Increase to increase tx power, valid range is -90 to 0 dB # Config Rx sdr.rx_lo = int(center_freq) sdr.rx_rf_bandwidth = int(sample_rate) sdr.rx_buffer_size = num_samps sdr.gain_control_mode_chan0 = 'manual' sdr.rx_hardwaregain_chan0 = 0.0 # dB, increase to increase the receive gain, but be careful not to saturate the ADC # Create transmit waveform (QPSK, 16 samples per symbol) num_symbols = 1000 x_int = np.random.randint(0, 4, num_symbols) # 0 to 3 x_degrees = x_int*360/4.0 + 45 # 45, 135, 225, 315 degrees x_radians = x_degrees*np.pi/180.0 # sin() and cos() takes in radians x_symbols = np.cos(x_radians) + 1j*np.sin(x_radians) # this produces our QPSK complex symbols samples = np.repeat(x_symbols, 16) # 16 samples per symbol (rectangular pulses) samples *= 2**14 # The PlutoSDR expects samples to be between -2^14 and +2^14, not -1 and +1 like some SDRs # Start the transmitter sdr.tx_cyclic_buffer = True # Enable cyclic buffers sdr.tx(samples) # start transmitting # Clear buffer just to be safe for i in range (0, 10): raw_data = sdr.rx() # Receive samples rx_samples = sdr.rx() print(rx_samples) # Stop transmitting sdr.tx_destroy_buffer() # Calculate power spectral density (frequency domain version of signal) psd = np.abs(np.fft.fftshift(np.fft.fft(rx_samples)))**2 psd_dB = 10*np.log10(psd) f = np.linspace(sample_rate/-2, sample_rate/2, len(psd)) # Plot time domain plt.figure(0) plt.plot(np.real(rx_samples[::100])) plt.plot(np.imag(rx_samples[::100])) plt.xlabel("Time") # Plot freq domain plt.figure(1) plt.plot(f/1e6, psd_dB) plt.xlabel("Frequency [MHz]") plt.ylabel("PSD") plt.show(),并分析该代码中QPSK信号的功率谱密度图的特点
这段Python代码的作用是使用ADI Pluto SDR设备生成并传输一个QPSK信号,并将接收到的信号进行功率谱密度分析。下面是对代码的注释:
```
import numpy as np
import adi
import matplotlib.pyplot as plt
# 设置采样率、中心频率和采样点数
sample_rate = 1e6 # Hz
center_freq = 915e6 # Hz
num_samps = 100000 # number of samples per call to rx()
# 连接ADI Pluto SDR设备
sdr = adi.Pluto("ip:192.168.2.1")
sdr.sample_rate = int(sample_rate)
# 配置发送端的参数
sdr.tx_rf_bandwidth = int(sample_rate) # filter cutoff, just set it to the same as sample rate
sdr.tx_lo = int(center_freq)
sdr.tx_hardwaregain_chan0 = -50 # Increase to increase tx power, valid range is -90 to 0 dB
# 配置接收端的参数
sdr.rx_lo = int(center_freq)
sdr.rx_rf_bandwidth = int(sample_rate)
sdr.rx_buffer_size = num_samps
sdr.gain_control_mode_chan0 = 'manual'
sdr.rx_hardwaregain_chan0 = 0.0 # dB, increase to increase the receive gain, but be careful not to saturate the ADC
# 创建发送的QPSK信号
num_symbols = 1000
x_int = np.random.randint(0, 4, num_symbols) # 0 to 3
x_degrees = x_int*360/4.0 + 45 # 45, 135, 225, 315 degrees
x_radians = x_degrees*np.pi/180.0 # sin() and cos() takes in radians
x_symbols = np.cos(x_radians) + 1j*np.sin(x_radians) # this produces our QPSK complex symbols
samples = np.repeat(x_symbols, 16) # 16 samples per symbol (rectangular pulses)
samples *= 2**14 # The PlutoSDR expects samples to be between -2^14 and +2^14, not -1 and +1 like some SDRs
# 启动发送端并发送信号
sdr.tx_cyclic_buffer = True # Enable cyclic buffers
sdr.tx(samples) # start transmitting
# 接收接收端的信号
for i in range (0, 10):
raw_data = sdr.rx() # Receive samples
rx_samples = sdr.rx()
print(rx_samples)
# 停止发送端
sdr.tx_destroy_buffer()
# 计算接收到的信号的功率谱密度
psd = np.abs(np.fft.fftshift(np.fft.fft(rx_samples)))**2
psd_dB = 10*np.log10(psd)
f = np.linspace(sample_rate/-2, sample_rate/2, len(psd))
# 绘制时域图
plt.figure(0)
plt.plot(np.real(rx_samples[::100]))
plt.plot(np.imag(rx_samples[::100]))
plt.xlabel("Time")
# 绘制频域图
plt.figure(1)
plt.plot(f/1e6, psd_dB)
plt.xlabel("Frequency [MHz]")
plt.ylabel("PSD")
plt.show()
```
以上代码生成了一个随机QPSK信号,通过ADI Pluto SDR设备将其传输,并使用Pluto SDR设备接收该信号。接收到的信号进行了功率谱密度分析,并绘制了频域图。
QPSK信号的功率谱密度图的特点是,其频谱表现为四个簇,每个簇对应QPSK信号的一个符号。每个簇的带宽约为基带信号的带宽,且由于使用矩形脉冲,每个簇的带宽之间有一定的重叠。此外,功率谱密度图中还可以看到一些其他频率分量,这些分量可能是由于接收信号中存在其他干扰或噪声导致的。
阅读全文