BigBoyB
BigBoyB

Reputation: 261

Create square/triangle/sawtooth and normal sine in matlab

I'm trying to create some sin(2x) 2000HZ, square wave 1000hz, triangle wave 1000 hz, sawtooth 1000 hz. Number of points per each graph should be 62000.

Is this a good start? For some reason y2,y3,y4 are not created..

t = 0:0.001:0.62; % Sampling frequency 6.2kHz
y1=sin(2*pi*2000*t);
y2 =square(2*pi*1000*t);
y3= sawtooth(2*pi*1000*t);
y4= sawtooth(2*pi*1000*t,1/2); %triangle

Update:

t = 0:0.001:0.62; % Sampling frequency 6.2kHz

y1 = sin(2000*t);
y2 = square(1000*t);
y3 = sawtooth(1000*t);
y4= 10 * sawtooth(1000*t ,0.5) + 5;

Upvotes: 0

Views: 4440

Answers (2)

obchardon
obchardon

Reputation: 10792

Square and sawtooth functions require the Signal Processing Toolbox. So you can also create your own function:

t = 0:0.1:8*pi;

y1 = sin(t);
y2 = square(t);
y3 = sawtooth(t);

With square.m:

function y = square(x)

inp = sin(x) >= 0;
y(~inp) = -1;
y(inp) = 1;

end

With sawtooth.m

function y = sawtooth(x)

y = ((mod(x,2*pi)/(pi*2))*2)-1;

end

Result:

subplot

Upvotes: 2

user2261062
user2261062

Reputation:

square and sawtooth functions require the Signal Processing Toolbox.

The two first lines generate a sinusoidal signal correctly.

Upvotes: 0

Related Questions