Moving Average

7 ビュー (過去 30 日間)
khatereh 2012 年 3 月 28 日
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

回答 (2 件)

Image Analyst
Image Analyst 2012 年 3 月 28 日
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);
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);
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);
grid on;
ylim([0 1]);
title('filtered y (the filtered data)', 'FontSize', fontSize);
  2 件のコメント
Image Analyst
Image Analyst 2012 年 3 月 28 日
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.


Daniel Shub
Daniel Shub 2012 年 3 月 28 日
Typically people use
doc conv
to do moving averages
  6 件のコメント
Jan 2012 年 3 月 28 日
FILTER handles complex signals and complex filter parameters. As far as I remember, conv2.c accepts real DOUBLEs only - perhaps SINGLEs in addition.


Community Treasure Hunt

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

Start Hunting!

Translated by