Sunteți pe pagina 1din 126

1.

Generation of sinusoidal waveform/signal


based on recursive
difference equation.
For the given difference equation, a sinusoidal signal/sequence is applied as the
input. Using the given initial conditions, the sinusoidal response of the given
discrete system is to be computed.
1.The difference equation is y(n)=x(n)+y(n-1), which is a first order system, with the
initial condition
y(-1)=4.
I=input('Enter the value of initial condition y(-1)')
n=0:0.001:1;
f=input('Enter the frequency')
x=sin(2*pi*f*n);
y=zeros(1,length(n));
for i=1:length(n)
y(i)=x(i)+I;
I=y(i);
end
subplot(2,1,1)
plot(n,x)
title('Input signal x(n) applied')
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plot(n,y)
title('Sinusoidal Response of the given first order system')
xlabel('Time')

ylabel('Amplitude')
Enter the value of initial condition y(-1)
I=4
Enter the frequency
f =10

Input signal x(n) applied

Amplitude

1
0.5
0

Amplitude

-0.5
40 -1
0

Sinusoidal Response y(n) of the given first order system


0.1

0.2

0.3

0.4

30

0.5
Time

0.6

0.7

0.8

0.9

20
10
0
0

0.1

0.2

0.3

0.4

0.5
Time

0.6

0.7

0.8

0.9

The sinusoidal response can also be obtained by using the in-built function filter.
For filtering, the input and output coefficients of the given difference equation
should be entered as two separate row vectors, starting from x(n) coefficient in the
matrix a /y(n) coefficient in the matrix b.
The given initial conditions should be entered as Y=[y(-1) y(-2) ..y(-N)] and X=[x(1) x(-2) x(-N)]. If x(n)=0, for n-1, X matrix need not be mentioned.
The equivalent initial condition input array is to be computed as xic= filter(a,b,Y,X).
The overall response of the given discrete system for the given input, is obtained
as
y=filter(a, b , given input, xic).
Using In-Built Function:
I=input('Enter the initial conditions in the form of row vector starting from y(-1)')
n=0:0.001:1;
f=input('Enter the frequency')
x=sin(2*pi*f*n);
a=input('Enter the input coefficients in the given system equation, starting from
x(n) coefficient')
b=input('Enter the input coefficients in the given system equation, starting from
y(n) coefficient')
xic=filter(a,b,I);
%Overall response of the system for the given input
y=filter(a,b,x,xic);
subplot(2,1,1)
plot(n,x)
title('Input signal x(n) applied')
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plot(n,y)
title('Sinusoidal Response y(n) of the given first order system')

xlabel('Time')
ylabel('Amplitude')

Enter the initial conditions in the form of row vector starting from y(-1)
I=4
Enter the frequency
f =10
Enter the input coefficients in the given system equation, starting from x(n)
coefficient
a=1
Enter the input coefficients in the given system equation, starting from y(n)
coefficient
b=1

-1

Input signal x(n) applied

Amplitude

1
0.5
0
-0.5

Amplitude

40 -1
0

Sinusoidal Response y(n) of the given first order system


0.1

0.2

0.3

0.4

30

0.5
Time

0.6

0.7

0.8

0.9

20
10
0
0

0.1

0.2

0.3

0.4

0.5
Time

0.6

0.7

0.8

0.9

2.The given difference equation is y(n)=x(n)+x(n-1)+y(n-1), which is a first order


system, with x(-1)=1, y(-1)=-4.

Ix=input('enter the initial condition for x')


Iy=input('enter the initial conditions for y')
n=0:0.001:1;
f=input('enter the frequency')
x=sin(2*pi*f*n);
y=zeros(1,length(n));
for i=1:length(n)
y(i)=x(i)+Ix+Iy;
Ix=x(i);
Iy=y(i);
end
subplot(2,1,1)
plot(n,x)
title('Input signal x(n) applied')
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plot(n,y)
title('Sinusoidal Response y(n) of the given first order system')
xlabel('Time')
ylabel('Amplitude')

enter the initial condition for x


Ix = 1

enter the initial conditions for y


Iy = 4
enter the frequency
f =10

Input signal x(n) applied

Amplitude

1
0.5
0
-0.5
-1

0.1

0.1

Amplitude

80

0.2

0.3

0.4

0.5
0.6
0.7
0.8
0.9
Time
Sinusoidal Response y(n) of the given first order system

0.2

0.3

0.4

60
40
20
0

0.5
Time

0.6

0.7

0.8

0.9

Using In-Built Function:


Ix=input('Enter the initial conditions in the form of row vector starting from x(-1)')
Iy=input('Enter the initial conditions in the form of row vector starting from y(-1)')

n=0:0.001:1;
f=input('Enter the frequency')
x=sin(2*pi*f*n);
a=input('Enter the input coefficients in the given system equation, starting from
x(n) coefficient')
b=input('Enter the input coefficients in the given system equation, starting from
y(n) coefficient')
xic=filter(a, b, Iy, Ix);
%Overall response of the system for the given input
y=filter(a, b, x, xic);
subplot(2,1,1)
plot(n,x)
title('Input signal x(n) applied')
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plot(n,y)
title('Sinusoidal Response y(n) of the given first order system')
xlabel('Time')
ylabel('Amplitude')

Enter the initial conditions in the from of row vector starting from x(-1)
Ix =1
Enter the initial conditions in the from of row vector starting from y(-1)

Iy =4
Enter the frequency
f =10
Enter the input coefficients in the given system equation, starting from x(n)
coefficient
a=1

Enter the input coefficients in the given system equation, starting from y(n)
coefficient
b=1

-1
Input signal x(n) applied

Amplitude

1
0.5
0
-0.5
-1

0.1

0.1

Amplitude

80

0.2

0.3

0.4

0.5
0.6
0.7
0.8
0.9
Time
Sinusoidal Response y(n) of the given first order system

0.2

0.3

0.4

60
40
20
0

0.5
Time

0.6

0.7

0.8

0.9

3.To find frequency response of a given system given in


(Transfer function/
Difference Equation form)
a)System expressed in Transfer Function:

1.%This program plots the frequency and phase response of a discrete system
specified by
H(z)=1/(1-0.9z -1)
n= input('enter the number of frequency points over which the response is to be
plotted')
w=0:2*pi/n:2*pi;
%Enter the System function with z replaced by exp(jw)
h1=zeros(1,length(w));
for i=1:length(w)
h1(i)=1/(1-0.9*exp(-j*w(i)));
end
Magh1=abs(h1);
phaseh1=angle(h1);
subplot(2,1,1)
plot(w/pi,Magh1,'k')
title('Magnitude Response of 1/(1-0.9exp(-jw)) with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('Magnitude')
grid
subplot(2,1,2)
plot(w/pi,phaseh1/pi,'k')
title('phase Response of 1/(1-0.9exp(-jw)) with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('phase in pi units')
grid

enter the number of frequency points over which the response is to be plotted100
n = 100

Magnitude Response of 1/(1-0.9exp(-jw))

Magnitude

15
10
5
0

0.2

0.4

0.6
0.8
1
1.2
1.4
1.6
frequency normalized with respect to pi
phase Response of 1/(1-0.9exp(-jw))

1.8

0.2

0.4

0.6
0.8
1
1.2
1.4
1.6
frequency normalized with respect to pi

1.8

phase in pi units

0.4
0.2
0
-0.2
-0.4

Frequency response can be obtained by using the In-Built function freqz.


The syntax is given by [H,W]=freqz(b,a,N). This returns the N-Point frequency
vector W and the N-Point complex frequency response vector H of the system,
given its numerator and denominator coefficients in vectors b and a. The
frequency response is evaluated at N points equally spaced around the upper half of
the unit circle.
Another syntax is [H,W]=freqz(b,a,N,whole), where, the N points used for
computation are around the unit circle.
Another syntax is H=freqz(b,a,w). It returns frequency response at frequencies
designated in vector w, normally between 0 and pi.
Using In-built Function:
n=input('enter the number of frequency points over which the response is to be
plotted')
a=input('enter the numerator coefficients as a vector starting from constant')

b=input('enter the numerator coefficients as a vector starting from constant')


[H w]=freqz(a,b,n,'whole');
magH=abs(H);
phaH=angle(H);
subplot(2,1,1)
plot(w/pi,magH,'k')
title('Magnitude Response of 1/(1-0.9exp(-jw)) with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('Magnitude')
grid
subplot(2,1,2)
plot(w/pi,phaH/pi,'k')
title('phase Response of 1/(1-0.9exp(-jw)) with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('phase in pi units')
grid

enter the number of frequency points over which the response is to be plotted
n = 100
enter the numerator coefficients as a vector starting from constant
a=1

enter the numerator coefficients as a vector starting from constant


b = [1.0000 -0.9000]

Mag
nitud
e

Magnitude Response of (1/(1-0.9exp(-jw))

15
10
5

phase in pi units

0
0

0.2

0.4
0.6
0.8
1.1 1.2
1.4
1 .6
frequency normalized with respect to pi
phase Response of (1/(1-0.9exp(-jw))

1.8

0.2

0.4
0.6
0.8
1
1.2
1.4
1.6
frequency normalized with respct to pi

1.8

0.4
0.2
0
-0.2
-0.4
0

2.%This program plots the frequency and phase response of a discrete system
specified by
H(z)=(1-0.9z -1)
n=input('enter the number of frequency points over which the response is to be
plotted')
w=0:2*pi/n:2*pi;
%Enter the System function with z replaced by exp(jw)
h1=zeros(1,length(w));
for i=1:length(w)
h1(i)=1-0.9*exp(-j*w(i));
end
Magh1=abs(h1);
phaseh1=angle(h1);
subplot(2,1,1)
plot(w/pi,Magh1,'k')
title('Magnitude Response of 1/(1-0.9exp(-jw))with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('Magnitude')
grid
subplot(2,1,2)
plot(w/pi,phaseh1/pi,'k')
title('phase Response of 1/(1-0.9exp(-jw))with n=100')
xlabel('frequency normalized with respect to pi')
ylabel('phase in pi units')
grid

enter the number of frequency points over which the response is to be plotted100
n =100

Magnitude Response of 1/(1-0.9exp(-jw))

2
Magnitude

1.5
1
0.5

phase in pi units

0.4

phase Response of 1/(1-0.9exp(-jw))


0
0

0.2

0.2

0.4
0.6
0.8
1
1.2
1.4
1.6
frequency normalized with respect to pi

1.8

0
-0.2
-0.4
0

0.2

0.4
0.6
0.8
1
1.2
1.4
1.6
frequency normalized with respect to pi

1.8

%This program plots the frequency and phase response of a discrete system
specified by
H(z)=(1-0.9z -1)using the In-Built function
n=input('enter the number of frequency points over which the response is to be
plotted')
a=input('Enter the denominator coefficients as a vector starting from constant')
b=input('Enter the numerator coefficients as a vector starting from constant')
[H w]=freqz(a,b,n,'whole');
magH=abs(H);
phaH=angle(H);
subplot(2,1,1)
plot(w/pi,magH,'k')
title('Magnitude Response of 1-0.9exp(-jw) with n=100')
xlabel('frequency normalised with respect to pi')
ylabel('Magnitude')
grid
subplot(2,1,2)
plot(w/pi,phaH/pi,'k')
title('phase Response of 1-0.9exp(-jw) with n=100')
xlabel('frequency normalised with respect to pi')
ylabel('phase in pi units')
grid

Enter the number of frequency points over which the response is to be plotted100
n = 100
Enter the denominator coefficients as a vector starting from constant
a=1
Enter the numerator coefficients as a vector starting from constant
b =[ 1.0000 -0.9000]

Magnitude Response of (1-0.9exp(-w))

Magnitude

15
10

Phase in pi units

5
0
0

0.2

0.4

0.4
0.8 of (1-0.9exp(-w))
1
1.2
1.4
1.6
phase0.6
Response
frequency normalised with respect to pi

1.8

0.2
0
-0.2
-0.4
0

0.2

0.4
0.6
0.8
1
1.2
1.4
1.6
frequency normalised with respect to pi

1.8

b) System expressed using Difference Equation.


1) The discrete system is represented by the difference equation y(n)= 0.81y(n2)+x(n)-x(n-2).

n=input('enter the number of frequency points over which the response is to


be plotted')
a=input('enter the coefficients of input starting from x(n)')
b=input('enter the coefficients of output starting from y(n)')
[H w]=freqz(a,b,n);
magH=abs(H);
phaH=angle(H);
subplot(2,1,1)
plot(w/pi,magH)
title('Magnitude Response of the system represented by y(n)=0.81y(n2)+x(n)-x(n-2)')
xlabel('frequency normalised with respect to pi')
ylabel('Magnitude')
grid
subplot(2,1,2)
plot(w/pi,phaH/pi)
title('phase Response of the system represented by y(n)=0.81y(n-2)+x(n)x(n-2)')
xlabel('frequency normalised with respect to pi')
ylabel('phase in pi units')
grid

enter the number of frequency points over which the response is to be


plotted100
n =100
enter the coefficients of input starting from x(n)
a= 1

-2

enter the coefficients of output starting from y(n)


b = 1.0000

0 -0.810

Magnitude

Magnitude Response of the system represented by y(n)=0.81y(n-2)+x(n)-x(n-2)


6
4

phase in pi units

2
0
phase Response
the system
y(n)=0.81y(n-2)+x(n)-x(n-2)
0
0.1 of 0.2
0.3 represented
0.4
0.5 by
0.6
0.7
0.8
0.9
1
1
frequency normalised with respect to pi
0.5
0
-0.5
-1
0

0.1

0.2
0.3
0.4
0.5
0.6
0.7
0.8
frequency normalised with respect to pi

0.9

17.Impulse response of first order and second order


systems.
1. Finding the Impulse response of the given first order system.
%This program finds the unit sample response of the first order discrete
system, expressed by its difference equation as y(n)= x(n)+2.y(n-1)
a=input('enter the coefficient vector of input starting from the coefficient of
x(n) term')
b=input('enter the coefficient vector of output starting from the coefficient
of y(n) term')
n1=input('enter the lower limit of the range of impulse response')
n2=input('enter the upper limit of the range of impulse response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
if n(i)==0
x(i)=1;
end
end

h=filter(a,b,x);
stem(n,h)
title('Unit sample response of the discrete system y(n)=x(n)+2.y(n-1) )
xlabel('Time ')
ylabel('Unit Sample Response')
axis([-1 7 0 35])
enter the coefficient vector of input starting from the coefficient of x(n) term
a=1
enter the coefficient vector of output starting from the coefficient of y(n)
term
b =1

-2

enter the lower limit of the range of impulse response


n1 = 0
enter the upper limit of the range of impulse response
n2 =5

35

Unit sample response of the discrete system y(n)=x(n)+2.y(n-1)

30

Ampl

25

itude
20

15

10

0
-1

3
Time

Impulse response can also be obtained by finding the inverse Z-Transform of


the Transfer function of the given discrete system.
[r,p,k] = residuez(a,b) finds the residues, poles, and direct terms of
a partial fraction expansion of the ratio of two polynomials, and a(z 1
) and b(z-1). Vectors a and bspecify the coefficients of the
polynomials of the discrete-time system a(z-1)/b(z-1), in descending
powers of z.
The program plots the inverse Z-Transform computed from the
residues obtained.
The Transfer function of the given system H(z)=1/(1-2z-1). The pole of the
system is z=2, and the residue is 1. Thus, the unit impulse response of the
system, i.e. the inverse Z-Transform of the system is 2n .u(n).

%This program finds the inverse Z-Transform of given H(z), expressed in


negative powers of Z.
a=input('enter the numerator coefficients as a vector starting from
constant')

b=input('enter the denominator coefficients as a vector starting from


constant')
[r,p,k]=residuez(a,b);
disp('The coefficients of each of the partial fractions of X(z) are respectively')
disp(r)
disp('The poles i.e.the roots of each polynomial in the partial fraction
expansion of x(z) are respectively')
disp(p)
if length(k)==0
disp(' There is No constant term in the partial fraction expansion')
else
disp('The constant term in the partial fraction expansion of x(z) is')
disp(k)
end
n1=input('enter the lower limit of the range of impulse response')
n2=input('enter the upper limit of the range of impulse response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
x(i)=(r*p)^(i-1);
end
stem(n,x)
xlabel('time')
ylabel('Amplitude')
title('Inverse Z-Transfrom,i.e.Unit Sample response of discrete system with
H(z)=1/1-2z-1)')
axis([-1 7 0 35])
Enter the numerator coefficients as a vector starting from constant
a =1
Enter the denominator coefficients as a vector starting from constant
b =1

-2

The coefficients of each of the partial fractions of X(z) are respectively


1
The poles i.e.the roots of each polynomial in the partial fraction expansion of
x(z) are respectively
2

There is No constant term in the partial fraction expansion


Enter the lower limit of the range of impulse response
n1 = 0
Enter the upper limit of the range of impulse response
n2 = 5

Inverse Z-Transfrom,i.e.Unit Sample response of discrete system with H(z)=1/1-2z-1)

35

Amplitude

30

25

20

15

10

0
-1

3
time

2.Finding the Impulse Response of the second order system.


% This program find the Unit Sample response of the second order discrete
system represented as y(n)-(3/4)y(n-1)+(1/8).y(n-2)=x(n)+(5/3).x(n-1).
a=input('enter the coefficient vector of input starting from the coefficient of
x(n) term')
b=input('enter the coefficient vector of output starting from the coefficient
of y(n) term')
n1=input('enter the lower limit of the range of impulse response')
n2=input('enter the upper limit of the range of impulse response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
if n(i)==0
x(i)=1;
end
end
h=filter(a,b,x);
stem(n,h)
title('Unit sample response of the discrete system y(n)-(3/4)y(n-1)+(1/8).y(n2)=x(n)+(5/3).x(n-1)')
xlabel('Time ')
ylabel('Amplitude')
axis([-1 7 0 3])
enter the coefficient vector of input starting from the coefficient of x(n) term
a = 1.0000

1.6667

enter the coefficient vector of output starting from the coefficient of y(n)
term
b = 1.0000 -0.7500

0.1250

enter the lower limit of the range of impulse response


n1 = 0
enter the upper limit of the range of impulse response
n2 =5

Unit sample response of the discrete system y(n)-(3/4)y(n-1)+(1/8).y(n-2)=x(n)+(5/3).x(n-1)


3

Amplitude

2.5

1.5

0.5

0
-1

3
Time

5z
3
1
The Transfer function of the given system is H(z)=
3z
z 2
[1
+
]
4
8
1+

Its partial Fraction expansion is H(z)=

7.66
8.66
+
1
z
z 1
1
1
4
2

The poles are and . These are available in the matrix p, after executing
the residuez instruction.
The residues -7.66 and -8.66 will be available in r matrix.
The inverse Z-transform of above H(z) is h(n)= -7.66(1/4) n +8.66(1/2)n.

%This program finds the inverse Z-Transform of given H(z), expressed in


negative powers of Z.
a=input('enter the numerator coefficients as a vector starting from
constant')
b=input('enter the denominator coefficients as a vector starting from
constant')
[r,p,k]=residuez(a,b);
disp('The coefficients of each of the partial fractions of X(z) are respectively')
disp(r)
disp('The poles i.e.the roots of each polynomial in the partial fraction
expansion of x(z) are respectively')
disp(p)
if length(k)==0
disp(' There is No constant term in the partial fraction expansion')
else
disp('The constant term in the partial fraction expansion of x(z) is')
disp(k)
end
n1=input('enter the lower limit of the range of impulse response')
n2=input('enter the upper limit of the range of impulse response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
x(i)=r(1)*(p(1)^(i-1))+r(2)*(p(2)^(i-1));

end
stem(n,x)
xlabel('time')
ylabel('Amplitude')
title('Inverse Z-Transfrom,i.e.Unit Sample response of given discrete system
')
axis([-1 7 0 3])
enter the numerator coefficients as a vector starting from constant
a = 1.0000

1.6667

enter the denominator coefficients as a vector starting from constant


b =1.0000 -0.7500

0.1250

The coefficients of each of the partial fractions of X(z) are respectively


8.6667
-7.6667
The poles i.e.the roots of each polynomial in the partial fraction expansion of
x(z) are respectively
0.5000
0.2500
There is No constant term in the partial fraction expansion
enter the lower limit of the range of impulse response
n1 = 0
enter the upper limit of the range of impulse response
n2 =5

Inverse Z-Transfrom,i.e.Unit Sample response of given discrete system

Amplitude

2.5

1.5

0.5

0
-1

3
time

%This program finds the unit step response of the given discrete system, expressed in
its difference Equation as y(n)-y(n-1)+0.9y(n-2)=x(n).

a=input('enter the coefficient vector of input starting from the coefficient of x(n) term')
b=input('enter the coefficient vector of output starting from the coefficient of y(n)
term')
n1=input('enter the lower limit of the range of step response')
n2=input('enter the upper limit of the range of step response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
if n(i)>=0
x(i)=1;
end
end
h=filter(a,b,x);
stem(n,h,'k')
title('Unit step response of y(n)-y(n-1)+0.9y(n-2)=x(n) ')
xlabel('Time')
ylabel('Amplitude')

axis([-1 6 0 3])

enter the coefficient vector of input starting from the coefficient of x(n) term
a =1
enter the coefficient vector of output starting from the coefficient of y(n)
term
b = 1.0000 -1.0000

0.9000

enter the lower limit of the range of step response


n1 = 0
enter the upper limit of the range of step response
n2 = 5

Unit step response of y(n)-y(n-1)+0.9y(n-2)=x(n)


3

Amplitude

2.5

1.5

0.5

0
-1

3
Time

%This program finds the unit sample response of the discrete system
expressed as y(n)+0.1y(n-1)-0.72y(n-2)=0.3x(n)-0.075x(n-2), from its unit
step response.
a=input('enter the coefficient vector of input starting from the coefficient of
x(n) term')
b=input('enter the coefficient vector of output starting from the coefficient
of y(n) term')
n1=input('enter the lower limit of the range of impulse response')
n2=input('enter the upper limit of the range of impulse response')
n=[n1:n2];
x=zeros(1,length(n));
for i=1:length(n)
if n(i)==0
x(i)=1;
end
end
h=filter(a,b,x);
x1=zeros(1,length(n));
x2=x1;
for i=1:length(n)
if n(i)>=0
x1(i)=1;
end
end
h1=filter(a,b,x1);
for i=1:length(n)
if n(i)>=1
x2(i)=1;
end
end
h2=filter(a,b,x2);
h3=h1-h2;
subplot(2,1,1)
plot(n,h)
title('Unit sample response obtained from Step response')
xlabel('Time')
ylabel('Amplitude')
axis([-1 6 -0.07 0.35])
subplot(2,1,2)

plot(n,h3)
title('Unit sample response obtained from step Response')
xlabel('Time')
ylabel('Amplitude')
axis([-1 6 -0.07 0.35])
Enter the coefficient vector of input starting from the coefficient of x(n)
term
a = 0.3000
0 -0.0750
Enter the coefficient vector of output starting from the coefficient of y(n)
term
b = 1.0000 0.1000 -0.7200
Enter the lower limit of the range of impulse response
n1 = 0
Enter the upper limit of the range of impulse response
n2 =5

Amplitude

Unit sample response obtained from Step response


0.3
0.2
0.1
0

Amplitude

-1
0.3

Unit sample response obtained from step Response


0
1
2
3
4
5
Time

0.2
0.1
0
-1

Time

12.Implementation of Decimation Process

Decimation is the process of reducing the sampling rate of a signal(sampling


rate compression). If F is the existing sampling rate, the new sampling
rate after decimating by an integer factor of M, is F/M. If x(n) is the original
signal, the decimated signal, by a factor of 3 will be y(n)=x(3n).
The Decimator is known as Sub Sampler, Down sampler or Under Sampler.
Sampling rate reduction(by a factor of M) is achieved by discarding M-1
samples, for every M samples. If M=2, one sample out of every two samples
of x(n) are discarded. Decimation process is a data compression operation.
1.Consider a sequence x(n)= (n-1)+2.(n-2)+3(n-3)+4(n-4)+5(n5)+6(n-6) +(n-8) +2(n-9) +3(n-10)+4(n-11)+5(n-12)+6(n-13)+(n15)+2(n-16)+3(n-17)+4(n-18)+5(n-19)
+6(n-20).
The decimated sequence by a factor of 3 will be y(n)=3(n-1)+6(n-2)+2(n3)+5(n-4)+
(n-5)+4(n-6) i.e.y(n) is the sequence x(n),
compressed by a factor of 3.
n=0:20;
x=zeros(1,length(n));
N=input('enter the Decimation factor')
for i=1:length(n)/3
x(i)=i-1;
x(i+length(n)/3)=x(i);
x(i+2*length(n)/3)=x(i);
end
x1=[];
j=1;
for i=1:length(n)
if rem(n(i),N)==0
x1(j)=x(i);
j=j+1;
end
end
n1=0:length(x1)-1;
subplot(2,1,1)
stem(n,x)
xlabel('Time')
ylabel('Amplitude')
title('The given sequence x(n)')
axis([-1 21 0 7])
subplot(2,1,2)
stem(n1,x1)
xlabel('Time')

ylabel('Amplitude')
title('The sequence x(n) decimated by a factor of 3')
axis([-1 21 0 7])
enter the Decimation factor
N =3

The sequence x(n) decimated by a factor of 3

Amplitude

The given sequence x(n)

6
4

Amplitude

2
6 0

10
Time

12

14

16

18

20

2
0

10
Time

12

14

16

18

20

2. x(n)= -3(n)+(n-1)+4(n-2)-3(n-3)+(n-4)+4(n-5). The sequence is to


be decimated by a factor of 2.
x=[-3 1 4 -3 1 4];
n=[0:5];
M=input('enter the decimation factor')
x1=[];
n1=n/M;
n11=[];
j=1;
for i=1:M:length(x)
x1(j)=x(i);
n11(j)=n1(i);
j=j+1;
end
subplot(2,1,1)
stem(n,x)
title('given sequence x(n)')
xlabel('Time')
ylabel('Amplitude')
axis([-1 6 -4 5])
subplot(2,1,2)
stem(n11,x1)
title('The sequence x(n), decimated by a factor of 2')
xlabel('Time')
ylabel('Amplitude')
axis([-1 6 -4 5])

enter the decimation factor


M =2

Amplitude

given sequence x(n)


4
2
0

Amplitude

-2
-4
-1
4

The sequence x(n), decimated by a factor of 2


0
1
2
3
4
Time

2
0
-2
-4
-1

Time

3. A sinusoidal signal of 10Hz frequency is to be sampled at 100 times per sec, and the
sampled signal is to be decimated by a factor of 2.

f=input('enter the frequency of the signal')


T=1/f;
t=-T:T/f:T;
x=sin(2*pi*10*t);
fs=input('enter the sampling frequency')
Ts=1/fs;

n=-T:T/fs:T;
x1= sin(2*pi*10*n*Ts);
N=input('Enter the decimation factor')
xd=[];
n1=n/N;
n11=[];
j=1;
for i=1:N:length(n)
xd(j)=x1(i);
n11(j)=n1(i);
j=j+1;
end
subplot(3,1,1)
stem(t,x)
title('The given sinusoidal signal of frequency 10Hz')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,2)
stem(n,x1)
title('The sinusoidal signal sampled at 100Hz')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,3)
stem(n11,xd)
title('The sampled signal decimated bya factor of 2')
xlabel('Time')
ylabel('Amplitude')

f = 10
enter the sampling frequency
fs = 100
Enter the decimation factor
N= 2

Amplitude
Amplitude
Amplitude

The given sinusoidal signal of frequency 10Hz

1
0
-1
-0.1

-0.08

-0.06

-0.04

-0.02

0.08

0.1

-0.08

-0.06

-0.04

-0.02

0.08

0.1

-0.04

-0.03

-0.02

-0.01

0.04

0.05

0.1

0
0.02 0.04 0.06
Time
The sinusoidal signal sampled at 100Hz

0
-0.1
-0.1
0.1

0
0.02 0.04 0.06
Time
The sampled signal decimated bya factor of 2

0
-0.1
-0.05

0
Time

0.01

0.02

0.03

4. An exponential signal x(n)= an, is to be sampled and should be


decimated.
t=0:0.1:1;
a=input ('Enter the value of the base of the exponent')
x= a.^t;
f=input('Enter the sampling Frequency')
Ts=1/f;
n=0:0.1/f:1;

x1=a.^(n*Ts);
N=input('Enter the decimation factor')
n1=n/M;
j=1;
xd=[];
n11=[];
for i=1:N:length(n)
xd(j)=x1(i);
n11(j)=n1(i);
j=j+1;
end
subplot(3,1,1)
plot(t,x)
title('The given exponential signal x(n)')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,2)
stem(n,x1)
title('The sampled exponential signal ')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,3)
stem(n11,xd)
title('Decimated signal')
xlabel('Time')
ylabel('Amplitude')

Enter the value of the base of the exponent


a = 0.2500
Enter the sampling Frequency
f =5
Enter the decimation factor
N=2

Amplitude
Amplitude
Amplitude

The given exponential signal x(n)

1
0.5
0

0.1

0.2

0.3

0.4

0.5
0.6
0.7
Time
The sampled exponential signal

0.8

0.9

0.1

0.2

0.3

0.4

0.5
0.6
Time
Decimated signal

0.7

0.8

0.9

0.05

0.1

0.15

0.2

0.35

0.4

0.45

0.5

1
0.5
0

1
0.5
0

0.25
Time

0.3

The decimation operation can be done by using the In-Built Function


decimate.
The syntax is a= decimate(h,k). It reduces the sampling rate of the given
sequence h, by a factor k. The resultant sequence is k times shorter in
length than the original sequence h.
5.Generate the signal x(t)=cos(4t) for 0t2, in steps of 0.03 and decimate
the signal by a factor of 2.
t=0:0.03:2;
x=cos(4*pi*t);
N=input('Enter the Decimation Factor')
y=decimate(x,N);
t1=decimate(t,N);
subplot(2,1,1)
stem(t,x)
title('The given signal x(t)')
xlabel('Time')
ylabel('Amplitude')
axis([-0.1 2.1 -2 2])
subplot(2,1,2)

stem(t1,y)
title('The Decimated signal ')
xlabel('Time')
ylabel('Amplitude')
axis([-0.1 2.1 -2 2])
Enter the Decimation Factor
N =2

The given signal x(t)

Amplitude

2
1
0
-1
-2

0.2

0.4

0.6

0.2

0.4

0.6

Amplitude

0.8

1
1.2
1.4
Time
The Decimated signal

1.6

1.8

1.6

1.8

1
0
-1
-2

0.8

1
Time

1.2

1.4

Downsample is also a In-Built function for decimation.


The Syntax is a= downsample(x,n), by which the given sequence x,
whose sampling rate is decreased by the integer n, and the resulting
sequence is a.
In the sequence a, every nth sample, starting from the first sample of x,
will be retained.

6.Let the given sequence be x(n)=2(n)+4(n-1)+7(n-3)+(n-4)+4(n5)+3N-6). Down sample the sequence by a factor of 2.
x=[2 4 7 1 4 3];
n=[0 1 2 3 4 5];
N=input('Enter the downsampling Factor')
x1=downsample(x,N);
n1=downsample(n,N);
subplot(2,1,1)
stem(n,x)
title('The given sequence x(n)')
xlabel('Time')
ylabel('Amplitude')
axis([-1 6 0 8])
subplot(2,1,2)
stem(n1,x1)
title('Down sampled sequence ')
xlabel('Time')
ylabel('Amplitude')
axis([min(n1)-1 max(n1)+1 0 8])

Enter the downsampling Factor


N=2

The given sequence x(n)

Amplitude

8
6
4
2
0
-1

Amplitude

3
Time
Down sampled sequence

6
4
2
0
-1

2
Time

13. Implementation of Interpolation Process


The process of increasing the sampling rate of a signal is called Interpolation
(Sampling Rate Expansion). If F is the existing sampling rate, the new
sampling rate after interpolating by an integer factor of L, is LF.
If x(n) is the original signal, the interpolated signal, by a factor of 3 will be
y(n)=x(n/3).
The Interpolator is known as Up Sampler.
Interpolation of a signal x(n) by a factor L refers to the process of
interpolating L-1 samples (zeros)between each pair of samples of x(n).
For a sequence x(n), Interpolation by a factor of 4, inserts 3(=4-1) zeros
between the adjacent two samples of x(n).
1.If x(n)=5(n-1)+4(n-2)+3(n-3)+(n-4)+2(n-5)+5(n-6)+5(n-8)+4(n9)+3(n-10)+
(n-11)+2(n-12)+5(n-13), interpolate the
sequence by a factor of 3.
x=[0 5 4 3 1 2 5 0 5 4 3 1 2 5];
n=[0 1 2 3 4 5 6 7 8 9 10 11 12 13];
xI=[];
j=1;
L=input('Enter interpolation factor')
n1=0:max(n)*L;
for i=1:length(n)
xI(j)=x(i);
j=j+L;
end
subplot(2,1,1)
stem(n,x)
title('The given sequence x(n)')
xlabel('Time')
ylabel('Amplitude')
axis([-1 15 0 6])
subplot(2,1,2)
stem(n1,xI)
title('The Interpolated sequence')
xlabel('Time')
Ylabel('Amplitude')
axis([-1 40 0 6])

Enter interpolation factor


L=3

The given sequence x(n)

Amplitude

6
4
2
0

15

Time
The Interpolated sequence

6
Amplitude

10

4
2
0

10

15

20
Time

25

30

35

40

2. Interpolate the sequence x(n)=n, for n>0, and zero for otherwise by a
factor of 2. The sequence to be obtained is y(n)=x(n/2).
Since Interpolation factor is 2, One zero is introduced between any two
adjacent samples of x(n).

N=input('Enter the upper limit for the Time axis')


n=0:N-1;
x=n;
L=input('Enter the Interpolation factor')
n1=0:L*max(n);
xL=[];
j=1;
for i=1:length(n)
xL(j)=x(i);
j=j+L;
end
subplot(2,1,1)
stem(n,x)
title('The given sequence x(n)')
xlabel('Time')
ylabel('Amplitude')
axis([-1 N 0 N])
subplot(2,1,2)
stem(n1,xL)
title(' Interpolated sequence ')
xlabel('Time')
ylabel('Amplitude')
axis([-1 L*max(n)+1 0 N])
Enter the upper limit for the Time axis
N = 10
Enter the Interpolation factor
L=2

The given sequence x(n)

Amplitude

10

0
-1

Amplitude

10

5
6
Time
Interpolated sequence

10

Time

10

12

14

16

18

3. A sinusoidal signal of 5Hz frequency is to be sampled at 20 times per sec, and the
sampled signal is to be interpolated by a factor of 2.

f=input('enter the frequency of the signal')


T=1/f;
t=-T:T/f:T;
x=sin(2*pi*10*t);
fs=input('enter the sampling frequency')
Ts=1/fs;
n=-T:T/fs:T;
x1= sin(2*pi*10*n*Ts);
L=input('Enter the Interpolation factor')
n1=-T:T/(L*fs):T;
xI=[];
j=1;
for i=1:length(x1)
xI(j)=x1(i);
j=j+L;
end
subplot(3,1,1)

stem(t,x)
title('The given sinusoidal signal of frequency 10Hz')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,2)
stem(n,x1)
title('The sinusoidal signal sampled at 30Hz')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,3)
stem(n1,xI)
title('The sampled signal Interpolated by a factor of 2')

enter the frequency of the signal


f =5
enter the sampling frequency
fs =20
Enter the Interpolation factor
L =2

Amplitude
Amplitude

The given sinusoidal signal of frequency 5Hz

1
0
-1
-0.2

-0.15

-0.1

-0.05

0.15

0.2

-0.15

-0.1

-0.05

0.15

0.2

-0.1

-0.05

0.15

0.2

0
0.05
0.1
Time
The sinusoidal signal sampled at 20Hz

0
-1
-0.2
1

0
0.05
0.1
Time
The sampled signal Interpolated by a factor of 2

0
-1
-0.2

-0.15

0.05

0.1

Interpolation can be implemented using the In-Built Function interp.


The syntax is a= interp(h,k). This increases the sampling rate of sequence
h by a factor of k. The resultant vector a is k times longer than the
original input vector h.

4. Generate the signal x(t)=cos(4t) for 0t2, in steps of 0.03 and


interpolate the signal by a factor of 2.
t=0:0.03:2;
x=cos(4*pi*t);
N=input('Enter the interpolation Factor')
y=interp(x,N);
t1=interp(t,N);
subplot(2,1,1)
stem(t,x)
title('The given signal x(t)')
xlabel('Time')
ylabel('Amplitude')

axis([-0.1 2.1 -2 2])


subplot(2,1,2)
stem(t1,y)
title('The interpolated signal ')
xlabel('Time')
ylabel('Amplitude')
axis([-0.1 2.1 -2 2])
Enter the Interpolation Factor
N =2
The given signal x(t)

Amplitude

2
1
0
-1
-2

0.2

0.4

0.6

0.2

0.4

0.6

Amplitude

0.8

1
1.2
1.4
Time
The Interpolated signal

1.6

1.8

1.6

1.8

1
0
-1
-2

0.8

1
Time

1.2

1.4

Upsample is the In-Built function to increase the sampling rate of the


sequence by inserting zeros between the samples.
The syntax is given as a= upsample(x,n), where, the given sequence x
for which the sampling rate is increased by the integer n number of times,
and the resulting vector is a.

This function increases the sampling rate of the sequence x, by inserting (n1) zeros between its samples.
4. Interpolate the sequence x(n)= (n-1)+2.(n-2)+3(n-3)+4(n-4)+5(n5)+6(n-6) +(n-8) +2(n-9) +3(n-10)+4(n-11)+5(n-12)+6(n-13)+(n15)+2(n-16)+3(n-17)+4(n-18)+
5(n-19) +6(n-20), by a
factor of 2.
x=[0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6];
n=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
N=input('Enter the Interpolation factor')
x1=upsample(x,N);
n1=0:N*max(n)+1;
subplot(2,1,1)
stem(n,x)
title('The given sequence x(n)')
xlabel('Time')
ylabel('Amplitude')
axis([-1 21 0 7])
subplot(2,1,2)
stem(n1,x1)
title('upsampled sequence ')
xlabel('Time')
ylabel('Amplitude')
axis([-1 42 0 7])
Enter the Interpolation factor
N =2

The given sequence x(n)

Amplitude

6
4
2
0
0

10
12
14
Time
upsampled sequence

16

18

20

Amplitude

6
4
2
0
0

10

15

20
Time

25

30

35

40

14. Implementation of I/D sampling rate


converters.
In some applications, the need often arises to change the sampling rate by a
non integer factor. In Practice, such a non-integer factor is represented by a
rational number, i.e. a ratio of two integers, such that L/M is as close to the
desired factor as possible(L and M are integers).
The sampling frequency change is then achieved by first interpolating the
data by L and then decimating by M.
It is necessary that the interpolation process precedes the decimation,
otherwise the decimation process would remove some of the desired
frequency components.
The over all system is shown

h1(k
)

h1(k
)

Interpolator
Decimator
The two LPFs h1(k) and h2(k) can be combined into a single filter, since they
are in cascade and have a common sampling frequency to give the
generalized sampling rate converter.

x(n)
y(m)

Fs
IFs/D

w(i)

v(i)

h(k)

IFs

IFs

5.Determination of power spectrum of a given


signal(s)
The Estimate of Power Spectral Density is called Periodogram.
N 1

It is given by

1
Pxx ( f )= x ( n ) . e j 2 fn 2 .
N i=0

The samples of the Periodogram can be obtained by suing DFT algorithm.


Let f=k/N, where k=0,1,2,---N-1.
The Periodogram becomes

Pxx

N1

( j 2 n)
k
1
N
= x (n) . e
2 , where k=0,1,2,--N
N i=0

( )

N-1.
If more samples are required in the frequency domain, the length of the
sequence can be increased by zero padding. Let the new length be L.
The PSD is

Pxx

L1

( j2 n)
k
1
L
= x ( n ) . e
2 , where k=0,1,2,--- L-1.
L L i=0

()

Power spectrum for a given sequence x(n) is given by

X ( k ) 2
1
, where

X(k) is the Fourier Transform of the sequence x(n).

1.Obtain the Power Spectrum of x(n)=Cos(2f1n)+Cos(2f2n),


taking n number of samples. Obtain the Power Spectrum for the
sequences of length 8, 16, 32, ..etc.

** The following program finds the 8 Point, 16 Point and 32 Point DFT of the
sequence x(n) of length n=8, and the corresponding Power Spectrum will be
plotted.

f1=input('Enter the frequency of the first signal')


f2=input('Enter the frequency of the second signal')
n1=input('Enter the number of samples')
n=0:n1-1;
x=cos(2*pi*f1*n)+cos(2*pi*f2*n);
k=input('Enter the length of DFT')
K=0:k-1;
X=fft(x,k);
PD=(1/k)*abs(X).^2;
subplot(2,1,1)
stem(K,PD)
title('Power Spectrum of the given sequence computed from 8 Point DFT')
xlabel('Frequency Variable K')
ylabel('Power Spectrum')
subplot(2,1,2)
plot(K,10*log(PD))
title('Power Spectrum of the given sequence computed from 8 Point DFT')
xlabel('Frequency Variable K')
ylabel('Power Spectrum(dB)')
8-Point DFT to plot the power spectrum:
Enter the frequency of the first signal
f1 = 0.6000

Enter the frequency of the second signal


f2 =0.6500
Enter the number of samples
n1 =8
Enter the length of DFT
k =8

Power Spectrum of the given sequence computed from 8 Point DFT

Power Spectrum

6
4
2

Power Spectrum(dB)

20

3
4
5
6
Frequency Variable K
Power Spectrum of the given sequence computed from 8 Point DFT

0
-20
-40
-60

3
4
Frequency Variable K

32-Point DFT to plot the power spectrum:


Enter the frequency of the first signal
f1 = 0.6000
Enter the frequency of the second signal
f2 = 0.6500
Enter the number of samples
n1 =8
Enter the length of DFT
k = 32

Power Spectrum

Power Spectrum of length=8 sequence computed from 32 Point DFT using zero padding
1.5
1
0.5
0

15
20
25
30
35
Frequency Variable K
Power Spectrum of length=8 sequence computed from 32 Point DFT using zero padding
50

Power Spectrum(dB)

10

10

0
-50
-100

15
20
Frequency Variable K

25

30

35

3. Enter the frequency of the first signal


f1 =0.6000
Enter the frequency of the second signal
f2 = 0.6500
Enter the number of samples
n1 = 32
Enter the length of DFT
k =32

Power Spectrum of length=32 sequence computed from 32 Point DFT

Power Spectrum

8
6
4
2

Power Spectrum(dB)

20

10

15
20
25
30
35
Frequency Variable K
Power Spectrum of length=32 sequence computed from 32 Point DFT

10

0
-20
-40

15
20
Frequency Variable K

25

30

35

2. Obtain the Power Spectrum of x(n)=Sin(2f1n)+Cos(2f2n),


taking n number of samples. Obtain the Power Spectrum for the
sequences of length 8, 16, 32, ..etc.
f1=input('Enter the frequency of the first signal')
f2=input('Enter the frequency of the second signal')
n1=input('Enter the number of samples')
n=0:n1-1;
x=Sin(2*pi*f1*n)+cos(2*pi*f2*n);
k=input('Enter the length of DFT')
K=0:k-1;
X=fft(x,k);
PD=(1/k)*abs(X).^2;
subplot(2,1,1)
stem(K,PD)
title('Power Spectrum of the given sequence computed from 8 Point DFT')
xlabel('Frequency Variable K')
ylabel('Power Spectrum')
subplot(2,1,2)
plot(K,10*log(PD))
title('Power Spectrum of the given sequence computed from 8 Point DFT')
xlabel('Frequency Variable K')
ylabel('Power Spectrum(dB)')
8-Point DFT to plot the power spectrum:
Enter the frequency of the first signal
f1 = 0.6000
Enter the frequency of the se

2.To find DFT/IDFT of a given Discrete Time


signal
The Fourier Transform of a sequence x(n) is defined as X(e

jn

x ( n ) .e

n=

)=

Here, x(n) is the discrete signal and X(e j ) is a continuous function of ,


and the range of is from ( to ) or (0 to 2).
Since, X(e j ) is periodic with period 2, it can be sampled with a sampling
frequency equal to an integer multiples of its period.
The Fourier Transform computed only at discrete points is called Discrete
Fourier Transform (DFT) . It is an Algorithm, which computes X(e j ), only at
discrete values of . The DFT is denoted by X(k), and is obtained by
sampling X(e j ) at N equally spaced points.
The spectral spacing is
given by

2
N , and X(k) is computed at k=0,1,2,N-1 and is

N 1

X(k)=

If

WN

= e

(2N )

j(

x ( n) . e

2
)nk
N

n=0

, where, k=0,1,2,N-1.

, called the Twiddle Factor, then the DFT of the sequence


N 1

x(n), is given by X(k)=

x( n)W nkN
n=0

, where, k=0,1,2,N-1.

The sequence x(n) can be obtained from X(k), by taking the Inverse Discrete
1
Fourier Transform (IDFT), and is given by x(n)= N

N 1

X (k ) W nk
N
k=0

n=0,1,2,N-1.
1. Find the 4 point DFT of the sequence x(n)= [ (n), (n-3)].
The given sequence is x= [1 0 0 1] and n=[0 1 2 3].
x=input('enter the given sequence');
n=input('Enter the time indices')
N=input('Enter the length of the DFT sequence required')
k=n;
nk=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n)
nk(i,m)=n(i)*k(m);
twid(i,m)=WN^nk(i,m);
end

, where,

end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+x(m)*twid(i,m);
end
end
magDF=abs(DF);
phaDF=angle(DF)*180/pi;
disp('The DFT of the given sequence is ')
disp(DF)
disp('The corresponding Magnitude Vector is ')
disp(magDF)
disp('The corresponding Phase Vector is')
disp(phaDF)
subplot(2,1,1)
stem(k,magDF)
title('Magnitude Spectrum')
xlabel('k')
ylabel('Magnitude')
axis([-1 N 0 max(magDF)+1])
subplot(2,1,2)
stem(k,phaDF)
title('Phase Spectrum')
xlabel('k')

ylabel('Phase')
axis([-1 N min(phaDF) max(phaDF)])

enter the given sequence


x=[1 0 0 1]
Enter the time indices[0 1 2 3]
n =[ 0

3]

Enter the length of the DFT sequence required


N=4
The DFT of the given sequence is
2.0000

1.0000 + 1.0000i

The corresponding Magnitude Vector is


2.0000

1.4142

0.0000

1.4142

The corresponding Phase Vector is


0 45.0000 -90.0000 -45.0000

0 - 0.0000i 1.0000 - 1.0000i

Magnitude Spectrum

Magnitude

3
2
1
0
-1

-0.5

0.5 Phase
1 Spectrum
1.5
2
k

Phase

40
20

2.5

3.5

0
-20
-40
-60
-80
-1

-0.5

0.5

1.5
k

2.5

2. Find the 4-Point DFT of the sequence x(n)=[1 1 1 1].


enter the given sequence
nx=[1 1 1 1]
Enter the time indices

3.5

n=
[0

3]

Enter the length of the DFT sequence required


N=4
The DFT of the given sequence is
4.0000

-0.0000 - 0.0000i

0 - 0.0000i 0.0000 - 0.0000i

The corresponding Magnitude Vector is


4.0000

0.0000

0.0000

0.0000

The corresponding Phase Vector is


0 -129.6009 -90.0000 -45.3479

Magnitude Spectrum

Magnitude

5
4
3
2

Phase

1
0

0
-1

-0.5

0.5

Phase Spectrum
1
1.5
2
k

2.5

3.5

-50

-100
-1

-0.5

0.5

1.5
k

2.5

3.5

3.Find the Inverse DFT of the sequence X(k)=[2,1+j,0,1-j].


X=input('enter the given sequence')
k=input('Enter the Frequency indices')
N=input('Enter the length of the DFT sequence ')
n=k;

nk=zeros(N,N);
WN=exp(-j*2*pi/N);
IDF=[];
twidd=[];
for i=1:length(k)
for m=1:length(n)
nk(i,m)=n(i)*k(m);
twidd(i,m)=WN^-nk(i,m);
end
end
IDF=zeros(1,N);
for i=1:length(IDF)
for m=1:length(IDF)
IDF(i)=IDF(i)+X(m)*twidd(i,m);
end
end
IDFF=IDF/N;
disp('The IDFT of the given sequence X(k) is ')
disp(IDFF)
stem(n,IDFF)
title('The Time domain sequence obtained')
xlabel('n-->')
ylabel('Amplitude-->')
axis([-1 N+1 0 max(IDFF)+0.2])

enter the given sequence


X = [ 2.0000

1.0000 + 1.0000i

1.0000 - 1.0000i]

Enter the Frequency indices


k= 0

Enter the length of the DFT sequence


N=4
The IDFT of the given sequence X(k) is
1.0000

-0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 - 0.0000i

The Time domain sequence obtained

Ampl

0.8

itude
0.6

0.4

0.2

0
-1

2
n-->

4.Find the Time domain representation of the sequence whose 4 point DFT is
X(k)=[3.8428, 0.3-j2.8572,-2.4428, 0.3+j2.8572].
enter the given sequence
X = [3.8428

0.3000 - 2.8572i

-2.4428

0.3000 + 2.8572i]

Enter the Frequency indices


k=0

Enter the length of the DFT sequence


N =4
The IDFT of the given sequence X(k) is
0.5000

3.0000 - 0.0000i 0.2000 + 0.0000i 0.1428 + 0.0000i

The Time domain sequence obtained


3

2.5
Ampl

itude
-

1.5

0.5

0
-1

2
n-->

To find the N-Point DFT of a sequence x(n) of lengthL(<N), x(n) is made of


lengthN, by appending (N-L) number zeros to x(n). This is referred to as
Zero Padding.
Zero Padding is an operation in which more zeros are appended to the
original sequence. The resulting longer DFT provides closely spaced samples
of the Fourier transform of the original sequence.
Zero Padding gives a high density spectrum and provides better displayed
version for plotting. But, it does not give a high resolution spectrum,
because, no new information is added to the signal, and only additional zeros
are added in the data.
To get high resolution spectrum, more data should be obtained.

5. Find the 8-Point DFT of the sequence x(n)=[1 1 1 1].


x=input('enter the given sequence')
n=input('Enter the time indices')

N=input('Enter the length of the DFT sequence required')


if N>length(x)
for i=length(x)+1:N
x(i)=0;
n(i)=n(i-1)+1;
end
end
k=n;
nk=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n)
nk(i,m)=n(i)*k(m);
twid(i,m)=WN^nk(i,m);
end
end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+x(m)*twid(i,m);
end
end
magDF=abs(DF);

phaDF=angle(DF)*180/pi;
disp('The DFT of the given sequence is ')
disp(DF)
disp('The corresponding Magnitude Vector is ')
disp(magDF)
disp('The corresponding Phase Vector is')
disp(phaDF)
subplot(2,1,1)
stem(k,magDF)
title('Magnitude Spectrum')
xlabel('k')
ylabel('Magnitude')
axis([-1 N 0 max(magDF)+1])
subplot(2,1,2)
stem(k,phaDF)
title('Phase Spectrum')
xlabel('k')
ylabel('Phase')
axis([-1 N min(phaDF) max(phaDF)])

enter the given sequence


x =1

Enter the time indices


n= 0

Enter the length of the DFT sequence required

N =8
The DFT of the given sequence is
4.0000 1.0000 - 2.4142i
-0.0000 - 0.0000i
1.0000 - 0.4142i
0 - 0.0000i
1.0000 + 0.4142i
0.0000 - 0.0000i
1.0000 +
2.4142i
The corresponding Magnitude Vector is
4.0000

2.6131

0.0000

1.0824

0.0000

1.0824

0.0000

2.6131

The corresponding Phase Vector is


0 -67.5000 -135.0000 -22.5000 -90.0000 22.5000 -45.0000 67.5000

Magnitude Spectrum

Magnitude

5
4
3
2
1

Phase

50

0
-1

Phase Spectrum
2
3
4
k

0
-50
-100
-1

6. Example to find the difference between High Density Spectrum


and High Resolution Spectrum:
Consider the sequence x(n)=cos(0.48n)+cos(0.52n).
a) Determine and plot the Fourier Transform of x(n), for 0n9.
n=0:1:99;

x= cos(0.48*n*pi)+cos(0.52*n*pi);
%Take only 10 samples of x and find the corresponding 10 point DFT.
n1=0:9;
x1=x(1:10);
N=input('Enter the length of the DFT sequence required')
k=n1;
n1k=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n1)
n1k(i,m)=n1(i)*k(m);
twid(i,m)=WN^n1k(i,m);
end
end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+x1(m)*twid(i,m);
end
end
magDF=abs(DF);
disp('The DFT of the given sequence is ')
disp(DF)
disp('The corresponding Magnitude Vector is ')
disp(magDF)
subplot(2,1,1)
stem(n,x)
title('The given sequence')
xlabel('n')
ylabel('Magnitude')
axis([-1 N -3 3])
subplot(2,1,2)
%Plotting the result with respect to 2*pi*k/N. Then, normalize wrt pi
plot(k*2/N,magDF)
stem(k,magDF)
title('Magnitude Spectrum')
xlabel('k')
ylabel('Magnitude')
axis([-1 N min(magDF)-1 max(magDF)+1])

Enter the length of the DFT sequence


N = 10

required

The DFT of the given sequence is


1.8460
1.8656 + 1.3223i 2.2114 + 5.8074i 2.2114 - 5.8074i 1.8656 - 1.3223i
1.8460 - 0.0000i 1.8656 + 1.3223i 2.2114 + 5.8074i 2.2114 - 5.8074i 1.8656 - 1.3223i
The corresponding Magnitude Vector is
1.8460
2.2867

2.2867

6.2141

6.2141

2.2867

1.8460

2.2867

6.2141

6.2141

The given sequence

Magnitude

2
0
-2
-1

5
6
n
Magnitude Spectrum

10

10

Magnitude

6
4
2
-1

The given sequence

Magnitude

2
0
-2
-1

5
6
n
Magnitude Spectrum

Magnitude

10

6
4
2
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
-1

5
6
n
Magnitude Spectrum

Magnitude

10

6
4
2
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

To get smoother version of the spectrum, use Zero padding.


b)For the same sequence x1(n), compute 50 point DFT using Zero Padding.
n=0:1:99;
x= cos(0.48*n*pi)+cos(0.52*n*pi);
%Append 40 zeros to the 10 samples of x and find the corresponding 50 point DFT.
n1=0:49;
x1=[x(1:10),zeros(1,90)];
N=input('Enter the length of the DFT sequence required')
k=n1;
n1k=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n1)
n1k(i,m)=n1(i)*k(m);
twid(i,m)=WN^n1k(i,m);
end
end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+x1(m)*twid(i,m);
end
end
magDF=abs(DF);
disp('The DFT of the given sequence is ')
disp(DF)

disp('The corresponding Magnitude Vector is ')


disp(magDF)
subplot(2,1,1)
stem(n1,x1)
title('The given sequence')
xlabel('n')
ylabel('Magnitude')
axis([-1 N -3 3])
subplot(2,1,2)
stem(k,magDF)
title('Magnitude Spectrum')
xlabel('k')
ylabel('Magnitude')

Enter the length of the DFT sequence required


N = 50
The DFT of the given sequence is
1.8460

1.3592 - 0.6466i 0.4362 - 0.4096i 0.1217 + 0.6381i

0.8397 + 1.5060i 1.8656 + 1.3223i 1.9995 + 0.3468i 0.8519 - 0.1076i


-0.5195 + 1.1040i -0.3573 + 3.6824i 2.2114 + 5.8074i 6.1588 + 5.4883i
9.1097 + 2.2593i 9.1097 - 2.2593i 6.1588 - 5.4883i 2.2114 - 5.8074i
-0.3573 - 3.6824i -0.5195 - 1.1040i 0.8519 + 0.1076i 1.9995 - 0.3468i
1.8656 - 1.3223i 0.8397 - 1.5060i 0.1217 - 0.6381i 0.4362 + 0.4096i
1.3592 + 0.6466i 1.8460 - 0.0000i 1.3592 - 0.6466i 0.4362 - 0.4096i
0.1217 + 0.6381i 0.8397 + 1.5060i 1.8656 + 1.3223i 1.9995 + 0.3468i
0.8519 - 0.1076i -0.5195 + 1.1040i -0.3573 + 3.6824i 2.2114 + 5.8074i
6.1588 + 5.4883i 9.1097 + 2.2593i 9.1097 - 2.2593i 6.1588 - 5.4883i
2.2114 - 5.8074i -0.3573 - 3.6824i -0.5195 - 1.1040i 0.8519 + 0.1076i
1.9995 - 0.3468i 1.8656 - 1.3223i 0.8397 - 1.5060i 0.1217 - 0.6381i
0.4362 + 0.4096i 1.3592 + 0.6466i
The corresponding Magnitude Vector is
1.8460

1.5052

0.5984

0.6496

1.7243

2.2867

2.0294

0.8586

1.2201

3.6997

6.2141

8.2494

9.3857

9.3857

8.2494

6.2141

3.6997

1.2201

0.8586

2.0294

2.2867

1.7243

0.6496

0.5984

1.5052

1.8460

1.5052

0.5984

0.6496

1.7243

2.2867

2.0294

0.8586

1.2201

3.6997

6.2141

8.2494

9.3857

9.3857

8.2494

6.2141

3.6997

1.2201

0.8586

2.0294

35

40

45

50

35

40

45

50

2.2867

1.7243

0.6496

0.5984

1.5052

The given sequence

Magnitude

2
0
-2
0

10

15

Magnitude

10

20

25
30
n
Magnitude Spectrum

10

15

20

25
k

30

The given sequence

Magnitude

2
0
-2
0

10

15

Magnitude

10

20

25
30
n
Magnitude Spectrum

35

40

45

50

35

40

45

50

10

15

20

25
k

30

The given sequence

Magnitude

2
0
-2
0

10

15

Magnitude

10

20

25
30
n
Magnitude Spectrum

35

40

45

50

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

0.2

0.4

0.6

For the same sequence, find 100 point DFT, using zero padding.
The given sequence

Magnitude

2
0
-2
0

Magnitude

10

10

20

30

0.2

0.4

0.6

40

50
60
70
80
90
n
Magnitude Spectrum from 100 Point DFT using zero padding

100

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The zero padding provides a smoother version of the spectrum.


The above plot shows that the sequence has dominant frequency at =0.5. But, the
sequence is consisting of two frequencies.

c)To get better spectral information, take the first 100 samples of the sequence, and find the
100 Point DFT of the sequence.
n=0:1:99;
x= cos(0.48*n*pi)+cos(0.52*n*pi);
%Take only 100 samples of x and find the corresponding 100 point DFT.
n1=0:99;
x1=x(1:100);
N=input('Enter the length of the DFT sequence required')
k=n1;
n1k=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n1)
n1k(i,m)=n1(i)*k(m);
twid(i,m)=WN^n1k(i,m);
end
end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+x1(m)*twid(i,m);
end
end
magDF=abs(DF);
disp('The DFT of the given sequence is ')
disp(DF)
disp('The corresponding Magnitude Vector is ')
disp(magDF)
subplot(2,1,1)
stem(n1,x1)
title('The given sequence')
xlabel('n')
ylabel('Magnitude')
axis([-1 N -3 3])
subplot(2,1,2)
%Plotting the result with respect to 2*pi*k/N. Then, normalize wrt pi.
plot(k*2/N,magDF)
title('Magnitude Spectrum from 100 Point DFT, taking 100 samples of x(n)')
xlabel('K in terms of pi units')
ylabel('Magnitude')

The given sequence

Magnitude

2
0
-2
0

Magnitude

60

10

20

30

0.2

0.4

0.6

40

50
60
70
80
90
100
n
Magnitude Spectrum from 100 Point DFT, taking 100 samples of x(n)

40
20
0

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
0

Magnitude

60

10

20

30

0.2

0.4

0.6

40

50
60
70
80
90
100
n
Magnitude Spectrum from 100 Point DFT, taking 100 samples of x(n)

40
20
0

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The above plot clearly shows that, there are two frequencies, which are very close to each
other. This is the high resolution spectrum of x(n).
It can be observed that, padding more zeros to the 100-point sequence, will result in a
smoother rendition of the above spectrum, but, will not reveal any new information.
Consider 200 point DFT of the above 100 point sequence, by using zero padding.

The given sequence

Magnitude

2
0
-2
0

20

40

60

0.2

0.4

0.6

80

100
120
140
160
180
200
n
Magnitude Spectrum from 200 Point DFT, by zero padding for 100 samples of x(n)
60
Magnitude

40
20
0

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
100
120
140
160
180
200
n
Magnitude Spectrum from 200 Point DFT, by zero padding for 100 samples of x(n)
60
Magnitude

20

40

60

0.2

0.4

0.6

80

40
20
0

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The spectrum is repeating, since the signal is also repeating. The given sequence x(n) is

The given sequence

Magnitude

-1

-2

-3

20

40

60

80

100
n

120

140

160

180

200

7.x(n)=cos(2f1n)+cos(2f2n), where f1=1/128 and f2=5/128, amplitude


modulates a signal
xc(n)=cos(2fcn), where fc =50/128. The
resulting amplitude modulated signal is y(n)= xc(n).cos2fcn.
a) Sketch the signals x(n), xc(n) and y(n), for 0n255.
n=0:1:255;
x=cos(2*pi*n/128)+cos(2*pi*5*n/128);
xc=cos(2*pi*50*n/128);
y=x.*xc;
subplot(3,1,1)
plot(n,x)
title('Baseband signal')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,2)
plot(n,xc)
title('Carrier Signal')
xlabel('Time')
ylabel('Amplitude')
subplot(3,1,3)
plot(n,y)
title('Modulated signal')
xlabel('Time')
ylabel('Amplitude')

Baseband signal

2
1.5
1

Amplitude

0.5
0
-0.5
-1
-1.5
-2

50

100

150
Time

200

250

300

200

250

300

Carrier signal

1
0.8
0.6

Amplitude

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

50

100

150
Time

Modulated signal

2
1.5
1

Amplitude

0.5
0
-0.5
-1
-1.5
-2

50

100

150
Time

200

250

300

b)Sketch the 128-point DFT of y(n), for 0n127.


The given sequence

Magnitude

2
0
-2
0

Magnitude

40

20

40

60
80
100
120
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

30
20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
0

Magnitude

40

20

40

60
80
100
120
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

30
20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The above spectrum shows that there are 4 frequencies, which are available after
multiplication.
Consider 256 point DFT of the same sequence of length 127, using zero padding.

n=0:1:127;
x=cos(2*pi*n/128)+cos(2*pi*5*n/128);
xc=cos(2*pi*50*n/128);
y=x.*xc;
%Take 128 samples of x and find the corresponding 128 point DFT.
n1=0:255;
y1=[y(1:128) zeros(1,128)];
N=input('Enter the length of the DFT sequence required')
k=n1;
n1k=zeros(N,N);
WN=exp(-j*2*pi/N);
DF=[];
twid=[];
for i=1:length(k)
for m=1:length(n1)
n1k(i,m)=n1(i)*k(m);
twid(i,m)=WN^n1k(i,m);
end
end
DF=zeros(1,N);
for i=1:length(DF)
for m=1:length(DF)
DF(i)=DF(i)+y1(m)*twid(i,m);

end
end
magDF=abs(DF);
subplot(2,1,1)
stem(n1,y1)
title('The given sequence')
xlabel('n')
ylabel('Magnitude')
axis([-1 N -3 3])
subplot(2,1,2)
%Plotting the result with respect to 2*pi*k/N. Then, normalize
wrt pi.
plot(k*2/N,magDF)
title('Magnitude Spectrum of 128 Point DFT, taking 128 samples of
y(n)')
xlabel('K in terms of pi units')
ylabel('Magnitude')

The given sequence

Magnitude

2
0
-2
0

Magnitude

40

50

100

150
200
250
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

30
20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
0

Magnitude

40

50

100

150
200
250
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

30
20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

This shows that, no new information about the signal is revealed, except smoothing the
spectrum.
c) Find the 128 point DFT of y(n), for 0n99. Here, zero padding is to be done.
The given sequence

Magnitude

2
0
-2
0

Magnitude

30

20

40

60
80
100
120
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

The given sequence

Magnitude

2
0
-2
0

Magnitude

30

20

40

60
80
100
120
n
Magnitude Spectrum of 128 Point DFT, taking 128 samples of y(n)

20
10
0

0.2

0.4

0.6

0.8
1
1.2
K in terms of pi units

1.4

1.6

1.8

4. Implementation of FFT of a given


sequence
Direct computation of DFT requires N2 complex Multiplications(4N2real
Multiplications) and N(N-1) complex Additions[N(4N-2) real Additions].
Hence, for large values of N, direct evaluation involves a considerable
amount of Computation.
The efficient approach for the evaluation of DFT is through the use of FFT
algorithm.
Divide and Combine Approach:
In the Computation of N-Point DFT of a Sequence, N is expressed as the
product of Integers, as N=N1xN2.
The data to be transformed is arranged into a Two Dimensional array, and
DFT of each row is individually computed.
Each element of the resulting array is then multiplied by a Twiddle Factor,
which is an appropriate power of WN= exp(-j.

2
.
N

The resultant Matrix is then transformed , column by column, to obtain the


final result.
If N is a Prime Number, so that factorization is not possible, the original
signal can be zero padded, and the resulting new composite number of
points is factorized.
The Final N-Point DFT of the sequence x(n) is given as X(k)=
N2
x ( .l+ m). W slN

N 1 1

ms
rm
} W N }W N , for 0l N 11 , 0r N 21 , 0m N 21 ,

l =0

N 21

m=0

0s

N 11

The N-Point FFT of a given sequence x(n) can be computed using the Built-in
function fft(x,n), where x is the given sequence and n is the length of the
DFT sequence required.

clear all
x=input('Enter the given sequence')
n=input('Enter the Time Indices')
N=input('Enter the length of the DFT sequence')
%Represent N=N1.N2
N1=input('Enter the Number of rows')
N2=input('Enter the Number of Columns')
%Zero Padding
if N>length(x)
for i=length(x)+1:N
x(i)=0;
n(i)=n(i-1)+1;
end
end
a=1;
x1=[];
w=[];

%Arranging the given sequence in the form of N1XN2 Matrix


for l=1:N1
for m=1:N2
x1(m,l)=x(a);
a=a+1;
end
end
%Computation of Twiddle Factor 1
for s=1:N1
for l=1:N1
w(s,l)=exp(-j*(2*pi/N1)*(s-1)*(l-1));
end
end
% Row DFT
G=x1*w;
%Computation of Twiddle Factor2
for s=1:N1
for m=1:N2
w1(s,m)=exp(-j*(2*pi/N)*(s-1)*(m-1));
end
end
G1=G.*w1';
%Computation of Twiddle Factor 3
for m=1:N2
for r=1:N2

w2(r,m)=exp(-j*(2*pi/N2)*(r-1)*(m-1));
end
end
Column DFT
X1=G1'*w2;
X=reshape(X1,1,N1*N2);
disp('The DFT of the given sequence is')
disp(X)
%Computation of fft using built in Function.
Y=fft(x,N)
Disp(The DFT of the given sequence computed using Built-in function is)
Disp(Y)

1. Find the 4-Point DFT of the sequence x(n)=[1 0 -1 0]


Enter the given sequence
x =[ 1

-1

0]

Enter the Time Indices


n =[ 0

3]

Enter the length of the DFT sequence


N=4
Enter the Number of rows
N1 =2
Enter the Number of Columns
N2 =2
The DFT of the given sequence is

2.0000 + 0.0000i

2.0000 + 0.0000i

The DFT of the given sequence computed using Built-in function is


0

2. Find the 6 point DFT of the sequence x(n)= [ 1 1 1 1 1 1]


Enter the given sequence x =[ 1
Enter the Time Indices

n =[0

1
1

1
2

1
3

1]
4

5]

Enter the length of the DFT sequence


N =6
Enter the Number of rows
N1 = 2
Enter the Number of Columns N2 = 3
The DFT of the given sequence is
6.0000
0.0000 - 0.0000i -0.0000 + 0.0000i -0.0000 - 0.0000i
0.0000 + 0.0000i
-0.0000 - 0.0000i
The DFT of the given sequence computed using Built-in function is
6

3. Find the 8-Point DFT of the sequence x(n)=[1 1 1 1]


Enter the given sequence
x =[1

1]

Enter the Time Indices


n =[ 0

3]

Enter the length of the DFT sequence


N=8

Enter the Number of rows


N1 =2
Enter the Number of Columns
N2 = 4
The DFT of the given sequence is
4.0000
- 0.0000i

1.0000 - 2.4142i -0.0000 - 0.0000i 1.0000 - 0.4142i


0
1.0000 + 0.4142i
0.0000 - 0.0000i 1.0000 + 2.4142i.

The DFT of the given sequence computed using Built-in function is


4.0000
1.0000 - 2.4142i
0
1.0000 - 0.4142i
1.0000 + 0.4142i
0
1.0000 + 2.4142i

IMPLEMENTATION OF FIR LPF FOR A GIVEN SEQUENCE.


Properties of Linear Phase Filters:
j
Consider the linear Phase constraint arg[H( e )] = -, for -,

where is a constant Phase Delay.


This requires that h(n) must be symmetric, i.e. h(n)=h(M-1-n), for 0n(M1), with =

M 1
.
2

Hence, h(n) is symmetric about , which is the index of symmetry.


There are two possible types of symmetry.
*M odd: In this case, =

*M Even: In this case, =

M 1
, is an integer
2
M 1
, is not an integer
2

There is a second Type of Linear Phase FIR filter, if the phase response
j
arg[H( e )] = , which is a straight line, but not through the origin.

In this case, is not a constant Phase Delay, but

arg[ H (e j)]

= - , is a

constant, which is a group delay. Hence, is called a Constant Group Delay.


For this type of linear Phase, h(n)=

- h(M-1-n), for 0n(M-1), with =

M 1

,
=

2
2 . This means that the impulse response h(n) is anti
symmetric. The index of symmetry is still =

M 1
.
2

*M odd: =

M 1
2

*M Even : =

is an integer .

M 1
2

is not an integer.

When the cases of Symmetry and Anti-symmetry are combined with Odd and
Even M, there will be four types of Linear Phase FIR filters.
Frequency Response of the Above Filters:
j
The Frequency Response , H( e ) =

M 1
H r ( e j )e j( ) , =
.
2 , =
2

-----(1)
Here,

H r () , is an amplitude response function, and not a magnitude

response function.
The amplitude response function is a real function, but, unlike the magnitude
response, which is always positive, the amplitude response may be both
positive and negative.
The phase response associated with the magnitude response is a
discontinuous function, while that associated with the amplitude response is
a continuous linear function.
Type-1:Linear Phase FIR Filter: Symmetrical Impulse Response: M
Odd:
In this case, =0, =

M 1
2

is an integer, and h(n)=h(M-1-n), for 0n(M-

1).
j
H( e ) =

M 1
2

a ( n ) .Cosn
n=0

. e j(M 1)/2

---------(2)

where the sequence a(n) is obtained as a(0) = h(

middle sample; and

a(n)=2. h(

M 1
, which is the
2

M 1
n , 1n
2

M 3
.
2

M1
2

H r ( ) = a ( n ) . Cosn .

By Comparing (1) and (2),

n=0

Type-2:Linear Phase FIR Filter: Symmetrical Impulse Response: M


Even:
In this case, =0, =

M 1
2

is not an integer, and h(n)=h(M-1-n), for

0n(M-1).
n
b ( n ) . cos { (
j
H( e ) =

1
)}
2

M
2

---------(3)

n=1

M
Where b(n)= 2h( 2 n , for n= 1,2,---M/2.
n
b ( n ) . cos { (
By Comparing (1) and (3),

H r ( ) =

M
2

1
)}
2

n=1

At =,

H r ( ) =0, regardless of b(n) or h(n). Thus, this type of filter can not

be used for the design of HPF and BEF.


Type-3:Linear Phase FIR Filter: Anti-Symmetrical Impulse Response:
M Odd:

M 1
2

In this case, =/2, =

0n(M-1), and h(

H( e

)=

is an integer, and h(n)= - h(M-1-n), for

M 1
=0.
2

M 1
2

( M1)
j[
]
2
2

c ( n ) . Sinn . e
n=1

, where c(n)= 2h(

M 1
n ,
2

M 1
2

for n= 1,2,---(M-1)/2, and

H r ( ) = c ( n ) . Sinn.
n=1

Type-4:Linear Phase FIR Filter: Anti-Symmetrical Impulse Response:


M Even:
In this case, =/2, =

0n(M-1), and h(

M 1
2

is an integer, and h(n)= - h(M-1-n), for

M 1
=0.
2
n
d ( n ) .sin {(

j
H( e ) =

M
2

1
)}
2
, where d(n)= 2h(

M
n ,
2

n=1

n
d ( n ) .sin {(
for n= 1,2,---M/2, and

1
)}
2

M
2

H r ( ) =
n=1

1.To find and to Plot the Amplitude Response of Various Linear Phase FIR
Filters:
function[Hr,a,w,M,L,pha]=Type1(h,w1)

M=length(h);
L=(M-1)/2;
a=[h(L+1) 2*h(L:-1:1)];
n=[0:1:L];
w=[0:1:w1]'*pi/w1;
Hr=cos(w*n)*a';
pha=[-w*(M-1)/2];

%save the function

function[Hr,a,w,M,L,pha]=Type2(h,w1)
M=length(h);
L=M/2;
a=2*[h(L:-1:1)];
n=[1:1:L];
n=n-0.5;
w=[0:1:w1]'*pi/w1;
Hr=cos(w*n)*a';
pha=[-w*(M-1)/2];
%Save the function
function[Hr,a,w,M,L,pha]=Type3(h,w1)
M=length(h);
L=(M-1)/2;
a=[2*h(L+1:-1:1)];
n=[0:1:L];
w=[0:1:w1]'*pi/w1;
Hr=sin(w*n)*a';
pha=[(pi/2)-w*(M-1)/2];
%save the function
function[Hr,a,w,M,L,pha]=Type4(h,w1)
M=length(h);
L=M/2;
a=2*[h(L:-1:1)];
n=[1:1:L];
n=n-0.5;
w=[0:1:500]'*pi/500;
Hr=sin(w*n)*a';
pha=[(pi/2)-w*(M-1)/2];
%save the function

h=input('Enter the Impulse response Sequence')


w1=input('enter the number of Frequency points')
if rem(length(h),2)==0
if h(1)==h(length(h))
[Hr,a,w,M,L]=Type2(h,w1);
else
[Hr,a,w,M,L]=Type4(h,w1);
end
else
if h(1)==h(length(h))
[Hr,a,w,M,L]=Type1(h,w1);
else
[Hr,a,w,M,L]=Type3(h,w1);

end

end

if rem(length(h),2)==0
p=1;
else
p=0;
end
subplot(4,1,1)
stem(0:M-1,h)
title('Impulse Response sequence of the given linear Phase System')
xlabel('n')
ylabel('h(n)')
axis([-1 length(h)+1 min(a)-1 max(a)+1])
subplot(4,1,2)
stem(p:L,a)
title('a(n) coefficients')
xlabel('n')
ylabel('a(n)')
axis([-1 L+1 min(a)-1 max(a)+1])
subplot(4,1,3)
plot(w/pi,Hr)
grid
title('Amplitude Response')
xlabel('Frequency in pi units')
ylabel('Hr')
subplot(4,1,4)
zplane(h,1)
title('Pole-Zero Plot of the given Linear Phase FIR System')

a)Plot the Amplitude Response of the linear Phase FIR system with
Impulse Response h(n)={-4,1,-1,-2,5,6,5,-2,-1,1,-4}
The given system is a Type-1 FIR Filter, where M=11(odd), and h(n)
is symmetric with respect to = (11-1)/2=5

Enter the Impulse response Sequence


h=

-4

-1

-2

-2

enter the number of Frequency points


w1 = 500

-1

-4

The given system is a Type-1 FIR Filter, where M=11(odd), and h(n)
is symmetric with respect to = (12-1)/2=5.

Impulse Response sequence of the given linear Phase System


10
8
6

h(n)

4
2
0
-2
-4
-6
-8
0

10

12

a(n) coeffcients
10
8
6
4
a(n)

2
0
-2
-4
-6
-8
-1

Amplitude Response

20
15
10

Hr

5
0
-5
-10
-15

0.1

0.2

0.3

0.4
0.5
0.6
Frequency in pi units

0.7

0.8

0.9

Phase Response of the Given System

2
0
-2

phase

-4
-6
-8
-10
-12
-14
-16

0.1

0.2

0.3

0.4
0.5
0.6
frequency in pi units

0.7

0.8

0.9

Pole-Zero Plot of the given Linear Phase FIR System

Imaginary Part

0.5

10

-0.5

-1

-1.5

-1

-0.5

0
Real Part

0.5

1.5

b)Plot the Amplitude Response of the linear Phase FIR system with
Impulse Response h(n)={-4,1,-1,-2,5,6,6,5,-2,-1,1,-4}.
The given system is a Type-2 FIR Filter, where M=12(even), and
h(n) is symmetric with respect to = (12-1)/2=5.5.

Enter the Impulse response Sequence


h=

-4

-1

-2

-2

-1

enter the number of Frequency points


w1 = 500

Impulse Response sequence of the given linear Phase System


12
10
8
6

h(n)

4
2
0
-2
-4
-6
-8
0

6
n

10

12

-4

a(n) coeffcients
12
10
8
6

a(n)

4
2
0
-2
-4
-6
-8
-1

3
n

Amplitude Response

25
20
15
10

Hr

5
0
-5
-10
-15
-20

0.1

0.2

0.3

0.4
0.5
0.6
Frequency in pi units

0.7

0.8

0.9

Phase Response of the Given System

2
0
-2

phase

-4
-6
-8
-10
-12
-14
-16

0.1

0.2

0.3

0.4
0.5
0.6
frequency in pi units

0.7

0.8

Pole-Zero Plot of the given Linear Phase FIR System

Imaginary Part

0.5

11

-0.5

-1
-1

-0.5

0
0.5
Real Part

1.5

0.9

c) Plot the Amplitude Response of the linear Phase FIR system with
Impulse Response h(n)={-4,1,-1,-2,5,0,-5,2,1,-1,4}.
The given system is a Type-3 FIR Filter, where M=11(odd), and h(n)
is symmetric with respect to = (11-1)/2=5.
Enter the Impulse response Sequence
h = -4

-1

-2

-5

-1

enter the number of Frequency points


w1 = 500
Impulse Response sequence of the given linear Phase System
10
8
6

h(n)

4
2
0
-2
-4
-6
-8
0

10

12

a(n) coefficients
10
8
6

a(n)

4
2
0
-2
-4
-6
-8
-1

Amplitude Response

25
20
15

Hr

10
5
0
-5
-10

0.1

0.2

0.3

0.4
0.5
0.6
Frequency in pi units

0.7

0.8

0.9

Phase Response of the Given System

2
0
-2

phase

-4
-6
-8
-10
-12
-14
-16

0.1

0.2

0.3

0.4
0.5
0.6
frequency in pi units

0.7

0.8

0.9

Pole-Zero Plot of the given Linear Phase FIR System

Imaginary Part

0.5

10

-0.5

-1

-1.5

-1

-0.5

0
Real Part

0.5

1.5

d) Plot the Amplitude Response of the linear Phase FIR system with
Impulse Response h(n)={-4,1,-1,-2,5,6,-6,-5,2,1,-1,4}.

The given system is a Type-4 FIR Filter, where M=12(even), and


h(n) is anti-symmetric with respect to = (12-1)/2=5.5
Enter the Impulse response Sequence
h=

-4

-1

-2

-6

-5

Enter the number of Frequency points w1 = 500

Impulse Response sequence of the given linear Phase System


12
10
8
6

h(n)

4
2
0
-2
-4
-6
-8
0

6
n

10

12

-1

a(n) coefficients
12
10
8
6

a(n)

4
2
0
-2
-4
-6
-8
-1

3
n

Amplitude Response

25
20
15

Hr

10
5
0
-5
-10

0.1

0.2

0.3

0.4
0.5
0.6
Frequency in pi units

0.7

0.8

0.9

Phase Response of the Given System

2
0
-2

phase

-4
-6
-8
-10
-12
-14
-16

0.1

0.2

0.3

0.4
0.5
0.6
frequency in pi units

0.7

0.8

0.9

Pole-Zero Plot of the given Linear Phase FIR System

Imaginary Part

0.5
11

-0.5

-1

-1.5

-1

-0.5

0
Real Part

0.5

1.5

Designing FIR Filter using Windowing.

The Designing of FIR filters involves choosing a proper ideal Frequency


Selective filter(which has a non causal and infinite duration impulse
response), and then truncate (windowing) its impulse response to obtain a
linear phase and causal FIR filter.
An Ideal Frequency selective filter is denoted by

H d (e j ) , which has a unity

magnitude gain, and linear phase characteristics over its pass band, and
zero response over its stop Band.
An ideal LPF, of band width

, is given by

H d (e ) = 1.

e j for c
= 0 for

where,

is called cutoff frequency, and is called the sample delay.

The impulse response of this filter is


sin [ wc ( n ) ]
(n )

for n

1
hd ( n )=
H d ( e j ) . ( e jn ) .d

,wc
, for n= .

The above impulse response is symmetric with respect to .


To obtain an FIR filter from

hd ( n ) ,

it is to be truncated on both sides. To

obtain a causal and linear phase FIR filter h(n) of length M, we must have
h(n)= hd ( n ) , for 0nM-1
=0 else
M 1
where, and =
.
2
This operation is called Windowing.

h(n) can be thought of as being formed by the product of

hd ( n ) ,

and a

window function w(n) as h(n)= hd ( n ) . w ( n ) , where , w(n)=


some symmetric function withrespect

, and zero other wise.


{ n M 1 }
0
Various window functions used are given as follows:
1.Rectangular Window:
W(n)= 1 for 0nN-1;
= 0 otherwise.

2. Hamming Window:
W(n)= 0.54 0.46

cos

n
( N2 1
) for 0 n N 1

= 0 otherwise.

3.Hanning Window:
W(n)= 0.5 0.5

cos

= 0 otherwise.
4.Blackman Window:
W(n)= 0.42 0.5
cos

n
n
( N2 1
)+0.08 cos ( N4 1
) for 0 n N 1
=0 otherwise.

n
( N2 1
) for 0 n N 1

5.Bartlett(Triangular) Window:

W(n)=

|
1

N1
2
for 0 n N 1
N 1

2 n

=0 otherwise.
Commonly used window functions characteristics:
Window
Name
Rectangular
Bartlett
Hanning
Hamming
Blackman

Transition width
Approxima Exact
te
Value
4/M
1.8/M
8/M
6.1/M
8/M
6.2/M
8/M
6.6/M
12/M
11/M

Min. Stop
Band
Attenuation.
21 dB
25dB
44dB
53dB
74dB

Built-in functions for various Windows:


**W=boxcar(M) returns the M-Point rectangular window function in array W.
**W=triang(M) returns the M-Point Bartlett(Triangular)window function in
array W.
**W=hanning(M) returns the M-Point Hanning window function in array W.
**W=hamming(M) returns the M-Point rectangular window function in array
W.
**W=blackman(M) returns the M-Point Blackman window function in array W.
**W=Kaiser(M,beta) returns the beta valued M-Point rectangular window
function in array W.

[h,w] = freqz(b,a,l) returns the frequency response vector h and the


corresponding angular frequency vector w for the digital filter whose transfer
function is determined by the (real or complex) numerator and denominator
polynomials represented in the vectors b and a, respectively. The vectors h
and w are both of length l. The angular frequency vector w has values
ranging from 0 to radians per sample. When you don't specify the integer
l, or you specify it as the empty vector [], the frequency response is
calculated using the default value of 512 samples.
h = freqz(b,a,w) returns the frequency response vector h calculated at the
frequencies (in radians per sample) supplied by the vector w. The vector w
can have any length.
Frequency Response of Various Windows:
%Frequency and Phase Response of Rectangular Window
clear all
m=input('Enter the length of the Window')
n=0:m-1;
% Window function computed from its expression
window=ones(1,length(n));
subplot(2,1,1)
stem(n,window)
title('Window function computed from its expression')
xlabel('n')
ylabel('w(n)')
% Window function using Built-in Function
WR=boxcar(m);
subplot(2,1,2)
stem(n,WR)
title('Window function using Built-in Function')

xlabel('n')
ylabel('w(n)')
%Computation of Amplitude response of Rectangular Window
%using expression for Frequency Response
w=linspace(0,pi,512);
for k=1:m
for l=1:length(w)
if n(k)==0||w(l)==0
h(l)=1;
else
h(l)=m*((sin(n(k)*w(l))/2)/(sin(n(k))/2))*(exp(-i*(w(l)*((m-1)/2))));
end
end
end
subplot(2,1,1)
plot(w/pi,20*log10(abs(h)))
grid
xlabel('w/pi Units')
ylabel('Amplitude Response')
axis([0 1 -40 40])
%Computation of Amplitude response of Rectangular Window
%using Built-in function
[FREWR1,w]=freqz(WR,1,512);
subplot(2,1,2)
plot(w/pi,20*log10(abs(FREWR1)))

grid
xlabel('w/pi Units')
ylabel('Amplitude Response')

1.Design a Low Pass FIR filter for N=4 and Wc=0.4 rad, using
hamming Window
%Design of the required filter using hamming window
N=input('enter the length of the filter')
Wc=input('enter the digital cut off frequency')
n=0:N-1;
%Computation of window coefficients
w=0.54-0.46*cos((2*pi*n)/(N-1));
alpha=(N-1)/2;
%Computation of coefficients of Ideal LPF
for i=0:N-1

if i==alpha
hd(i+1)=Wc/pi;
else
hd(i+1)=sin(Wc*(i-alpha))/(pi*(i-alpha));
end
end
%hn will be filter coefficients of the windowed filter
hn=hd.*w;
%Computation of filter coefficients using built-in function.
%The Built-in function gives the window coefficients in a column vector
w1=hamming(N);
hn1=hd.*w1';
disp('Filter coefficients computed using window function')
disp(hn)
disp('Filter coefficients computed using Built-in function')
disp(hn1)
enter the length of the filter N =4
enter the digital cut off frequency Wc = 0.4000
Filter coefficients computed using window function
0.0096

0.0974

0.0974

0.0096

Filter coefficients computed using Built-in function


0.0096
2.

0.0974

0.0974

0.0096

S-ar putea să vă placă și