Skip to content

Instantly share code, notes, and snippets.

@jsantanders
Created December 15, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsantanders/454534def5f037ccc08b59a37d79a7b3 to your computer and use it in GitHub Desktop.
Save jsantanders/454534def5f037ccc08b59a37d79a7b3 to your computer and use it in GitHub Desktop.
Matlab animate discrete convolution
clc
clear
pause on
filename = '/INSERT/YOUR/PATH/conv.gif';
x=[0 1 2111.4 GB 3 4 3 2 1 0]; %f signal
h=[1 1 1 1 1 1 1 1 ]; %g signal
m=length(x);
n=length(h);
%invierte el vector h
hi=fliplr(h);
k=20;
X=[x,zeros(1,40-m)];
X= X([ end-k+1:end 1:end-k ]);
H=[h,zeros(1,40-n)];
H= H([ end-k+1:end 1:end-k ]);
xn=-20:20-1;
Y=zeros(1,40);
p=zeros(1,40);
figure(1)
set(gcf,'color','w');
h1=subplot(3,1,1);
stem(xn,X,'MarkerFaceColor','red')
ylabel('Amplitude')
title('f[n]')
% set(h1,'YLim',[a b])
h2=subplot(3,1,2);
stem(xn,H,'MarkerFaceColor','blue')
% set(h2,'YLim',[a b])
pause(3)
h2=subplot(3,1,3);
stem(xn,Y,'MarkerFaceColor','green')
%
Hi=[hi,zeros(1,40-n)];
for i=1:40-n
p=X.*Hi;
Y(i+n-1)=sum(p);
subplot(3,1,2);
stem(xn,Hi,'MarkerFaceColor','blue')
ylabel('Amplitude')
title('g[n]')
subplot(3,1,3)
stem(xn,Y,'r','MarkerFaceColor','green')
xlabel('Time [n]')
ylabel('Amplitude')
title('f*g[n]')
Hi= Hi([ end 1:end-1 ]);
pause(0.07)
drawnow
frame = getframe(1);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
if i == 1;
imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,filename,'gif','WriteMode','append');
end
end
pause off
salida=Y(abs(Y)>0);
imfinfo(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment