Anda di halaman 1dari 11

Waveform Generation: Time Vectors and Sinusoids

On this page Time Vectors Common Sequences: Unit Impulse, Unit Step, and Unit Ramp Multichannel Signals Common Periodic Waveforms Common Aperiodic Waveforms The pulstran Function The Sinc Function The Dirichlet Function

Time Vectors
Most toolbox functions require you to begin with a vector representing a time base. Consider generating data with a 1000 Hz sample frequency, for example. An appropriate time vector is
>> 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. Given t, you can create a sample signal y consisting of two sinusoids, one at 50 Hz and one at 120 Hz with twice the amplitude.
>> y = sin(2*pi*50*t) + 2*sin(2*pi*120*t);

The new variable y, formed from vector t, is also 1001 elements long. You can add normally distributed white noise to the signal and plot the first 50 points using
>> randn('state',0); >> yn = y + 0.5*randn(size(t)); >> plot(t(1:50),yn(1:50))

Common Sequences: Unit Impulse, Unit Step, and Unit Ramp


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 y y y y = = = = = (0:0.001:1)'; [1; zeros(99,1)]; ones(100,1); t; t.^2; % impulse % step (filter assumes 0 initial cond.) % ramp

>> y = square(4*t);

All of these sequences are column vectors. The last three inherit their shapes from t.

Multichannel Signals
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 = [t t.^2 square(4*t)];

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)]';

To duplicate column vector a into a matrix without performing any multiplication, use the MATLAB colon operator and the ones function:
>> c = a(:,ones(1,3));

Common Periodic Waveforms


The toolbox provides functions for generating widely used periodic waveforms: sawtooth generates a sawtooth wave with peaks at 1 and a period of . An optional width parameter specifies a fractional multiple of at which the signal's maximum occurs. square generates a square wave with a period of . 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])

Common Aperiodic Waveforms


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


>> spectrogram(y,256,250,256,1000,'yaxis')

The pulstran Function


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


The sinc function computes the mathematical sinc function for an input vector or matrix x. The sinc function is the continuous inverse Fourier transform of the rectangular pulse of width and height 1. The sinc function has a value of 1 where x is 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 Dirichlet Function


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 is

where n is a user-specified positive integer. For n odd, the Dirichlet function has a period of ; for n even, its period is . The magnitude of this function is (1/n) times the magnitude of the discretetime 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;

tripuls
Sampled aperiodic triangle Syntax
y = tripuls(T) y = tripuls(T,w) y = tripuls(T,w,s)

Description y = tripuls(T) returns a continuous, aperiodic, symmetric, unity-height triangular pulse at the times indicated in array T, centered about T=0 and with a default width of 1. y = tripuls(T,w) generates a triangular pulse of width w. y = tripuls(T,w,s) generates a triangular pulse with skew s, where -1 < s < 1. When s is 0, a symmetric triangular pulse is generated. Examples Create a triangular pulse with width 0.4.
fs = 10000; t = -1:1/fs:1;

w = .4; x = tripuls(t,w); figure,plot(t,x) xlabel('Time (sec)');ylabel('Amplitude'); title('Triangular Aperiodic Pulse')

gauspuls
Gaussian-modulated sinusoidal pulse Syntax
yi = gauspuls(t,fc,bw) yi = gauspuls(t,fc,bw,bwr) [yi,yq] = gauspuls(...) [yi,yq,ye] = gauspuls(...) tc = gauspuls('cutoff',fc,bw,bwr,tpe)

Description gauspuls generates Gaussian-modulated sinusoidal pulses. yi = gauspuls(t,fc,bw) returns a unity-amplitude Gaussian RF pulse at the times indicated in array t, with a center frequency fc in hertz and a fractional bandwidth bw, which must be greater than 0. The default value for fc is 1000 Hz and for bw is 0.5. yi = gauspuls(t,fc,bw,bwr) returns a unity-amplitude Gaussian RF pulse with a fractional bandwidth of bw as measured at a level of bwr dB with respect to the normalized signal peak. The fractional bandwidth reference level bwr must be less than 0, because it indicates a reference level less than the peak (unity) envelope amplitude. The default value for bwr is -6 dB. [yi,yq] = gauspuls(...) returns both the in-phase and quadrature pulses. [yi,yq,ye] = gauspuls(...) returns the RF signal envelope. tc = gauspuls('cutoff',fc,bw,bwr,tpe) returns the cutoff time tc (greater than or equal to 0) at which the trailing pulse envelope falls below tpe dB with respect to the peak envelope amplitude. The trailing pulse envelope level tpe must be less than 0, because it indicates a reference level less than the peak (unity) envelope amplitude. The default value for tpe is -60 dB. Remarks

Default values are substituted for empty or omitted trailing input arguments. Examples

Plot a 50 kHz Gaussian RF pulse with 60% bandwidth, sampled at a rate of 1 MHz. Truncate the pulse where the envelope falls 40 dB below the peak:
tc = gauspuls('cutoff',50e3,0.6,[],-40); t = -tc : 1e-6 : tc; yi = gauspuls(t,50e3,0.6); plot(t,yi)

rectpulse
Rectangular pulse shaping Syntax y = rectpulse(x,nsamp) Description y = rectpulse(x,nsamp) applies rectangular pulse shaping to x to produce an output signal having nsamp samples per symbol. Rectangular pulse shaping means that each symbol from x is repeated nsamp times to form the output y. If x is a matrix with multiple rows, the function treats each column as a channel and processes the columns independently. Note To insert zeros between successive samples of x instead repeating the samples of x, use the upsample function instead. Examples of

An example in Combining Pulse Shaping and Filtering with Modulation uses this function in conjunction with modulation. The code below processes two independent channels, each containing three symbols of data. In the pulse-shaped matrix y, each symbol contains four samples. nsamp = 4; % Number of samples per symbol nsymb = 3; % Number of symbols ch1 = randint(nsymb,1,2,68521); % Random binary channel ch2 = [1:nsymb]'; x = [ch1 ch2] % Two-channel signal y = rectpulse(x,nsamp)

The output is below. In y, each column corresponds to one channel and each row corresponds to one sample. Also, the first four rows of y

correspond to the first symbol, the next four rows of y correspond to the second symbol, and the last four rows of y correspond to the last symbol. x = 1 1 0 1 2 3

y = 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3

rectpuls
Sampled aperiodic rectangle Syntax y = rectpuls(t) y = rectpuls(t,w) Description y = rectpuls(t) returns a continuous, aperiodic, unity-height rectangular pulse at the sample times indicated in array t, centered about t = 0 and with a default width of 1. Note that the interval of non-zero amplitude is defined to be open on the right, that is, rectpuls(-0.5) = 1 while rectpuls(0.5) = 0. y = rectpuls(t,w) generates a rectangle of width w. rectpuls is typically used in conjunction with the pulse train generating function pulstran.

sawtooth
Sawtooth or triangle wave Syntax sawtooth(t) sawtooth(t,width) Description sawtooth(t) generates a sawtooth wave with period 2 for the elements of time vector t. sawtooth(t) is similar to sin(t), but creates a sawtooth wave with peaks of -1 and 1 instead of a sine wave. The sawtooth wave is defined to be -1 at multiples of 2 and to increase linearly with time with a slope of 1/ at all other times. sawtooth(t,width) generates a modified triangle wave where width, a scalar parameter between 0 and 1, determines the point between 0 and 2 at which the maximum occurs. The function increases from -1 to 1 on the interval 0 to 2*width, then decreases linearly from 1 to -1 on the interval 2*width to 2. Thus a parameter of 0.5 specifies a standard triangle wave, symmetric about time instant with peak-to-peak amplitude of 1. sawtooth(t,1) is equivalent to sawtooth(t).

square
Square wave Syntax x = square(t) x = square(t,duty) Description x = square(t) generates a square wave with period 2 for the elements of time vector t. square(t) is similar to sin(t), but creates a square wave with peaks of 1 instead of a sine wave. x = square(t,duty) generates a square wave with specified duty cycle, duty, which is a number between 0 and 100. The duty cycle is the percent of the period in which the signal is positive.

sin
Sine of argument in radians Syntax

Y = sin(X) Description The sin function operates element-wise on arrays. The function's domains and ranges include complex values. All angles are in radians. Y = sin(X) returns the circular sine of the elements of X. Examples

Graph the sine function over the domain - < x < . x = -pi:0.01:pi; plot(x,sin(x)), grid on

The expression sin(pi) is not exactly zero, but rather a value the size of the floating-point accuracy eps, because pi is only a floating-point approximation to the exact value of .

pulstran

Pulse train Syntax

pulstran y = pulstran(t,d,'func') pulstran(t,d,'func',p1,p2,...) pulstran(t,d,p,fs) pulstran(t,d,p) pulstran(...,'func') Description

pulstran generates prototype pulses.

pulse

trains

from

continuous

functions

or

sampled

y = pulstran(t,d,'func') generates a pulse train based on samples of a continuous function, 'func', where 'func' is 'gauspuls', for generating a Gaussian-modulated sinusoidal pulse

'rectpuls', for generating a sampled aperiodic rectangle 'tripuls', for generating a sampled aperiodic triangle pulstran is evaluated length(d) times and returns evaluations y = func(t-d(1)) + func(t-d(2)) + ... the sum of the

The function is evaluated over the range of argument values specified in array t, after removing a scalar argument offset taken from the vector d. Note that func must be a vectorized function that can take an array t as an argument. An optional gain factor may be applied to each delayed evaluation by specifying d as a two-column matrix, with the offset defined in column 1 and associated gain in column 2 of d. Note that a row vector will be interpreted as specifying delays only. pulstran(t,d,'func',p1,p2,...) allows additional parameters to be passed to 'func' as necessary. For example: func(t-d(1),p1,p2,...) + func(t-d(2),p1,p2,...) + ...

pulstran(t,d,p,fs) generates a pulse train that is the sum of multiple delayed interpolations of the prototype pulse in vector p, sampled at the rate fs, where p spans the time interval [0,(length(p)-1)/fs], and its samples are identically 0 outside this interval. By default, linear interpolation is used for generating delays. pulstran(t,d,p) assumes that the sampling rate fs is equal to 1 Hz. pulstran(...,'func') specifies alternative interp1 for a list of available methods. Examples Example 1 interpolation methods. See

This example generates an asymmetric sawtooth waveform with a repetition frequency of 3 Hz and a sawtooth width of 0.1s. It has a signal length of 1s and a 1 kHz sample rate: t = 0 : 1/1e3 : 1; % 1 kHz sample freq for 1 sec d = 0 : 1/3 : 1; % 3 Hz repetition freq y = pulstran(t,d,'tripuls',0.1,-1); plot(t,y)

Example 2

This example generates a periodic Gaussian pulse signal at 10 kHz, with 50% bandwidth. The pulse repetition frequency is 1 kHz, sample rate is 50 kHz, and pulse train length is 10 msec. The repetition amplitude should attenuate by 0.8 each time: 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)

Example 3

This example generates a train of 10 Hamming windows: p = hamming(32); t = 0:320; d = (0:9)'*32; y = pulstran(t,d,p); plot(t,y)

See Also

chirp, cos, diric, gauspuls, rectpuls, sawtooth,

Anda mungkin juga menyukai