numpy - How to generate PWM using python -


i using python codes generate pwm signal in using vectorization method.but still facing issues.could me in this.

import numpy np import matplotlib.pyplot plt  percent=input('enter percentage:'); timeperiod=input('enter time period:'); cycles=input('enter number of cycles:');  y=1;  x=np.linspace(0,cycles*timeperiod,0.01); t=(percent/100)*timeperiod;  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 

a vectorized solution :

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) 

enter image description here

above speed (100x loop version here), numpy docs :

  • vectorized code more concise , easier read
  • fewer lines of code means fewer bugs
  • the code more closely resembles standard mathematical notation (making easier, typically, correctly code mathematical constructs)

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -