Appendix: Matlab Examples

GUIDE: Mathematics of the Discrete Fourier Transform (DFT) - Julius O. Smith III. Appendix: Matlab Examples

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.

NOTE: THIS DOCUMENT IS OBSOLETE, PLEASE CHECK THE NEW VERSION: "Mathematics of the Discrete Fourier Transform (DFT), with Audio Applications --- Second Edition", by Julius O. Smith III, W3K Publishing, 2007, ISBN 978-0-9745607-4-8. - Copyright © 2017-09-28 by Julius O. Smith III - Center for Computer Research in Music and Acoustics (CCRMA), Stanford University

<< Previous page  TOC  INDEX  Next page >>

Appendix: Matlab Examples

Here's how Fig. 6.1 was generated in Matlab:

>> x = [2 3];                  % coordinates of x
>> origin = [0 0];             % coordinates of the origin
>> xcoords = [origin(1) x(1)]; % plot() expects coordinate lists, not endpoints
>> ycoords = [origin(2) x(2)];
>> plot(xcoords,ycoords);      % Draw a line from origin to x
Mathematica can plot a list of ordered pairs:
In[1]:
    ListPlot[{{0,0},{2,3}},PlotJoined->True]; (* Draw a line from (0,0) to (2,3) *)

In Matlab, the mean of the row-vector $x$ can be computed as

\

or by using the built-in function mean().

In Matlab, if x = [x1 ... xN] is a row vector, we can compute thetotal energy as

\

Matlab has a function orth() which will compute an orthonormal basis for a space given any set of vectors which span the space.

>> help orth

ORTH Orthogonalization. Q = orth(A) is an orthonormal basis for the range of A. Q’*Q = I, the columns of Q span the same space as the columns of A and the number of columns of Q is the rank of A.
    See also QR, NULL.</pre>Below is an example of using <tt>orth()</tt> to orthonormalize a <a href="http://mathworld.wolfram.com/linearlyindependent.php">linearly

independent basis set for $N=3$:
% Demonstration of the Matlab function orth() for
% taking a set of vectors and returning an orthonormal set
% which span the same space.
v1 = [1; 2; 3];  % our first basis vector (a column vector)
v2 = [1; -2; 3]; % a second, linearly independent column vector
v1’ * v2         % show that v1 is not orthogonal to v2

ans =


<< Previous page TOC INDEX Next page >>
 6

V = [v1,v2] % Each column of V is one of our vectors
V =
 1     1
 2    -2
 3     3

W = orth(V) % Find an orthonormal basis for the same space
W =
0.2673    0.1690
0.5345   -0.8452
0.8018    0.5071

w1 = W(:,1) % Break out the returned vectors
w1 =
0.2673
0.5345
0.8018

w2 = W(:,2)
w2 =
0.1690

-0.8452 0.5071
w1’ * w2 % Check that w1 is orthogonal to w2 (to working precision)
ans =
2.5723e-17
w1’ * w1 % Also check that the new vectors are unit length in 3D
ans =
 1

w2’ * w2
ans =
 1

W’ * W % faster way to do the above checks (matrix multiplication)
ans =
1.0000    0.0000
0.0000    1.0000

% Construct some vector x in the space spanned by v1 and v2: x = 2 * v1 - 3 * v2
x =
-1
10
-3

% Show that x is also some linear combination of w1 and w2: c1 = x’ * w1 % Coefficient of projection of x onto w1
c1 =
2.6726

c2 = x’ * w2 % Coefficient of projection of x onto w2
c2 =
-10.1419
xw = c1 * w1 + c2 * w2 % Can we make x using w1 and w2?
xw =
-1.0000 10.0000 -3.0000
error = x - xw
error =
1.0e-14 *
0.1332
     0
     0<a href="https://ccrma.stanford.edu/~jos//interpolation/lp_norms.php">norm</a>(error)       % typical way to summarize a vector error

ans =
1.3323e-15
% It works!
% Now, construct some vector x NOT in the space spanned by v1 and v2: y = [1; 0; 0]; % Almost anything we guess in 3D will work
% Try to express y as a linear combination of w1 and w2: c1 = y’ * w1; % Coefficient of projection of y onto w1 c2 = y’ * w2; % Coefficient of projection of y onto w2 yw = c1 * w1 + c2 * w2 % Can we make y using w1 and w2?
yw =
0.1000
0.0000
0.3000

yerror = y - yw
yerror =
0.9000
0.0000

-0.3000
norm(yerror)
ans =
0.9487

% While the error is not zero, it is the smallest possible % error in the least squares sense. % That is, yw is the optimal least-squares approximation % to y in the space spanned by v1 and v2 (w1 and w2). % In other words, norm(yerror) <= norm(y-yw2) for any other vector yw2 made % using a linear combination of v1 and v2.
% In yet other words, we obtain the optimal least squares approximation % of y (which lives in 3D) in some subspace W (a 2D subspace of 3D) % by projecting y orthogonally onto the subspace W to get yw as above. % % An important property of the optimal least-squares approximation % is that the approximation error is orthogonal to the the subspace % in which the approximation lies. Let’s show this:
W’ * yerror % must be zero to working precision
ans =
1.0e-16 *
-0.2574 -0.0119
 

© 1998-2023 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy