BENG 280A_Offline Assignment_Mihika Sonalkar

.pdf

School

University of California, San Diego *

*We aren’t endorsed by this school

Course

280

Subject

Electrical Engineering

Date

Dec 6, 2023

Type

pdf

Pages

21

Uploaded by CaptainWombatMaster1024

Question 1: Phasor patterns: MATLAB Code: (Done without AI) % script for MATLAB grader problem based on be280a12quiz1.m % Define a 9x2 matrix of kx,ky values here % if zero, just use 0 % if non-zero, specify the value as a signed fraction of the form: 1/L or -1/L % where L is an integer kxy = zeros ( 9 , 2 ); %initialize the value. %******************* % Learner Template
%******************* %PUT YOUR DEFINITION OF kxy here kxy = [ 0 0 ; 0.25 0.25 ; -0.25 0.25 ; -0.5 0.5 ; -0.25 0 ; 0.25 -0.5 ; -0.5 -0.5 ; 0.25 0.5 ; 0.25 0 ]; kxy1 = kxy( 1 : 3 ,:); % first three pairs be checked kxy2 = kxy( 4 : 6 ,:); % next three pairs to be checked kxy3 = kxy( 7 : 9 ,:); % last three pairs be checked % define the spatial grid for the phasors dx = 1 ; xspan = -2 : 0.5 : 2 ; [x,y] = meshgrid (xspan,-xspan); nx = length (xspan); labs = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' }; xmax = 2.5 ; nk = size (kxy, 1 ); % LOADING IN OBJECTS FOR LEARNER TEMPLATE load be280a_objects nobj = length (obj1); %% Plot out the phasor patterns corresponding to the entries in the kxy % matrix. % The phasor patterns should match the one given in the homework % assignment. % Each assessment will check the values of [kx,ky] for each row, for a total % of 3 assessments of (kx,ky) values. figure( 1 );clf; nr = 3 ;nc = 3 ; j = sqrt ( -1 ); for ik = 1 :nk; subplot(nr,nc,ik); g = exp (- j * 2 * pi *(kxy(ik, 1 )*x + kxy(ik, 2 )*y)); hl = quiver(x,y, real (g), imag (g), 0.4 );set(hl, 'Linewidth' , 1.5 );hold on; viscircles(gca,[x(:) y(:)], 0.25 * ones (nx^ 2 , 1 ), 'LineStyle' , '-' , 'LineWidth' , 0.5 );hold off; axis([-xmax xmax -xmax xmax]); title([ '(' ,labs{ik}, ')' ]);set(gca, 'FontSize' , 12 );xlabel( 'x' );ylabel( 'y' ) end %% plot the objects for reference % Note that the grid lines are just for reference -- they are not part of the % image figure( 2 ); clf; for iobj = 1 :nobj; subplot( 2 , 2 ,iobj); imagesc((obj1{iobj}),[ -1 1 ]);grid on; set(gca, 'XTick' ,[ 1 : 2 :nx], 'YTick' ,[ 1 : 2 :nx]); set(gca, 'XTickLabel' ,{ '-2' , '-1' , '0' , '1' , '2' }); set(gca, 'YTickLabel' ,{ '2' , '1' , '0' , '-1' , '-2' }); % when labeling the objects, increasing iCol corresponds to increasing % x-value; increasing iRow corresponds to decreasing y-value %for iRow = 1:5;for iCol = 1:5;text(iRow,iCol,num2str(obj1{iobj}(iRow,iCol)));end;end; colorbar; title(sprintf( 'Object %d' ,iobj));xlabel( 'x' );ylabel( 'y' ) end
%% Fourier Coefficients %******************* % Learner template: %******************* % Write code to determine the Fourier coefficient for % each kxy pair and object, where the Fourier coefficient is obtained by % multiplying the object by the phasor pattern and then summing up over all % voxels. % The answers should be put into a 9x4 matrix called fmat where the % ith column corresponds to the ith object fmat = NaN* ones (nk,nobj); fmat1 = NaN* ones ( 3 ,nobj); fmat2 = NaN* ones ( 3 ,nobj); fmat3 = NaN* ones ( 3 ,nobj); %PUT YOUR CODE HERE for k = 1 :nk % Create phasor pattern for the current kxy pair g = exp (- j * 2 * pi * (kxy(k, 1 ) * x + kxy(k, 2 ) * y)); % Iterate through each object for obj_idx = 1 :nobj % Multiply object by the phasor pattern and sum over all voxels fmat(k, obj_idx) = sum(sum(obj1{obj_idx} .* g)); end end fmat1 = fmat( 1 : 3 ,:); % assess Fourier coefficients at first 3 kxy pairs fmat2 = fmat( 4 : 6 ,:); % assess Fourier coefficients at next 3 kxy pairs fmat3 = fmat( 7 : 9 ,:); % assess Fourier coefficients at last 3 kxy pairs %% %******************* % Learner template: %******************* % write code to determine the kxy pair that % maximizes the absolute value of the Fourier transform for each object; % make a 4x2 matrix called kxymax % where rows 1 through 4 list the kx,ky pair that maximizes the absolute value of % Fourier transform for objects 1 through 4 respectively. kxymax = NaN* ones (nobj, 2 ); % initialize the value %PUT YOUR CODE HERE for iobj = 1 :nobj [val, idx] = max(fmat(:,iobj)); kxymax(iobj, :) = kxy(idx, :); end Provide a brief explanation of why your answer for the (kx,ky) pairs that maximize the Fourier Transform for each of the objects makes sense, including an explanation of why the signs (i.e. plus or minus) make sense. The value of kxymax is:
Comparing this to the graphs produced: As we notice the kx and ky values in the kxymax matrix match the period of change in the graph produced in Figure 2. We know the magnitude of a Fourier coefficient indicates the amplitude or strength of a specific spatial frequency in the object's composition. A higher magnitude suggests a more significant contribution of that spatial frequency. The direction and orientation of the spatial frequency are represented by the signs (plus or minus) of the corresponding kx and ky values. Positive values indicate a spatial frequency in one direction, while negative values indicate the opposite direction. When determining the kxy pair that maximizes the Fourier transform for each object, the code is identifying the dominant spatial frequency that contributes most significantly to the object's composition. The signs of kx and ky in the kxy pairs play a crucial role in determining the direction of the spatial frequency. For example, if the kx value is positive, it signifies a spatial frequency along the positive x-direction, and if it's negative, it represents a frequency along the negative x-direction.
Thus, the chosen kxy pairs in kxymax represent the specific spatial frequencies that contribute most significantly to the structure and features of each object. The signs of kx and ky provide information about the orientation and direction of these dominant spatial frequencies. Question 2: Function Code: (Done without AI) function [tGrad,aGrad] = idealGrad (kValue,maxGrad,desiredT) % INPUTS: % kValue: desired value in k-space in 1/cm % maxGrad: maximum allowable gradient in G/cm % desiredT: minimum desired pulse width in milliseconds; % your function should use gam = 4257; % Hz/G (for gamma/(2pi))-- this is already defined in the template % % OUTPUTS: % tGrad: gradient width in milliseconds; should be greater than or equal to desiredT % aGrad: gradient amplitude in G/cm; should be less than or equal to maxGrad % gam = 4257 ; % Hz/G % PUT YOUR CODE BELOW THIS LINE aGrad = kValue / (gam * desiredT / 1000 ); if aGrad > maxGrad aGrad = maxGrad; tGrad = kValue * 1000 / (gam * aGrad); else tGrad = desiredT; end end Code to call function: %Note: the first two assessments will be run with these values. kValue = 1 ; % 1/cm; maxGrad = 4 ; % G/cm desiredT = 1 ; % ms [tGrad, aGrad] = idealGrad(kValue, maxGrad, desiredT) Provide a brief explanation of why your answer for the default parameters makes sense. The function idealGrad is designed to calculate the gradient width (tGrad) and gradient amplitude (aGrad) based on the input parameters kValue, maxGrad, and desiredT. The function then checks if the calculated gradient amplitude (aGrad) is greater than the
maximum allowable gradient (maxGrad). If it is, the amplitude is set to the maximum allowable, and the pulse width is recalculated. If aGrad is not greater than maxGrad, tGrad is set to the desired pulse width (desiredT). The purpose of this function is to ensure that the calculated gradient parameters meet the specified constraints. For the given default parameters: kValue = 1 l/cm, maxGrad = 4 G/cm and desiredT = 1 ms, the calculated aGrad = 0.2349 G/cm. Since aGrad is less than maxGrad, tGrad is set to the desired pulse width (desiredT = 1 ms). Therefore, the output values tGrad and aGrad are consistent with the specified constraints and make sense for the given input parameters. Question 3: Function Code: (Done without AI) function [gx,gy,kx,ky] = square_Spiral (FOV,res,dt) % Inputs % FOV = field of view in cm % res = resolution in cm % dt = time increment in microseconds % % Outputs % gx = row vector with gx values (dimension: 1 x Npts) % gy = row vector with gy values (dimension: 1 x Npts) % kx = row vector with kx values (dimension: 1 x (Npts+1)) % ky = row vector with ky values (dimension: 1 x (Npts+1)) gam = 4257 ; %Hz/G Npts = 24 ; %% Your code will go below this line delta_k = 1 / FOV Nsteps = FOV / res; % Initialize the gradient and k-space vectors deltakx = [ 0 , delta_k, 0 , 0 , -delta_k, -delta_k, 0 , 0 , 0 , delta_k, delta_k, delta_k, 0 , 0 , 0 , 0 , -delta_k, -delta_k, -delta_k, -delta_k, 0 , 0 , 0 , 0 ]; deltaky = [-delta_k, 0 , +delta_k, +delta_k, 0 , 0 , -delta_k, -delta_k, -delta_k, 0 , 0 , 0 , delta_k, delta_k, delta_k, delta_k, 0 , 0 , 0 , 0 , -delta_k, -delta_k, -delta_k, -delta_k]; gx = deltakx / (dt*gam*( 10 ^ -6 )); % Initialize gradient vector gx gy = deltaky / (dt*gam*( 10 ^ -6 )); % Initialize gradient vector gy
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help