% Convolution Tool (L Braile, October 2001; revised January 2002) % define signals each 201 points long, dt=0.01 dt=0.01; t=0:dt:2; % % b1 short boxcar b1=[zeros(1,75) ones(1,51) zeros(1,75)]; % b2 long boxcar b2=[zeros(1,50) ones(1,101) zeros(1,50)]; % sq square wave sq=[zeros(1,75) ones(1,25) -1*ones(1,25) zeros(1,76)]; % sp spike sp=[zeros(1,100) 1 zeros(1,100)]; % st step function st=[zeros(1,50) ones(1,151)]; % ra random number ra=randn(1,201); ramax=max(abs(ra)); ra=ra/ramax; % un uniformly distributed random number un=rand(1,201); un=2*un - 1; % c1 low frequency cosine c1=cos(2*pi*1*t); % c2 high frequency cosine c2=cos(2*pi*5*t); % si sin x/x or sinc function sit=sin(2*pi*4*t(2:101))./(2*pi*4*t(2:101)); si=[fliplr(sit) 1 sit]; % df differentiation operator df=[zeros(1,99) 1 -1 zeros(1,100)]; % t1 short triangle tr1=[1:1:26]/26; tr2=[25:-1:1]/26; t1=[zeros(1,75) tr1 tr2 zeros(1,75)]; % t2 long triangle tr1=[1:1:51]/51; tr2=[50:-1:1]/51; t2=[zeros(1,50) tr1 tr2 zeros(1,50)]; % ga, gaussian ga1=exp(-(8*t(2:101).*t(2:101))); ga=[fliplr(ga1) 1 ga1]; % cb cosine bell nsw=100; for i = 1:nsw j = nsw-i; fa1(i) = 0.5*(cos(pi*j/nsw) + 1); end cb=[fa1 1 fliplr(fa1)]; % ex exponential thalf=0.25; x1=t(2:151)*0.696/thalf; ex1=exp(-x1); ex=[zeros(1,50) 1 ex1]; % w1 Ricker wavelet [rk]=ricker(201,20,dt,0,25); rmax=max(abs(rk)); w1=rk/rmax; % w2 Ricker wavelet [rk]=ricker(201,80,dt,0,25); rmax=max(abs(rk)); w2=rk/rmax; % s1 spike series 1 s1=[zeros(1,10) 1 zeros(1,80) -0.7 zeros(1,100) 0.4 zeros(1,8)]; % s2 spike series 2 s2=[zeros(1,10) 1 zeros(1,45) .4 zeros(1,34) -0.7 zeros(1,55) ... -.3 zeros(1,44) 0.4 zeros(1,8)]; % s3 spike series 3 s3=[zeros(1,50) 1 zeros(1,8) -0.2 zeros(1,9) 0.7 zeros(1,8) -0.8 zeros(1,5) 0.3... zeros(1,12) -0.4 zeros(1,8) 0.25 zeros(1,5) 0.1 zeros(1,9) -0.1 zeros(1,6)... 0.1 zeros(1,71)]; % rp ramp rp1=[1:1:101]/101; rp=[zeros(1,50) rp1 zeros(1,50)]; % sw sweep 1-5 Hz, 1000 points, dt=0.01 for i=1:183 ti=(i-1)*dt; f=2 + 1*(ti); sw1(i)=sin(2*pi*f*ti); end sw=[zeros(1,9) sw1 zeros(1,9)]; % di Dirac comb di=[zeros(1,10) 1 zeros(1,19) 1 zeros(1,19) 1 zeros(1,19) 1 zeros(1,19) 1 ... zeros(1,19) 1 zeros(1,19) 1 zeros(1,19) 1 zeros(1,19) 1 zeros(1,19) 1 ... zeros(1,10)]; % w3 wavelet - signal n=2 ts=0.21; n=2; npts=201; na=25; thalf=0; [w3]=signal(ts,dt,n,0,npts,na,thalf); % w4 wavelet - signal n=4 ts=0.41; n=4; npts=201; na=25; thalf=0; [w4]=signal(ts,dt,n,0,npts,na,thalf); % w5 wavelet - signal n=5 ts=0.51; n=5; npts=201; na=25; thalf=0; [w5]=signal(ts,dt,n,0,npts,na,thalf); % w6 wavelet - signal n=7 ts=0.71; n=7; npts=201; na=25; thalf=0; [w6]=signal(ts,dt,n,0,npts,na,thalf); % w7 wavelet - signal n=7 thalf=0.15 ts=0.71; n=7; npts=201; na=25; thalf=0.15; [w7]=signal(ts,dt,n,0,npts,na,thalf); % w8 wavelet - signal n=9 thalf=0.2 ts=0.91; n=9; npts=201; na=25; thalf=0.2; [w8]=signal(ts,dt,n,0,npts,na,thalf); % % % plot signals figure subplot(5,6,1), plot(t,b1,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'b1, boxcar','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) % subplot(5,6,2), plot(t,b2,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'b2, boxcar','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,3), plot(t,sq,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) title(' Functions and Signals for Convolution and Correlation Tool',... 'fontweight','bold','fontsize',12) text(0.1,-0.9,'sq, sq. wave','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,4), plot(t,sp,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'sp, spike','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,5), plot(t,df,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'df, differentiator','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,6), plot(t,st,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'st, step func.','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,7), plot(t,ra,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'ra, random nor.','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) % subplot(5,6,8), plot(t,un,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'un, random unif.','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,9), plot(t,c1,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'c1, cosine','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,10), plot(t,c2,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'c2, cosine','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,11), plot(t,si,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'si, sinc func.','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,12), plot(t,t1,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'t1, triangle','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,13), plot(t,t2,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'t2, triangle','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) ylabel('Relative Amplitude','FontWeight','bold') % subplot(5,6,14), plot(t,ga,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'ga, gaussian','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,15), plot(t,cb,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'cb, cosine bell','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,16), plot(t,ex,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'ex, exponential','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,17), plot(t,rp,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'rp, ramp','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,18), plot(t,di,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'di, dirac comb','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,19), plot(t,s1,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'s1, spike series','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) % subplot(5,6,20), plot(t,s2,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'s2, spike series','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,21), plot(t,s3,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'s3, spike series','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,22), plot(t,sw,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'sw, sweep','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,23), plot(t,w1,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w1, ricker wavelet','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,24), plot(t,w2,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w2, ricker wavelet','FontWeight','bold') set(gca,'XTickLabelMode','manual','XTickLabel',[]) set(gca,'YTickLabelMode','manual','YTickLabel',[]) % subplot(5,6,25), plot(t,w3,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w3, wavelet','FontWeight','bold') xlabel('Time (s)','FontWeight','bold') % subplot(5,6,26), plot(t,w4,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w4, wavelet','FontWeight','bold') set(gca,'YTickLabelMode','manual','YTickLabel',[]) xlabel('Time (s)','FontWeight','bold') % subplot(5,6,27), plot(t,w5,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w5, wavelet','FontWeight','bold') set(gca,'YTickLabelMode','manual','YTickLabel',[]) xlabel('Time (s)','FontWeight','bold') % subplot(5,6,28), plot(t,w6,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w6, wavelet','FontWeight','bold') set(gca,'YTickLabelMode','manual','YTickLabel',[]) xlabel('Time (s)','FontWeight','bold') % subplot(5,6,29), plot(t,w7,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w7, wavelet','FontWeight','bold') set(gca,'YTickLabelMode','manual','YTickLabel',[]) xlabel('Time (s)','FontWeight','bold') % subplot(5,6,30), plot(t,w8,'r-','linewidth', 1.5), axis([0 2 -1.1 1.1]) text(0.1,-0.9,'w8, wavelet','FontWeight','bold') set(gca,'YTickLabelMode','manual','YTickLabel',[]) xlabel('Time (s)','FontWeight','bold') % % pause % % while 1 % n1=input('Type 1 for convolution or 2 for correlation or q to quit ','s'); % if strcmp(n1,'q') | strcmp(n1,'Q'), break, end % nc=str2num(n1); sig1=input('Type 2 character name for first signal '); sig2=input('Type 2 character name for second signal '); to=0:dt:4; % Convolution if nc==1 out=conv(sig1,sig2); outmax=max(abs(out)); out=out/outmax; end % Correlation if nc==2 out=xcorr(sig1,sig2); outmax=max(abs(out)); out=out/outmax; end % Plot selected signals and output figure subplot(2,2,1), plot(t,sig1,'r-','linewidth', 1.5), axis([0 4 -1.1 1.1]) title('Signal 1','FontWeight','bold') xlabel('Time (s)','FontWeight','bold') ylabel('Relative Amplitude','FontWeight','bold') subplot(2,2,2), plot(t,sig2,'r-','linewidth', 1.5), axis([0 4 -1.1 1.1]) title('Signal 2','FontWeight','bold') xlabel('Time (s)','FontWeight','bold') ylabel('Relative Amplitude','FontWeight','bold') if nc==1 subplot(2,2,3), plot(to,out,'r-','linewidth', 1.5), axis([0 4 -1.1 1.1]) title('Convolution of Signals 1 and 2','FontWeight','bold') xlabel('Time (s)','FontWeight','bold') ylabel('Relative Amplitude','FontWeight','bold') end if nc==2 subplot(2,2,3), plot(to,out,'r-','linewidth', 1.5), axis([0 4 -1.1 1.1]) title('Correlation of Signals 1 and 2','FontWeight','bold') xlabel('Time (s)','FontWeight','bold') ylabel('Relative Amplitude','FontWeight','bold') end % end