【问题标题】:How to print a amplitude modulated signal using python如何使用python打印调幅信号
【发布时间】:2019-11-08 11:09:19
【问题描述】:

我有这个程序的 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 打印图形。图表必须看起来像您可以在图像上看到的图表(mathlab 示例)。

【问题讨论】:

    标签: python modulation


    【解决方案1】:

    忽略获取输入的所有麻烦(根据其他约束条件会有很大差异),生成调幅信号的代码应该很简单。

    我首先引入 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()
    

    我加入了 seaborn,并正在使用他们的 ticks 风格让它更漂亮一些。

    【讨论】:

    • 但是如何指定调制信号的长度和绘制图形的起点?
    • 只是在代码的顶部块中玩弄数字,它故意是一小段代码,所以很容易试验。如果你刚开始,Jupyter 笔记本非常适合玩这些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2020-04-18
    • 2015-01-13
    相关资源
    最近更新 更多