format longg;
format compact;
clc;
workspace;
fontSize = 15;
elementsPerHalfPeriod = 30;
amplitude = 5;
verticalOffset = -2;
numberOfPeriods = 4;
risingSignal = linspace(0, amplitude, elementsPerHalfPeriod);
fallingSignal = linspace(amplitude, 0, elementsPerHalfPeriod);
oneCycle = [risingSignal, fallingSignal(2:end-1)] + verticalOffset;
x = 0 : length(oneCycle)-1;
subplot(3, 1, 1);
plot(x, oneCycle, 'bo-');
grid on;
title('One Cycle of the Triangle', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
triangleWaveform = repmat(oneCycle, [1 numberOfPeriods]);
x = 0 : length(triangleWaveform)-1;
subplot(3, 1, 2);
plot(x, triangleWaveform, 'bo-');
grid on;
title('Several Cycles of the Triangle', 'FontSize', fontSize);
windowWidth = 9;
smoothTriangleWave = conv(triangleWaveform, ones(1,windowWidth)/windowWidth, 'same');
subplot(3, 1, 3);
plot(x, smoothTriangleWave, 'bo-');
grid on;
title('Smoothed Triangle Wave', 'FontSize', fontSize);
Best Answer