Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Project B - Inverse problem

BMEN 5401 - Biomedical Imaging

By
Thomas Boerstad

Minneapolis, November 2009

University of Minnesota
Contents I

Contents
1 Mathematical equations - Inverse problem 1
1.1 Defining the model . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Governing equations . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Field generated using transfer matrix description . . . . . . . 2

2 Inverse solutions with N = 10 and M = 8 4


2.1 Noiseless case . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 10 % additive noise . . . . . . . . . . . . . . . . . . . . . . . . 5

3 Inverse solutions with N = 10 and M = 12 8


3.1 Noiseless case . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 10 % additive noise . . . . . . . . . . . . . . . . . . . . . . . . 8

4 Tikhonov regularization 10

5 Discussion 13
5.1 M = 8 compared to M = 12 . . . . . . . . . . . . . . . . . . . 13
5.2 Interpretation of results . . . . . . . . . . . . . . . . . . . . . 13

6 Matlab code 16
6.1 Main script . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
6.2 gen tf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.3 Plot src . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1 Mathematical equations - Inverse problem 1

1 Mathematical equations - Inverse problem


1.1 Defining the model
The two fundamental problems of biomedical imaging are the forward and
the inverse problem. In this context the forward problem refers to calculating
the magnetic field or electric potential at points in space, given a set of
sources. The forward problem was discussed and solved in the previous
project.
The inverse problem is the reverse operation of the forward problem.
Instead of finding a distribution given a set of sources, the inverse problem
problem is to find a set of sources corresponding to a given distribution.
Though the forward and inverse problem are related, the inverse problem is
in general more difficult to solve than the forward problem. The primary
reason for this is that the solution to the inverse problem is, generally non-
unique. Without any form of constraints, the same magnetic field could
in theory be generated by infinitely many different source configurations.
So in order to be able to generate realistic solutions, one has to impose
certain constraints on the solution. By utilizing a priori information based
on anatomy, physiology, limiting the number of possible sources to a sensible
number and placing them at realistic points in space, good solutions may
still be found.

1.2 Governing equations


Consider M 2 current source dipoles uniformly distributed on a horizontal
M xM grid in space. The equations for calculating the electric potential and
magnetic field at a point r in space was found in the previous project report:

1 X r − r0 i
Φ(r) = Pi · (1.1)
4πσ |r − r0 i |3
i
1 X r − r0 i
H(r) = Pi × (1.2)
4π |r − r0 i |3
i

where Φ is the electric potential, H is the magnetic field and σ is the electric
conductivity of the medium. Pi is the dipole moment associated with dipole
number i, and ri is the location of dipole i. This project report focuses only
on the magnetic field strength.
Now consider a N xN grid of sensors, equivalent with N 2 points in space.
The magnetic field at each sensor can be mapped to a sensor vector y with
3N 2 elements. There are 3 degrees of freedom (the magnetic field is a three
dimensional vector quantity) associated with each sensor. In the same way
the dipole sources can be mapped to a source vector g with 3M 2 elements,
since each dipole is described by a dipole moment vector. Equation (1.2)
1 Mathematical equations - Inverse problem 2

may then be reformulated in matrix form:

y = Fg (1.3)

where F is a 3N 2 x 3M 2 transfer matrix, often called a lead-field matrix.


With the linear relationship in eq (1.3) it possible to take advantage of
well developed methods for linear systems. The forward problem consists of
calculating g given y. Equation (1.3) may be written out in the following
way:

r1z −r10z ry −r0y 0z ry −r0y


 
r1z −rM
0 |r1 −r01 |3
− |r11−r01|3 ··· 0 |r1 −r0M |3
− |r11−r0M|3
1 M
 r1z −r10z r1x −r10x rz −r0z r1x −rM0x
 
 − |r1 −r0 |3 0 ··· − |r11−r0M|3 0

 y 0y1 |r1 −r01 |3 M
0
|r1 −rM |3
0y
r1y −rM

 r1 −r1 rx −r0x rx −r0x
− |r11−r01|3 0 ··· − |r11−r0M|3 0

 |r1 −r0 |3 0
|r1 −rM |3

1 1 M
1  .. .. .. .. .. .. ..

F=
 
. . . . . . .
4π 
 
ry −r0y y 0y
z −r 0z z −r 0z

rN rN rN −rM
0 1
− |rNN −r01|3 ··· 0 M
− |rN −r0 |3 
 
|rN −r01 |3 |rN −r0M |3

1 M 
z −r 0z x −r 0x rz −r0z x −r 0x

 rN rN rN
− |rN −r01|3 0 1
··· − |rNN−r0M|3 0 M 
 y 0y1 |rN −r01 |3 M |rN −r0M |3 
x −r 0x y 0y x −r 0x

rN −r1 rN rN −rM rN
|rN −r01 |3
− 1
|rN −r01 |3
0 ··· |rN −r0M |3
− M
|rN −r0M |3
0
y1
 x 
P1 x
 y1y   P1y 
 z  z
y  P 
 1  1
y =  ...  , g =  ... 
   
 x  x
y  P 
 N  Ny 
y y 

P 
N N
yN z PNz

1.3 Field generated using transfer matrix description


Using the transfer matrix description of eq (1.3), the resulting magnetic
field is shown in figure 1. Here N =10, M =8 and the sensor grid is located 4
units above the source grid. Both the inter-source and inter-sensor distance
is 1. Dipole 1 (bottom left corner) has a moment (-0.2, -0.2, 1) and dipole
2 (top right corner) has a moment (0.5, 0.5, 0.1). Figure 2 shows the true
magnitude and positions of the sources over the source-space, and will be
used as a reference to judge the quality of the inverse solutions.
1 Mathematical equations - Inverse problem 3

Figure 1: Magnetic field strength calculated using linear equation y=Fg

Figure 2: Placement and magnitude of actual source distribution


2 Inverse solutions with N = 10 and M = 8 4

2 Inverse solutions with N = 10 and M = 8


2.1 Noiseless case
Firstly the inverse problem is solved given the noiseless distribution shown
in figure 1. With N = 10 and M = 8 there is a total of 300 sensor values
(i.e 300 equations, and 192 unknown dipole parameteres. Since there is
no noise, equation (1.3) perfectly describes the relationship between the
measurements y and the unknowns g. F is not a square matrix, so it is not
immediatly possible to find it’s inverse F−1 . However through some algebra
we are able to find an expression for g:

y = Fg (2.1)
T T
F y = F Fg (2.2)
T −1 T
(F F) F y=g (2.3)

where (FT F)−1 FT is called a pseudoinverse. Using eq (2.3) the estimated


solutions are shown in figure 3.

Figure 3: Noiseless reconstruction with pseudoinverse

The top half of figure 3 shows the source magnitudes, and the bottom
half plots all the values of gest compared to the true value g. As we can see
the reconstructed solution matches the actual solution perfectly. Although
this seems like a good result, it is important to have in mind that these
are not realisitic conditions. First of all the model sources and sensors are
too simple, and more importantly in real life conditions there will always be
2 Inverse solutions with N = 10 and M = 8 5

some noise present. As will be shown, even low amounts of noise can render
eq 2.3 useless.

2.2 10 % additive noise


Guassian white noise with a standard deviation of 10% of y(i) is added
to each element y(i) to create a noisy measurement vector ynoisy . Equa-
tion (2.3) now becomes:

(FT F)−1 FT (y + e) = gest (2.4)

where e is the error vector. Since it is not possible to separate e from y


we are no longer able to generate an exact estimation of g. Even worse, we
are not able to generate even a decent solution! Using the pseudoinverse
generates a useless solution shown in figure 4. The numerical values are off
the charts and obviously not of any value. Clearly the pseudoinverse is not
useful in this noisy case.

Figure 4: Noisy reconstruction with pseudoinverse

In order to generate a better solution, a mathematical technique called


truncated singular value decomposition (TSVD) is used. Any N xM matrix
F can be decomposed in the following way:

F = UΣVT (2.5)

where U and V are matrices with orthonormal columns, and Σ is a diagonal


matrix consisting of M elements called the singular values of F ordered
decreasing order. By keeping only the t largest singular values and ignoring
the remaining singular values a truncated matrix Ft approximating F can
2 Inverse solutions with N = 10 and M = 8 6

be created. By chosing an appropriate threshold value t, Ft can be shown to


be a good estimation of F while being less sensitive to noise (see [1]). The
TSVD algorithm solves the following optimization problem:

min ||gest ||2 subject to min ||Ft gest − y||2 (2.6)

This basically means that the algorithm searches for the lowest norm solution
gest that satisfies eq (1.3) the best. The singular values of F are shown in
figure 5, and only the 47 (t = 47) first singular values are kept to solve the
inverse problem. Deciding on how many singular values to keep is important
to the quality of the inverse solution, and will be discussed in more detail
later. However from figure 5 it is seen that singular values 48 and above
are fairly low, and it seems reasonable that we have captured most of the
dynamics present in F.

Figure 5: Singular values of F

The solution using the TSVD algorithm is shown in figure 6. The recon-
structed solution with 10% additive noise is far from perfect, but we are at
least now able to see that there probably is one source in the top right and
one in the bottom left corner. The estimated sources are now distributed
over more locations in space than what is actually the case. While the ideal
solution has two distinct sources with large amplitudes, they are now recon-
structed as several distributed sources with smaller amplitudes. The spatial
resolution of the estimated solution gest is not good enough to distinguish
the two sources from their neighbours. However compared to the noisy re-
sult generated with the pseudoinverse, the TSVD result is clearly superior.
After all we are now getting two areas of activity, centered at the positions
of the true sources.
2 Inverse solutions with N = 10 and M = 8 7

Figure 6: Noisy reconstructed solution, with TSVD algorithm


3 Inverse solutions with N = 10 and M = 12 8

3 Inverse solutions with N = 10 and M = 12


3.1 Noiseless case
We now increase the source grid to M = 12, giving a total of 432 unknowns
while still keeping 300 measurements. We now have more unknown than
known parameters in the system and the pseduoinverse will not give good
results, or may even cease to exist [1]. Using the TSVD algorithm and
keeping all but the last two singular values (K = 298) gave the result shown
in fig 7. This basically gives us the solution gest of (1.3) with the lowest
norm.

Figure 7: Noiseless reconstruction with TSVD

It is seen that the reconstructed solution without noise is still very good.
The solution is no longer perfect as it was when we had more measurements
than unknowns, so the number of unknowns clearly affects the possibility
we have to find a good estimate gest . Basically we are now trying to solve a
problem with more degrees of freedom with the same amount of information,
so there is no surprise that the solution has worsened. All in all however,
the result is very good.

3.2 10 % additive noise


Noise is now added to the measurements in the same way as in section 2.2.
Using the TSVD algortihm, and keeping the 38 largest singular values gives
the reconstructed sources shown in figure 8
We see that it is still possible to make out distinct activity in the bottom
left and top right corners. Compared to the noiseless case however, the
3 Inverse solutions with N = 10 and M = 12 9

Figure 8: Noisy reconstruction with TSVD

results have gone from being near perfect to moderatly inaccurate. When
compared to the noisy results with M = 8, the amplitudes of the true
sources are now lower compared to the “false” sources being generated. It
has become more difficult to distinguish between areas with high and low
activity.
4 Tikhonov regularization 10

4 Tikhonov regularization
So far, the only method used to solve the inverse problem has been the
truncated singular value decomposition. Another commonly used method
is called Tikhonov regularization. The Tikhonov regularization algorithm
solves the following optimization problem:

min λ||gest ||22 + ||Fgest − y||22 (4.1)

where λ is a parameter supplied to the algorithm. Finding a proper value for


λ is the biggest difficulty when using Tikhonov regularization, as the choice
of λ may drastically alter the result. It is seen from (4.1) that a λ value that
is too high will force the solution vector gest towards zero, while a too small
value will give a solution which is highly sensitive to noise. For the TSVD
algorithm, we have a similar problem when choosing the t value. To find a
good value of λ, a so called L-curve may drawn. For the problem described
by eq (4.1) the L-curve is a plot of the solution norm ||gest ||2 versus the
residual norm ||Fgest − y||2 for different choices of λ. A good λ will often
be at a corner point of the L-curve, where a trade off value for keeping both
the norms small is found. Also note that in a similar way one may draw
L-curves for finding the parameter t of the TSVD as well. For the case of
N = 10, M = 8 and 10% additive noise of section 2.2, the corresponding
(Tikhonov) L-curve is shown in figure 9. The corner value corresponds to a
λ0 around 0.02665.

Figure 9: L-curve for finding λ

This λ0 value was used to create the reconstructed sources of figure 10.
4 Tikhonov regularization 11

The results are quite similar to the TSVD results in figure 6, however the
Tikhonov solution does a slightly better job of supressing sources that are
not there.

Figure 10: Reconstructed solution with λ0 = 0.02665

Following the previous discussion on the importance of chosing a rea-


sonable λ-parameter, the results for λ = 10λ0 is shown in figure 11 and
λ = 0.1λ0 in figure 12. Both of these solutions are inferior to both the TSVD
solution and the Tikhonov solution with λ0 . Choosing the right paramaters
is obviously critical to the correctness of the result. As expected, a high λ
value gives a solution with low values for the dipole sources, while a low λ
gives high source values.
4 Tikhonov regularization 12

Figure 11: Reconstructed solution with λ = 0.2665

Figure 12: Reconstructed solution with λ0 = 0.002665


5 Discussion 13

5 Discussion
5.1 M = 8 compared to M = 12
With M = 8 the linear system consists of 300 equations and 192 unknown
parameters. This means that the equation system (if there is no noise)
contains redundant information. This is referred to as a overdetermined
problem. With M = 12 we have still have a system of 300 equations, but
432 parameters. This means that there exists infinitly many solutions. This
makes it harder to generate good results since we do not have sufficient
information to solve the problem exactly. This is also seen in this project,
as even in the noiseless case we are not able to reconstruct the sources
perfectly in the underdetermined case. The more information we have the
better are the chances of generating good solutions to the inverse problem.
This is specially important when noise is present. However in a clinical
setting there is only room for a finite number of electrodes, this naturally
limits the number of equations in our system.

5.2 Interpretation of results


As discussed in the previous project, a difference between cardiac and brain
imaging is the number of sources needed to get realistic results. The cardiac
application can generally be modelled with fewer sources than what is needed
for the brain application. This means that brain imaging inverse problem is
more difficult to solve than the cardiac case. To get comparable results one
would need more sensors for the brain application compared to the cardiac
application.
Furthermore it has been shown how devastating the effect of measure-
ment noise can be. First of all a dipole which is located in a single position
is “smeared out” over a bigger area due to the effects of noise. This reduces
the spatial accuracy with which we are able to locate sources. To achieve
high accuracy one would also naturally have to increase the number of possi-
ble source positions (M ). This will again quickly lead to a underdetermined
system, which again makes it difficult to achieve high accuracy. This ex-
plains why it may be difficult to achieve very fine grain spatial resolution
when solving the inverse problem.
It should also be noted that in this project true source vector g was
known in advance. We have been able to compare reconstructed solutions
with the true solution. This is clearly not possible in a clinical setting. The
knowledge of g made it easy to judge whether the regularization parameter
chosen was good or not. It might not have been so easy to judge the quality
of the solution gest if g was not known. Obviously it is harder to choose
good parameters in real life. However utilizing methods like the “L-curve”
approach in section 4 makes it easier to choose good parameters. Using
anatomical information about the heart/brain, such as knowing the dipole
5 Discussion 14

distribution in normal conditions could also help in judging the quality of


the estimated gest .
References 15

References
[1] Per Christian Hansen. Regularization tools manual book.
http://www2.imm.dtu.dk/ pch/Regutools/RTv4manual.pdf.
6 Matlab code 16

6 Matlab code
Note that the functions gen tf and gen tf inc are identical, except that M =
8 in gen tf and M = 12 in gen tf inc.

6.1 Main script

close all
clear all
[F,y,g,Src x,Src y,Src z]=gen tf();

%% Noiseless case
figure(3)
g noiseless = inv(F'*F)*F'*y;
subplot(2,1,1)
plot src(Src x,Src y,Src z,g noiseless);
title(['Reconstructed magnitude of source dipoles (no noise)']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g noiseless,'r');
legend('True source vector', 'Estimated source vector (pseudoinverse)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f3', ['figures/1b rec ideal'])

%% 10% noise
close all
y noisy=y+0.1.*y.*randn(size(y));
[U,s,V]=csvd(F);
figure(4);
plot(s,'r');
hold on;
title(['Singular values of F−matrix']);
xlabel('Singular value number');ylabel('Singular value');
line(47*ones(1,20),linspace(0.2,0,20))
text(20,0.3,'Cutoff at singular value number 47')
print('−depsc2','−zbuffer','−f4', ['figures/1c F'])
figure(5);
g tenp=tsvd(U,s,V,y noisy,47);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g tenp);
title(['Reconstructed magnitude of source dipoles (10% noise, TSVD)']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g tenp,'r');
legend('True source vector', 'Estimated source vector (TSVD)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f5', ['figures/1c rec n'])
figure(10)
g inv = inv(F'*F)*F'* y noisy;
subplot(2,1,1)
plot src(Src x,Src y,Src z,g inv);
title(['Reconstructed magnitude of source dipoles (10% noise, pseudoinverse)']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g inv,'r');
legend('True source vector', 'Estimated source vector (pseudoinverse)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f10', ['figures/1c inv'])

%% Tikhonov
figure(6);
lambda=l curve(U,s,y noisy);
print('−depsc2','−zbuffer','−f6', ['figures/l curve'])
figure(7);
g tik=tikhonov(U,s,V,y noisy,lambda);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g tik);
title(['Reconstructed magnitude of source dipoles (10% noise, tikhonov (corner value))']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g tik,'r');
legend('True source vector', 'Tikohnov (corner value)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f7', ['figures/tikh cor'])
figure(8);
g tik=tikhonov(U,s,V,y noisy,lambda*10);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g tik);
title(['Reconstructed magnitude of source dipoles (10% noise, tikhonov (10 x corner value))']);
6 Matlab code 17

xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');


subplot(2,1,2); plot(g,'−'); hold on; plot(g tik,'r');
legend('True source vector', 'Tikohnov (10 x corner value)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f8', ['figures/tikh cor 10'])
figure(9);
g tik=tikhonov(U,s,V,y noisy,lambda*.10);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g tik);
title(['Reconstructed magnitude of source dipoles (10% noise, tikhonov (0.1 x corner value))']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g tik,'r');
legend('True source vector', 'Tikohnov (0.1 x corner value)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f9', ['figures/tikh cor 01'])

%% Increased source space


close all; clear all;
[F,y,g,Src x,Src y,Src z]=gen tf inc();

%% Noiseless case
figure(3)
[U,s,V]=csvd(F);
g noiseless=tsvd(U,s,V,y,298);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g noiseless);
title(['Reconstructed magnitude of source dipoles M=12 (no noise, TSVD)']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g noiseless,'r');
legend('True source vector', 'Estimated source vector (TSVD)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f3', ['figures/1d rec ideal'])

%% 10% noise
close all
y noisy=y+0.1.*y.*randn(size(y));
figure(5);
g tenp=tsvd(U,s,V,y noisy,39);
subplot(2,1,1)
plot src(Src x,Src y,Src z,g tenp);
title(['Reconstructed magnitude of source dipoles (10% noise, TSVD)']);
xlabel('X−axis − Lateral');ylabel('Y−axis − Caudal');
subplot(2,1,2); plot(g,'−'); hold on; plot(g tenp,'r');
legend('True source vector', 'Estimated source vector (TSVD)')
ylabel('Dipole source vector values g(i)')
xlabel('i'); print('−depsc2','−zbuffer','−f5', ['figures/1d est'])

6.2 gen tf

function [F,y,g,Src x,Src y,Src z]=gen tf

clear all
z sensor=4; % Sensors 4 units superior to dipole sources
N=10;
M=8;
addpath('regu');
close all;

% Create sensors with inter−sensor distance=1


sensor positions=linspace(−N/2+1/2,N/2−1/2,N);
[Sen x, Sen y, Sen z]=meshgrid(sensor positions,sensor positions,z sensor);

% Create sources with inter−source distance=1


src positions=linspace(−3.5,3.5,M);
[Src x, Src y, Src z]=meshgrid(src positions,src positions,0);

% Place the two dipoles:


Dipole1=M+2;
Dipole2=M*(M−1)−1;
g=zeros(Mˆ2*3,1);
g(3*(Dipole1−1)+1)=−0.2; % p x
g(3*(Dipole1−1)+2)=−0.2; % p y
g(3*(Dipole1−1)+3)=1; % p z
g(3*(Dipole2−1)+1)=0.5; % p x
g(3*(Dipole2−1)+2)=0.5; % p y
g(3*(Dipole2−1)+3)=0.1; % p z

% Generate transfer matrix:


F=inf(Nˆ2*3,Mˆ2*3);
6 Matlab code 18

for i=1:3:size(F,1)
[v,u]=ind2sub(N,ceil(i/3));
x=Sen x(u,v);
y=Sen y(u,v);
z=Sen z(u,v);
for j=1:3:size(F,2)
src nmb=ceil(j/3);
[q,w]=ind2sub(M,src nmb);
R=sqrt((x−Src x(w,q))ˆ2+(y−Src y(w,q))ˆ2+(z−Src z(w,q))ˆ2);
d x=x−Src x(w,q);
d y=y−Src y(w,q);
d z=z−Src z(w,q);
F(i,j)=0;
F(i,j+1)=d z/Rˆ3;
F(i,j+2)=−d y/Rˆ3;
F(i+1,j)=−d z/Rˆ3;
F(i+1,j+1)=0;
F(i+1,j+2)=d x/Rˆ3;
F(i+2,j)=d y/Rˆ3;
F(i+2,j+1)=−d x/Rˆ3;
F(i+2,j+2)=0;
end
end
F=(1/4*pi)*F;

% Generate magnetic field


y=F*g;

% Split components of sources to be able to graph them


M x=inf(M,M); M y=inf(M,M); M z=inf(M,M);
for i=1:3:length(g)
[v,u]=ind2sub(M,ceil(i/3));
M x(u,v)=g(3*ceil(i/3)−2);
M y(u,v)=g(3*ceil(i/3)−1);
M z(u,v)=g(3*ceil(i/3)−0);
end

% Generate magnetic field potential


field=inf(N,N);
for i=1:3:size(y,1)
[v,u]=ind2sub(N,ceil(i/3));
field(u,v)=sqrt(y(i)ˆ2+y(i+1)ˆ2+y(i+2)ˆ2);
end

figure(1)
surf(Sen x,Sen y,Sen z,field)
hold on; grid on
quiver3(Src x,Src y,Src z,M x,M y,M z,2,'r');
plot3(Src x,Src y,Src z,'bo','MarkerSize',5);
[v,u]=ind2sub(M,Dipole1);
plot3(Src x(u,v),Src y(u,v),Src z(u,v),'r.','MarkerSize',17);
[v,u]=ind2sub(M,Dipole2);
plot3(Src x(u,v),Src y(u,v),Src z(u,v),'r.','MarkerSize',17);
title(['Magnetic field strength, generated by matrix equation: h=Fg']);
xlabel('X−axis − Lateral');zlabel('Z−axis − Anterior');
ylabel('Y−axis − Caudal');
view(37.5, 30);
print('−depsc2','−zbuffer','−f1', ['figures/1a pot']);
figure(2)
plot src(Src x,Src y,Src z,g);
title(['True magnitude of source dipoles']);
xlabel('X−axis − Lateral');zlabel('Z−axis − Anterior');
print('−depsc2','−zbuffer','−f2', ['figures/1a src mag']);
end

6.3 Plot src

function plot src(Src x,Src y,Src z,g)


M=size(Src x,1);
% calculate magnitude
mag=inf(M,1);
for i=1:3:length(g)
mag(ceil(i/3))=sqrt(g(i)ˆ2+g(i+1)ˆ2+g(i+2)ˆ2);
end
maxi=max(mag);
sizes=[1,9,17,25,33,41,49,57];
hold on
for i = 1:length(mag)
[v,u]=ind2sub(M,i);
text(Src x(u,v)+.15,Src y(u,v)+0.4,num2str(mag(i),'%0.2f'))
if mag(i) > 0
6 Matlab code 19

plot3(Src x(u,v),Src y(u,v),Src z(u,v),'r.','MarkerSize',sizes(ceil((8/maxi)*mag(i))));


end
end
plot3(Src x,Src y,Src z,'bo','MarkerSize',5);
view(0,90)

You might also like