generate square wave using piecewise function and plot it

조회 수: 7 (최근 30일)
Han Gao
Han Gao 2021년 6월 12일
댓글: Walter Roberson 2021년 6월 13일
Hi,
I'd like to generate a square wave using piecewise function rather than square function, is there someone that can help me? m`Many thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 6월 13일
PulseLength = 50;
CycleLength = 1000;
ReduceCycle = @(x) x - CycleLength*floor(x/CycleLength)
ReduceCycle = function_handle with value:
@(x)x-CycleLength*floor(x/CycleLength)
syms t
y(t) = piecewise( ReduceCycle(t) < PulseLength, 1, 0)
y(t) = 
fplot(y, [0 3100]); ylim([-1.1 1.1])
S(t) = sin(2*pi*t/CycleLength)
S(t) = 
fplot(S, [0 3100]); ylim([-1.1 1.1])
ys(t) = y(t) * S(t)
ys(t) = 
fplot(ys, [0 3100]); ylim([-1.1 1.1])
  댓글 수: 3
Han Gao
Han Gao 2021년 6월 13일
Thanks for your reply and it is working. One more question is I'd like to present the FFT spectrum and how to do a fft of the synthetic signal? I know FFT is designed only to work numerically on discrete data. But I don't know how to deal with the symbolic expression. Thank y ou.
Walter Roberson
Walter Roberson 2021년 6월 13일
PulseLength = 50/1000;
CycleLength = 1000/1000;
ReduceCycle = @(x) x - CycleLength*floor(x/CycleLength)
ReduceCycle = function_handle with value:
@(x)x-CycleLength*floor(x/CycleLength)
syms t
y(t) = piecewise( ReduceCycle(t) < PulseLength, 1, 0)
y(t) = 
fplot(y, [0 3100]/1000); ylim([-1.1 1.1])
S(t) = sin(2*pi*t/CycleLength)
S(t) = 
fplot(S, [0 3100]/1000); ylim([-1.1 1.1])
ys(t) = y(t) * S(t)
ys(t) = 
fplot(ys, [0 3100]/1000); ylim([-1.1 1.1])
yf = fourier(ys)
yf = 
... not very useful ;-)
You should create a vector of specific numeric times to sample at, and ys(times) to get specific values there. double() that and you have something you can fft()
Watch out that you use a full cycle of times without the time that would be the start of a new cycle. For example for 100 Hz if you sampled for 1 second, do not use times 0:1/100:1, and instead use 0:1/100:1-1/100 . Otherwise the first and the last point would both be the start of a cycle, and that will distort your fft.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by