Moving Average

I have a plot of residuals vs Distance and I want to run moving average window over it to capture the trend. My window should have a cos fun on both side and then reach the value of 1 between these two. I don't know how should I code it up?Thankss

Respuestas (2)

Image Analyst
Image Analyst el 28 de Mzo. de 2012

1 voto

Perhaps this demo will be instructive for you:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Make sample data
y = rand(1,100)
subplot(2, 2, 1);
plot(y);
grid on;
ylim([0 1]);
windowWidth = 15;
t = 1:windowWidth;
title('Y (the data)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Construct cosine kernel
kernel = cos(2*pi*(t - ceil(windowWidth/2))/ (2*windowWidth));
subplot(2, 2, 2);
plot(kernel);
grid on;
ylim([0 1]);
title('The cosine kernel', 'FontSize', fontSize);
% Normalize kernel so that mean of filtered data
% will be the same as the mean of the original data.
kernel = kernel / sum(kernel);
% Now filter the data with the normalized cosine kernel.
filtered_y = conv(y, kernel);
subplot(2, 2, 3);
plot(filtered_y);
grid on;
ylim([0 1]);
title('filtered y (the filtered data)', 'FontSize', fontSize);

2 comentarios

Daniel Shub
Daniel Shub el 28 de Mzo. de 2012
Nice demo, care to comment on why you chose conv over filter?
Image Analyst
Image Analyst el 28 de Mzo. de 2012
No reason really. I'm just more familiar with conv() because of my optics background. I also use conv2 from habit even though there is imfilter that does the same thing without the flipping that convolution does. 'Course it doesn't matter for symmetrical kernels. I probably should use imfilter instead of conv2 because I think Jeff said he optimized that a great deal so that it's actually faster than conv2.

Iniciar sesión para comentar.

Daniel Shub
Daniel Shub el 28 de Mzo. de 2012

0 votos

Typically people use
doc conv
to do moving averages

6 comentarios

Jan
Jan el 28 de Mzo. de 2012
Or FILTER. Usually the parameters of a moving average filter do *not* reach 1 at the center.
Daniel Shub
Daniel Shub el 28 de Mzo. de 2012
Is there an advantage of filter over conv (or conv over filter) in this case? I think I usually use conv with fir filters and filter with iir filters, but I haven't really thought about it in a long time.
Titus Edelhofer
Titus Edelhofer el 28 de Mzo. de 2012
Yes: the documentation of filter contains an example of how to use filter for a moving average ;-).
Jan
Jan el 28 de Mzo. de 2012
@Daniel: I'm not happy with the implementation of FILTER. E.g. the multi-threading starts only, if the signal has more than 16 columns. Therefore I've re-implemented this function as C-Mex, which is surprisingly faster.
I have not seen speed problems with CONV yet - are the C-sources still included in Matlab 2012a?
Daniel Shub
Daniel Shub el 28 de Mzo. de 2012
I couldn't find the c source for conv/conv2 r2011a. When there are a number of "identical" functions for solving a problem, I find my intuition is close to worthless. This isn't so bad since I don't spend much time optimizing or dealing with things that push numerical stability.
Jan
Jan el 28 de Mzo. de 2012
FILTER handles complex signals and complex filter parameters. As far as I remember, conv2.c accepts real DOUBLEs only - perhaps SINGLEs in addition.

Iniciar sesión para comentar.

Categorías

Más información sobre Statistics and Linear Algebra en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mzo. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by