# Help with plotting triangular wave

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).

3.
t=2;
Ts=t/200;
w=(2*pi)/t;
n=0:9999;
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:

gneill
Mentor
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).

3.
t=2;
Ts=t/200;
w=(2*pi)/t;
n=0:9999;
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.
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.

lewando
Homework Helper
Gold Member
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).
"...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.

t=2;
Ts=t/200;
w=(2*pi)/t;
n=0:9999;
x=((2*n+1).^-2).*(cos((2*n+1)*w*Ts)); plot(x)
"...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.

"...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.

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.

y=;
n=0;
T=2;
Ts=T/200;
t=n*Ts;
w=(2*pi)/T;
s = 0;
for n = 0 : 8
s = s + ((2*n+1).^-2).*(cos((2*n+1)*w*t));
y(n+1) = s;
end
plot(y,t)

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.

lewando
Homework Helper
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=". 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 . 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.

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.

n=0;
T=2;
Ts=T/200;
t=-T/2:Ts:T/2;
w=(2*pi)/T;
s = 0;
for n = 0:5
s = s + ((2*n+1)^-2)*(cos((2*n+1)*w*t));
end
plot(t,s)

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

lewando
Homework Helper
Gold Member

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.

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).
Y(n)=h(f)*exp(2pjnfTs)
H(f)=((4 - 12exp(-2pjfTs) + 17exp(-4pjfTs) - 8exp(-6pjfTs))/3
H=(4-12*exp(-2*pi*j*f*Ts)+17*exp(-4*pi*j*f*Ts)-8*exp(-6*pi*j*f*Ts))/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.

Ts=0.001;f=0:500; for System C

lewando
Homework Helper
Gold Member
Ts=0.001;f=0:500; for System C
"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:
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.

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.

lewando
Homework Helper
Gold Member
Your systems, A, B, and C are already specified in difference equation form.

Oh right, I'm new to the terminology. This is what i've done and it seems to work

T=1;
for n=4:length(s)
y(n)=(4*s(n)-12*s(n-1)+17*s(n-2)-8*s(n-3))/3;
end
figure (2)
plot(t,y)
title('System C')
xlabel('Time(s)')
ylabel('Amplitude')

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?

Thanks

lewando
Homework Helper
Gold Member
Do you know if i cascaded system A and B together correctly?
Does not look right. I cannot understand your method.
And do you know the method to write an expression for the impulse response function of the systems?

Thanks
What textbook are you using? You must have been exposed to some examples, at least, yes?

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.

lewando
Homework Helper
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

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

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

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.

n=0;
T=2;
Ts=T/200;
t=-T/2:Ts:T/2;
w=(2*pi)/T;
s = 0;
figure(1);
maxTerms = 6; % also use 10 and 30
for n = 0 : maxTerms - 1
s = s + ((2*n+1)^-2) * (cos((2*n+1)*w*t));
end
% Make wave start at 0
s = s - s(1);
subplot(311)
plot(t,s)
T=1;
for n=6:length(s)
y(n)=(s(n)-3*s(n-1)+4.5*s(n-2)-3.5*s(n-3)+2*s(n-4)-s(n-5))/3;
end
figure (2)
plot(t,y)

lewando
Homework Helper
Gold Member
Well if it was not different, that would indicate a problem .

What did you get?

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.

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.

lewando
Homework Helper
Gold Member

I've uploaded the figure with 3 plots each representing 6, 10 and 30 n terms. and System C with the signal.

#### Attachments

• graphs.jpg
32.4 KB · Views: 449
lewando
Homework Helper
Gold Member
Neat! --looks like you have a differentiator on you hands!

Why are your amplitudes changing on your plots of the 3 triangle waves? They should be more or less the same amplitude. Hint: re-run everything starting at 30, 10 and lastly 6.

I re-ran everything but it doesn't seem to be having any effect.

lewando
Homework Helper
Gold Member
Are you saying that on your new run, 30 gives the smallest amplitude and 6 gives the largest?