Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

square

说明

示例

x = square(t) 为时间数组 t 的元素生成周期为 2π 的方波。square 类似于正弦函数,但会创建值为 -1 和 1 的方波。

示例

x = square(t,duty) 生成指定占空比为 duty 的方波。占空比是方波为正的信号周期的百分比。

示例

全部折叠

创建一个由 03π 之间的 100 个等间距数字组成的向量。生成周期为 2π 的方波。

t = linspace(0,3*pi)';
x = square(t);

绘制方波并叠加一个正弦波。用 π 归一化 x 轴。对于具有偶数 n 的区间 [nπ,(n+1)π),生成的方波的值为 1,而对于具有奇数 n 的区间 [nπ,(n+1)π),生成的方波的值为 -1。该波的值永远不会是 0

plot(t/pi,x,'.-',t/pi,sin(t))
xlabel('t / \pi')
grid on

Figure contains an axes object. The axes object with xlabel t / blank pi contains 2 objects of type line.

重复计算,但现在,基于 -π2π 之间的 121 个等间距数字计算 square(2*t)。将振幅更改为 1.15。绘制该波,并叠加具有相同参数的正弦波。此新波在 t=0 处为负值,而在端点 -π2π 处为正值。

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

Figure contains an axes object. The axes object with xlabel t / blank pi contains 2 objects of type line.

生成一个 30 Hz 方波,其采样率为 1 kHz,持续时间为 70 毫秒。指定占空比为 37%。添加方差为 1/100 的高斯白噪声。

t = 0:1/1e3:0.07;
y = square(2*pi*30*t,37)+randn(size(t))/10;

计算波的占空比。绘制波形并为占空比添加注释。

dutycycle(y,t)

Figure Duty Cycle Plot contains an axes object. The axes object with xlabel Time (seconds), ylabel Level (Volts) contains 9 objects of type line. One or more of the lines displays its values using only markers These objects represent signal, mid cross, upper boundary, upper state, lower boundary, mid reference, lower state.

ans = 0.3639

输入参数

全部折叠

时间数组,指定为向量、矩阵或 N 维数组。square 沿大小大于 1 的数组 t 的第一个维度进行运算。

数据类型: single | double

占空比,指定为从 0 到 100 的实数标量。

数据类型: single | double

输出参数

全部折叠

方波,以向量、矩阵或 N 维数组形式返回。

扩展功能

C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。

版本历史记录

在 R2006a 之前推出