在Scilab中生成锯齿波

时间:2019-03-21 09:34:22

标签: scilab

虽然我知道如何使用<targetPath>在Matlab中生成锯齿波,但是我不知道如何在Scilab中生成锯齿波。我是否需要为此使用sawtooth(x)?还是其他方式?除了xcos中的SAWTOOTH_F之外,找不到其他在线内容。

1 个答案:

答案 0 :(得分:0)

请在下面找到产生锯齿信号的函数

  function y = sawtooth(t,percent)
  //SAWTOOTH Sawtooth and triangle wave generation.
  //   sawtooth(t) generates a sawtooth wave between -1 and +1 with period
  //   2*%pi for the time discretization in t such that for t=0 y=-1.

  //   The percent argument gives the raise time in  percentage of the
  //   period. The signal raises linearly from the beginning of each period
  //   up to that point and then decreases linearly up to end of the period.  
  //   If percent is omitted it assumed to be equal to 1.
    if typeof(t)<>"constant"|~isreal(t)|and(size(t)>1) then
      error(msprintf(_("%s: Wrong type for argument #%d: Real vector expected.\n"),"sawtooth",1))
    end

    if argn(2)==1 then 
      percent=1;
    else
      if typeof(percent)<>"constant"|~isreal(percent)|or(size(percent)<>1) then
        error(msprintf(_("%s: Wrong type for argument #%d: Real scalar expected.\n"),"sawtooth",2))
      end
      if percent > 1|percent < 0 then
        error(msprintf(_("%s: Wrong value for input argument #%d: Must be in [%d %d].\n"),"sawtooth",2,0,1))
      end
    end

    //shift t to make it positive and have y=-1 for t=0
    mnt=min(t)
    if mnt<0 then
      t=t+ceil(abs(mnt)/(2*%pi))*(2*%pi)
    end

    //periods indicator
    pind=modulo(t,2*%pi)/(2*%pi);

    //set the increasing parts
    //the indices of the increasing signal parts
    iinc=find(pind<=percent);
    if iinc<>[] then
      //set the increasing parts
      y(iinc)=2*pind(iinc)-percent;
      if percent<>0 then y(iinc)=y(iinc)/percent;end
    end
    //set the decreasing parts
    //the indices of the decreasing signal parts
    idec=1:size(t,"*");
    idec(iinc)=[];      
    if idec<>[] then
      y(idec)=-2*pind(idec)+1+percent;
      if percent<>1 then y(idec)=y(idec)/(1-percent);end
    end
    //special case 
    if percent==0 then
      y(pind==0)=1;
    end
  endfunction