Adding noise to a Gaussian

6 views (last 30 days)
Nicole Bonino
Nicole Bonino on 8 Aug 2015
Commented: Image Analyst on 9 Aug 2015
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 Comment
Walter Roberson
Walter Roberson on 8 Aug 2015
Please show the traceback of the error message. Which line is reporting that error?

Sign in to comment.

Accepted Answer

Neo
Neo on 9 Aug 2015
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 Comments
Walter Roberson
Walter Roberson on 9 Aug 2015
noise = randn(size(y));
for Kb = 1 : nb
b = bvals(Kb);
Gnew{Kb} = y + b .* noise;
end
Image Analyst
Image Analyst on 9 Aug 2015
Put that code into a function. Then just call the function for each of the 4 Gnew variables that you have.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 8 Aug 2015
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 Comment
Image Analyst
Image Analyst on 8 Aug 2015
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().

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!