% Fig. 4.28
% Wraparound error due to periodicity implied by DFT

% Dr. W. David Pan
% EE 604, UAH

% Spatial domain convolution
f = zeros(1, 400);
f(1: 300) = 3;

h = zeros(1, 400);
h(1: 200) = 2;

y = conv(f, h);
plot(y);

% DFT domain filtering with wraparound error
F = fft(f);
H = fft(h);
Y = F .* H;
y_err = ifft(Y);
figure; plot(y_err);

% Zero padding to avoid wraparound error
% P >= A + B - 1 = 400 + 400 - 1 = 799
% Thus append 399 zeros to both f and h

fp = [f, zeros(1, 399)];
hp = [h, zeros(1, 399)];

Fp = fft(fp);
Hp = fft(hp);
Yp = Fp .* Hp;
yp = ifft(Yp);
figure; plot(yp);



