Digital Signal Processing Reference
In-Depth Information
for k = 0:1:30,
f(k+1) = 2*cos(pi/15*k);
% In M ATLAB , the index of a
% vector or a matrix must
end
% not be zero.
In M ATLAB , the index of a vector or matrix cannot be zero. Therefore, we use
two row vectors k and f to store the DT function. The row vector k specifies
the time indices at which function f is evaluated, while f contains the value
of the DT function at the corresponding time index stored in k . The above
initialization can also be performed in M ATLAB more quickly and in a much
more compact way.
clear
% user-defined variables are cleared
k = 0:30;
% k is a row vector of dimensions 1x30
f = 2*cos(5*k)
% f has the same dimensions as k
returns the following answer:
f=
Columns 1 through 7
2.0000 0.5673 -1.6781 -1.5194 0.8162 1.9824 0.3085
Columns 8 through 14
-1.8074 -1.3339 1.0506 1.9299 0.0443 -1.9048 -1.1249
Columns 15 through 21
1.2666 1.8435 -0.2208 -1.9688 -0.8961 1.4603 1.7246
Columns 22 through 28
-0.4819 -1.9980 -0.6516 1.6284 1.5754 -0.7346 -1.9922
Columns 29 through 31
-0.3956 1.7677 1.3985
In terms of execution time, implementation 2 is more efficient than the first
implementation. Since M ATLAB is an interpretive language, loops take a long
time to be executed. An efficient M ATLAB code avoids loops and, if possible,
replaces them with matrix or vector multiplications.
Example E.4
Initialize the following DT function:
g [ k ] =
f [ k ]
for
0 k 6 .
Solution
In the above example, it has been assumed that the matrix f has been initialized
as per Example E.3. The following M ATLAB code will initialize row vector
g :
>> g = f(1:7);
Search WWH ::




Custom Search