簡體   English   中英

如何使用python打印調幅信號

[英]How to print a amplitude modulated signal using python

我有這個程序的 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 打印圖形。 圖形必須看起來像您可以在圖像上看到的圖形(數學實驗室示例)。

忽略對獲取輸入的所有擺弄(這將根據其他約束而有很大差異),生成調幅信號的代碼應該很簡單。

我首先引入 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風格使它更漂亮一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM