Matlab 在V形坡度DEM中插入随机噪波
使用以下代码,我将生成一个具有两个不同坡度的V平面,分别为10°和20°Matlab 在V形坡度DEM中插入随机噪波,matlab,matlab-figure,noise,Matlab,Matlab Figure,Noise,使用以下代码,我将生成一个具有两个不同坡度的V平面,分别为10°和20° % /* % Assumptions % */ % resolution [m] res = 1; % inclination [deg] i1 = 10; i2 = 20; % /* % DEM -> V shape % */ % pre-allocate output testDEM = zeros(513); % required elevation step [m] hstep =
% /*
% Assumptions
% */
% resolution [m]
res = 1;
% inclination [deg]
i1 = 10; i2 = 20;
% /*
% DEM -> V shape
% */
% pre-allocate output
testDEM = zeros(513);
% required elevation step [m]
hstep = res*tan(i1*(pi/180));
% elevation start right [m]
k = 513*(2/3)*tan(i1*(pi/180));
% coordinates
q = length(1:513*(2/3));
% initialize
nStep = 0;
for jj = 1:q
testDEM(:,jj) = k-nStep;
nStep = nStep + hstep;
end
% change elevation step
step = res*tan(i2*(pi/180));
% update nStep
nStep = step;
% elevation start left [m]
start = testDEM(end,q);
for jj = q+1:513
testDEM(:,jj) = start + nStep;
nStep = nStep + step;
end
testDEM = testDEM(1:507,1:507);
%//Plot test DEM
f_tSlope = figure();
set(gca,'PlotBoxAspectRatio',[1 1 1]);
surf(testDEM, 'EdgeColor', 'none')
colormap jet;
hb = colorbar('location','eastoutside');
hb.Label.String = '[m]';
hb.Label.Rotation = 0;
hb.Label.HorizontalAlignment = 'Left';
下面我将在每个位置添加噪音
sigma = 1;
testDEM = testDEM + sigma*randn(size(testDEM));
但我想要的是在随机位置添加随机噪声,而不是在任何地方。我怎么做
提前谢谢这个怎么样:
N_locations = 100; % no. of locations to add random noise
% randomize 'N_locations' linear indecies in 'testDEM':
noise_location = randi(numel(testDEM),N_locations,1);
% add the noise:
testDEM(noise_location) = testDEM(noise_location)+sigma*randn(N_locations,1);
这将随机化地图上的N_位置
随机位置,并对每个位置应用不同的随机噪声
如果您希望向所有随机位置添加相同的噪声,只需编写sigma*randn
,后面不带括号
对于小型N_位置
,这应该足够了。但是,如果要确保不拾取同一位置两次,或者N\u locations
较大,可以如下设置noise\u location
:
noise_location = randperm(numel(testDEM),N_locations);
因此,在
testDEM
中只有索引的非重复值。该代码以0.5的概率添加了噪声
testDEM = testDEM + sigma*randn(size(testDEM)) .* (rand(size(testDEM)) > 0.5);
你曾经用过randperm吗?老实说,您最好只使用randperm。
randperm
是一个较慢的程序,因此如果这将对少量位置应用多次,那么randi
可能就足够了。公平地说,对于我所做的事情,创建随机变量不到总处理时间的.01%,所以我通常不担心它。