Main Content

Create Uniform and Nonuniform Time Vectors

You can create uniform and nonuniform time vectors for use in computations involving time series.

Uniform Time Vectors

Use the colon operator if you know the sampling frequency. If your system samples time at a rate of 15 Hz during one second, you get 16 readings, including the one at zero.

Fs = 15; Ts = 1/Fs; ts = 0:Ts:1;

Uselinspaceif you know the beginning and end of the time interval and the number of samples. Suppose you start a stopwatch and stop it one second later. If you know your instrument took 15 readings, you can generate the time vector.

tl = linspace(0,1,15);

You can compute the sample rate directly from the samples and use it to reconstruct the time vector.

sf = 1/(tl(2)-tl(1)); TL = (0:length(tl)-1)/sf; ErrorTL = max(abs(tl-TL))
ErrorTL = 0

You can also reconstructtsusinglinspace.

lts = length(ts); TS = linspace(ts(1),ts(lts),lts); ErrorTS = max(abs(ts-TS))
ErrorTS = 1.1102e-16

linspaceand the colon operator create row vectors by default. Transpose them to obtain column vectors.

tcol = tl'; ttrans = ts';

N o t e To import an evenly spaced time vector into 万博1manbetx ( R ) , use an expression of the form t i m e V e c t o r = t i m e S t e p * ( s t a r t T i m e / t i m e S t e p : e n d T i m e / t i m e S t e p ) rather than t i m e V e c t o r = ( s t a r t T i m e : t i m e S t e p : e n d T i m e ) . For more information, see "Load Data to Root - Level Input Ports" in the 万博1manbetx documentation.

Nonuniform Time Vectors

Combinelinspaceand the colon operator to generate nonuniform time vectors of arbitrary characteristics.

Suppose you have a Gaussian-modulated sinusoidal pulse that you must sample. The pulse changes rapidly during a one-second interval but slowly during the preceding and following seconds.

Sample the region of interest at 100 Hz and take only five samples before and after. Concatenate the vectors using square brackets.

gpl = @(x) 2.1*gauspuls(x-1.5,5,0.4); Ffast = 100; Tf = 1/Ffast; Nslow = 5; tdisc = [linspace(0,1,Nslow) 1+Tf:Tf:2-Tf linspace(2,3,Nslow)];

Generate 20001 samples of the function to simulate the continuous-time pulse. Overlay a plot of the samples defined bytsf.

Tcont = linspace(0,3,20001)'; plot(Tcont,gpl(Tcont),tdisc,gpl(tdisc),'o','markersize',5)

Figure contains an axes object. The axes object contains 2 objects of type line.

See Also