Rick T
Rick T

Reputation: 3389

creating a perodic triangle waveform with phase shift

I can create a periodic triangle waveform by changing the variable freq but how can I have the triangle waveform start at t=0 with an upward going slope like a sine wave. I was trying to figure out something like the sine wave equation Sine wave equation but basically for triangle waveforms. Does something like this exist?

where:

Text

Code below:

t=linspace(0,2*pi,1000);
freq=2; %how many in 1 sec
A = 1; % amplitude
T = 2*pi/freq; % period of the signal

% triangle
figure(1);
triangle = (mod(t * A / T, A) > 0.5).*mod(t * A / T, A) + (mod(t * A / T, A) <= 0.5).*(1 - mod(t * A / T, A));
triangle = 2*triangle - 1.5;
plot(t, triangle);
title('triangle');

What the code it looks like

PS: I'm using octave 4.0 which is like matlab.

Upvotes: 4

Views: 3013

Answers (2)

tim
tim

Reputation: 10186

Do you also know sawtooth from the signal processing toolbox? Have a look here for a good example.

--> If you want it to start downwards, just invert it. And if you want it to be symmetric around y=0, then simple substract/add an offset.

Upvotes: 2

Rick T
Rick T

Reputation: 3389

Here's the final code in case someone else needs to do this

clear all
figure(1);
freq=2
fs=1000;
t2=linspace(0,2*pi,fs);
phase_shift=pi/2; %phase shift
tri_w_phase=sawtooth(freq*t2+phase_shift,.5);
plot(t2,tri_w_phase)
title('triangle with freq edit and phase shift');

Upvotes: 2

Related Questions