% fig3_29
% Covolution and Correlation

x = [0 0 0 1 0 0 0 0];
h = [1 2 3 2 8];

% Convolution
conv(x, h)
doc conv
% Same length as x
conv(x, h, 'same')
% Without zero padding
conv(x, h, 'valid')

% Correlation
% If the input vectors have different lengths, the 
% function appends zeros to the end of the shorter 
% vector so it has the same length as the other.
xcorr(x,h)

% Use convolution to implement correlation
conv(x, flip(h))

% 2D convolution
f = zeros(5, 5);
f(3, 3) = 1;
w = reshape(1:9, 3, 3)'
conv2(f, w)
% Same size as x
conv2(f, w, 'same')

% 2D correlation using 2D convolution
w1 = flip(w, 1)
w2 = flip(w1, 2)
conv(x, w2, 'same')
