利用自己擅长的计算机语言,编程绘制2FSK和2PSK的调制波形。 1、输入固定十六进制数据0xEB5A,能绘制调制波形; 2、输入变长度十六进制数据,绘制对应的波形。
时间: 2024-09-25 18:17:16 浏览: 64
利用Python和matplotlib库可以编写一个简单的程序来实现这个需求,这里我将展示如何使用这两个条件来绘制2FSK (Frequency Shift Keying) 和 2PSK (Phase Shift Keying) 调制波形。
首先,我们需要导入所需的库:
```python
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import square
```
接下来,我们定义两个函数,分别用于2FSK和2PSK调制:
```python
def fsk_modulate(hex_data, symbol_rate=1, bit_duration=1):
bits = [int(c, 16) for c in hex_data]
carrier_freqs = np.arange(len(bits)) * symbol_rate / len(bits)
fsk_signal = np.zeros(carrier_freqs.shape)
for i, bit in enumerate(bits):
if bit == 1:
fsk_signal[i] = 1
else:
fsk_signal[i] = -1
return carrier_freqs, fsk_signal
def psk_modulate(hex_data, num_states=2, symbol_rate=1, phase_shift=pi):
bits = [int(c, 16) % num_states for c in hex_data]
t = np.linspace(0, len(bits) * bit_duration, len(bits))
psk_signal = np.zeros_like(t)
for i, bit in enumerate(bits):
psk_signal[i::num_states] += bit * np.sin(phase_shift * t[i::num_states])
return t, psk_signal
```
现在我们可以对固定和变长十六进制数据进行绘制:
1. 固定十六进制数据绘制:
```python
hex_data_fixed = 'EB5A'
carrier_freqs_fsk, fsk_waveform = fsk_modulate(hex_data_fixed)
t_psk, psk_waveform = psk_modulate(hex_data_fixed)
plt.figure()
plt.subplot(2, 1, 1)
plt.title('2FSK Waveform')
plt.plot(carrier_freqs_fsk, fsk_waveform)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.subplot(2, 1, 2)
plt.title('2PSK Waveform')
plt.plot(t_psk, psk_waveform)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.show()
```
2. 变长十六进制数据绘制:
```python
# 示例变长数据
hex_data_variable = '1B3F7D4A'
# 对每个十六进制字符处理并合并波形
fsk_variable = []
psk_variable = []
for c in hex_data_variable:
freqs, waveform = fsk_modulate(c)
fsk_variable.append(waveform)
t, phase = psk_modulate(c)
psk_variable.append(phase)
fsk_total = np.sum(fsk_variable, axis=0)
psk_total = np.sum(psk_variable, axis=0)
plt.figure()
plt.subplot(2, 1, 1)
plt.title('2FSK Combined Waveform')
plt.plot(carrier_freqs_fsk, fsk_total)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.subplot(2, 1, 2)
plt.title('2PSK Combined Waveform')
plt.plot(t_psk, psk_total)
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.show()
```
阅读全文