如何使用python打印调幅信号

时间:2019-06-26 13:23:59

标签: python modulation

我有一个该程序的matlab示例,但无法在Python中完成。 它的外观(数学示例)https://imgur.com/a/oam3jXl

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import hilbert, chirp
duration = int(input("Input duration of signal = "))
A = int(input("Input amplitude of signal = "))
start = int(input("Input start of modulation time = "))
end = int(input("Input lenght of modulation = "))
fs = 400.0
samples = int(fs*duration)
t = np.arange(samples) / fs
main_t = []
result = []
counter=0
for l in range(0,samples):
    main_t.append(0)
    result.append(0)
start_mod = np.arange(start*fs)
end_mod = np.arange(end*fs)
signal = chirp(t, 20.0, t[-1], 100.0)
signal *= (A + 0.5 * np.sin(2.0*np.pi*3.0*t) )

for i in np.arange(0,start*fs):
    signal.insert(i,0)

fig = plt.figure()
ax0 = fig.add_subplot(211)
ax0.plot(main_t, result)
ax0.set_xlabel("time in seconds")
plt.show()

我希望从控制台输入一些数据,然后例如使用matplotlib打印图形。图形必须看起来像您可以在图像上看到的图形(数学示例)。

1 个答案:

答案 0 :(得分:0)

忽略所有获取输入的麻烦(随其他限制而变化很大),生成调幅信号的代码应该很简单。

我首先引入numpy,生成一组要在其上采样信号的点,计算振幅,然后将它们组合起来:

import numpy as np

x = np.linspace(0, 10, 501)
ampl = np.exp(-(x - 3.5)**2 / 0.8)
y = np.sin(x * 25) * ampl

然后我们可以使用matplotlib将它们绘制成类似以下内容:

import matplotlib.pyplot as plt

plt.figure(figsize=(10,5))
plt.plot(x, y, label='signal')
plt.plot(x, ampl, ':', label='amplitude')
plt.xlabel('time')
plt.ylabel('value')
plt.legend()

demo plot

我已经穿上seaborn,并且正在使用他们的ticks风格来使其更漂亮。

相关问题