% Attique Rehman.(28395921).
% Project 5.
% Chirp Z Transform.
% Digital Signal Processing.

%  **************************************************
%  The concept of Chirp Z-transform and Application
%  **************************************************

%  +++++++++++++++++++++++++++++++++++++++++++++++++++++++
%  (A)  Chirp Z-Transform & Chirp Transform Algorithm (CTA)
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

% The chirp z transform algorithm is a means by which one can determine 
% samples of the z transformequally spaced along a contour in the z plane.
% Chirp z transform finds these samples along the uni circle, while chirp z transform
% is that it enables one to calculate the samples of the z transform. 
%  Chirp Transform Algorithm (CTA) is another expression of DFT as a convolution.

% The z transform of a sequence x[n] is:

% X(n)=sumx[n]*z^-n,  N is the no of samples.
% Defining the points zk of an M point contour as:

% zk=AW^-k, for k=0,1,....M-1
% A-----strating point

% A=Aoe^jw0, wo=2*pi*thetao

%  The Chirp Transform Algorithm (CTA) is :

% 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)

%  hence the impulse response is defined as:

%   h(n) = w^(-n^2/2)=0     
%   for  -(N-1) <= n <= M-1     otherwise

%---------------------------------------------------------

%  ******************************************
%  (B) Key advantages and Applications Of CZT. 
%  ******************************************

% The advantage of the CZR is that it enables one to calculate the samples 
% z transform equally spaced over an arc or a spiral contour with an arbitary 
% starting point and arbitarary frequency range.If the format of the frequency of the 
% interest is in the range 200Hz to 900 Hz, then samples of the z transform 
% may be calculated in this region. In contrast, the fequency
% range of the DFT is strictly elated to the sampling frequency.
% The chirp Z transform allows one to calculate an arbitrary no of samples
% along the contour, which arbitrarily reduces the error in fre. representation of


%  ****************************************
%  (C) Advatage of CZT Using Matlab Coding
%  ****************************************

        %Parameters.
% fs Sets stop frequency
% f Set frequency to be 60Hz
% w Finds angular frequency
% dw sets frequency increment
% t set time ranges
% x is the signal which is defined below.
% Xz is the Z Transform of x
% h1 is the filter defined below.
% s1 is the filter ouput.
% Sz is the Z transform of s1

fs=1000;												
f=15;												
w=2*pi*f;													
dw=2*pi*3/4;												
t=0:1/fs:1.5;												

x=sin(w*t)+2*sin((w+dw)*t)+3*cos((w-dw)*t);		%  signal defined
Xz=freqz(x);									%  Z-Transform of x

h1=fir1(50,[0.015 0.035],kaiser(51,0.5));		% filter defined
s1=conv(x,h1);									% filter output
Sz=freqz(s1);									% Z-transform of s1
	
figure(1);												
plot(abs(Sz));	%plot for the Z transform of s1							
title('Plot of Z-Transform using normal FT function');		
xlabel('frequency');										
ylabel('amplitude');								
%----------------------------------

%Parameters
% m1 Sets number of elements	
% w1 Sets complex ratio
% a1 Set complex starting point
% fz1 Set frequency range
%
f1=7.5;
f2=17.5;
m1=512;																					
w1=exp(-j*2*pi*(f2-f1)/(m1*fs));						
a1=exp(j*2*pi*f1/fs);									

z1=czt(s1,m1,w1,a1);	% Compute using CZT function
fz1=((0:length(z1)-1)'*(f2-f1)/length(z1))+f1;	

figure(2);									
plot(fz1,abs(z1));		% the Chirp Z-transform computed plot
title('The Chirp Z Transform z1');
axis([f1 f2 0 4000]);				


h2=fir1(50,[0.2 0.3],kaiser(51,0.5));			%  filter defined
s2=conv(x,h2);									% Finds filter output
Sz2=freqz(s2);									% Finds Z-transform of s1


f2=17.5;
f3=27.5;
m2=1024;										% Set number of elements									
w2=exp(-j*2*pi*(f3-f2)/(m2*fs));				% Set complex ratio
a2=exp(j*2*pi*f2/fs);							% Set complex starting point

z2=czt(s2,m2,w2,a2);							% Compute using CZT function
fz2=((0:length(z2)-1)'*(f3-f2)/length(z2))+f2;  % Set frequency range

figure(3);												
plot(fz2,abs(z2));				% Plot the Chirp Z-transform computed
title('chirp z transform z2');
					
% h3 Define filter
% y3 Find filter output
% Yz Find Z-transform of y1

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

% m3 Sets number of elements		
% w3 Sets complex ratio
% a3 Sets complex starting point
% fz3 sets the frequency range.

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);						 % Using CZT function
fz3=((0:length(z3)-1)'*(f4-f3)/length(z3))+f3;	

figure(4);												
plot(fz3,abs(z3));			%  Plot computed
title('The Chirp z transform z3.');
								
%*******************End Of The Project****************
%*****************************************************

