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 comentario

Walter Roberson
Walter Roberson el 8 de Ag. de 2015
Please show the traceback of the error message. Which line is reporting that error?

Iniciar sesión para comentar.

 Respuesta aceptada

Neo
Neo el 9 de Ag. de 2015

0 votos

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 comentarios

Walter Roberson
Walter Roberson el 9 de Ag. de 2015
Editada: Walter Roberson el 9 de Ag. de 2015
degrees = [1 2 10 15];
for K = 1 : length(degrees)
degree = degrees(K);
pp = polyfit(x, Gnew, degree);
fit_Gnew{K} = polyval(x, pp);
end
Neo
Neo el 9 de Ag. de 2015
How do I graph it then?
Nicole Bonino
Nicole Bonino el 9 de Ag. de 2015
how did you write the code for part b for Gnew to keep it one variable. I have Gnew1, Gnew2, etc. for each noise factor. to use this code for part C I would need one Gnew variable.
Nicole Bonino
Nicole Bonino el 9 de Ag. de 2015
I figure I must use a loop, but I can't seem to get it to work.
Neo
Neo el 9 de Ag. de 2015
I am unsure of how to use just one Gnew variable either. Also can't figure out how to use Gnew = y + factor*noise cause if you manually put the values in for factor it keeps giving a size error
Nicole Bonino
Nicole Bonino el 9 de Ag. de 2015
I used noise=rand(1,100) to get my noise values and then Gnew1=y+0*noise, Gnew2=y+.5*noise, etc. and it worked. But I don't know how to use that code I made with Gnew for part C. I also am unsure of how many figures it is asking for. I read it as 4 figures each with 4 subplots but I don't think that is right.
degrees = [1 2 10 15];
nd = length(degrees);
bvals = [0.0, 0.5, 7.5, 15];
nb = length(bvals);
for Kb = 1 : nb
b = bvals(Kb);
Gnew{Kb} = .....
end
for Kd = 1 : nd
subplot(nd, 1, Kd)
degree = degrees(Kd);
for Kb = 1 : nb
b = bvals(Kb)
pp = polyfit(x, Gnew{Kb}, degree);
fit_Gnew = polyval(x, pp);
plot(x, fit_Gnew);
hold on
end
end
Walter Roberson
Walter Roberson el 9 de Ag. de 2015
4 subplots per figure, one per noise level. all of the different fitting degrees combined in the same subplot.
The code I show above does it the other way around, but you can easily switch the order of the statements.
Nicole Bonino
Nicole Bonino el 9 de Ag. de 2015
Editada: Nicole Bonino el 9 de Ag. de 2015
how would you make the Gnew variable? right now I have 4 seperate Gnew variables for each noise factor so this code cant work for me unless I have a single Gnew factor. I've been trying to make a for loop but it keeps giving me an error saying my dimensions don't agree for the multiplication in the Gnew equation. this is what I have so far.
A=100;
x=[-0.5:.01:(.5-.01)];
x_0=0;
s=1;
y=A.*exp((-(x-x_0).^2)./s);
figure(2)
G = Gaussian(A, x, x_0, s);
plot(x,G)
title('Gaussian with Given Values')
xlabel('x-axis')
ylabel('y-axis')
% part b
noise = randn(10);
x=1;
n=[0, .5, 7.5, 15];
for x=1:1:length(n);
Gnew(n(x))=y+n*noise;
x=x+1;
end
noise = randn(size(y));
for Kb = 1 : nb
b = bvals(Kb);
Gnew{Kb} = y + b .* noise;
end
Image Analyst
Image Analyst el 9 de Ag. de 2015
Put that code into a function. Then just call the function for each of the 4 Gnew variables that you have.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 8 de Ag. de 2015

0 votos

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 comentario

Image Analyst
Image Analyst el 8 de Ag. de 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().

Iniciar sesión para comentar.

Categorías

Preguntada:

el 8 de Ag. de 2015

Comentada:

el 9 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by