因為案例需要,是以這裡先看一下linspace這個函數的用法:
y = linspace(x1,x2);
均勻産生位于x1 到 x2 之間的100個點;
y = linspace(x1,x2,n);
均勻産生位于x1 到 x2 之間的n個點。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iNwgDOyImM2YzMlFGOjV2YxYzXyATOwcTMxMzLcJTMxIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
generates a square wave with period 2π for the elements of the time array
x
= square(
t
)
.
t
square
is similar to the sine function but creates a square wave with values of –1 and 1.
産生一個周期為 2π 的方波信号;
generates a square wave with specified duty cycle
x
= square(
t
,
duty
)
duty
. The duty cycle is the percent of the signal period in which the square wave is positive.
産生一個周期為 2π 的方波信号,duty表示占空比,例如duty = 30,則占空比為30%,也就是正幅度與整個周期的比值。
案例1:在0 到 3π之間等間隔産生100個點,然後産生一個周期為2π的方波
%Create a vector of 100 equally spaced numbers from 0 to 3π. Generate a square wave with a period of 2π.
clear
clc
close all
t = linspace(0, 3*pi);
x = square(t);
plot(t/pi,x,'.-',t/pi,sin(t)); %Plot the square wave and overlay a sine. Normalize the x-axis by .
xlabel('t / \pi')
grid on
在 -pi 到 2*pi 之間等間隔産生121個點作為時間軸,産生一個幅值為1.15的周期方波,同時在同一幅圖上畫一個正弦波,參數與方波一緻;
%evaluate square(2*t) at 121 equally spaced numbers between -pi and 2*pi .
%Change the amplitude to 1.15 . Plot the wave and overlay a sine with the same parameters.
clear
clc
close all
t = linspace(-pi,2*pi,121);
x = 1.15*square(2*t);
plot(t/pi,x,'.-',t/pi,1.15*sin(2*t))
xlabel('t / \pi')
grid on