Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python生成PWM_Python_Numpy_Matplotlib - Fatal编程技术网

如何使用python生成PWM

如何使用python生成PWM,python,numpy,matplotlib,Python,Numpy,Matplotlib,我正在使用Python代码生成PWM信号,使用矢量化方法。但仍然面临问题。有人能帮我吗 import numpy as np import matplotlib.pyplot as plt percent=input('Enter the percentage:'); TimePeriod=input('Enter the time period:'); Cycles=input('Enter the number of cycles:'); y=1; x=np.linspace(0,Cy

我正在使用Python代码生成PWM信号,使用矢量化方法。但仍然面临问题。有人能帮我吗

import numpy as np
import matplotlib.pyplot as plt

percent=input('Enter the percentage:');
TimePeriod=input('Enter the time period:');
Cycles=input('Enter the number of cycles:');

y=1;

x=np.linspace(0,Cycles*TimePeriod,0.01);
t=(percent/100)*TimePeriod;

for n in range(0,Cycles):
    y[(n*TimePeriod < x) & (x < n*TimePeriod+t)] = 1;
    y[(n*TimePeriod+t < x)& (x < (n+1)*TimePeriod)] = 0;

    plt.plot(y)
    plt.grid()
end
将numpy导入为np
将matplotlib.pyplot作为plt导入
百分比=输入('输入百分比:');
TimePeriod=输入('输入时间段:');
周期=输入('输入周期数:');
y=1;
x=np.linspace(0,周期*时间段,0.01);
t=(百分比/100)*时间段;
对于范围内的n(0,周期):
y[(n*时间段
最大的问题是,除非y是一个向量,否则不能分配给y[index],但您将其设置为一个数字。现在有很多方法可以做周期赋值,我个人喜欢使用模运算

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

percent=float(raw_input('on percentage:'))
TimePeriod=float(raw_input('time period:'))
Cycles=int(raw_input('number of cycles:'))
dt=0.01  # 0.01 appears to be your time resolution

x=np.arange(0,Cycles*TimePeriod,dt);  #linspace's third argument is number of samples, not step

y=np.zeros_like(x)   # makes array of zeros of the same length as x
npts=TimePeriod/dt

i=0
while i*dt< Cycles*TimePeriod:
    if (i % npts)/npts < percent/100.0:
        y[i]=1
    i=i+1

plt.plot(x,y,'.-')
plt.ylim([-.1,1.1])
将numpy导入为np
将matplotlib.pyplot作为plt导入
%matplotlib内联
百分比=浮动(原始输入('on percentage:'))
时间段=浮动(原始输入('时间段:'))
Cycles=int(原始输入('周期数:'))
dt=0.01#0.01似乎是您的时间分辨率
x=np.arange(0,周期*时间段,dt)#linspace的第三个参数是样本数量,而不是步骤
y=np。类似于(x)的零构成与x长度相同的零数组
npts=时间周期/dt
i=0
当i*dt<周期*时间段:
如果(i%NPT)/NPT<百分比/100.0:
y[i]=1
i=i+1
plt.绘图(x,y'.-')
plt.ylim([-.1,1.1])

最大的问题是,除非y是向量,否则不能分配给y[index],但您将其设置为数字。现在有很多方法可以做周期赋值,我个人喜欢使用模运算

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

percent=float(raw_input('on percentage:'))
TimePeriod=float(raw_input('time period:'))
Cycles=int(raw_input('number of cycles:'))
dt=0.01  # 0.01 appears to be your time resolution

x=np.arange(0,Cycles*TimePeriod,dt);  #linspace's third argument is number of samples, not step

y=np.zeros_like(x)   # makes array of zeros of the same length as x
npts=TimePeriod/dt

i=0
while i*dt< Cycles*TimePeriod:
    if (i % npts)/npts < percent/100.0:
        y[i]=1
    i=i+1

plt.plot(x,y,'.-')
plt.ylim([-.1,1.1])
将numpy导入为np
将matplotlib.pyplot作为plt导入
%matplotlib内联
百分比=浮动(原始输入('on percentage:'))
时间段=浮动(原始输入('时间段:'))
Cycles=int(原始输入('周期数:'))
dt=0.01#0.01似乎是您的时间分辨率
x=np.arange(0,周期*时间段,dt)#linspace的第三个参数是样本数量,而不是步骤
y=np。类似于(x)的零构成与x长度相同的零数组
npts=时间周期/dt
i=0
当i*dt<周期*时间段:
如果(i%NPT)/NPT<百分比/100.0:
y[i]=1
i=i+1
plt.绘图(x,y'.-')
plt.ylim([-.1,1.1])

矢量化解决方案:

percent=30.0
TimePeriod=1.0
Cycles=10
dt=0.01 

t=np.arange(0,Cycles*TimePeriod,dt); 
pwm= t%TimePeriod<TimePeriod*percent/100 
plot(t,pwm)
百分比=30.0
时间段=1.0
周期=10
dt=0.01
t=np.arange(0,周期*时间段,dt);

pwm=t%时间段矢量化解决方案:

percent=30.0
TimePeriod=1.0
Cycles=10
dt=0.01 

t=np.arange(0,Cycles*TimePeriod,dt); 
pwm= t%TimePeriod<TimePeriod*percent/100 
plot(t,pwm)
百分比=30.0
时间段=1.0
周期=10
dt=0.01
t=np.arange(0,周期*时间段,dt);

pwm=t%timeperiod这看起来令人印象深刻,但在图表上给了我一条平线。你能解释一下它是怎么工作的吗?我不知道为什么。t%t是介于0和1之间的saw信号。
pwm
是一个布尔数组,它在30%的时间内为真。Ty,明白了。我使用的是Python2.7,忘记了“来自未来的导入除法”这一行,所以我得到了整数除法。出色的解决方案。@B.M:我在图形上得到了一条平线,因为pwm显示为false,而且%matplotlib inline给出了语法错误。我使用的是Python 2.7.6。我编辑了我的文章,将百分比和时间段定义为float,以便与Python 2兼容。很抱歉matplotlib可以替换为来自pylab import*这看起来令人印象深刻,但在图表上给了我一条平面线。你能解释一下它是怎么工作的吗?我不知道为什么。t%t是介于0和1之间的saw信号。
pwm
是一个布尔数组,它在30%的时间内为真。Ty,明白了。我使用的是Python2.7,忘记了“来自未来的导入除法”这一行,所以我得到了整数除法。出色的解决方案。@B.M:我在图形上得到了一条平线,因为pwm显示为false,而且%matplotlib inline给出了语法错误。我使用的是Python 2.7.6。我编辑了我的文章,将百分比和时间段定义为float,以便与Python 2兼容。很抱歉matplotlib可以由from pylab import替换*