如何使用python生成PWM

时间:2016-04-19 18:17:19

标签: 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,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

2 个答案:

答案 0 :(得分:1)

最大的问题是你不能分配给y [index],除非y是一个向量,但是你把它作为一个数字。现在有很多方法可以进行定期分配,我个人喜欢使用// validate required fields function validateForm(evt){ if(evt.preventDefault){ evt.preventDefault(); } else { evt.returnValue = false; } formValidity = true; validateFormControls(); } function validateFormControls(){ var inputElements = document.querySelectorAll("fieldset:first-of-type input"); var errorDiv = document.getElementById("errorMessage"); var crustBoxes = document.getElementsByName("crust"); var fieldsetValidity = true; var elementCount = inputElements.length; var currentElement; try { for(var i = 0; i < elementCount; i++){ currentElement = inputElements[i]; if(currentElement.value === ""){ currentElement.style.border = "3px solid #0B907A"; currentElement.style.backgroundColor = "#FFC58A"; fieldsetValidity = false; } else { currentElement.style.border = ""; currentElement.style.backgroundColor = ""; } } currentElement.querySelectorAll("select")[0]; if (currentElement.selectedIndex === 0){ currentElement.style.border = "3px solid #0B907A"; currentElement.style.backgroundColor = "#FFC58A"; fieldsetValidity = false; } else{ currentElement.style.border = ""; currentElement.style.backgroundColor = ""; } if (!crustBoxes[0].checked && !crustBoxes[1].checked && !crustBoxes[2].checked){ crustBoxes[0].style.outline = "3px solid #0B907A"; crustBoxes[1].style.outline = "3px solid #0B907A"; crustBoxes[2].style.outline = "3px solid #0B907A"; } else { crustBoxes[0].style.outline = ""; crustBoxes[1].style.outline = ""; crustBoxes[2].style.outline = ""; } if (fieldsetValidity === false){ throw "Please complete all required fields."; } else { errorDiv.style.display = "none"; errorDiv.innerHTM = ""; } catch(msg){ errorDiv.style.display = "block"; errorDiv.innerHTML = msg; formValidity = false; } } } function orderTotal(){ var itemTotal = 5; var crusts = document.getElementsByName("crust"); var toppings = document.getElementsByName("goodnight"); var sizes = document.querySelectorAll("select")[0]; if (sizes.selectedIndex > 0) { itemTotal += ((sizes.selectedIndex * 1) * 2); } for (var i=0; i < toppings.length; i++){ if (toppings[i].checked) { itemTotal += 1; } } for (var i=0; i < crusts.length; i++){ if (crusts[i].checked) { itemTotal += (crusts[i].value * 1); } } alert ("Your order total is $" + itemTotal); document.getElementById("hidden").value = itemTotal; } modulo运算符。

%

enter image description here

答案 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)

enter image description here

高于此速度(此处为循环版本的100倍),来自numpy docs

  
      
  • 矢量化代码更简洁,更易于阅读
  •   
  • 更少的代码行通常意味着更少的错误
  •   
  • 代码更接近于标准数学符号(通常可以更容易地正确编码数学结构)
  •