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 个评论

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.
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

请先登录,再进行评论。

 采纳的回答

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 个评论

Got it. Also, I should assign values to sigma and mu...sigma should be 1/4 and mu=0?
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.

请先登录,再进行评论。

更多回答(1 个)

x=0:.01:2*pi;
sigma=0.25;
s=sigma*randn(1,length(x));
y=sin(x);
plot(x,y,x,y+s);

类别

产品

版本

R2022a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by