Adding noise to a Gaussian

4 次查看(过去 30 天)
My code keeps returning an error saying my signal-to-noise ratio must be a real scalar.
this is my code:
y=A.*exp((-(x-x_0).^2)./s);
% part a
A=100;
x=[-0.5:.01:.5-.01];
x_0=0;
s=1;
figure(2)
G=Gaussian(A,x,x_0,s);
plot(x,G)
% part b
n = rand(1,100);
Gnew1= y + 0*n;
Gnew2= y + 0.5*n;
Gnew3= y + 7.5*n;
Gnew4= y + 15*n;
figure(3)
w = awgn(x,n);
plot(x,G,x,w)
legend('Original Gaussian','Gaussian with Noise');
plot(x, Gnew1)
hold on
plot(x, Gnew2)
plot(x, Gnew3)
plot(x, Gnew4)
hold off
Here is the problem:
This problem deals with data fitting in the presence of noise.
a. Write a function Gaussian.m which will generate a 1D Gaussian function of the form y=A.*exp((-(x-x_0).^2)./s);, where s is the spread of the Gaussian, A is a constant factor and the mean x_0. The inputs to the function should be a vector of values (x), A, , and !. To test your function, plot the Gaussian corresponding to x= [-0.5:0.01:0.5-0.01], A = 100, s = 1, and x0= 0.
b. Add noise the Gaussian you generated above and plot the corresponding result. You may use the randn.m function in Matlab to generate a 100 random (noise) values between 0-1. Hence the new Gaussian function (Gnew = y + factor*noise) can be obtained. On the same graph, plot out Gnew for 4 different values of factor = {0.0, 0.5, 7.5, 15}.
  1 个评论
Walter Roberson
Walter Roberson 2015-8-8
Please show the traceback of the error message. Which line is reporting that error?

请先登录,再进行评论。

采纳的回答

Neo
Neo 2015-8-9
Does anyone know how to do part C for this problem?
C. Use the polyval and polyfit functions to fit polynomials of different degrees to the Gnew functions generated in (b) above. Fit 4 polynomials corresponding to degrees of 1, 2, 10, and 15 to Gnew for each value of factor (i.e. 0.0, 0.5, 7.5, 15). You should use the subplot function to generate 4 subplots on a single figure, each subplot corresponding to a different noise level.
  11 个评论
Walter Roberson
Walter Roberson 2015-8-9
noise = randn(size(y));
for Kb = 1 : nb
b = bvals(Kb);
Gnew{Kb} = y + b .* noise;
end
Image Analyst
Image Analyst 2015-8-9
Put that code into a function. Then just call the function for each of the 4 Gnew variables that you have.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-8-8
y = awgn(x,snr) adds white Gaussian noise to the vector signal x. The scalar snr specifies the signal-to-noise ratio per sample, in dB.
But your code has
n = rand(1,100);
w = awgn(x,n);
so the value you pass in for the second parameter is a vector 1 x 100, where the awgn routine needs a scalar.
  1 个评论
Image Analyst
Image Analyst 2015-8-8
Not to mention the fact that it said to use randn() - it didn't mention awgn(). Why do you want to use both? I'd use only the one noise addition function that you were told to use, and that is randn().

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Propagation and Channel Models 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by