Skip to content

Instantly share code, notes, and snippets.

@stephenHartzell
Created July 26, 2017 03:09
Show Gist options
  • Select an option

  • Save stephenHartzell/06b94963ee8e8047e354d6b264ad2eb4 to your computer and use it in GitHub Desktop.

Select an option

Save stephenHartzell/06b94963ee8e8047e354d6b264ad2eb4 to your computer and use it in GitHub Desktop.
% The convolution is calculated as shown in this figure
f_pad = f;
g_pad = [g, zeros(1,length(f)-length(g))];
g_pad = circshift(fliplr(g_pad)',1)';
MF = zeros(1,length(f_pad));
figure('Position',[100 100 850 600])
v = VideoWriter('test.avi','Uncompressed AVI');
v.FrameRate = 1;
open(v)
MF(1) = sum(f_pad.*g_pad);
subplot(2,1,1), stem(f_pad,'r','filled')
title('Flip and Shift')
hold on
stem(g_pad,'m','filled')
legend('f','g flipped')
xlim([-1,length(f_pad)+1])
ylim([-2,9])
hold off
subplot(2,1,2), stem(MF,'b','filled')
xlim([-1,length(f_pad)+1])
ylim([-10,50])
set(gca,'NextPlot','replacechildren');
F(1:length(f_pad)) = struct('cdata', [], 'colormap', []);
for j = 1:length(f_pad)
MF(j) = sum(f_pad.*g_pad);
subplot(2,1,1), stem(f_pad,'r','filled')
hold on
stem(g_pad,'m','filled')
title('Flip and Shift')
legend('f','g fliped')
xlim([-1,length(f_pad)+1])
ylim([-2,9])
hold off
subplot(2,1,2), stem(MF,'b','filled')
title('Circular Convolution of f and g')
xlim([-1,length(f_pad)+1])
ylim([-10,50])
frame = getframe(gcf);
writeVideo(v, frame.cdata);
g_pad = circshift(g_pad',1)';
end
close(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment