Create a vector of 100 equally spaced numbers from to . Generate a square wave with a period of .
t = linspace(0,3*pi)'; x = square(t);
Plot the square wave and overlay a sine. Normalize the x-axis by . The generated square wave has a value of for intervals [) with even and a value of for intervals [) with odd . The wave never has a value of .
plot(t/pi,x,'.-',t/pi,sin(t)) xlabel('t / \pi') grid on
Repeat the calculation, but now evaluate
square(2*t) at 121 equally spaced numbers between and . Change the amplitude to . Plot the wave and overlay a sine with the same parameters. This new wave is negative at and positive at the endpoints, and .
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
Generate a 30 Hz square wave sampled at 1 kHz for 70 ms. Specify a duty cycle of 37%. Add white Gaussian noise with a variance of 1/100.
t = 0:1/1e3:0.07; y = square(2*pi*30*t,37)+randn(size(t))/10;
Compute the duty cycle of the wave. Plot the waveform and annotate the duty cycle.
ans = 0.3639
t— Time array
Time array, specified as a vector, matrix, or N-D
square operates along the first array dimension of
t with size greater than 1.
duty— Duty cycle
50(default) | real scalar from 0 to 100
Duty cycle, specified as a real scalar from 0 to 100.
x— Square wave
Square wave, returned as a vector, matrix, or N-D array.