天天看點

【 MATLAB 】信号處理工具箱的信号産生函數之 square 函數簡記

因為案例需要,是以這裡先看一下linspace這個函數的用法:

​y = linspace(x1,x2);​

​均勻産生位于x1 到 x2 之間的100個點;​

​y = linspace(x1,x2,n);​

​均勻産生位于x1 到 x2 之間的n個點。​

【 MATLAB 】信号處理工具箱的信号産生函數之 square 函數簡記

​x​

​ = square(​

​t​

​)​

​ generates a square wave with period 2π for the elements of the time array ​

​t​

​​. ​

​square​

​ is similar to the sine function but creates a square wave with values of –1 and 1.

産生一個周期為 2π 的方波信号; 

​x​

​ = square(​

​t​

​,​

​duty​

​)​

​​ generates a square wave with specified duty cycle ​

​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      
【 MATLAB 】信号處理工具箱的信号産生函數之 square 函數簡記

在 -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      
【 MATLAB 】信号處理工具箱的信号産生函數之 square 函數簡記