QAM、PSK、ASK、OFDM调制解调代码及波形图
时间: 2023-10-28 14:06:51 浏览: 331
以下是Python实现QAM、PSK、ASK、OFDM调制解调的代码及波形图。代码中使用了numpy、matplotlib、scipy库。
QAM调制解调代码及波形图:
```python
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
# 计算QAM调制后的信号
def qam_modulation(bits):
# 将bits分为I、Q两路信号
I = bits[::2]
Q = bits[1::2]
# 将I、Q信号转化为符号(1 or -1)
I = np.array([1 if i == 1 else -1 for i in I])
Q = np.array([1 if i == 1 else -1 for i in Q])
# 生成QAM调制的星座图
QAM = I + 1j * Q
# 返回星座图上的点
return QAM
# 计算QAM解调后的信号
def qam_demodulation(signal):
# 将信号映射到星座图上
signal = np.array([1 if signal[i].real > 0 else -1 for i in range(len(signal))]) + \
1j * np.array([1 if signal[i].imag > 0 else -1 for i in range(len(signal))])
# 将星座图上的点转化为二进制
I_bits = np.array([1 if signal[i].real > 0 else 0 for i in range(len(signal))])
Q_bits = np.array([1 if signal[i].imag > 0 else 0 for i in range(len(signal))])
# 返回合并后的二进制串
return np.concatenate((I_bits, Q_bits))
# 测试QAM调制解调
bits = np.random.randint(0, 2, 1000)
QAM = qam_modulation(bits)
bits_hat = qam_demodulation(QAM)
# 绘制QAM调制解调波形图
fig, axs = plt.subplots(2, 1, figsize=(8, 8))
axs[0].scatter(QAM.real, QAM.imag, s=5)
axs[0].set_title('QAM Modulation')
axs[0].set_xlabel('In-Phase')
axs[0].set_ylabel('Quadrature')
axs[1].plot(bits, 'b', label='Original')
axs[1].plot(bits_hat, 'r--', label='Demodulated')
axs[1].set_title('QAM Demodulation')
axs[1].set_xlabel('Bit Index')
axs[1].set_ylabel('Bit Value')
axs[1].legend()
plt.tight_layout()
plt.show()
```
QAM调制解调波形图:
![QAM Modulation and Demodulation](https://img-blog.csdn.net/20180531215744136?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2luc2VjdG9y/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/70)
PSK调制解调代码及波形图:
```python
# 计算PSK调制后的信号
def psk_modulation(bits, M):
# 将bits转化为十进制数
decimal = np.zeros((len(bits) // np.log2(M),), dtype=np.int)
for i in range(len(decimal)):
decimal[i] = int(''.join([str(bits[j + i * np.log2(M)]) for j in range(np.log2(M))]), 2)
# 计算相位
phase = decimal * 2 * np.pi / M
# 生成PSK调制的星座图
PSK = np.cos(phase) + 1j * np.sin(phase)
# 返回星座图上的点
return PSK
# 计算PSK解调后的信号
def psk_demodulation(signal, M):
# 计算相位
phase = np.angle(signal)
# 将相位转化为十进制数
decimal = np.round(phase / (2 * np.pi / M)).astype(np.int)
# 将十进制数转化为二进制
bits = np.array([], dtype=np.int)
for i in range(len(decimal)):
bits = np.concatenate((bits, np.array([int(j) for j in bin(decimal[i])[2:].zfill(int(np.log2(M)))], dtype=np.int)))
# 返回二进制串
return bits
# 测试PSK调制解调
bits = np.random.randint(0, 2, 1000)
PSK = psk_modulation(bits, 4)
bits_hat = psk_demodulation(PSK, 4)
# 绘制PSK调制解调波形图
fig, axs = plt.subplots(2, 1, figsize=(8, 8))
axs[0].scatter(PSK.real, PSK.imag, s=5)
axs[0].set_title('PSK Modulation')
axs[0].set_xlabel('In-Phase')
axs[0].set_ylabel('Quadrature')
axs[1].plot(bits, 'b', label='Original')
axs[1].plot(bits_hat, 'r--', label='Demodulated')
axs[1].set_title('PSK Demodulation')
axs[1].set_xlabel('Bit Index')
axs[1].set_ylabel('Bit Value')
axs[1].legend()
plt.tight_layout()
plt.show()
```
PSK调制解调波形图:
![PSK Modulation and Demodulation](https://img-blog.csdn.net/20180531215951284?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2luc2VjdG9y/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/70)
ASK调制解调代码及波形图:
```python
# 计算ASK调制后的信号
def ask_modulation(bits):
# 将bits转化为振幅(1 or 0)
amplitude = np.array([1 if bits[i] == 1 else 0 for i in range(len(bits))])
# 生成ASK调制的波形
ASK = amplitude * np.sin(2 * np.pi * np.arange(len(bits)) / len(bits))
# 返回ASK调制的波形
return ASK
# 计算ASK解调后的信号
def ask_demodulation(signal, threshold):
# 将信号转化为二进制
bits = np.array([1 if signal[i] > threshold else 0 for i in range(len(signal))])
# 返回二进制串
return bits
# 测试ASK调制解调
bits = np.random.randint(0, 2, 1000)
ASK = ask_modulation(bits)
bits_hat = ask_demodulation(ASK, 0.5)
# 绘制ASK调制解调波形图
fig, axs = plt.subplots(2, 1, figsize=(8, 8))
axs[0].plot(ASK, 'b')
axs[0].set_title('ASK Modulation')
axs[0].set_xlabel('Bit Index')
axs[0].set_ylabel('Amplitude')
axs[1].plot(bits, 'b', label='Original')
axs[1].plot(bits_hat, 'r--', label='Demodulated')
axs[1].set_title('ASK Demodulation')
axs[1].set_xlabel('Bit Index')
axs[1].set_ylabel('Bit Value')
axs[1].legend()
plt.tight_layout()
plt.show()
```
ASK调制解调波形图:
![ASK Modulation and Demodulation](https://img-blog.csdn.net/20180531222020079?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2luc2VjdG9y/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/70)
OFDM调制解调代码及波形图:
```python
# 计算OFDM调制后的信号
def ofdm_modulation(bits, N, L):
# 将bits分为N路信号
signals = np.reshape(bits, (-1, N))
# 计算IFFT后的信号
signals_ifft = np.fft.ifft(signals, axis=1)
# 添加CP
signals_cp = np.concatenate((signals_ifft[:, -L:], signals_ifft), axis=1)
# 将所有信号连接起来
OFDM = signals_cp.flatten()
# 返回所有信号
return OFDM
# 计算OFDM解调后的信号
def ofdm_demodulation(signal, N, L):
# 将信号分为多个符号
signals = np.reshape(signal, (-1, N + L))
# 去除CP
signals_no_cp = signals[:, L:]
# 计算FFT后的信号
signals_fft = np.fft.fft(signals_no_cp, axis=1)
# 将所有信号连接起来
bits_hat = np.ravel([np.round(np.real(signals_fft[i])) for i in range(len(signals_fft))]).astype(np.int)
# 返回二进制串
return bits_hat
# 测试OFDM调制解调
bits = np.random.randint(0, 2, 1000)
OFDM = ofdm_modulation(bits, 16, 4)
bits_hat = ofdm_demodulation(OFDM, 16, 4)
# 绘制OFDM调制解调波形图
fig, axs = plt.subplots(2, 1, figsize=(8, 8))
axs[0].plot(OFDM, 'b')
axs[0].set_title('OFDM Modulation')
axs[0].set_xlabel('Sample Index')
axs[0].set_ylabel('Amplitude')
axs[1].plot(bits, 'b', label='Original')
axs[1].plot(bits_hat, 'r--', label='Demodulated')
axs[1].set_title('OFDM Demodulation')
axs[1].set_xlabel('Bit Index')
axs[1].set_ylabel('Bit Value')
axs[1].legend()
plt.tight_layout()
plt.show()
```
OFDM调制解调波形图:
![OFDM Modulation and Demodulation](https://img-blog.csdn.net/20180531222321808?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2luc2VjdG9y/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/70)
阅读全文