【问题标题】:Insert random noise in a V slope DEM在 V 斜率 DEM 中插入随机噪声
【发布时间】:2017-06-19 10:30:31
【问题描述】:

使用以下代码,我生成了一个具有 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));

但我想要的是在随机位置添加随机噪声,而不是在任何地方。我该怎么做?

提前致谢

【问题讨论】:

标签: matlab matlab-figure noise


【解决方案1】:

这个怎么样:

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 随机位置,并对每个位置应用不同的随机噪声。

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

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

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

所以testDEM 中只有非重复的索引值。

【讨论】:

  • 老实说,你最好只使用 randperm。
  • randperm 速度较慢,因此如果要在少数位置多次应用此功能,那么randi 可能就足够了。
  • 很公平,对于我所做的事情,创建随机变量不到总处理时间的 0.01%,所以我通常不用担心。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2019-01-25
    • 2020-06-18
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    相关资源
    最近更新 更多