Project

General

Profile

% Copyright 2015 The MathWorks, Inc.
% Last Edited: Sean de Wolski, 05/24/2013

function blockAvg(Nx,Ny,Nxavg,Nyavg)
%% Example 1
% Input values Nx, Ny = 1500, Nxavg, Nyavg = 25;
%
% Want to create a surface and look at it averaged over a
% a particular resolution

if ~nargin
Nx = 1500;
Ny = 1500;
Nxavg = 25;
Nyavg = 25;
end

%% Initial Parameter Values

tic

Lx = 10; % Length of x dimension
Ly = 10; % Length of y dimension

x = linspace(1,Lx,Nx); % Creating x vector
y = linspace(1,Ly,Ny); % Creating y vector

%% Setting up values of surface on grid


for i = 1:length(x)
for j = 1:length(y)
yp = y(j);
xp = x(i);
mysurf(i,j) = 5*cos((xp+yp)*2*pi)+...
2*sin(xp*2*pi)+2*cos(xp*2*pi);
end
end

%% Averaging values over the grid

for i = Nxavg:length(x)
for j = Nyavg:length(y)
if (mod(j,Nyavg)==0 && mod(i,Nxavg)==0)

idown = i-Nxavg+1;
jdown = j-Nyavg+1;
iup = i;
jup = j;
iavg = floor(i/Nxavg);
javg = floor(j/Nyavg);
mysurf_avg(iavg,javg) = ...
sum(sum(mysurf(idown:iup,jdown:jup)))/(Nxavg*Nyavg);
xavg(iavg) = (x(iup)+x(idown))/2;
yavg(javg) = (y(jup)+y(jdown))/2;
end
end
end

toc

%% Plotting the results to compare

ax(1) = subplot(2,1,1);
imagesc(x,y,mysurf);
title('Original Function');
axis off

ax(2) = subplot(2,1,2);
imagesc(xavg,yavg,mysurf_avg);
title('Averaged Function');
axis off

linkaxes(ax,'xy');

(1-1/6)