2017-08-03 49 views
1

我有如下两个向量:如何基于值创建色彩映射?

x = 0:5:50; 
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20] 

x表示在x轴和sir_dB的SNR的距离。对于这一点,我需要生成一个彩色地图为50×60米类似于这样一个网格:基于对sir_dB

enter image description here

我试过如下:

sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]; 
xrange = 0:50; 
yrange = -30:30; 
% create candidate set 
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1. 
candidate_set = [X(:), Y(:)]; 
test_pt = [0 30]; 
radius = 5; 
% find which of these are within the radius of selected point: 
idx = rangesearch(candidate_set, test_pt, radius); 
neighborhood = candidate_set(idx{1}, :); 

一旦我有5米半径的邻居,我需要的色彩为基础的sir_dB值相应x值电网的一部分。

我需要有情节以这样的方式,对于比15 sir_dB更大所有值,电网应为绿色,黄色为y大于0,红色y低于-20更大。

有人可以提供我如何做到最好的输入?

+0

这听起来像你想的'contourf'功能。 – jodag

回答

1

林不知道你想要什么,但这应该让你开始contourf。我增加了xrange和yrange的粒度以使半径更加平滑,但如果需要,可以将其更改回去。

x = 0:5:50; 
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]; 
xrange = 0:0.1:50; 
yrange = -30:0.1:30; 
% create candidate set 
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1. 
candidate_set = [X(:), Y(:)]; 

test_pt = [0 30]; 
r = sqrt((test_pt(1)-X(:)).^2 + (test_pt(2)-Y(:)).^2); 
idx = r>5; 
snr = nan(size(X)); 
snr(idx) = interp1(x,sir_dB,X(idx),'linear'); 

% Some red, yellow, green colors 
cmap = [0.8500 0.3250 0.0980; 
     0.9290 0.6940 0.1250; 
     0   0.7470 0.1245]; 

figure(); 
colormap(cmap); 
contourf(X,Y,snr,[-20,0,15],'LineStyle','none'); 

在原始sir_dB旁边绘制轮廓图,我们看到它排成一列(假设您想要线性插值)。如果您不想使用线性插值,请使用'prev'或'next'作为interp1方法。

figure(); 
colormap(cmap); 
subplot(2,1,1); 
contourf(X,Y,snr,[-20,0,15],'LineStyle','none'); 
subplot(2,1,2); 
plot([0,50],[-20,-20],'-r',[0,50],[0,0],'-y',[0,50],[15,15],'-g',x,sir_dB); 

enter image description here

+0

这正是我所需要的。一个问题,但。如果我想增加轮廓图的X轴,比如从0到80而不是0到50,我在这个区域得到一个50到80之间的空白区域,而我希望它是红色的。如何修改这种情况下的轮廓图? – smyslov

+0

默认情况下,Matlab不会外推。如果你想推断你需要告诉'interp1'外推。例如:更改'xrange = 0:0.1:80;'和'snr(idx)= interp1(x,sir_dB,X(idx),'linear','extrap');'。或者,你可以使用像'-20'这样的常量,而不是'extrap''。 – jodag

0

这里是另一个建议,使用imagesc了点。我nothed在下面的代码的变化与% ->

x = 0:5:50; 
sir_dB = [50 20 10 5 2 0 -5 -10 -20 -20 -20]; 
xrange = 0:50; 
yrange = -30:30; 
% create candidate set 
[X, Y] = ndgrid(xrange, yrange); % grid of points with a spacing of 1. 
% -> create a map for plotting 
Signal_map = nan(size(Y)); 
candidate_set = [X(:), Y(:)]; 
test_pt = [10 20]; 
radius = 35; 
% find which of these are within the radius of selected point: 
idx = rangesearch(candidate_set,test_pt,radius); 
neighborhood = candidate_set(idx{1}, :); 
% -> calculate the distance form the test point: 
D = pdist2(test_pt,neighborhood); 
% -> convert the values to SNR color: 
x_level = sum(x<D.',2); 
x_level(x_level==0)=1; 
ColorCode = sir_dB(x_level); 
% -> apply the values to the map: 
Signal_map(idx{1}) = ColorCode; 
% -> plot the map: 
imagesc(xrange,yrange,rot90(Signal_map,2)) 
axis xy 
% -> apply custom color map for g-y-r: 
cmap = [1 1 1 % white 
     1 0 0 % red 
     1 1 0 % yellow 
     0 1 0];% green 
colormap(repelem(cmap,[1 20 15 35],1)) 
c = colorbar; 
% -> scale the colorbar axis: 
caxis([-21 50]); 
c.Limits = [-20 50]; 
c.Label.String = 'SNR'; 

结果:

enter image description here

+0

这段代码在'x_level = sum(x smyslov

+0

@smyslov处抛出一个错误,你使用什么版本的Matlab?如果早于2016b,则将此行更改为:'x_level = sum(bsxfun(@ lt,x,D。',2);' – EBH