1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with plotting triangular wave

  1. Dec 30, 2012 #1
    1.Use MATLAB to demonstrate how the series converges to the triangular wave.

    2.Generate a plot(properly labelled) with 6, 10 and 30 terms for a value of T = 2.

    2. *A triangular wave with period T may be written as: 1/(2n+1)^2 * cos((2n+1)*w0*t) (this is a series, n starts at 0 and goes on until infinity). where w0 = 2pi/T. This wave form is sampled, with a sampling time of TS = T/200, to yield the sampled signal x(n).

    x=((2*n+1).^-2).*(cos((2*n+1)*w*Ts)); plot(x)

    When i plot this it doesn't give a triangular wave. I must have done something wrong or missed a detail. Any help would be appreciated.
    Last edited by a moderator: Dec 31, 2012
  2. jcsd
  3. Dec 30, 2012 #2


    User Avatar

    Staff: Mentor

    I'm not very familiar with the use of MATLAB, but I can see by the problem statement that you need to plot a sampled waveform. That is, you'll be plotting points for sample times t = j*Ts, where j = 0:N. (I just introduced j and N as suggested variable names; pick anything you like). Maybe choose a value of N so that you "sample" a couple of periods of the waveform.

    So you have two index variables to worry about; n handles the series terms, while j handles the sampling.

    When you write the instruction "plot(x)", how is MATLAB to know that you want to sample at times t = 0, Ts, 2*Ts, 3*Ts,.... and so on? And I'm not seeing any mechanism to handle the summation of the series terms, or to control the number of series terms (6, 10, or 30) for different plots.
  4. Dec 30, 2012 #3


    User Avatar
    Gold Member

    "...the sampled signal x(n)" --this phrase might be a bit confusing. "n" is the series-sum index. x(t) might be clearer, where t is a sampled time vector. xn might be better for referring to the nth vector term of the series. More on that below.

    "...x=((2*n+1).^-2).*(cos((2*n+1)*w*Ts))" --The heart of your problem is in this statement (and what does not surround this statement). As you may already know, MATLAB can operate on vectors (1-dimensional matricies) and scalars (variable or constant), as well as larger-dimensioned matricies. The initial problem specified x, the output vector, as a series-sum. As gneill points out, you have no summing mechanism (for loop) specified the summing of the xn vector terms. "n" is what would control the number of terms that get summed.

    Use of ".^" and ".*" --you don't need the dot "." (read up on its special meaning) because "(2*n+1)" is a scalar (a constant for a given series term) and therefore so would be "(2*n+1)^2".

    "cos((2*n+1)*w*Ts)" --is a scalar and needs to be a vector. "n" is the index variable for a given series term, and "w" is a constant, so that leaves "Ts" for you to scrutinize. You have specified Ts as a constant (2/200). Look again at the original problem. They differentiate "T" (the period) from "t" (the independent variable time). The way MATLAB works in this application (graphing a function of time), is to use a sampled time vector. You need to create one using Ts as your sample-time increment. The scalar, (2*n+1)*w, operates on this time vector (essentially converting it to a phase vector, then cos() operates on the phase vector. Multiplying this by (2*n+1)^2 results in an xn vector term. You must accumulate all these xn terms to get your final result.

    "n=0:9999" --you don't need to make n a vector. It is a loop index. You will need to specify the maximum number of times to loop, say N=9999, if you want to loop that many times.
    I imagine you need to get something working before you run with N=6,10 and 30 terms.

    "plot(x)" --this will generate a raw plot of a vector x. The graph will not have much meaning (no timescale, no axis lables, etc.), just x as a function of the running index of x.
  5. Dec 31, 2012 #4

    I've made these new modifications but i know something is definitely wrong and I'm not sure if I'm plotting the right thing.

    s = 0;
    for n = 0 : 8
    s = s + ((2*n+1).^-2).*(cos((2*n+1)*w*t));
    y(n+1) = s;

    I am also quite unsure on how to use T because in the [(2*n+1).^-2).*(cos((2*n+1)*w*t)]
    equation it is cancelled out.
  6. Dec 31, 2012 #5


    User Avatar
    Gold Member

    If you are going to use MATLAB extensively in the future, you might as well become familiar with the Mathworks website. Tons of examples, such as for plot(), review this: http://www.mathworks.com/help/matlab/ref/plot.html

    The Command Window is useful for evaluating lings like "y=[9]". That is probably not doing what it is that you think it should (its the same as saying "y=9").

    Still using ".^" and ".*" --its not hurting you but using "^" and "*" makes more sense for reasons cited in post #3.

    Still need a vector for cos() to operate on. Not sure of the reasoning behind "t=n*Ts;". Not sure you are either :wink:. To save some time, a sampled time vector, a half-second long, sampled at Ts, looks like this:
    t = 0:Ts:0.5;

    Go back to just doing plot(s) until you get a result that makes sense.

    MATLAB is good for immediately visualizing the result of each line. Omit the ";" at the end and you will see the result in the Command Window. Do this to debug a particular line that you might hold suspect.
  7. Dec 31, 2012 #6
    Using this code i do get a triangular wave but when i plot for n=10,30 terms the graph doesn't really change much.

    s = 0;
    for n = 0:5
    s = s + ((2*n+1)^-2)*(cos((2*n+1)*w*t));

    Is this the correct code to demonstrate how the series converges to a triangular wave? and do i just use new number of n terms to complete question 2?

    I really do appreciate the help
  8. Jan 1, 2013 #7


    User Avatar
    Gold Member

    Glad you got it working!

    You should see somewhat less rounding of the peak for N=30, compared with 10.

    You might want to plot the 3 different results on the same plot (hmm, how might you do that?), add a title, proper axis labels, maybe a legend. Again, lot's of examples for you to follow exist.
  9. Jan 1, 2013 #8
    Cool i managed to get the plots working now. Thanks for your help.

    I'm struggling to do the next part of the question. I don't know how to input the signal into an LTI equation.

    System C: y(n) = (4x(n)-12x(n-1)+17x(n-2)-8x(n-3))/3
    I managed to find the equation of the frequency response (H(f)) of this system. Which can be used to find the Amplitude and phase response function in matlab using abs(H) and -angle(H).
    H(f)=((4 - 12exp(-2pjfTs) + 17exp(-4pjfTs) - 8exp(-6pjfTs))/3

    The question is: 'Now suppose that this signal (my triangular wave) (with T =1 second) is input to the combined System C, use MATLAB to determine the output?'

    I'm not sure how i input ((2*n+1)^-2)*(cos((2*n+1)*w*t)) into system C.
  10. Jan 1, 2013 #9
    Ts=0.001;f=0:500; for System C
  11. Jan 1, 2013 #10


    User Avatar
    Gold Member

    "f=0:500" --what did you mean by this? Frequency (fundamental) of the triangle wave?

    You have a couple options.
    1. Do it directly by implementing the difference equation in another for loop. You'll be constructing vector y one element at a time. If your choice of range of index, n, is 1:length(s), (your vector s is x in the difference equation--rename one or the other to be consistent), then you'll note that the x(n-3) term will cause a problem when n=1,2,3. Simplest workaround is to not start your loop index at 1 (or 2 or 3).

    2. Use a built-in feature of MATLAB, namely filter(). You'll need to know how to convert your difference equation into filter coefficients in order to use filter(). If you are familiar with the z-transform, it is pretty simple. You'll need to read up on this a bit, I'm guessing.
    Last edited: Jan 1, 2013
  12. Jan 2, 2013 #11
    f=0:500 is to say our input signals have no frequency above 500.

    My main problem with this question is making a difference equation.

    In the previous question we had system A (x(n) – 3x (n-1)+4x (n-2)-2x(n-3))/3. and system B (2x(n)+x(n-2))/2. System A was the input to system B to form a cascaded system C. My method of cascading was to let system A be equal to x(n) to represent the input. So my working was something like this y(n)= 2(2x(n)-6x(n-1)+8x(n-2)-4x(n-3))+x(n-2)/3. That's how i ended up with system C. I'm not sure if this is right however.

    I'm not sure how to come up with a difference equation when cascading system C and the triangular wave.
  13. Jan 2, 2013 #12
    oops system A was divided by 2 and system B is divided by 3. system A (x(n) – 3x (n-1)+4x (n-2)-2x(n-3))/2. and system B (2x(n)+x(n-2))/3.
  14. Jan 2, 2013 #13


    User Avatar
    Gold Member

    Your systems, A, B, and C are already specified in difference equation form.
  15. Jan 2, 2013 #14
    Oh right, I'm new to the terminology. This is what i've done and it seems to work

    for n=4:length(s)
    figure (2)
    title('System C')

    Do you know if i cascaded system A and B together correctly? And do you know the method to write an expression for the impulse response function of the systems?

  16. Jan 2, 2013 #15


    User Avatar
    Gold Member

    Does not look right. I cannot understand your method.
    What textbook are you using? You must have been exposed to some examples, at least, yes?
  17. Jan 2, 2013 #16
    We weren't given any textbooks, though I did look at a couple of books and neither gave examples specific to these questions. We were given examples on how to find the frequency and amplitude responses but no examples on impulse response or on how to cascade a system.

    And because it's the holidays we don't have contact with our teacher.
  18. Jan 2, 2013 #17


    User Avatar
    Gold Member

    I have more time now and maybe we can clear a few things up. Let's go in reverse order.

    1. Impulse responses of the systems. I think you were trying to indicate for A and B:
    yA(n) = [x(n) – 3x(n-1) + 4x(n-2) - 2x(n-3)]/2
    yB(n) = [2x(n) + x(n-2)]/3.

    To find the impulse response of A, apply an impulse, δ(n), as the input, x(n). The output is the impulse response, hA(n).

    hA(n) = [δ(n) – 3δ(n-1) + 4δ(n-2) - 2δ(n-3)]/2

    2. Cascaded system. Your cascaded system looks like this:

    x(n) --->[ hA(n) ] --- w(n) ---> [ hB(n) ] ----> y(n)

    Your difference equations are:

    w(n) = [x(n) – 3x(n-1) + 4x(n-2) - 2x(n-3)]/2
    y(n) = [2w(n) + w(n-2)]/3

    You can construct the overall difference equation a couple of ways:
    a. substitution method
    b. discrete convolution method
    c. z-transform method

    At this point, I don't know what you are familiar with. Methods b and c are tedious to explain. You need to get a book or do some web-based research on these topics. Method a is fairly straightforward. Substitute w(n) into y(n), noting that:

    w(n-2) = [x(n-2) – 3x(n-2 - 1) + 4x(n-2 - 2) - 2x(n-2 - 3)]/2
  19. Jan 3, 2013 #18
    I replaced x with a delta sign for the impulse response inititally but i thought it was wrong as it seemed too simple.

    So now by applying substitution
    System C = y(n) = [x(n)-3x(n-1)+4.5x(n-2)-3.5x(n-3)+2x(n-4)-x(n-5)]/3

    When i input my triangular wave into this system i get a different type of output.

    s = 0;
    maxTerms = 6; % also use 10 and 30
    for n = 0 : maxTerms - 1
    s = s + ((2*n+1)^-2) * (cos((2*n+1)*w*t));
    % Make wave start at 0
    s = s - s(1);
    for n=6:length(s)
    figure (2)
  20. Jan 3, 2013 #19


    User Avatar
    Gold Member

    Well if it was not different, that would indicate a problem :smile:.

    What did you get?

    Other comments:

    You said in post #9 that Ts was 0.001. You are showing 2/200 = 0.01.

    "subplot(311)" --you probably meant subplot(3,1,1) for the first plot, assuming you are planning to display 3 plots on 1 figure.
  21. Jan 3, 2013 #20
    I just copied a part of the code, the reason i used subplot(311) was to have a plot each for when there are 6, 10 and 30 n terms.

    I'm not sure what the signal that i got is called. I've never seen that type of signal before.

    The Ts=0.001 related to a previous question i got it muddled up. T should = 1 for y.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Help with plotting triangular wave
  1. Python Help in Plotting (Replies: 14)