[英]How to create noise for a 2D Gaussian?
I'm trying to practice curve fitting on a 2D Gaussian, but in order to do that I need to add random noise to my predefined Gaussian. 我正在尝试在2D高斯曲线上进行曲线拟合,但为此,我需要向预定义的高斯曲线中添加随机噪声。 My first instinct was to cycle through two for loops and create two matrices X and Y with random numbers, but when I tried that (I don't have the code anymore) Matlab wouldn't let me plot the Gaussian because I didn't generate my X and Y values using the meshgrid function.
我的第一个直觉是在两个for循环中循环,并创建两个具有随机数的矩阵X和Y,但是当我尝试这样做时(我不再有代码了),Matlab不会让我绘制高斯曲线,因为我没有使用meshgrid函数生成我的X和Y值。 Since I seem to need to use meshgrid, can anyone help me figure out how to generate a random meshgrid so I can add some noise to my Gaussian?
由于我似乎需要使用Meshgrid,因此有人可以帮助我找出如何生成随机MeshGrid的方法,以便为高斯添加一些噪声吗?
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
%X = 1:1:100;
%Y = 1:1:100;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Add noise now
EDIT: So I found out that rand can return a random matrix which will work with the surf function (for some reason it wasn't working for me earlier though). 编辑:所以我发现rand可以返回一个随机矩阵,该矩阵可以与surf函数一起工作(由于某种原因,它对我而言较早就不起作用了)。 The result looks something like this: noisy 2D gaussian
结果看起来像这样: 嘈杂的2D高斯
amp = 1;
x0 = 0;
y0 = 0;
sigmaX = 1;
sigmaY = 1;
[X,Y] = meshgrid(-3:.1:3);
%Z = X .* exp(-X.^2 - Y.^2);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z);
%Make some noise
[xRows, xColumns] = size(X);
[yRows, yColumns] = size(Y);
figure(2)
X = -.1 + (.1+.1)*rand(61,61);
Y = -.1 + (.1+.1)*rand(61,61);
Z = amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)));
surf(X, Y, Z)
But I feel like the Gaussian has largely lost it's typical bell shape and looks more like a slope field than anything. 但是我觉得高斯人已经大大失去了典型的钟形形状,看起来更像是坡地。 I'm going to try and refine it but I would love any input.
我将尝试完善它,但我会很乐意提供任何输入。
That's what i would do. 那就是我会做的。
amp=1;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
noiseAmp=.1;
x=[-2:.1:2];
y=[-2:.1:2];
%Create two Noise Vectors
noisez1=noiseAmp.*rand(1,length(x));
noisez2=noiseAmp.*rand(1,length(x));
% Make an meshgrid out of the two Vectors
[noiseZ1,noiseZ2]=meshgrid(noisez1,noisez2);
% Add the Meshgrids togehter
Noise=noiseZ1+noiseZ2;
[X,Y]=meshgrid(x,y);
% Add the Noise to the result of Z
Z=amp*exp(-((X-x0).^2/(2*sigmaX^2)+(Y-y0).^2/(2*sigmaY^2)))+Noise;
surf(X,Y,Z);
if you just want a 2D plot you can try this 如果您只想要2D图,可以尝试一下
amp=1;
noiseAmp=0.01;
x0=0;
y0=0;
sigmaX=1;
sigmaY=1;
x=[-5:.01:5];
noiseY=noiseAmp*rand(1,length(x));
y=noiseY+amp*exp(-((x-x0).^2/(2*sigmaX^2)));
plot(x,y);
where noiseAmp is the Amplitude of the noise. 其中noiseAmp是噪声的振幅。
But if you still want to create a 3D plot with the surf() function, you have to add a random meshgrid to the Z result. 但是,如果仍然要使用surf()函数创建3D图,则必须向Z结果添加随机网格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.