# Trying to recreate sawtooth and square wave in Matlab

I've been asked to recreate the square and sawtooth wave shown below:

And I've managed to recreate the square wave form but I can't seem to get my sawtooth wave to match.

% Input Variables
L = 31; % Input Length
A = 1; % Peak Value
N = 10; %Input Period

% Define omega and time vector t
omega = 2*pi/N;
t = 0:omega:(L-1)*omega;

% X-axis lenghth determined by L-1
X = 0:L-1;

Vsaw = A*sawtooth(t);
Vsquare = A*square(t,60);

hold on;
subplot(2,1,1)
stem(X,Vsaw)
xlabel('Time');
ylabel('Amplitude');

subplot(2,1,2), stem(X,Vsquare);
xlabel('Time');
ylabel('Amplitude');


Is it an error in the code or my values as I can't get the correct number of values or correct amplitude on the saw tooth.

## 4 Answers

You need to think of it as if you were sampling a sawtooth wave. The positive peak of your sawtooth wave actually occurs between your 11th and 12th sample but your sampling rate is just too low to catch it. You need to increase your input period and length to get your positive peak to hit +1.

In your book, the period of the sawtooth is 11 samples, not 10.

A simple way to obtain the first figure of the book is use this code:

Vsaw = A*[(-1:.2:1) (-1:.2:1) (-1:.2:.6)];


You can use the following:

 Vsaw=linspace(-A,A,11);
Vsaw=[Vsaw Vsaw Vsaw];