%Ridwan A Kabir
%Project 5
%EE 416
%Chirp Z Transform

%1. Chirp Z Tranform and Chirp Transform algorithm (CTA):

%The cirp-z transform is a mean by which one can determine samples of the z-transform 
%equally spaced along a contour in the z-plane. Strictly speaking, the chirp transform finds 
%these samples a long the unit circle. while the chirp-z transform algorithm refers to 
%samples along a general contour. 

%The z-transform of a sequence x[n] is as follow:
%X(n)=sumx[n]*z^-n, n is the number of samples.
% The chirp-z transform CZT(X, M, W, A) is the M-element z-transform of sequence X,
%    where M, W and A are scalars which specify the contour in the z-plane
%   on which the z-transform is computed.  M is the length of the transform,
%    W is the complex ratio between points on the contour, and A is the
%    complex starting point.  More explicitly, the contour in the z-plane
%    (a spiral or "chirp" contour) is described by
%        z = A * W.^(-(0:M-1))

%The chirp-z transform algorithm is defined as follow:
%X(ejwn)=(sum g[k]W^((-(n-k)^2)/2)) W^(n^2/2)
%    where g[n]=x[n]e(-jwn)w^(n^2/2).   
%******************************************************************************
%2. Key advantages and applications of CZT:

%We understand the advantage being thus that it enables one to calculate the samples of the 
%z-transform equally spaced over an arc or a spiral contour with an arbitrary starting 
%point and arbitrary frequency range.Furthermore the chirp-z transform allows one to 
%calculate an arbitrary number of samples along with contour, which arbitrarily reduecs 
%the error in frequency representation. Lastly,the chirp-z transform allows one to leave 
%the unit circle and  calculate samples in the z-plane, while DFT is limited in a unit circle.

%*****************************************************************************

%Using matlab to show advantege of using CZT over its counterpart in a particular case:

fs=1000; %Sets stopping frequency;
f=15;    %Sets the frequency to be 15Hz;
w=2*pi*f;%Here we find the angular frequency;
dw=2*pi*3/4;     %Increment in frequency;
t=0:1/fs:1.5;
x=sin(w*t)+2*sin((w+dw)*t)+3*cos((w-dw)*t);  %THis is the signal we chhose to be defined;
Xz=freqz(x);     %The frequency response of x.

h1=fir1(50,[0.015 0.035],kaiser(51, 0.5));  %We implement the filter design here.
s1=conv(x,h1);   %Filter output;
Sz=freqz(s1);    %Filter output;

figure(1);
plot(abs(Sz));   %Plot of Z-transform of s1;
title('Plot of Z-Transform using FT function');
xlabel('freq');
ylabel('amplitude');

%***************************************************************************************

%Exercise 3b;

f1=7.5;
f2=17.5;
m=512;   %Sets the number of elements;
w=exp(-j*2*pi*(f2-f1)/(m*fs));
a=exp(j*2*pi*f1/fs);   %Set of complex starting point;

z1=czt(s1,m,w,a);      %Computes using czt function.
fz1=((0:length(z1)-1)'*(f2-f1)/length(z1))+f1;

figure(2);
plot(fz1,abs(z1));
title('Chirp Z Transform z1');
axis([f1 f2 0 4000]);

h2=fir1(50,[0.2 0.3],kaiser(51,0.5));
s2=conv(x,h2);
Sz2=freqz(s2);

f2=17.5;
f3=27.5;
m2=1024;
w2=exp(-j*2*pi*(f3-f2)/(m2*fs));% this is the complex ratio between points on the contour described
a2=exp(j*2*pi*f2/fs);           %by z=Aw^(-(0:M:1)).

z2=czt(s2,m2,w2,a2);
fz2=((0:length(z2)-1)'*(f3-f2)/length(z2))+f2;

figure(3);
plot(fz2,abs(z2));
title('Chirp Z Transform of z2');
xlabel('Frequency');
ylabel('amplitude');

h3=fir1(50,[0.4 0.5],kaiser(51,0.5));
s3=conv(x,h3);
Sz=freqz(s3);

f3=27.5;
f4=37.5;
m3=2048;
w3=exp(-j*2*pi*(f4-f3)/(m3*fs));
a3=exp(j*2*pi*f3/fs);

z3=czt(s3,m3,w3,a3);
fz3=((0:length(z3)-1)'*(f4-f3)/length(z3))+f3;

figure(4);
plot(fz3,abs(z3));
title('Chirp-Z transform of z3');
xlabel('frequency');
ylabel('amplitude');

%******************************************************************************************************
