2017-08-03 51 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