how can i plot a rectangular train wave ???

hi.i want to plot a rectangular train wave with a given width and frequency.the wave should have positive value around the origin(i mean it should not start from the origin).what should i do???

4 件のコメント

Sara Hafeez
Sara Hafeez 2012 年 12 月 2 日
MATLAB has a built in function for this.
Image Analyst
Image Analyst 2012 年 12 月 2 日
What's it called? I didn't find it, unless you're referring to repmat().
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 2 日
It' called square but there is some work to do, because it's default period is 2pi, and ...
Image Analyst
Image Analyst 2012 年 12 月 2 日
square() is not a built-in function unless you have the Signal Processing Toolbox. As far as I can tell it's not part of base MATLAB. I'll add that toolbox to the "Products" below.

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2012 年 12 月 2 日

1 投票

If you don't have the Signal Processing Toolbox, which has the square() function, then you could do it with repmat():
clc; % Clear command window.
% Create the signal using repmat().
halfWidth = 10;
oneCycle = [ones(1, halfWidth), zeros(1, halfWidth)]
numberOfCycles = 5;
fullSignal = repmat(oneCycle, [1, numberOfCycles])
% Signal has now been created, plot it.
plot(fullSignal, 'LineWidth', 5);
ylim([0 1.5]);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

1 件のコメント

Naga Sai
Naga Sai 2017 年 5 月 23 日
how to create a square wave with out using repmat() or other functions of matlab

サインインしてコメントする。

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 2 日
編集済み: Azzi Abdelmalek 2012 年 12 月 2 日

0 投票

You can use the function square which generate a 2pi periodic square wave signal
w=50 % positive signal width in percentage
F=1 % signal frequency
T=1/F % signal period
w0=2*pi*F % signal pulse
ts=T/50 % sample time
np=2 % number of period = np*2
t=-np*T:ts:np*T; % time vector allowing np*2 period
y=square(w0*(t-T*w/200),w)
close;plot(t,y,'r')
Note that y is varying from -1 to 1, if you want vaiation from 0 to 1
y=(y+1)/2;
you can also add an amplitude
y=Amp*y
If you have'nt a function square, it's easy to create
function y=square(t,p)
% t: time vector
% p: width in percentage
% the period by default is 2*pi
tt=mod(t,2*pi)
idx1=find(tt<=2*pi*p/100)
y=-ones(1,numel(t))
y(idx1)=1

1 件のコメント

Naga Sai
Naga Sai 2017 年 5 月 22 日
can you an explain this function briefly step by step please

サインインしてコメントする。

yaseen
yaseen 2022 年 9 月 23 日

0 投票

w=50 % positive signal width in percentage
F=1 % signal frequency
T=1/F % signal period
w0=2*pi*F % signal pulse
ts=T/50 % sample time
np=2 % number of period = np*2
t=-np*T:ts:np*T; % time vector allowing np*2 period
y=square(w0*(t-T*w/200),w)
close;plot(t,y,'r')

1 件のコメント

Image Analyst
Image Analyst 2022 年 9 月 24 日
This is not an original answer - it's just a repost of @Azzi Abdelmalek's code. Was posting it a mistake, like you were intending to run it rather than offer it as an Answer of your own creation? Do you want me to delete it for you?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2012 年 12 月 2 日

コメント済み:

2022 年 9 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by