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 件のコメント

NURRUZAINI MOHD NASIR
NURRUZAINI MOHD NASIR 2016 年 5 月 22 日
how to generate this coding ?
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 日

1 投票

How about repmat()? One of your toolboxes may also have a function.

9 件のコメント

Danish Ali
Danish Ali 2014 年 11 月 22 日
Repmat command is supposed to be used for multi array declaring. Isn't it? How can it be used to plot a square wave like this one?
Image Analyst
Image Analyst 2014 年 11 月 22 日
No, not really. It's for replicating a matrix vertically and horizontally. Did you look at the help and try it? Did you try anything like this:
% Make one cycle.
oneCycle = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1];
% Replicate 10 times.
squareWave = repmat(oneCycle, [1, 10]);
% Plot it.
plot(squareWave, 'bo-', 'LineWidth', 2);
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
Danish Ali
Danish Ali 2014 年 11 月 22 日
Yeah That worked. Thanks
Naga Sai
Naga Sai 2017 年 5 月 19 日
but it is not like square right
Naga Sai
Naga Sai 2017 年 5 月 19 日
if we use a square function it will be very clear
Image Analyst
Image Analyst 2017 年 5 月 19 日
It is a square wave. From one element to the next, it instantly jumps to the new value. There are no intermediate values - just the high value and the low value, only two values, so it's a square wave.
Naga Sai
Naga Sai 2017 年 5 月 21 日
編集済み: Image Analyst 2017 年 5 月 22 日
but that jumping of values should be instant know sir
close all; clear all;
f = 7;
fs = 1000;
t = 1.5;
n = [0:1/fs:t];
cycles = t*f;
x = ones(1,length(n));
duty = 50;
oc_samp = fs/f;
on_samp = (oc_samp * duty)/100;
off_samp = oc_samp - on_samp;
temp = 0;
for i = 1 : ceil(cycles);
x(temp+on_samp+1:i*oc_samp) = 0;
temp = temp + oc_samp;
end
plot(n,x(1:length(n)),'LineWidth',2);ylim([-1 1.5]);
% stem(n,x); ylim([-1.5 1.5]);
i think this code is better to get exact square wave
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 日

0 投票

% 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 件のコメント

Image Analyst
Image Analyst 2016 年 2 月 8 日
No. Evidently not. Just try it.
Naga Sai
Naga Sai 2017 年 5 月 19 日
here n is an undefined function
Naga Sai
Naga Sai 2017 年 5 月 19 日
sir what is n in above function sir please give me an explaination or a code to generate a square wave form without using function
Image Analyst
Image Analyst 2017 年 5 月 19 日
Like I said, this code doesn't work. Did you see my code (now hidden in the comments) with code that DOES work? https://www.mathworks.com/matlabcentral/answers/163673-matlab-code-for-square-wave#comment_251311
% Make one cycle.
oneCycle = [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1];
% Replicate 10 times.
squareWave = repmat(oneCycle, [1, 10]);
% Plot it.
plot(squareWave, 'bo-', 'LineWidth', 2);
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
Naga Sai
Naga Sai 2017 年 5 月 21 日
sir help me how to use this code as a function to use it in another program
Image Analyst
Image Analyst 2017 年 5 月 21 日
Do you that you need to know how to use copy and paste? It's control-c and control-v. Or you need help in writing a function? I really don't know what you need help with.
Naga Sai
Naga Sai 2017 年 5 月 22 日
i want this above program to use it as a function to generate a square wave sir
Naga Sai
Naga Sai 2017 年 5 月 22 日
sir how to create a fft function please help me regarding this also sir
Naga Sai
Naga Sai 2017 年 5 月 23 日
sir pl respond to my question sir
Image Analyst
Image Analyst 2017 年 5 月 23 日
"how to create a fft function please help me regarding this also" <=== You don't need to create an fft function. There is an fft() function built into MATLAB.
Naga Sai
Naga Sai 2017 年 5 月 23 日
but i have some restrictions from my guide not to use that function it should be created by own
Image Analyst
Image Analyst 2017 年 5 月 24 日
I'm sure you can find fft() code on the internet somewhere. It should be readily available. Are you able to use that (someone else's low level code)?
Naga Sai
Naga Sai 2017 年 5 月 25 日
sir i am unable to find the code sir please help me in this concern
Image Analyst
Image Analyst 2017 年 5 月 25 日
Learning how to use Google would be a beneficial thing for you. I used it and look what I found:
There are more if you want them.
Naga Sai
Naga Sai 2017 年 5 月 25 日
Sir please clear the error in this code sir
function y = MYFFT(x)
a=size(x);
N=a(2);
n=log2(N);
l=1;
N2=N/2;
NU1=n-1;
k=0;
while(l<=n)
while(k<=N-1)
I=1;
while(I<=N2)
M=fix(k/(2^NU1));
b=dec2bin(M,n);
q=seqreverse(b);
P=bin2dec(q);
W=exp(-2*i*P*pi/N);
T1=W*(x(k+1+N2));
x(k+1+N2)=x(k+1)-T1;
x(k+1)=x(k+1)+T1;
k=k+1;
I=I+1;
end
k=k+N2;
end
end
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 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by