Search code examples
matlabplotwaveformtriangular

Matlab: how to plot a triangular wave


So I have a Matlab function that creates a series of square impulses, then I apply a noise over them and a filter, the problem is I need to change the form of the impulses into triangular form:

x = zeros(1,1000)
x(100:200) = 1
x(400:500) = 1
x(700:800) = 1
plot(x)

Solution

  • Try to define a function that creates a triangle wave and then use it (or define it by hand everytime). Something like this should work fine:

    x = zeros(1,1000);
    tri = @(x) [(0:(floor(x/2)-1))/floor(x/2),1,((floor(x/2)-1):-1:0)/floor(x/2)];
    x(100:200) = tri(101);
    x(400:500) = tri(101);
    x(700:800) = tri(101);
    plot(x)
    

    If this is not what you was asking for, eg if you meant that you want a sawtooth wave, then you should check out the sawtooth function, try:

    x = 0:0.1:15;
    y=sawtooth(x,0.5);
    plot(x,y);
    

    However, I would encourage you to change the name of the question, which is really about what to plot and not which plot function you are supposed to use.