%% conv1demo1
% show a signal and convolution pair
% show DFT of convolving mask
% show |DFT| of signal and filtered signal

% S. Allen Broughton - 18 Oct 10

%% define signal, sample rate, sample domain
N = 128;
t = (0:1:(N-1))/N;
I = sqrt(-1);
sig = 2*cos(5*2*pi*t)+0.5*sin(60*2*pi*t);
%sig = cos(5*2*pi*t);
%sig = sin(30*2*pi*t);
%sig = sin(60*2*pi*t);
%k = 96; sig = [ones(1,k),zeros(1,N-k)]*(N/k);
%sig = rand(1,N);
%sig = 2*rand(1,N)-1;
%k =50, sig = exp(k*2*I*pi*t);
SD = 0:(N-1);
FD = SD-N/2;


%% define convolving mask
f = [1/2,1/2], f = [f,zeros(1,N-length(f))];
%f= [1/2,-1/2], f = [f,zeros(1,N-length(f))];
%f = [-1/4,1/2,-1/4], f = [f ,zeros(1,N-length(f))];
%f= [3/4,-1/2,-1/4], f = [f,zeros(1,N-length(f))];
%f= [1/2,3/4,0,-1/4], f = [f,zeros(1,N-length(f))];
%f = [1/2,-1/2], f = [f,zeros(1,N-length(f))];
%f1 = [3,2,1]/9, f2 = [1,2]/9,  f = [f1,zeros(1,N-length(f1)-length(f2)),f2];
%f1= [1/2,1/4], f2 = [1/4], f = [f1,zeros(1,N-length(f1)-length(f2)),f2];


%% compute convolution and 3 dft's
fsig =circconv(sig,f);
sff =fftshift(fft(f));
sfsig = fftshift(fft(sig));
sffsig = fftshift(fft(fsig));

%% display results 
figure(1)
subplot(2,1,1)
plot(SD, real(sig),'r',SD, imag(sig),'g')
title('sampled signal');
axis tight
subplot(2,1,2)
plot(SD, real(fsig),'r',SD, imag(fsig),'g')
title('filtered signal')
axis tight

figure(2)
subplot(2,1,1)
plot(FD, abs(sfsig),'b-' )
title('magnitude of shifted DFT of signal')
axis tight

subplot(2,1,2)
plot(FD, abs(sffsig),'k-' )
title('magnitude of shifted DFT of filtered signal')
axis tight

figure(3)
subplot(2,1,1)
plot(FD, abs(sff),'r-' )
title('magnitude of shifted DFT of mask')
axis tight

subplot(2,1,2)
plot(FD, angle(sff),'g-' )
title('phase of shifted DFT of mask')
axis tight
