190 likes | 334 Views
Matrix Computations. ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne. Dot Product. Scalar formed by sum of products of vector elements dot_product = sum(A.*B); dot_product = A_row * B_col; dot(A,B);. Example. Find Total Mass num_items = [ 3 5 2 1 ]
E N D
Matrix Computations ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Dot Product • Scalar formed by sum of products of vector elements • dot_product = sum(A.*B); • dot_product = A_row * B_col; • dot(A,B); 206_M6
Example • Find Total Mass • num_items = [ 3 5 2 1 ] • mass_items = [3.5 1.5 .79 1.75] • item_totals = num_items .* mass_items • total_mass = sum(item_totals) • total_mass = num_items * mass_items’ • total_mass = dot(num_items,mass_items) 206_M6
Matrix Multiplication • Value in position (i,j) is dot product of row i of the first matrix with column j of the second matrix • Inner dimensions must be the same (conformable) • A = [2 5 1;0 3 -1] (2 x 3) • B = [1 0; -1 4; 5 2] (3 x 2) • C = A * B (2 x 2) • C11 = sum(A(1,:).*B(:,1)') 206_M6
Matrix Powers • Square of each element of a matrix • A.^2 • Square of a matrix • A^2 • A*A • Other powers • A^3 • A*A*A • ... 206_M6
Other Matrix Functions • Matrix Inverse • Product of matrix and it's inverse yields identity matrix • AA-1 = I and A-1A = I • B = inv(A) • I = A*B • Determinant • Scalar computed from entries of a square matrix • e.g. (2 x 2): |A| = a1,1a2,2 - a2,1a1,2 • det(A) 206_M6
Special Matrices • Matrix of Zeros • zeros(3) • zeros(3,2) • Matrix of Ones • ones(3) • ones(3,2) • Identity Matrix • eye(3) 206_M6
Systems of Linear Equations • System of 3 equations with 3 unknowns • 3x1 + 2x2 - x3 = 10 • -x1 + 3x2 + 2x3 = 5 • x1 - x2 - x3 = -1 • Matrix representation • AX = B 206_M6
Solving Simultaneous Equations • Solution Using the Matrix Inverse • AX = B • A-1AX = A-1B • IX = A-1B • X = A-1B • MATLAB Solution • X = inv(A)*B • Better MATLAB Solution using Left Division • X = A\B • Uses Gaussian elimination (without forming the inverse) 206_M6
Signal-to-Noise Ratio • Signal Power (Amplitude) • power = sum(x.^2)/length(x) • Signal Power (Variance and Mean) • power = std(x)^2 + mean(x)^2 • Signal Power (Sinusoid) • x = A*sin(2*pi*t) • power = A^2/2 • Signal-to-Noise Ratio • SNR = (signal power)/(noise power) 206_M6
Random Numbers • Uniform Random Numbers • rand('seed',n), rand(n), rand(m,n) • Interval 0 to 1 • Interval a to b • x = (b - a)*r + a; • Gaussian Random numbers • randn('seed',n), randn(n), randn(m,n) • Normal Distribution • Mean = 0, Standard Deviation = 1.0 • Modified Distribution • Mean = b, Standard Deviation = a • x = a*r + b; 206_M6
Uniform Noise rand(1,n) Interval -a to +a mean = 0 variance = a2/3 x = 2*a*r - a Gaussian Noise randn(1,n) Standard Deviation = a mean = 0 variance = a2 x = a*r Random Noise 206_M6
Sinusoid plus Uniform Noise % Sine plus Uniform Noise t=0:0.01:2; sine=4*sin(2*pi*t); noise_u=2*rand(1,201)-1; s_u=sine+noise_u; power_sine=sum(sine.^2)/length(sine) power_noise_u=sum(noise_u.^2)/length(noise_u) SNR_u=power_sine/power_noise_u figure(1) plot(t,s_u) figure(2) hist(noise_u) 206_M6
Sinusoid plus Gaussian Noise % Sine plus Gaussian Noise t=0:0.01:2; sine=4*sin(2*pi*t); noise_g=1/sqrt(3)*randn(1,201); s_g=sine+noise_g; power_sine=sum(sine.^2)/length(sine) power_noise_g=sum(noise_g.^2)/length(noise_g) SNR_g=power_sine/power_noise_g figure(3) plot(t,s_g) figure(4) hist(noise_g) 206_M6
Problem Solving Applied • Signal Generation with Noise • Problem Statement • Generate a sinusoidal signal with a given amplitude and addition of uniform noise with a specified signal-to-noise ratio • Input/Output Description Signal Plot Sine Wave Amplitude Signal and Noise Power Desired SNR SNR 206_M6
Problem Solving Applied • Hand Example • SNR = (signal power)/(noise power) • signal power = A2/2 • noise power = a2/3 • Algorithm Development • Prompt for A and SNR • Compute a • Generate sine and noise • Compute powers and SNR • Plot sine plus noise 206_M6
MATLAB Solution % Sine plus Uniform Noise at SNR A = input('Enter amplitude of sinusoid: '); SNR = input('Enter desired signal-to-noise ratio: '); a=sqrt(1.5*A^2/SNR); t=0:0.01:2; sine=A*sin(2*pi*t); noise_u=2*a*rand(1,201)-a; power_sine=sum(sine.^2)/length(sine); power_noise_u=sum(noise_u.^2)/length(noise_u); SNR_u=power_sine/power_noise_u s_u=sine+noise_u; plot(t,s_u) title('Sinusoid with Uniform Noise') 206_M6
Summary • Matrix Computations • Systems of Simultaneous Equations • Signal-to-Noise Ratio 206_M6