简体   繁体   English

如何在 Python 中的 plot 正弦波具有突然的幅度变化?

[英]How to plot sine wave in Python with sudden amplitude change?

Posted: 7/4/2020发布时间:2020 年 7 月 4 日

I was wondering if anyone knows how to plot a sine wave with let's say amplitude of 0.1 as a start and then continuing on as usual.我想知道是否有人知道如何 plot 一个正弦波,假设幅度为 0.1 作为开始,然后像往常一样继续。 Until at one point, the amplitude change to 1.0.直到某一时刻,幅度变为 1.0。 Like a sudden surge of change in amplitude.就像幅度变化的突然激增。 It's like I was an oscillatory system that was stable, and becoming unstable at one point.就像我是一个稳定的振荡系统,并且在某一时刻变得不稳定。 The plot that I am expecting is as follow:我期待的 plot 如下:

故障电流模型

Regards, Anis问候, 阿尼斯

Updated progress: 18/4/2020更新进度: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()

Above is the code that I am currently working on.以上是我目前正在处理的代码。 It keeps on popping an error to due to ambiguity.由于模棱两可,它不断弹出错误。 Requesting me to use a.all() and a.any() function to solve it.要求我使用 a.all() 和 a.any() function 来解决它。 When I did do so, I am not getting the surge point that I am expecting.当我这样做时,我没有得到我所期望的激增点。 So any ideas on it?那么有什么想法吗? I am using time as x axis instead of indexing.我使用时间作为 x 轴而不是索引。 And I am using numoy sine instead of math library.我正在使用 numoy sine 而不是数学库。 This is because when I tried FFT for code proposed below, I am not getting a 50 Hz, it was more of 30 or 10 Hz, and that is understandable given that the frequency was not set and it depends on the periodic cycle created by the sinusoid itself.这是因为当我为下面提出的代码尝试 FFT 时,我没有得到 50 Hz,它更多的是 30 或 10 Hz,这是可以理解的,因为没有设置频率并且它取决于由正弦曲线本身。

Regards, Anis问候, 阿尼斯

Just like a sine wave in reality if the amplitude changes.如果幅度发生变化,就像现实中的正弦波一样。 You connect the dots of the amplitude just before and just after the change.您在变化之前和之后连接振幅的点。 It's not different from plotting the sine wave itself.这与绘制正弦波本身没有什么不同。 How it looks, sharps edges for example, depends only of the moment the change happens.它看起来如何,例如锐利的边缘,仅取决于变化发生的那一刻。

This is a very basic way of calculating the points and plotting the lines between them.这是计算点和绘制它们之间的线的一种非常基本的方法。

At x=5 I double the amplitude.在 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()

在此处输入图像描述

After structuring it some more and putting the desired amplitude changes in a list, it's easy to produces nice spikes.在对其进行更多结构化并将所需的幅度变化放在列表中之后,很容易产生漂亮的尖峰。

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

在此处输入图像描述

You could plot a piece-wise sin function where the second part defines the surge happening and you can change the amplitude there.您可以 plot 分段sin function 其中第二部分定义发生的浪涌,您可以在那里更改幅度。

For instance:例如:

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

And you will get:你会得到:

分段函数

I have converted the code to period time:我已将代码转换为周期时间:

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

Result结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM