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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Stefan
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.

*No products are associated with this question.*

Answer by Azzi Abdelmalek
on 23 Nov 2012

w=4; % signal width Amp=4; % signal amplitude tt=-w:w t1=-2*w:0; t2=0:2*w; t=[t1 t2]; y1=Amp-Amp*abs(tt)/w ; y=[y1 -y1] plot(t,y)

for the fft you need just one period

Answer by Matt J
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];

signal=data(f(1:1024));

Answer by Image Analyst
on 23 Nov 2012

Edited by Image Analyst
on 23 Nov 2012

You can try wavegen() here: http://daimi.au.dk/~pmn/Matlab/dochome/toolbox/commtbx (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);

## 1 Comment

## Star Strider (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54626#comment_112984

Does ‘symmetrical with respect to the origin’ mean that it's an

evenoroddfunction?