根据一下描述生成代码:基于瑞利平坦衰落信道,进一步建立频率选择性衰落信道模型,输入功率时延谱和多普勒频率扩展,生成频率选择性衰落信道。选择不同的输入信号,同时改变多径信道参数,观察信号的输出波形,频谱,分析多径传播对信号接收的影响,对比信道参数或者信号特性变化时,输出信号的变化情况。 输入信号的类型至少包括单音信号,QPSK调制信号、MSK调制信号以及OFDM信号, ,针对上述不同输入信号,绘制并分析输入信号与输出信号的波形、功率谱。
时间: 2024-03-20 08:42:18 浏览: 63
以下是基于Python实现的代码示例,用于建立频率选择性衰落信道模型,生成频率选择性衰落信道,以及绘制输入信号和输出信号的波形和功率谱。
```
import numpy as np
import matplotlib.pyplot as plt
# 生成频率选择性衰落信道
def freq_sel_fading_channel(delay, doppler, sampling_rate, num_samples):
num_paths = len(delay)
num_taps = int(np.max(np.ceil(np.array(delay) * doppler))) + 1
h = np.zeros((num_paths, num_taps), dtype=np.complex)
for i in range(num_paths):
for j in range(num_taps):
h[i, j] = np.exp(-1j * 2 * np.pi * doppler[i] * j / sampling_rate) * np.sqrt(1 / num_taps)
h = np.sum(h, axis=0)
h = np.concatenate((h, np.zeros(num_samples - num_taps)))
return h
# 生成单音信号
def generate_tone_signal(freq, sampling_rate, duration):
t = np.arange(0, duration, 1 / sampling_rate)
s = np.sin(2 * np.pi * freq * t)
return s
# 生成QPSK调制信号
def generate_qpsk_signal(symbols, sampling_rate, symbol_rate):
num_samples_per_symbol = int(sampling_rate / symbol_rate)
t = np.arange(0, len(symbols) * num_samples_per_symbol) / sampling_rate
i = np.real(symbols)
q = np.imag(symbols)
i = np.repeat(i, num_samples_per_symbol)
q = np.repeat(q, num_samples_per_symbol)
s = i * np.cos(2 * np.pi * symbol_rate * t) - q * np.sin(2 * np.pi * symbol_rate * t)
return s
# 生成MSK调制信号
def generate_msk_signal(symbols, sampling_rate, symbol_rate, modulation_index):
num_samples_per_symbol = int(sampling_rate / symbol_rate)
t = np.arange(0, len(symbols) * num_samples_per_symbol) / sampling_rate
phase = np.cumsum(np.angle(symbols[1:] * np.conj(symbols[:-1]))) % (2 * np.pi)
phase = np.concatenate(([0], phase))
s = np.cos(2 * np.pi * symbol_rate * t + modulation_index * phase)
return s
# 生成OFDM信号
def generate_ofdm_signal(data, num_subcarriers, cp_length):
num_symbols = int(np.ceil(len(data) / num_subcarriers))
data = np.concatenate((data, np.zeros(num_symbols * num_subcarriers - len(data))))
data = data.reshape((num_symbols, num_subcarriers))
s = np.zeros((num_symbols, num_subcarriers + cp_length), dtype=np.complex)
for i in range(num_symbols):
s[i, :] = np.fft.ifft(data[i, :])
s[i, :cp_length] = s[i, -cp_length:]
s = np.concatenate(s)
return s
# 主程序
sampling_rate = 100e3
num_samples = 1e5
delay = [0, 1e-5, 2e-5, 3e-5]
doppler = [10, 20, 30, 40]
channel = freq_sel_fading_channel(delay, doppler, sampling_rate, num_samples)
# 生成单音信号
tone_freq = 5e3
tone_signal = generate_tone_signal(tone_freq, sampling_rate, 1)
# 生成QPSK调制信号
symbol_rate = 10e3
qpsk_symbols = np.random.choice([-1-1j, -1+1j, 1-1j, 1+1j], int(num_samples / symbol_rate))
qpsk_signal = generate_qpsk_signal(qpsk_symbols, sampling_rate, symbol_rate)
# 生成MSK调制信号
modulation_index = 0.5
msk_symbols = np.random.choice([-1-1j, -1+1j, 1-1j, 1+1j], int(num_samples / symbol_rate))
msk_signal = generate_msk_signal(msk_symbols, sampling_rate, symbol_rate, modulation_index)
# 生成OFDM信号
num_subcarriers = 64
cp_length = 16
ofdm_data = np.random.choice([-1-1j, -1+1j, 1-1j, 1+1j], 2 * num_subcarriers)
ofdm_signal = generate_ofdm_signal(ofdm_data, num_subcarriers, cp_length)
# 建立并应用频率选择性衰落信道
channel_output_tone = np.convolve(channel, tone_signal)
channel_output_qpsk = np.convolve(channel, qpsk_signal)
channel_output_msk = np.convolve(channel, msk_signal)
channel_output_ofdm = np.convolve(channel, ofdm_signal)
# 绘制输入信号和输出信号的波形和功率谱
plt.figure(figsize=(12, 10))
plt.subplot(4, 2, 1)
plt.plot(np.arange(len(tone_signal)) / sampling_rate, tone_signal)
plt.title('Input Tone Signal')
plt.subplot(4, 2, 2)
plt.magnitude_spectrum(tone_signal, Fs=sampling_rate)
plt.title('Input Tone Signal Spectrum')
plt.subplot(4, 2, 3)
plt.plot(np.arange(len(channel_output_tone)) / sampling_rate, channel_output_tone)
plt.title('Output Tone Signal')
plt.subplot(4, 2, 4)
plt.magnitude_spectrum(channel_output_tone, Fs=sampling_rate)
plt.title('Output Tone Signal Spectrum')
plt.subplot(4, 2, 5)
plt.plot(np.arange(len(qpsk_signal)) / sampling_rate, qpsk_signal)
plt.title('Input QPSK Signal')
plt.subplot(4, 2, 6)
plt.magnitude_spectrum(qpsk_signal, Fs=sampling_rate)
plt.title('Input QPSK Signal Spectrum')
plt.subplot(4, 2, 7)
plt.plot(np.arange(len(channel_output_qpsk)) / sampling_rate, channel_output_qpsk)
plt.title('Output QPSK Signal')
plt.subplot(4, 2, 8)
plt.magnitude_spectrum(channel_output_qpsk, Fs=sampling_rate)
plt.title('Output QPSK Signal Spectrum')
plt.tight_layout()
plt.show()
```
阅读全文