sawtooth.m

来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 45 行

M
45
字号
function y = sawtooth(t,width)
%SAWTOOTH Sawtooth and triangle wave generation.
%   SAWTOOTH(T) generates a sawtooth wave with period 2*pi for the
%   elements of time vector T.  SAWTOOTH(T) is like SIN(T), only
%   it creates a sawtooth wave with peaks of +1 to -1 instead of
%   a sine wave.
%
%   SAWTOOTH(T,WIDTH) generates a modified triangle wave where WIDTH, a
%   scalar parameter between 0 and 1, determines the fraction between 0
%   and 2*pi at which the maximum occurs. The function increases from -1
%   to 1 on the interval 0 to WIDTH*2*pi, then decreases linearly from 1
%   back to -1 on the interval WIDTH*2*pi to 2*pi. Thus WIDTH = .5 gives
%   you a triangle wave, symmetric about time instant pi with peak amplitude
%   of one.  SAWTOOTH(T,1) is equivalent to SAWTOOTH(T).
%
%   Caution: this function is inaccurate for huge numerical inputs
%
%   See also SQUARE, SIN, COS

%   Author(s): T. Krauss, 4/19/93, revised
%   Copyright (c) 1988-98 by The MathWorks, Inc.
%       $Revision: 1.10 $  $Date: 1997/11/26 20:13:35 $

if nargin == 1,
    width = 1;
end
if (width > 1) | (width < 0),
    error('WIDTH parameter must be between 0 and 1.')
end

rt = rem(t,2*pi)*(1/2/pi);
i1 = find( ((rt<width)&(rt>=0)) | ((rt<width-1)&(rt<0)) );
i2 = 1:length(t(:));
i2(i1) = [];      % complement set
y = zeros(size(t));
y(i1) = ( ((t(i1)<0)&(rt(i1)~=0)) + rt(i1) - .5*width)*2;
if (width ~= 0),
    y(i1) = y(i1)*(1/width);
end
y(i2) = ( -(t(i2)<0) - rt(i2) + 1 - .5*(1-width))*2;
if (width ~= 1),
    y(i2) = y(i2)*(1/(1-width));
end
%  y = [y; ((t < 0) + rem(t,2*pi)/2/pi - .5)*2];     % OLD METHOD

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?