How to generate a wave form

I wrote this code for generating a square wave for (t = -5.5 to 4.5) and a triangular wave in the same time domain. The code does produce the desired waveform but I think I am using a very elementary technique. Is there a more efficient way to generate wave forms without using the Wave Generation Toolbox in Signals and Systems. The code is pasted here:
%************************************************************ clear all; close all; % Generating Square Wave [u,t] = gensig('square',2,10,0.01); t = t-5.5; for n = 1:length(t) if or(t(n) < -4, t(n) > 4) u(n) = 0; end end plot(t,u); ylim([-2 2]); grid on;
% Generating triangular wave t = -5.5:0.01:4.5; h(1:length(t)) = 0; for n = 1:length(t) if t(n) == 0 pos1 = n; end end pos2 = pos1 + 1/0.01; i = 1; for n = pos1:pos2 h(n) = 1 - i*0.01; i = i+1; end figure(); plot(t,h); ylim([-2 2]); grid on; %************************************************************ Thanks.

 Réponse acceptée

Paulo Silva
Paulo Silva le 8 Fév 2011

0 votes

Just create the first period of the wave and use the repmat function to make the entire wave.
Example:
x=[0 1]
y=repmat(x,1,10)

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by