Write a function that given a vector x generates a vector y=sin(x) in which it has added a certain noise (simulating to be typical of the environment). This noise must be of mean 0 and ¼ standard deviation, use the randn() function (doc randn for more information). Graph the original function and the function with added noise, contrast both graphs with different colors. Vary the mean and/or standard deviation on the added noise and analyze the behavior on the graph.
Based on this, I have to do another function that receives this vector, filters it and graphs it by using a formule. My problem is that I don't get how to do this with the code

2 Commenti

Steven Lord
Steven Lord il 13 Apr 2022
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Levi Ackerman
Levi Ackerman il 13 Apr 2022
I'm having problems to use plot, I don't know if I'm using it correctly or if I'm missing something. I did a script and a function:
Script:
%%
x=rand([1,n]);
n=input('Insert columns quantity: \n');
plot(y,'r');
Function:
function [y] = NoiseGen1(x,n)
% Calculates sin for vector x
% y(x)=sin(x)
y=sin(x);
plot(y,r)
end

Accedi per commentare.

 Risposta accettata

Image Analyst
Image Analyst il 13 Apr 2022
In this:
function [y] = NoiseGen1(x,n)
% Calculates sin for vector x
% y(x)=sin(x)
y=sin(x);
plot(y,r)
end
You don't need to pass in n. And r is not defined in the function so that will throw an error (perhaps you meant 'r'). Plus the plotting is done in your main program so don't do it in your function. Then you need to make noise with randn() and add it to your signal like it specifically told you to do. So the function needs to be like this
function y = NoiseGen1(x, mu, sigma)
% Calculates sin for vector x
% y(x)=sin(x)
noiseSignal = mu + sigma * randn(size(x));
y = sin(x) + noiseSignal;
end
Next, don't use rand() to get your x values. Use linspace(). Look it up.
In general this is how to create a sine wave
x = linspace(xMin, xMax, numSamples); % Say for example 0, 40, 500 or whatever
period = 5; % or whatever
y = amplitude * sin(2 * pi * x / period);

2 Commenti

Levi Ackerman
Levi Ackerman il 13 Apr 2022
Got it. Also, I should assign values to sigma and mu...sigma should be 1/4 and mu=0?
Image Analyst
Image Analyst il 13 Apr 2022
At least, but it sounds like they want those in a vector and you loop over all the values in the vector because it said it wants you to try different values.

Accedi per commentare.

Più risposte (1)

David Hill
David Hill il 13 Apr 2022
x=0:.01:2*pi;
sigma=0.25;
s=sigma*randn(1,length(x));
y=sin(x);
plot(x,y,x,y+s);

Prodotti

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by