[英]How to plot sine wave in Python with sudden amplitude change?
發布時間:2020 年 7 月 4 日
我想知道是否有人知道如何 plot 一個正弦波,假設幅度為 0.1 作為開始,然后像往常一樣繼續。 直到某一時刻,幅度變為 1.0。 就像幅度變化的突然激增。 就像我是一個穩定的振盪系統,並且在某一時刻變得不穩定。 我期待的 plot 如下:
問候, 阿尼斯
更新進度:18/4/2020
import numpy as np
import matplotlib.pyplot as plotter
from scipy import signal
# How many time points are needed i,e., Sampling Frequency
samplingFrequency = 1500
# At what intervals time points are sampled
samplingInterval = 1 / samplingFrequency;
# Begin time period of the signals
beginTime = 0;
# End time period of the signals
endTime = 0.3;
# Frequency of the signals
signal1Frequency = 50;
#Time points
time = np.arange(beginTime, endTime, samplingInterval);
phase = 180
pi = np.pi
phi = phase*pi/180
# Create two waves- sine and square
amplitude1 = np.sin(2*np.pi*signal1Frequency*time)
amplitude2 = signal.square(2 * np.pi * 50 * time+ phi )
figure, axis = plotter.subplots(1, 1)
plotter.subplots_adjust(hspace=1)
if (time >0.2):
amplitude = 3*amplitude1
plotter.plot(time, amplitude)
plotter.title('test')
plotter.show()
以上是我目前正在處理的代碼。 由於模棱兩可,它不斷彈出錯誤。 要求我使用 a.all() 和 a.any() function 來解決它。 當我這樣做時,我沒有得到我所期望的激增點。 那么有什么想法嗎? 我使用時間作為 x 軸而不是索引。 我正在使用 numoy sine 而不是數學庫。 這是因為當我為下面提出的代碼嘗試 FFT 時,我沒有得到 50 Hz,它更多的是 30 或 10 Hz,這是可以理解的,因為沒有設置頻率並且它取決於由正弦曲線本身。
問候, 阿尼斯
如果幅度發生變化,就像現實中的正弦波一樣。 您在變化之前和之后連接振幅的點。 這與繪制正弦波本身沒有什么不同。 它看起來如何,例如銳利的邊緣,僅取決於變化發生的那一刻。
這是計算點和繪制它們之間的線的一種非常基本的方法。
在 x=5 時,幅度加倍。
import matplotlib.pyplot as plt
import math
def y_func(x):
return math.sin(x)
x_values = []
y_values = []
x = 0
amplitude = 1
while x < 5:
x_values.append(x)
y_values.append(amplitude * y_func(x))
x += 0.1
amplitude = 2
while x < 10:
x_values.append(x)
y_values.append(amplitude * y_func(x))
x += 0.1
plt.plot(x_values, y_values)
plt.title('test')
plt.show()
在對其進行更多結構化並將所需的幅度變化放在列表中之后,很容易產生漂亮的尖峰。
import matplotlib.pyplot as plt
import math
# ------------------------------------------------------------------------
def get_amplitude(x):
for amplitude_change in amplitude_changes:
if x >= amplitude_change['x']:
amplitude = amplitude_change['amplitude']
return amplitude
# --------------------------------------------------------------------------
def y_func(x, amplitude):
return amplitude * math.sin(x)
# --------------------------------------------------------------------------
amplitude_changes = [
{'x': -1, 'amplitude': 1},
{'x': 6.5, 'amplitude': 2.2},
{'x': 6.7, 'amplitude': 1},
{'x': 9.1, 'amplitude': 0.5},
{'x': 9.2, 'amplitude': 1.2},
{'x': 9.4, 'amplitude': 1},
]
x_values = []
y_values = []
x = 0
max_x = 10
step = 0.1
while x <= max_x:
x_values.append(x)
amplitude = get_amplitude(x)
y_values.append(y_func(x, amplitude))
x += step
plt.plot(x_values, y_values)
plt.title('test')
plt.show()
您可以 plot 分段sin
function 其中第二部分定義發生的浪涌,您可以在那里更改幅度。
例如:
import numpy as np
import matplotlib.pyplot as plt
import math
surge_point = 50
amplitudeAfterSurge = 4
T = 50
x_normal = np.linspace(0, surge_point, 1000)
x_surge = np.linspace(surge_point, 150, 1000)
y_normal = [math.sin(2*math.pi*i/T) for i in x_normal] # first part of the function
# second part ,note `amplitudeAfterSurge` multiplying the function
y_surge = [amplitudeAfterSurge * math.sin(2*math.pi*i/T) for i in x_surge]
plt.plot(x_normal, y_normal , 'r')
plt.plot(x_surge, y_surge , 'r')
plt.show()
你會得到:
我已將代碼轉換為周期時間:
import matplotlib.pyplot as plt
import math
# ------------------------------------------------------------------------
# uses the list amplitude_changes to get the amplitude for time t
def get_amplitude(t):
for amplitude_change in amplitude_changes:
if t >= amplitude_change['t']:
amplitude = amplitude_change['amplitude']
return amplitude
# --------------------------------------------------------------------------
def y_func(time, period_time, amplitude):
return amplitude * math.sin((time / period_time) * 2 * math.pi)
# --------------------------------------------------------------------------
t_values = []
amplitude_values = []
signal1Frequency = 50
period_time = 1 / signal1Frequency
sampling_frequency = 1500
delta_t = 1 / sampling_frequency
amplitude_changes = [
{'t': 0, 'amplitude': 1},
{'t': period_time * 0.9, 'amplitude': 1.5},
{'t': period_time * 0.95, 'amplitude': 1},
{'t': period_time * 1.2, 'amplitude': 0.8},
{'t': period_time * 1.25, 'amplitude': 1},
]
max_t = period_time * 3 # plot 3 periods
t = 0
while t <= max_t:
t_values.append(t)
amplitude = get_amplitude(t)
amplitude_values.append(y_func(t, period_time, amplitude))
t += delta_t
plt.plot(t_values, amplitude_values)
plt.title(f'f = {signal1Frequency} Hz (T = {period_time}) - Sampling frequency = {sampling_frequency} Hz')
plt.show()
結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.