在 V 斜率 DEM 中插入随机噪声

Insert random noise in a V slope DEM

使用以下代码,我生成了一个具有 2 个不同斜率的 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 = 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_locations 个随机位置,并对每个位置应用不同的随机噪声。

如果您希望将相同噪声添加到所有随机位置,只需写sigma*randn,后面没有括号。

对于小 N_locations 这应该足够了。但是,如果你想确保你不会选择相同的位置两次,或者 N_locations 很大,你可以这样设置 noise_location

noise_location = randperm(numel(testDEM),N_locations);

所以您在 testDEM.

中只有非重复的索引值

此代码以 0.5 的概率添加噪声

testDEM = testDEM + sigma*randn(size(testDEM)) .* (rand(size(testDEM)) > 0.5);