Code for generating triangle wave in Matlab

When it comes to simulation of signal processing techniques, Matlab is number one choice of researchers and students alike. Though open source substitutes of Matlab are available, none has been able to come close to Matlab in ease of implementation and breadth of inbuilt algorithms. In this article, I will provide a working Matlab code for generating triangular wave. Few types of waves like square wave, sawtooth wave, triangular wave, etc. are often needed to test various signal processing techniques. To better understand the code, be ready with your Matlab to test it as we go.

General Triangular Waveform

What can be the most general form of triangular wave to suit our purpose? As per me, the image below represents the most general triangular waveform which we will need in signal processing.

triangle wave Matlab
A general triangle wave

Amplitude of the wave is represented by ‘A’. Shift ‘S’ can be positive (represents time delay) or negative (time advance). Time period ‘T’ is any integer (Matlab is discrete 🙂 ). Further, we need the wave to be generated for any number of cycles or part of it.

Matlab Code

The code for generating triangle wave in Matlab is as under:-

function [tr] = generate_triangular_wave(T,A,wave_size,shift)
% T is time period of triangular waveform
% A is amplitude of wave
% wave_size is length of wave to generate
% shift is delay or advance

tr = zeros(wave_size,1);

for n = 0:wave_size-1
    r = mod(n-shift,T);
    if r <= 25
        tr(n+1) = r*4/T;
    elseif r >=75
        tr(n+1) = -(T-r)*4/T;
    else
        tr(n+1) = (T/2-r)*4/T;
    end
        
end
tr = A*tr;

end

I have presented the code in a form of function so that it can be copied and pasted anywhere and used.

Test Examples

I will now present few examples of triangular signal generated using the above code. As images are more powerful medium than words, I will plot the generated wave. The code used for testing the function and plotting the generated triangular wave is as under:-

generate_triangular_wave(T,A,wave_size,shift);

figure;
plot(0:wave_size-1, tr);
xlabel('n');
ylabel('tr');
tx = ['Triangular wave with T=',num2str(T), ', A=', num2str(A), ' shift=', num2str(shift)];
title (tx);

One has to provide values for the variables ‘T’, ‘A’, ‘wave_size’ and ‘shift’ to use the above code for testing the function. The plots obtained for two sets of values for these variables are shown below.

Triangle wave in Matlab
Example: Triangular wave with time advance
Trinagle wave in Matlab
Example 2: Triangle with time delay

It is to be noted that when it comes to periodic waves, time advance and delay behavior is identical and one can be simulated using other one.

That’s all in this post on generating a triangle wave in Matlab. In the post, I have presented a simple Matlab code in a function form. I have also provided a code to test the function and plot the resulting wave. If you have any query, feel free to comment below. I will be more than happy to answer your queries.

Looking for other Matlab codes, please check my earlier write-ups on Matlab code for camera calibration and image diffusion.

Cheers!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments