Contour Plot Issue – Is the data shown correctly?
Code does the following: Given 3 mic locations and a source S location, it calculates the intersection points from any location in the x-y plane with a circle centered at the mic locations with radius as the distance between the mic and the source.
The distance between the respective intersection points are calculated and added together. So at the source location, the total distance is approximately zero.
The contour plot is meant to plot the total added distance(‘nt’ in the code) against the X-Y plane. I am not sure if I am plotting it correctly. The contour plot doesn’t seem to change shape with change of the source location and that is not right. I checked at the source location if Z1 = Z2 = Z3 and they do. I am not sure if I am plotting incorrectly.
The code should run smoothly – all the functions I created are at the very end of the code.
clear;clc; %Clearing Variables
%% Creating Data
% defining locations
S = [ 0 40 0 ]; %source location [x y z]
M1 =[10 0 0];% Mic 1 location
M2 = [20 0 0];%Mic 2
M3 =[30 0 0]; %Mic 3
% Plane
a = -50;
b = 50;
x = a:b;
y = (a:b).’;
v = numel(x);
%% Calculations
%distance calculated for x-y plane
r1 = normal(S(1,1),M1(1,1),S(1,2),M1(1,2));
r2 = normal(S(1,1),M2(1,1),S(1,2),M2(1,2));
r3 = normal(S(1,1),M3(1,1),S(1,2),M3(1,2));
% Getting the intersection points
Z1 =intersect2(x,y,M1(1),M1(2),r1);
Z2=intersect2(x,y,M2(1),M2(2),r2);
Z3=intersect2(x,y,M3(1),M3(2),r3);
% Calculating the distance between intersection points
[A,~]= size(Z1);
n1 = zeros(A,1);
n2 = zeros(A,1);
n3 = zeros(A,1);
for i = 1:A
n1(i) = norm(Z2(i)-Z1(i));
n2(i) = norm(Z3(i)-Z2(i));
n3(i) = norm(Z1(i)-Z3(i));
end
%total distance for each set of intersection points
nt = n1 + n2 + n3;
%% PLOTTING
%making circles
%for x-y plane
[xm1,ym1] = circle(M1(1,1),M1(1,2),r1);
[xm2,ym2] = circle(M2(1,1),M2(1,2),r2);
[xm3,ym3] = circle(M3(1,1),M3(1,2),r3);
% % %for the x-y plane
%
[X, Y]= ndgrid(x,x);
ZZ = [X(:) , Y(:)];
X = reshape(ZZ(:,1),[v,v]);
Y = reshape(ZZ(:,2),[v,v]);
Z = reshape(nt,[v,v]);
plot(xm1,ym1,’k’)
hold on
plot(M1(1),M1(2),’ks’)
hold on
plot(xm2,ym2,’r’)
plot(M2(1),M2(2),’rs’)
plot(xm3,ym3,’g’)
plot(M3(1),M3(2),’gs’)
plot(S(1),S(2),’ks’)
contour(X,Y,Z,30,’ShowText’,’off’)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xunit, yunit] = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
hold off
end
function d = normal(x1,x2,y1,y2)
d = sqrt((x2-x1).^2+(y2-y1).^2);
end
function [Z] = intersect2(x,y,cx,cy,r)
%calculates the intersection points of a line and a circle
u = numel(x);
Z = zeros(u*u,2);
% x = -a:b;
% y = (-a:b).’;
theta = atan2((y-cy) ,(x-cx));
% chk = r* sin(theta)
Zx = cx + r*cos(theta);
Zy = cy + r*sin(theta);
Z = [Zx(:) Zy(:)];
endCode does the following: Given 3 mic locations and a source S location, it calculates the intersection points from any location in the x-y plane with a circle centered at the mic locations with radius as the distance between the mic and the source.
The distance between the respective intersection points are calculated and added together. So at the source location, the total distance is approximately zero.
The contour plot is meant to plot the total added distance(‘nt’ in the code) against the X-Y plane. I am not sure if I am plotting it correctly. The contour plot doesn’t seem to change shape with change of the source location and that is not right. I checked at the source location if Z1 = Z2 = Z3 and they do. I am not sure if I am plotting incorrectly.
The code should run smoothly – all the functions I created are at the very end of the code.
clear;clc; %Clearing Variables
%% Creating Data
% defining locations
S = [ 0 40 0 ]; %source location [x y z]
M1 =[10 0 0];% Mic 1 location
M2 = [20 0 0];%Mic 2
M3 =[30 0 0]; %Mic 3
% Plane
a = -50;
b = 50;
x = a:b;
y = (a:b).’;
v = numel(x);
%% Calculations
%distance calculated for x-y plane
r1 = normal(S(1,1),M1(1,1),S(1,2),M1(1,2));
r2 = normal(S(1,1),M2(1,1),S(1,2),M2(1,2));
r3 = normal(S(1,1),M3(1,1),S(1,2),M3(1,2));
% Getting the intersection points
Z1 =intersect2(x,y,M1(1),M1(2),r1);
Z2=intersect2(x,y,M2(1),M2(2),r2);
Z3=intersect2(x,y,M3(1),M3(2),r3);
% Calculating the distance between intersection points
[A,~]= size(Z1);
n1 = zeros(A,1);
n2 = zeros(A,1);
n3 = zeros(A,1);
for i = 1:A
n1(i) = norm(Z2(i)-Z1(i));
n2(i) = norm(Z3(i)-Z2(i));
n3(i) = norm(Z1(i)-Z3(i));
end
%total distance for each set of intersection points
nt = n1 + n2 + n3;
%% PLOTTING
%making circles
%for x-y plane
[xm1,ym1] = circle(M1(1,1),M1(1,2),r1);
[xm2,ym2] = circle(M2(1,1),M2(1,2),r2);
[xm3,ym3] = circle(M3(1,1),M3(1,2),r3);
% % %for the x-y plane
%
[X, Y]= ndgrid(x,x);
ZZ = [X(:) , Y(:)];
X = reshape(ZZ(:,1),[v,v]);
Y = reshape(ZZ(:,2),[v,v]);
Z = reshape(nt,[v,v]);
plot(xm1,ym1,’k’)
hold on
plot(M1(1),M1(2),’ks’)
hold on
plot(xm2,ym2,’r’)
plot(M2(1),M2(2),’rs’)
plot(xm3,ym3,’g’)
plot(M3(1),M3(2),’gs’)
plot(S(1),S(2),’ks’)
contour(X,Y,Z,30,’ShowText’,’off’)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xunit, yunit] = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
hold off
end
function d = normal(x1,x2,y1,y2)
d = sqrt((x2-x1).^2+(y2-y1).^2);
end
function [Z] = intersect2(x,y,cx,cy,r)
%calculates the intersection points of a line and a circle
u = numel(x);
Z = zeros(u*u,2);
% x = -a:b;
% y = (-a:b).’;
theta = atan2((y-cy) ,(x-cx));
% chk = r* sin(theta)
Zx = cx + r*cos(theta);
Zy = cy + r*sin(theta);
Z = [Zx(:) Zy(:)];
end Code does the following: Given 3 mic locations and a source S location, it calculates the intersection points from any location in the x-y plane with a circle centered at the mic locations with radius as the distance between the mic and the source.
The distance between the respective intersection points are calculated and added together. So at the source location, the total distance is approximately zero.
The contour plot is meant to plot the total added distance(‘nt’ in the code) against the X-Y plane. I am not sure if I am plotting it correctly. The contour plot doesn’t seem to change shape with change of the source location and that is not right. I checked at the source location if Z1 = Z2 = Z3 and they do. I am not sure if I am plotting incorrectly.
The code should run smoothly – all the functions I created are at the very end of the code.
clear;clc; %Clearing Variables
%% Creating Data
% defining locations
S = [ 0 40 0 ]; %source location [x y z]
M1 =[10 0 0];% Mic 1 location
M2 = [20 0 0];%Mic 2
M3 =[30 0 0]; %Mic 3
% Plane
a = -50;
b = 50;
x = a:b;
y = (a:b).’;
v = numel(x);
%% Calculations
%distance calculated for x-y plane
r1 = normal(S(1,1),M1(1,1),S(1,2),M1(1,2));
r2 = normal(S(1,1),M2(1,1),S(1,2),M2(1,2));
r3 = normal(S(1,1),M3(1,1),S(1,2),M3(1,2));
% Getting the intersection points
Z1 =intersect2(x,y,M1(1),M1(2),r1);
Z2=intersect2(x,y,M2(1),M2(2),r2);
Z3=intersect2(x,y,M3(1),M3(2),r3);
% Calculating the distance between intersection points
[A,~]= size(Z1);
n1 = zeros(A,1);
n2 = zeros(A,1);
n3 = zeros(A,1);
for i = 1:A
n1(i) = norm(Z2(i)-Z1(i));
n2(i) = norm(Z3(i)-Z2(i));
n3(i) = norm(Z1(i)-Z3(i));
end
%total distance for each set of intersection points
nt = n1 + n2 + n3;
%% PLOTTING
%making circles
%for x-y plane
[xm1,ym1] = circle(M1(1,1),M1(1,2),r1);
[xm2,ym2] = circle(M2(1,1),M2(1,2),r2);
[xm3,ym3] = circle(M3(1,1),M3(1,2),r3);
% % %for the x-y plane
%
[X, Y]= ndgrid(x,x);
ZZ = [X(:) , Y(:)];
X = reshape(ZZ(:,1),[v,v]);
Y = reshape(ZZ(:,2),[v,v]);
Z = reshape(nt,[v,v]);
plot(xm1,ym1,’k’)
hold on
plot(M1(1),M1(2),’ks’)
hold on
plot(xm2,ym2,’r’)
plot(M2(1),M2(2),’rs’)
plot(xm3,ym3,’g’)
plot(M3(1),M3(2),’gs’)
plot(S(1),S(2),’ks’)
contour(X,Y,Z,30,’ShowText’,’off’)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xunit, yunit] = circle(x,y,r)
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
hold off
end
function d = normal(x1,x2,y1,y2)
d = sqrt((x2-x1).^2+(y2-y1).^2);
end
function [Z] = intersect2(x,y,cx,cy,r)
%calculates the intersection points of a line and a circle
u = numel(x);
Z = zeros(u*u,2);
% x = -a:b;
% y = (-a:b).’;
theta = atan2((y-cy) ,(x-cx));
% chk = r* sin(theta)
Zx = cx + r*cos(theta);
Zy = cy + r*sin(theta);
Z = [Zx(:) Zy(:)];
end contour, plot, data, time of flight, numerical calculation, matlab MATLAB Answers — New Questions