Matlab - Ayuda con trazar una gráfica smoothed!!


Ayuda con trazar una gráfica smoothed!!

Publicado por Samuel Otero (1 intervención) el 01/12/2010 23:03:25
Hola a todos, no acostumbro a preguntar nada pq no me gusta recibir ayuda a menos que realmente la necesite pero ya esto es el colmo. Estoy haciendo unos "assignments" del MIT como entrenamiento para trabajar en cierto sitio, y en una se me pide que traze una gráfica pero de forma "Smoothed" osea, que de una gráfica q tiene un monton de "puntos" traze una sacando la "media" (mean) de un vector que se me es dado. Estas son las instrucciones:

Smoothing filter. Although it is a really useful function, Matlab does not contain an easy to use smoothing filter. Write a function with the declaration: smoothed=rectFilt(x,width). The filter should take a vector of noisy data (x) and smooth it by doing a symmetric moving average with a window of the specified width. For example if width=5, then smoothed(n) should equal mean(x(n-2:n+2)). Note that you may have problems around the edges: when n<3and n>length(x)-2.
The lengths of x and smoothedshould be equal.
For symmetry to work, make sure that widthis odd. If it isn’t, increase it by 1 to make it odd and display a warning, but still do the smoothing.
Make sure you correct edge effects so that the smoothed function doesn’t deviate from the original at the start or the end. Also make sure you don’t have any horizontal offset between the smoothed function and the original (since we’re using a symmetric moving average, the smoothed values should lie on top of the original data).
You can do this using a loop and mean (which should be easy but may be slow), or more efficiently by using conv (if you are familiar with convolution).
Load the mat file called noisyData.mat. It contains a variable x which is a noisy line. Plot the noisy data as well as your smoothed version, like below (a width of 11 was used):

Then it shows an image that have a grafic with two lines. The first one is with many points and the second one is a line more "striaigh foward" with not that many points but it crosses the same path of the first one. The code that I have so far is this one:

function rectFilt(x,width)
if nargin < 2 , x = 0; end
if nargin < 2 , width = 0; end

if( rem(width,2) == 0)
width = width+1;

primerNum = floor (width / 2);

for n = primerNum : width

if n > (length(x)- 2)

smoothed(n) = mean(x(n-primerNum:n+primerNum));



hold on
plot (smoothed, 'r')


When I call the functions I plot the first line but the smoothed one only crosses a very short path. What I'm doing wrong????
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil