如何在Python中绘制振幅突然变化的正弦波?

2024-10-03 13:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

发布日期:2020年7月4日

我想知道是否有人知道如何绘制一个正弦波,比如说振幅为0.1作为开始,然后像往常一样继续。直到某一点,振幅变为1.0。就像振幅的突然变化。就像我是一个稳定的振荡系统,在某一点上变得不稳定。我期待的情节如下:

fault current model

问候,, 茴香

更新进度: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()

以上是我目前正在编写的代码。由于模棱两可,它不断地弹出一个错误。要求我使用.all()和.any()函数来解决它。当我这么做的时候,我并没有得到我期望的喘振点。有什么想法吗?我使用时间作为x轴,而不是索引。我用numoysine代替了数学库。这是因为当我尝试对下面提出的代码进行FFT时,我得到的不是50 Hz,而是30或10 Hz,这是可以理解的,因为没有设置频率,它取决于正弦本身创建的周期周期

问候,, 茴香


Tags: oftheimportsignaltimeasnppi
3条回答

您可以绘制一个分段sin函数,其中第二部分定义发生的喘振,您可以在此处更改振幅

例如:

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()

您将获得:

piecewisesinfunction

我已将代码转换为时段时间:

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()

结果

enter image description here

就像一个正弦波在现实中,如果振幅变化。你在改变之前和之后连接振幅的点。这和绘制正弦波本身没有什么不同。它的外观,例如锐利的边缘,只取决于变化发生的时刻

这是计算点和绘制点与点之间的线的一种非常基本的方法

当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()

enter image description here

在对其进行进一步构造并将所需的振幅变化放入列表后,很容易产生漂亮的尖峰

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()

enter image description here

相关问题 更多 >