|On this page…|
t = (0:0.001:1)';
where the MATLAB® colon operator creates a 1001-element row vector that represents time running from 0 to 1 s in steps of 1 ms. The transpose operator (') changes the row vector into a column; the semicolon (;) tells MATLAB to compute, but not display the result.
y = sin(2*pi*50*t) + 2*sin(2*pi*120*t);
rng default; yn = y + 0.5*randn(size(t)); plot(t(1:50),yn(1:50))
Since MATLAB is a programming language, an endless variety of different signals is possible. Here are some statements that generate several commonly used sequences, including the unit impulse, unit step, and unit ramp functions:
t = (0:0.001:1)'; imp= [1; zeros(99,1)]; % Impulse unit_step = ones(100,1); % Step (with 0 initial cond.) ramp_sig= t; % Ramp quad_sig=t.^2; % Quadratic sq_wave = square(4*pi*t); % Square wave with period 0.5
All of these sequences are column vectors. The last three inherit their shapes from t.
Use standard MATLAB array syntax to work with multichannel signals. For example, a multichannel signal consisting of the last three signals generated above is
z = [ramp_sig quad_sig sq_wave];
You can generate a multichannel unit sample function using the outer product operator. For example, a six-element column vector whose first element is one, and whose remaining five elements are zeros, is
a = [1 zeros(1,5)]';
c = a(:,ones(1,3));
The toolbox provides functions for generating widely used periodic waveforms:
sawtooth generates a sawtooth wave with peaks at ±1 and a period of 2π. An optional width parameter specifies a fractional multiple of 2π at which the signal's maximum occurs.
square generates a square wave with a period of 2π. An optional parameter specifies duty cycle, the percent of the period for which the signal is positive.
To generate 1.5 s of a 50 Hz sawtooth wave with a sample rate of 10 kHz and plot 0.2 s of the generated waveform, use
fs = 10000; t = 0:1/fs:1.5; x = sawtooth(2*pi*50*t); plot(t,x), axis([0 0.2 -1 1])
The toolbox also provides functions for generating several widely used aperiodic waveforms:
gauspuls generates a Gaussian-modulated sinusoidal pulse with a specified time, center frequency, and fractional bandwidth. Optional parameters return in-phase and quadrature pulses, the RF signal envelope, and the cutoff time for the trailing pulse envelope.
chirp generates a linear, log, or quadratic swept-frequency cosine signal. An optional parameter specifies alternative sweep methods. An optional parameter phi allows initial phase to be specified in degrees.
To compute 2 s of a linear chirp signal with a sample rate of 1 kHz, that starts at DC and crosses 150 Hz at 1 s, use
t = 0:1/1000:2; y = chirp(t,0,1,150);
To plot the spectrogram, use
The pulstran function generates pulse trains from either continuous or sampled prototype pulses. The following example generates a pulse train consisting of the sum of multiple delayed interpolations of a Gaussian pulse. The pulse train is defined to have a sample rate of 50 kHz, a pulse train length of 10 ms, and a pulse repetition rate of 1 kHz; D specifies the delay to each pulse repetition in column 1 and an optional attenuation for each repetition in column 2. The pulse train is constructed by passing the name of the gauspuls function to pulstran, along with additional parameters that specify a 10 kHz Gaussian pulse with 50% bandwidth:
T = 0:1/50E3:10E-3; D = [0:1/1E3:10E-3;0.8.^(0:10)]'; Y = pulstran(T,D,'gauspuls',10E3,0.5); plot(T,Y)
The sinc function computes the mathematical sinc function for an input vector or matrix x. Viewed as a function of time, or space, the sinc function is the inverse Fourier transform of the rectangular pulse in frequency centered at zero of width 2π and height 1. The following equation defines the sinc function:
The sinc function has a value of 1 when x is equal to zero, and a value of
for all other elements of x.
To plot the sinc function for a linearly spaced vector with values ranging from -5 to 5, use the following commands:
x = linspace(-5,5); y = sinc(x); plot(x,y)
The toolbox function diric computes the Dirichlet function, sometimes called the periodic sinc or aliased sinc function, for an input vector or matrix x. The Dirichlet function, D(x) is:
where N is a user-specified positive integer. For N odd, the Dirichlet function has a period of 2π; for N even, its period is 4π. The magnitude of this function is (1/N) times the magnitude of the discrete-time Fourier transform of the N-point rectangular window.
To plot the Dirichlet function over the range 0 to 4π for N = 7 and N = 8, use
x = linspace(0,4*pi,300); plot(x,diric(x,7)); axis tight; plot(x,diric(x,8)); axis tight;