clc clear all close all %------------------------------------------------------------------------- % color of axis constant axis_color= [0.5 0.5 0.5]; % sampling interval constant s_int = 0.1; %functions t = [ -5:s_int:5 ]; %time for f Af = 1; Ag = 1; tf = 1; tg = 1; Df = 1; Dg = 1; f=Af*rectpuls(t-tf,Df); go=Ag*rectpuls(t-tg,Dg); t1 = [-5:s_int:5]; %time for g %convolution c = s_int * conv(f, go); %------------------------------------------------------------------------- % Animation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % flip 'go(t1)' for the graphical convolutions g = go(-t1) g = fliplr(go); tf = fliplr(-t1); % slide range of 'g' to discard non-ovelapping areas with 'f' in the convolution tf = tf + ( min(t)-max(tf) ); % get the range of function 'c' which is the convolution of 'f(t)' and 'go(t1)' tc = [ tf t(2:end)]; tc = tc+max(t1); % start graphical output with three subplots a_fig = figure; set(a_fig, 'Name', 'Animated Convolution', 'unit', 'pixel','WindowState','fullscreen'); % plot f(t) and go(t1) ax_1 = subplot(3,1,1); op = plot(t,f, 'b', t1, go, 'r'); hold on; %grid on; set(ax_1, 'XColor', axis_color, 'YColor', axis_color, 'Color', 'w', 'Fontsize', 9); xlim( [ ( min(t)-abs(max(tf)-min(tf)) - 1 ) ( max(t)+abs(max(tf)-min(tf)) + 1 ) ] ); ylim([0 2]) title('Graph of f(t) and go(t)', 'Color', axis_color ); legend({'f(t)' 'go(t)'}); % initialize animation the plot of 'g' is slided over the plot of 'f' % plot f in the subplot number 2 ax_2 = subplot(3,1,2); p = plot(t, f); hold on; %grid on; title('Graphical Convolution: f(t) and g = go(-t1)', 'Color', axis_color ); % plot g in the subplot number 2 q = plot(tf, g, 'r'); xlim( [ ( min(t)-abs(max(tf)-min(tf))-1 ) ( max(t)+abs(max(tf)-min(tf))+1 ) ] ); ylim([0 2]) u_ym = get(ax_2, 'ylim'); % plot two vertical lines to show the range of ovelapped area s_l = line( [min(t) min(t)], [u_ym(1) u_ym(2)], 'color', 'g' ); e_l = line( [min(t) min(t)], [u_ym(1) u_ym(2)], 'color', 'g' ); hold on; %grid on; set(ax_2, 'XColor', axis_color, 'YColor', axis_color, 'Color', 'w', 'Fontsize', 9); % initialize the plot the convolution result 'c' ax_3 = subplot(3,1,3); r = plot(tc, c); hold on;%grid on; set(ax_3, 'XColor', axis_color, 'YColor', axis_color, 'Fontsize', 9); xlim( [ min(tc)-1 max(tc)+1 ] ); xlim( [ ( min(t)-abs(max(tf)-min(tf)) - 1 ) ( max(t)+abs(max(tf)-min(tf)) + 1 ) ] ); ylim([0 1]) grid on title('Convolutional Product c(t)', 'Color', axis_color ); % animation block for i=1:length(tc) pause(0.1); drawnow; % update the position of sliding function 'g', its handle is 'q' tf=tf+s_int; set(q,'EraseMode','xor'); set(q,'XData',tf,'YData',g); % show a vetical line for a left boundary of overlapping region sx = min( max( tf(1), min(t) ), max(t) ); sx_a = [sx sx]; set(s_l,'EraseMode','xor'); set(s_l, 'XData', sx_a); % show a second vetical line for the right boundary of overlapping region ex = min( tf(end), max(t) ); ex_a = [ex ex]; set(e_l,'EraseMode','xor'); set(e_l, 'XData', ex_a); % update the plot of convolutional product 'c', its handle is r set(r,'EraseMode','xor'); set(r,'XData',tc(1:i),'YData',c(1:i) ); end;