用scilab生成锯齿波

问题描述 投票:0回答:1

虽然我知道如何使用sawtooth(x)在Matlab中生成锯齿波,但我不知道如何在Scilab中生成锯齿波。我需要使用xcos吗?或者是其他一些方式呢?除了在SAWTOOTH_Fxcos之外,在网上找不到任何东西。

scilab
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.