generate square wave using piecewise function and plot it
9 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Walter Roberson
2021-6-13
PulseLength = 50;
CycleLength = 1000;
ReduceCycle = @(x) x - CycleLength*floor(x/CycleLength)
syms t
y(t) = piecewise( ReduceCycle(t) < PulseLength, 1, 0)
fplot(y, [0 3100]); ylim([-1.1 1.1])
S(t) = sin(2*pi*t/CycleLength)
fplot(S, [0 3100]); ylim([-1.1 1.1])
ys(t) = y(t) * S(t)
fplot(ys, [0 3100]); ylim([-1.1 1.1])
3 个评论
Walter Roberson
2021-6-13
PulseLength = 50/1000;
CycleLength = 1000/1000;
ReduceCycle = @(x) x - CycleLength*floor(x/CycleLength)
syms t
y(t) = piecewise( ReduceCycle(t) < PulseLength, 1, 0)
fplot(y, [0 3100]/1000); ylim([-1.1 1.1])
S(t) = sin(2*pi*t/CycleLength)
fplot(S, [0 3100]/1000); ylim([-1.1 1.1])
ys(t) = y(t) * S(t)
fplot(ys, [0 3100]/1000); ylim([-1.1 1.1])
yf = fourier(ys)
... 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 Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!