|

楼主 |
发表于 2023-9-18 19:25:27
|
显示全部楼层
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy.signal import hilbert
- from scipy import signal
- from matplotlib.pylab import mpl
- mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
- mpl.rcParams['axes.unicode_minus'] = False # 显示负号
- # 生成调制信号
- fs = 1000 # 采样频率
- t = np.arange(0, 1, 1/fs) # 定义时间序列
- baseband_signal = 2 * np.sin(2*np.pi*10*t) # 频率设为10Hz
- # 生成载波信号
- fc = 100 # 载波信号频率
- carrier_signal = 1/2 * np.sin(2*np.pi*fc*t) # 调制信号幅度为0.5
- # 进行调制
- modulated_signal = baseband_signal * (1 + carrier_signal)
- # 进行解调
- analytic_signal = hilbert(modulated_signal) # 进行希尔伯特变换
- amplitude_envelope = np.abs(analytic_signal) # 得到包络线
- demodulated_frequency_unfiltered = modulated_signal / amplitude_envelope
- b, a = signal.butter(4, [2*50/fs], 'lowpass') # 4阶带通滤波器
- demodulated_frequency = signal.filtfilt(b, a, demodulated_frequency_unfiltered)
- ###测试###
- def to_fft(data,i,j):
- freq = np.fft.fftfreq(len(data), 1/fs) # 自动生成频率
- y = np.fft.fft(data) # FFT变换
- y = 2*np.abs(y)/fs # 归一化,能量集中处理
- y_ed = y * 2 / fs #*2能量集中化,/N归一化
- plt.subplot(4, 2, i)
- plt.plot(freq, y_ed)
- plt.title('频域')
- plt.xlim(0,j)
- to_fft(baseband_signal,2,150)
- to_fft(carrier_signal,4,150)
- to_fft(demodulated_frequency_unfiltered,6,150)
- to_fft(demodulated_frequency,8,150)
- ###测试###
- # 绘制信号图形
- plt.subplot(4, 2, 1)
- plt.plot(t, baseband_signal)
- plt.title('Baseband Signal')
- plt.subplot(4, 2, 3)
- plt.plot(t, carrier_signal)
- plt.title('Carrier Signal')
- plt.subplot(4, 2, 5)
- plt.plot(t, demodulated_frequency_unfiltered)
- plt.title('demodulated_frequency_unfiltered')
- axes_1 = plt.subplot(4, 2, 7)
- plt.plot(t, demodulated_frequency,label='demodulated_frequency')
- plt.title('demodulated_frequency')
- axes_1.legend()
- plt.tight_layout()
- plt.show()
复制代码
这是自己改了下的 |
|