简体   繁体   English

使用Python使用正弦波和PWM信号生成脉冲幅度调制

[英]Generating Pulse Amplitude Modulation using sine wave and PWM signal using python

I am using the below codes so as to generate a Pulse Amplitude Modulation signal by using the Boolean operation between sine wave and Pulse Width Modulation(PWM) signal.I am using the vectorisation method so as to get zero values where the PWM signal is low(zero or false) and sine wave where the PWM values as high (True or one). 我正在使用以下代码,以通过正弦波和脉宽调制(PWM)信号之间的布尔运算生成脉冲幅度调制信号。我正在使用矢量化方法,以便在PWM信号为低电平时获得零值(零或假)和正弦波,其中PWM值较高(真或一)。 Please refer the below screen shot for the required output.In addition to this how do you automate the PAM wave generation as I am facing problem with spacing of x values? 请参考下面的屏幕截图以获取所需的输出。此外,当我面临x值间距的问题时,如何自动生成PAM波?

        import numpy as np
        import matplotlib.pyplot as plt
        from pylab import *

        percent=50.0
        TimePeriod=10.0 #Frozen Value Do not change
        Cycles=10 #Frozen Value Do not change
        dt=0.01 #Frozen Value Do not change

        t=np.arange(0,Cycles*TimePeriod,dt); 
        pwm= t%TimePeriod<TimePeriod*percent/100

        x=np.linspace(-10,10,10000) #Frozen Value Do not change
        y=(np.sin(x))

        y[(pwm =='False')] = 0      #Vectorisation for zero values
        y[(pwm =='True')] = (y-pwm) # #Vectorisation for sine wave
        plt.plot(t,y)

        plt.ylim([-3,3])
        plt.grid()
        plt.show() 

在此处输入图片说明

When removing the line y[(pwm =='True')] = (y-pwm) (which I don't understand) and not comparing to strings, you would get the following, which looks pretty much like the desired plot. 当删除线y[(pwm =='True')] = (y-pwm) (我不理解)并且不与字符串进行比较时,您将获得以下内容,该内容看起来非常类似于所需的绘图。

import numpy as np
import matplotlib.pyplot as plt

percent=40.0
TimePeriod=10.0
Cycles=30
dt=0.01

t=np.arange(0,Cycles*TimePeriod,dt); 
pwm= (t%TimePeriod) < (TimePeriod*percent/100)

x=np.linspace(-10,10,len(pwm))
y=(np.sin(x))

y[pwm == 0] = 0

plt.plot(t,y)

plt.ylim([-3,3])
plt.grid()
plt.show() 

在此处输入图片说明

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

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