How to generate a wave form

2 次查看(过去 30 天)
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.

采纳的回答

Paulo Silva
Paulo Silva 2011-2-8
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)

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by