Contour Plot Issue - Is the data shown correctly?

2 ビュー (過去 30 日間)
Shrishti Yadav
Shrishti Yadav 2021 年 11 月 3 日
回答済み: Nipun 2024 年 5 月 16 日
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

回答 (1 件)

Nipun
Nipun 2024 年 5 月 16 日
Hi Shrishti,
I understand that you are trying to generate a contour plot that visualizes the total distance from any point in the XY plane to the intersection points of circles centered at three microphone locations, with the radius being the distance from the microphones to a source location. You suspect that the contour plot is not accurately reflecting changes in the source location, and you are looking for a way to correct this issue in your MATLAB code.
To address this, ensure that each step in your process—from calculating the distances and intersection points to plotting the contour—is correctly implemented. Here is a concise approach to generating an accurate contour plot:
  1. Ensure Intersection Points Are Correct: Verify that the function intersect2 correctly calculates the intersection points between the grid lines and the circles.
  2. Accurately Calculate Distances: Make sure the distances between the correct pairs of intersection points are calculated accurately.
  3. Correctly Reshape and Plot nt: The total distance nt must be reshaped according to the grid used for the contour plot. Use meshgrid to generate the grid and ensure nt matches this grid's dimensions.
Here is the corrected MATLAB code focusing on generating the contour plot correctly:
% Assuming nt is calculated correctly for each point in the grid
[X, Y] = meshgrid(x, y); % Generate the grid for the XY plane
% Reshape nt to match the grid's dimensions for accurate plotting
Z = reshape(nt, [length(y), length(x)]); % Adjust dimensions as necessary
% Plotting setup
figure; hold on; % Open a new figure and hold it for multiple plots
plot(xm1, ym1, 'k', xm2, ym2, 'r', xm3, ym3, 'g'); % Plot circles for microphones
plot([M1(1), M2(1), M3(1)], [M1(2), M2(2), M3(2)], 's'); % Plot microphone locations
plot(S(1), S(2), 'kx'); % Plot source location
% Generate and display the contour plot
contour(X, Y, Z, 30, 'ShowText', 'off');
xlabel('X'); ylabel('Y'); title('Total Distance Contour Plot');
hold off;
Ensure that:
  • The meshgrid function is used correctly to generate X and Y grids that cover the entire XY plane you are interested in.
  • nt is reshaped accurately to match the dimensions of X and Y. This ensures that the contour plot correctly represents the total distances at each point in the XY plane.
  • You verify the logic in calculating nt to ensure that it accurately represents the total distances from the intersection points to ensure the contour plot changes as expected with the source location.
Hope this helps.
Regards,
Nipun

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by