Adding noise to a Gaussian

조회 수: 4 (최근 30일)
Nicole Bonino
Nicole Bonino 2015년 8월 8일
댓글: Image Analyst 2015년 8월 9일
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