Matlab Programming: For a dataset \"x\" with value 1007x1 double, do the followi
ID: 1765960 • Letter: M
Question
Matlab Programming: For a dataset "x" with value 1007x1 double, do the following:
i) Estimate the mean using the entire data. Plot this as a horizontal dashed line on a new figure.
ii) Estimate the mean from the first N points of the data, letting N vary from 1 to the maximum number of samples in the dataset. Overlay a plot of the computed means vs. the number of points N on the plot from step i). Comment on what you observe.
iii) Estimate the mean using a moving window approach1. Consider the following lengths of the window: 30 data points (i.e. 30 day moving average) for the stock data and 441 samples (i.e., average over 10 ms of data) for the audio file. Superimpose the computed means on the plot from step i). Comment on what you observe.
iv) Repeat step iii) with the following values for window lengths: 90 data points (i.e. 3-month moving average) for stock data and 882 samples (i.e. average over 20 ms of data) for the audio file.
Explanation / Answer
% function Y = runmean(X,m) ;
% % RUNMEAN - Very fast running mean filter for vectors
% % Y = RUNMEAN(X,M) computes a running mean on vector X using a window of
% % 2*M+1 datapoints. X is a vector, and M an positive integer defining
% % (half) the size of the window. In pseudo code:
% % Y(i) = sum(X(j)) / (2*M+1), for j = (i-M):(i+M), and i=1:length(X)
% %
% % If the total window size (2M+1) is larger than the length of the vector, the overall
% % average is returned.
% %
% % Example:
% % runmean(1:10,1) % ->
% % [1.3333 2 3 4 5 6 7 8 9 9.6667]
% %
% % This is an incredibly fast implementation of a running average, since
% % execution time does not depend on the size of the window.
% %
% % X should not contains NaNs (a NaN will result in a all NaN result)
% % At both ends the values of Y can be inaccurate, as the first and last
% % values of X are used multiple times.
% %
% % See also MEAN
%
% % for Matlab R13
% % version 1.3 (feb 2006)
% % Jos van der Geest
% % email: jos@jasen.nl
%
% % History:
% % 1.0 (2003) created, after a snippet from Peter Acklam (?)
% % 1.1 (feb 2006) made suitable for the File Exchange (extended help and
% % documentation)
% % 1.2 (feb 2006) added a warning when the window size is too big
% % 1.3 (feb 2006) improved help section
%
% error(nargchk(2,2,nargin)) ;
%
% sz = size(X) ;
%
% if numel(sz) ~= 2 || (min(sz) ~= 1),
% error('X should be a vector') ;
% end
%
% if any(isnan(X)),
% error('NaNs cannot be dealt with') ;
% end
%
% if ~isnumeric(m) || (numel(m) ~= 1) || (m < 0) || fix(m) ~= m,
% error('The window size (M) should be a positive integer') ;
% elseif m == 0,
% Y = X ;
% return ;
% end
%
% mm = 2*m+1 ;
%
% if mm >= prod(sz),
% % if the window is larger than X, average all
% warning('Window size is larger than the length of the vector.')
% Y = repmat(mean(X),sz) ;
% else
% % the cumsum trick ...
% Y = [repmat(X(1),m,1) ; X(:) ; repmat(X(end),m,1)] ;
% Y = [0 ; cumsum(Y)] ;
% Y = (Y(mm+1:end)-Y(1:end-mm)) / mm ;
% Y = reshape(Y,sz) ;
% end