Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How To Create A Periodic Triangle Function?

Asked by Stefan

Stefan (view profile)

on 23 Nov 2012

I have the task of creating a periodic triangular function that I then must calculate the Fourier transform with fft and plot the amplitude spectrum.

The curve should be symmetrical with respect to the origin in 1024 points. It should look like:

[0 1 2 3 4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 …]

I must also be able to test with different time periods and use more points.

1 Comment

Star Strider

Star Strider (view profile)

on 23 Nov 2012

Does ‘symmetrical with respect to the origin’ mean that it's an even or odd function?


Stefan (view profile)


No products are associated with this question.

3 Answers

Answer by Azzi Abdelmalek

Azzi Abdelmalek (view profile)

on 23 Nov 2012
w=4; % signal width
Amp=4; % signal amplitude
t=[t1 t2];
y1=Amp-Amp*abs(tt)/w ;
y=[y1 -y1]

for the fft you need just one period


Azzi Abdelmalek

Azzi Abdelmalek (view profile)

Answer by Matt J

Matt J (view profile)

on 23 Nov 2012
 f=@(x) mod(x-1,16)+1;
 data=[0 1 2 3 4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1];


Matt J

Matt J (view profile)

Answer by Image Analyst

Image Analyst (view profile)

on 23 Nov 2012
Edited by Image Analyst

Image Analyst (view profile)

on 23 Nov 2012

You can try wavegen() here: (Thanks to Walter for the link. It also requires triangle there).

Or you can try my demo. It's longer than the others' code because it defines parameters to make it flexible and has code for making fancy plots.

clc;	% Clear command window.
workspace;	% Make sure the workspace panel is showing.
fontSize = 15;
% Define some parameters that define the triangle wave.
elementsPerHalfPeriod = 30; % Number of elements in each rising or falling section.
amplitude = 5; % Peak-to-peak amplitude.
verticalOffset = -2; % Also acts as a phase shift.
numberOfPeriods = 4; % How many replicates of the triangle you want.
% Construct one cycle, up and down.
risingSignal = linspace(0, amplitude, elementsPerHalfPeriod);
fallingSignal = linspace(amplitude, 0, elementsPerHalfPeriod);
% Combine rising and falling sections into one single triangle.
oneCycle = [risingSignal, fallingSignal(2:end-1)] + verticalOffset;
x = 0 : length(oneCycle)-1;
% Now plot the triangle.
subplot(2, 1, 1);
plot(x, oneCycle, 'bo-');
grid on;
title('One Cycle of the Triangle', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') 
% Now replicate this cycle several (numberOfPeriods) times.
waveform = repmat(oneCycle, [1 numberOfPeriods]);
x = 0 : length(waveform)-1;
% Now plot the triangle wave.
subplot(2, 1, 2);
plot(x, waveform, 'bo-');
grid on;
title('Several Cycles of the Triangle', 'FontSize', fontSize);


Image Analyst

Image Analyst (view profile)

Contact us