Adding noise to a Gaussian
이전 댓글 표시
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
2015년 8월 8일
Please show the traceback of the error message. Which line is reporting that error?
채택된 답변
추가 답변 (1개)
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
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().
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!