Matlab Code for Square wave

조회 수: 33 (최근 30일)
Danish Ali
Danish Ali 2014년 11월 21일
댓글: shreyas kulkarni 2019년 6월 7일
Hi,
I have to generate a square wave like the one shown in the fig below. Can any one please indicate the appropriate code for that?
Thanks,
  댓글 수: 4
Naga Sai
Naga Sai 2017년 5월 21일
sir help me how to use this code as userdefinedfunction in matlab
shreyas  kulkarni
shreyas kulkarni 2019년 6월 7일
function [z t] = square(n1,n2)
% n1=2 give similar value
% n2=4 give this value
z=[];
t=1:1:n2
x= [];
for i=1:5
for t1= 0.01:0.01:n1
a=1
z=[z a];
x=[x t1]
end
for t2=n1+0.01:0.01:n2
a=-1
z=[z a];
x=[x t2]
end
t=1:0.01:n2;
x=1:0.01:n2;
plot(z);
end

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 11월 22일
How about repmat()? One of your toolboxes may also have a function.
  댓글 수: 9
Image Analyst
Image Analyst 2017년 5월 22일
It's basically the same, you just have finer x resolution so when plotted the slopes from low to high appear steeper. Either way, they're both square waves with instantaneous jumps, regardless of how they look while plotted.
Naga Sai
Naga Sai 2017년 5월 23일
thank your sir sir pl help how to generate a square wave without using any functions of matlab with basic functions only

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ridwan Maassarani
Ridwan Maassarani 2016년 2월 8일
편집: Ridwan Maassarani 2016년 2월 8일
% endCan it be done with this code ?
t = n*T:1:((n+1)*T);
%lengtht = length(t);
% Define x(t) and plot
if (mod(n,2) == 0)
%number is even
x = 1;
else
%number is odd
x = -1;
end
plot(x)
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
  댓글 수: 17
Naga Sai
Naga Sai 2017년 6월 8일
sir please clear error in above function sir
Image Analyst
Image Analyst 2017년 6월 8일
Please give the link to your source code so I can compare your MATLAB code to the original, which presumably is in pseudocode or another language.

댓글을 달려면 로그인하십시오.


Daniel Robin
Daniel Robin 2019년 3월 13일
The following square wave function can be used just like sin(x) providing a phase argument and duty-cycle and getting a signal in the range -1 to 1 accordingly.
function y = square(x, D)
r = mod(x/(2*pi), 1); % generate a ramp over every cycle
y = 1 - 2*(r > D); % output result from -1 to 1
end

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by