0
votes

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). 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?

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

enter image description here

1

1 Answers

1
votes

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.

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

enter image description here