Matlab Code for Square wave
33 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Danish Ali
le 21 Nov 2014
Commenté : shreyas kulkarni
le 7 Juin 2019
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 commentaires
shreyas kulkarni
le 7 Juin 2019
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
Réponse acceptée
Image Analyst
le 22 Nov 2014
How about repmat()? One of your toolboxes may also have a function.
9 commentaires
Image Analyst
le 22 Mai 2017
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
le 23 Mai 2017
thank your sir sir pl help how to generate a square wave without using any functions of matlab with basic functions only
Plus de réponses (2)
Ridwan Maassarani
le 8 Fév 2016
Modifié(e) : Ridwan Maassarani
le 8 Fév 2016
% 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 commentaires
Image Analyst
le 8 Juin 2017
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
le 13 Mar 2019
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
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!