How To Create A Periodic Triangle Function?

9 次查看(过去 30 天)
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 个评论
Star Strider
Star Strider 2012-11-23
Does ‘symmetrical with respect to the origin’ mean that it's an even or odd function?

请先登录,再进行评论。

回答(3 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-11-23
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

Matt J
Matt J 2012-11-23
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));

Image Analyst
Image Analyst 2012-11-23
编辑:Image Analyst 2012-11-23
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);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by