0% found this document useful (0 votes)
14 views

DSP_ IPCC

dsp lab

Uploaded by

manasatp773
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

DSP_ IPCC

dsp lab

Uploaded by

manasatp773
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Prepared by Dr.

Harish M S, AIT

Digital Signal Processing (BEC502)


Practical component of IPCC
Programs to be implemented & executed using any programming languages like Moku:
Go/ MATLAB/OCTAVE (but not limited to).

Expt No 1: Program to generate the following discrete time signals. a) Unit sample sequence,
b)Unit step sequence, c) Exponential sequence, d)Sinusoidal sequence, e) Random sequence
Theory: Discrete time signals denoted as 𝒙(𝒏), discrete time signal values are defined at discrete
instant of time only. In discrete signals, time is discritized but amplitude is continuous. The
discrete time signals are derived from continuous time signal by uniform sampling,
mathematically
𝑥(𝑛) = 𝑥(𝑡)|𝑡=𝑛𝑇𝑠 , 𝑛 = 0, ±1, ±2, … …
Where, 𝑥(𝑡) is a continuous time signal, in continuous signal, signal value is defined for all the
values of time t. And 𝑇𝑠 is the sampling interval or sampling period in seconds, it also a spacing
between the successive samples or sequence. Let us generate different types of discrete time
signals using MatLab codes.
a) Unit sample sequence: The unit sample sequence or unit impulse is denoted as 𝛿 (𝑛) and is
defined as
1, 𝑓𝑜𝑟 𝑛 = 0
𝛿 (𝑛 ) = {
0, 𝑓𝑜𝑟 𝑛 ≠ 0
MatLab program for unit impulse signal
clc;
clear all;
close all;
n=-2:1:2;
y=[zeros(1,2),ones(1,1),zeros(1,2)];
stem(n,y)
title('Unit impulse signal')
xlabel('Independent variable n')
ylabel('Amplitude')

b) Unit step sequence: The unit step signal is denoted as 𝑢(𝑛) and is defined as
1, 𝑓𝑜𝑟 𝑛 ≥ 0
𝑢 (𝑛 ) = {
0, 𝑓𝑜𝑟 𝑛 < 0

1
Prepared by Dr. Harish M S, AIT

MatLab program unit step signal


clc;
clear all;
close all;
n=0:1:7;
y=ones(1,8);
stem(n,y)
title('Unit stem signal')
xlabel('Independent variable n')
ylabel('Amplitude

c) Exponential sequence: The exponential sequence is defined as


𝑥(𝑛) = 𝑎𝑛 , for all n
If the parameter 𝑎 is real, then 𝑥(𝑛) is real exponential signal.
MatLab program real exponential signal if 0 < 𝑎 < 1, 𝑎 > 1, −1 < 𝑎 < 0 , 𝑎 < −1
clc;
clear all;
close all;
a=input('Enter the value of a');
n=0:8;
y=a.^n;
stem(n,y);
title('Real exponential signal');
xlabel('Independent variable n')
ylabel('Amplitude')

When the parameter 𝑎 is complex, it can be expressed as 𝑎 ≡ 𝑟𝑒 𝑗𝜃 , then 𝑥(𝑛) is complex


exponential, now 𝑟 and 𝜃 are the parameters. Hence 𝑥(𝑛) can be expressed as
𝑥(𝑛) = (𝑟𝑒 𝑗𝜃 )𝑛 = 𝑟 𝑛 𝑒 𝑗𝜃𝑛 = 𝑟 𝑛 (𝑐𝑜𝑠𝜃𝑛 + 𝑗𝑠𝑖𝑛𝜃𝑛) = 𝑟 𝑛 𝑐𝑜𝑠𝜃𝑛 + 𝑟 𝑛 𝑗𝑠𝑖𝑛𝜃𝑛
Now real part of 𝑥(𝑛) is 𝑥𝑅 (𝑛) = 𝑟 𝑛 𝑐𝑜𝑠𝜃𝑛 and imaginary part of 𝑥(𝑛) is 𝑥𝐼 (𝑛) = 𝑟 𝑛 𝑠𝑖𝑛𝜃𝑛.
The graphs of 𝑥(𝑛) has two plots, to plot the graphs of 𝑥𝑅 (𝑛) and 𝑥𝐼 (𝑛) let us take 𝑟 = 0.9
𝜋
and 𝜃 = 10. We observed that both 𝑥𝑅 (𝑛) and 𝑥𝐼 (𝑛)decaying exponentially.

MatLab program for real part of complex exponential signal


clc;
clear all;
close all;
n=0:20;
r=input('Enter the value of r');
theta=pi/10;
y=(r.^n).*cos(theta*n);

2
Prepared by Dr. Harish M S, AIT

stem(n,y)
title('Real part of complex exponential signal');
xlabel('Independent variable n')
ylabel('Amplitude')

MatLab program for imaginary part of complex exponential signal


clc;
clear all;
close all;
n=0:20;
r=input('Enter the value of r');
theta=pi/10;
y=(r.^n).*sin(theta*n);
stem(n,y)
title('Imaginary part of complex exponential signal');
xlabel('Independent variable n')
ylabel('Amplitude')

d) Sinusoidal sequence: The discrete time sinusoidal signal is defined as


𝑥(𝑛) = 𝐴𝑠𝑖𝑛(𝜔𝑛 + 𝜃) = 𝐴𝑠𝑖𝑛(2𝜋𝑓𝑛 + 𝜃) −∞≤𝑛≤∞
The frequency 𝑓 has dimension of cycles per sample. Since sample is the unit less quantity, thus
𝜋 𝜋
the digital frequency 𝑓 is in cycles. Example, consider 𝑥 (𝑛) = 3𝑠𝑖𝑛 ( 6 𝑛 + 3 ) , is a discrete-
𝜋 𝜋
time sinusoidal with frequency 𝜔 = 6 𝑟𝑎𝑑/𝑠𝑎𝑚𝑝𝑙𝑒, phase 𝜃 = 3 𝑟𝑎𝑑 .

MatLab program to generate sinusoidal signal


clc;
clear all;
close all;
n=-20:1:20;
r=input('Enter the amplitude of the signal');
omega=pi/6;
theta=pi/3;
y=r*sin(omega.*n+theta);
stem(n,y)
title('Sinusoidal signal');
xlabel('Independent variable n')
ylabel('Amplitude')

e) Random sequence: In many real time applications, signals cannot be expressed by


mathematical expressions are known as random signals. Therefore, in random signals, there
is an uncertainty with respect to its value before its actual occurrence. The random signals

3
Prepared by Dr. Harish M S, AIT

cannot be completely represented by mathematical expression. The mathematical analysis of


random is provided by theory of probability and stochastic process. Example for random
signals are, noise signal, speech signal, seismic signal etc.
To generate a discrete time random signal, we can use various random number generation
methods that depend on the distribution that the signal to follow. For uniform distribution,
random values have an equal probability within a specified range. And for Gaussian
(Normal) distribution random values follow a bell-shaped distribution characterized by a
mean and standard deviation.
MatLab program to generate random signal
clc;
clear all;
close all;
N = 100; % Number of samples
amplitude = 5; % Amplitude range for uniform random signal
mean_val = 0; % Mean for Gaussian random signal
std_dev = 1; % Standard deviation for Gaussian random signal
% Uniform random signal generation
random_uniform = amplitude * (2*rand(1, N) - 1) % Values between -amplitude and
+amplitude
% Gaussian (normal) random signal generation
random_gaussian = mean_val + std_dev * randn(1, N)
% Plot uniform random signal
subplot(2,1,1);
stem(random_uniform, 'r');
title('Uniform Random Signal');
xlabel('Time Index');
ylabel('Amplitude');
% Plot Gaussian random signal
subplot(2,1,2);
stem(random_gaussian, 'b');
title('Gaussian Random Signal');
xlabel('Time Index');
ylabel('Amplitude');

Result and conclusion:

4
Prepared by Dr. Harish M S, AIT

Expt No 2: Program to perform the following operations on signals. a) Signal addition, b) Signal
multiplication, c) Scaling, d) Shifting, e) Folding
Theory: If the signal is applied to the system, the system performs the prescribed operation on
the input signal and produces the desired output signal. The operations on signals are of two
types.
1. Operation performed on dependent variables
2. Operation performed on the independent variables
Operation performed on the dependent variable are
i. Signal addition
ii. Signal multiplication
iii. Signal scaling
Operation performed on the independent variable are
i. Signal shifting
ii. Signal folding
Signal addition: Let 𝑥1 (𝑛) and 𝑥2 (𝑛) are pair of discrete time signal. Then the signal 𝑦(𝑛) is
obtained by the addition of 𝑥1 (𝑛) and𝑥2 (𝑛) is defined by 𝑦(𝑛) = 𝑥1 (𝑛) + 𝑥2 (𝑛)
Signal multiplication: Let 𝑥1 ( 𝑛) and 𝑥2 (𝑛) are pair of discrete time signal. Then the signal
𝑦(𝑛) is obtained by the multiplication of 𝑥1 (𝑛) and𝑥2 (𝑛) is defined by
𝑦(𝑛) = 𝑥1 (𝑛)𝑥2 (𝑛)
Signal scaling: Let 𝑥(𝑛) denotes a discrete time signal. Then the signal 𝑦(𝑛) resulting from
amplitude scaling applied to 𝑥(𝑛) is defined by 𝑦(𝑛) = 𝑎𝑥(𝑛) .Where 𝑎 is scaling factor, if 𝑎 <
1 signal is attenuated, if 𝑎 > 1 signal is amplified.
Signal shifting: Let 𝑥(𝑛) denotes the discrete time signal. The time shifted version of 𝑥(𝑛)
is 𝑦(𝑛) = 𝑥(𝑛 − 𝑘).Where 𝑘 is the time shift operator, if 𝑘 > 0 the signal y (n) is obtained by
shifting 𝑥(𝑛) towards right by k samples. If 𝑘 < 0 the signal 𝑦(𝑛) is obtained by shifting 𝑥(𝑛)
to the left by k samples, and shift operator k must be an integer.
Signal folding: Let 𝑥(𝑛) denotes the continuous time signal, the reflection or folded version of
𝑥(𝑛) is 𝑦(𝑛) is obtained by replacing t by −𝑛 . Therefore reflection of 𝑥(𝑛) is
𝑦(𝑛) = 𝑥(−𝑛)

MatLab program for signal addition

5
Prepared by Dr. Harish M S, AIT

clc;
clear all;
close all;
n1=input('Enter the range of first signal');
x1=input('Enter the samples of first signal');
n2=input('Enter the range of second signal');
x2=input('Enter the samples of second signal');
if n1(1)<n2(1)
x2=[zeros(1,abs((abs(n1(1)))-(abs(n2(1))))),x2];
end
if n1(1)>n2(1)
x1=[zeros(1,abs((abs(n1(1)))-(abs(n2(1))))),x1];
end
if n1(2)<n2(2)
x1=[x1,zeros(1,abs((abs(n1(2)))-(abs(n2(2)))))];
end
if n1(2)>n2(2)
x2=[x2,zeros(1,abs((abs(n1(2)))-(abs(n2(2)))))];
end
N1=min(n1(1),n2(1));
N2=max(n1(2),n2(2));
n=N1:N2;
subplot(3,1,1)
stem(n,x1)
subplot(3,1,2)
stem(n,x2)
y=x1+x2;
subplot(3,1,3)
stem(n,y)

MatLab program for signal multiplication


clc;
clear all;
close all;
n1=input('Enter the range of first signal');
x1=input('Enter the samples of first signal');
n2=input('Enter the range of second signal');
x2=input('Enter the samples of second signal');
if n1(1)<n2(1)
x2=[zeros(1,abs((abs(n1(1)))-(abs(n2(1))))),x2];
end
if n1(1)>n2(1)
x1=[zeros(1,abs((abs(n1(1)))-(abs(n2(1))))),x1];
end
if n1(2)<n2(2)
x1=[x1,zeros(1,abs((abs(n1(2)))-(abs(n2(2)))))];

6
Prepared by Dr. Harish M S, AIT

end
if n1(2)>n2(2)
x2=[x2,zeros(1,abs((abs(n1(2)))-(abs(n2(2)))))];
end
N1=min(n1(1),n2(1));
N2=max(n1(2),n2(2));
n=N1:N2;
subplot(3,1,1)
stem(n,x1)
subplot(3,1,2)
stem(n,x2)
y=x1.*x2;
subplot(3,1,3)
stem(n,y)

MatLab program for signal scaling


clc;
clear all;
close all;
n1=input('Enter the range of the signal');
x1=input('Enter the samples of the signal');
a=input('Enter the value of scaling factor');
n=n1(1):n1(2);
subplot(2,1,1)
title(‘Orginal signal’)
stem(n,x1)
y=a*x1;
subplot(2,1,2)
stem(n,y)
title(‘Scaled signal)

MatLab program for signal shifting


clc;
clear all;
close all;
n=input('Enter the range of the signal');
x=input('Enter the samples of the orginal signal');
n1=n(1):n(2);
subplot(2,1,1)
stem(n1,x)
title('Orginal signal')
xlabel('Time index n')
ylabel('Amplitude of the orginal signal')
shift=input('Enter the value for shift operator k');
if shift>0

7
Prepared by Dr. Harish M S, AIT

shifted=n1+shift;
else
shifted=n1+shift;
end
subplot(2,1,2)
stem(shifted,x)
title('Shifted signal')
xlabel('Time index n')
ylabel('Amplitude of the signal')

MatLab program for signal folding


clc;
clear all;
close all;
n=input('Enter the range of the signal');
x=input('Enter the samples of the orginal signal');
n1=n(1):n(2);
subplot(2,1,1)
stem(n1,x)
title('Orginal signal')
xlabel('Time index n')
ylabel('Amplitude of the orginal signal')
y=flip(x);
n2=flip(n1);
subplot(2,1,2)
stem(-n2,y)
title('Folded signal')
xlabel('Time index n')
ylabel('Amplitude of the folded signal')

Result and conclusion:

8
Prepared by Dr. Harish M S, AIT

Expt No 3: Program to perform convolution of two given sequences (without using built-in
function) and display the signals.
Theory: Convolution is a mathematical operation used to express the relation between input and
output of an LTI system. It relates input, output and impulse response of an LTI system. The
impulse response is the response due to impulse input denoted input ℎ(𝑛). The important
significance of convolution is, if we know the impulse response h (n) of the system we can find
the output y (n) for any arbitrary input signal x (n). For discrete time signal x (n) and h (n), the
linear convolution is defined as
𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛) = ∑𝑘𝑘=−∞ 𝑥(𝑘)ℎ(𝑛 − 𝑘) = ∑𝑘𝑘=−∞ ℎ(𝑛 − 𝑘)𝑥(𝑘) −∞ ≤ 𝑛 ≤ ∞

MatLab program for convolution two signals without using built-in function
clc;
clear all;
close all;
x1=input('Enter the samples of the first signal');
x2=input('Enter the samples of the second signal');
N1=length(x1);
N2=length(x2);
N=N1+N2-1;
n=0:N-1;
x1=[x1,zeros(1,N-N1)];
x2=[x2,zeros(1,N-N2)];
subplot(3,1,1);
stem(n, x1);
title('First signal')
xlabel('Time index n')
ylabel('Amplitude')
subplot(3,1,2)
stem(n, x2);
title('Secong signal')
xlabel('Time index n')
ylabel('Amplitude')
for n1=1:N
y(n1)=0;
for i=1:N
j=n1-i+1;
if (j<=0)
j=N+j;
end
y(n1)=y(n1)+x1(i)*x2(j);
end
end

9
Prepared by Dr. Harish M S, AIT

disp('y')
disp(y);
l=0:1:N-1;
subplot(3,1,3)
stem(l,y)
title('Convolued signal')
xlabel('Time index n')
ylabel('Amplitude')

MatLab program for Convolution of two signal using built-in function


clc;
clear all;
close all;
n1=input('Enter the range of the first signal');
x1=input('Enter the samples of the first signal');
n2=input('Enter the range of the second signal');
x2=input('Enter the samples of the second signal');
n3=n1(1)+n2(1):n1(2)+n2(2);
y=conv(x1,x2);
stem(n3,y);
title('Convoled signal')
xlabel('Time index n')
ylabel('Amplitude')

Result and conclusion:

10
Prepared by Dr. Harish M S, AIT

Expt No 4: Consider a causal system 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛). a) Determine 𝐻(𝑧) and
sketch its pole zero plot. b) Plot |𝐻(𝑒 𝑗𝜔 )| and ∠ 𝐻(𝑒 𝑗𝜔 ). c) Determine the impulse response
ℎ(𝑛).
Theory:
a. Determine 𝑯(𝒛) and sketch its pole zero plot.
Any digital linear time invariant (LTI) system can be represented by difference equation. The
difference equation relates the input and output of the system. The general form of linear
constant coefficient difference equation is
𝑁 𝑀

∑ 𝑎𝑘 𝑦(𝑛 − 𝑘) = ∑ 𝑏𝑘 𝑥(𝑛 − 𝑘)
𝑘=0 𝑘=0

Where 𝑎𝑘 and 𝑏𝑘 are contents, 𝑥(𝑛) is an input and 𝑦(𝑛) is the output of the system and N is the
order of the system. The given difference equation 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛) is a first order
system.
The z-transform is a powerful mathematical tool for the analysis of discrete time LTI systems
and signals. The z-transform of a discrete time signal 𝑥(𝑛) is defined as

𝑧[𝑥(𝑛)] = 𝑋(𝑧) = ∑ 𝑥(𝑛)𝑧 −𝑛


𝑛=−∞

The z-transform of the given difference equation 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛) is


𝑌 (𝑧) = 0.9𝑌(𝑧)𝑧 −1 + 𝑋(𝑧)
𝑌(𝑧)[1 − 0.9𝑧 −1 ] = 𝑋(𝑧)
𝑌(𝑧) 1 𝑧
The transfer function of the system is 𝐻 (𝑧) = 𝑋(𝑧) = 1−0.9𝑧 −1 = 𝑧−0.9

Single pole is at 𝑧 = 0.9, and single zero is at 𝑧 = 0.


MatLab program for pole-zero plots on the plane
clear all;
close all
clc;
num=[1 ];
den=[1 -0.9];
zplane(num,den)
title('Pole-Zero plot')
b. Plot |𝑯(𝒆𝒋𝝎 )| and ∠ 𝑯(𝒆𝒋𝝎 )

11
Prepared by Dr. Harish M S, AIT

In this part we have to plot magnitude plot |𝐻(𝑒 𝑗𝜔 )| and phase angle plot∠ 𝐻(𝑒 𝑗𝜔 ) of Discrete
Time Fourier Transform (DTFT) of ℎ(𝑛). Let ℎ(𝑛)is impulse response of the system and the
given difference equation is 𝑦(𝑛) = 0.9𝑦(𝑛 − 1) + 𝑥(𝑛) and its corresponding transfer
1
function of the system is 𝐻 (𝑧) = 1−0.9𝑧 −1

Apply inverse z-Transform, we get impulse response ℎ(𝑛)


ℎ(𝑛) = 0.9𝑛 u(n)
The Discrete-Time Fourier Transform (DTFT) of a discrete-time sequence ℎ(𝑛) is a frequency
domain representation. The DTFT of ℎ(𝑛) is 𝐻(𝑒 𝑗𝜔 ) defined as a continuous function of a real-
valued frequency variable 𝜔 (normalized angular frequency), and it represents how the sequence
is composed of different frequency components. The 𝐻(𝑒 𝑗𝜔 ) is continuous function of 𝜔 and it
is periodic with period 2𝜋radian.
The DTFT of a sequence ℎ(𝑛) is given by the formula:

𝑗𝜔 )
𝐻(𝑒 = 𝐻 (𝜔) = ∑ ℎ(𝑛)𝑒 −𝑗𝜔𝑛
𝑛=−∞

The DTFT of ℎ(𝑛) is 𝐻 (𝜔) is a complex quantity and it is a continuous function of 𝜔, it


periodic and it period is 2𝜋 radian. The DTFT of ℎ(𝑛) = 0.9𝑛 u(n) is
∞ ∞ ∞
−𝑗𝜔𝑛 𝑛 −𝑗𝜔𝑛
1
𝐻 (𝜔 ) = ∑ ℎ(𝑛 )𝑒 = ∑ 0.9 𝑒 = ∑(0.9𝑒 −𝑗𝜔 )𝑛 =
1 − 0.9𝑒 −𝑗𝜔
𝑛=−∞ 𝑛=0 𝑛=0

1 1 1
𝐻 (𝜔 ) = −𝑗𝜔
= =
1 − 0.9𝑒 1 − 0.9(𝑐𝑜𝑠𝜔 − 𝑗𝑠𝑖𝑛𝜔) 1 − 0.9𝑐𝑜𝑠𝜔 − 𝑗0.9𝑠𝑖𝑛𝜔
1
Magnitude of 𝐻 (𝜔)is |𝐻(𝜔)| = and phase angle of 𝐻(𝜔)is∠ 𝐻(𝑒 𝑗𝜔 ) =
√(1−0.9𝑐𝑜𝑠𝜔)2 +(0.9𝑠𝑖𝑛𝜔)2

0.9𝑠𝑖𝑛𝜔
−𝑡𝑎𝑛−1 (1−0.9𝑐𝑜𝑠𝜔 ).

MatLab program for Plot |𝑯(𝝎)| and ∠ 𝑯(𝝎)


clear all;
close all
clc;
omega=linspace(0,pi,1000);
H=exp(i*omega)./(exp(i*omega)-0.9);
subplot(2,1,1)
plot(omega,abs(H))
title('Magnitude plot')
xlabel('Omega in radian')

12
Prepared by Dr. Harish M S, AIT

ylabel('Amplitude')
subplot(2,1,2)
plot(omega,angle(H))
title('Phase plot')
xlabel('Omega in radian')
ylabel('Amplitude')

c. Determine the impulse response 𝒉(𝒏).


1 𝑧
The transfer function of the system is 𝐻(𝑧) = =
1−0.9𝑧 −1 𝑧−0.9

Apply the inverse z-transform, we get impulse response ℎ(𝑛) = 0.9𝑚 𝑢(𝑛)
MatLab program for impulse response 𝒉(𝒏)
clc;
close all;
N=input('Enter the length of the impulse respone');
num=input('Enter the coefficient of the numerator');
den=input('Enter the coefficient of the denominator');
y=impz(num,den,N);
n=0:N-1;
stem(n,y)
title('Impulse response')
xlabel('Time index n')
ylabel('Amplitude')

Result and conclusion:

13
Prepared by Dr. Harish M S, AIT

Expt No 5: Computation of N point DFT of a given sequence (without using built-in function)
and to plot the magnitude and phase spectrum.
Introduction to DFT: In sampling theorem we have studied time domain sampling, it involves
conversion of continuous signal in to discrete time signal.
The Discrete Fourier Transform (DFT) is a frequency domain sampling. We know that for the
frequency domain analysis of digital signal, the digital signal 𝑥(𝑛) is converted in to frequency
domain. The frequency domain representation of signal 𝑥(𝑛) is Discrete Time Fourier transform
(DTFT) denoted as 𝑋(𝜔).The DTFT 𝑋(𝜔) is a continuous periodic signal with period 2𝜋
radians, which is function of function of 𝜔 radians. To process the 𝑋(𝜔) in the DSP processor,
we need to take N samples in one period of 𝑋(𝜔). The sampled values of 𝑋(𝜔) are known as
DFT coefficients denoted as 𝑋(𝑘). The 𝑋(𝑘) is a complex quantity, the basic equation to find
the N-point DFT of a sequence 𝑥(𝑛) is given by
𝑋(𝑘) = ∑𝑁−1 𝑛𝑘
𝑛=0 𝑥 (𝑛 )𝑊𝑁 0≤ 𝑘 ≤ 𝑁−1
−𝑗2𝜋𝑛𝑘
𝑊𝑁𝑛𝑘 = 𝑒 𝑁 is known as twiddle factor
Suppose if the digital signal 𝑥(𝑛) consists of L samples, and 𝑋(𝑘) be the N-point DFT of𝑥(𝑛).
If 𝑁 ≥ 𝐿, there is no under sampling and signal x (n) is recovered from the 𝑋(𝑘) by taking IDFT.
If 𝑁 < 𝐿, there is an under sampling and x (n) cannot be reconstructed by taking IDFT.
MatLab program for N-point DFT
clc;
close all;
x=input('Enter the sequence x[n]=');
N=input('Enter the length of DFT=');
%Zero padding
m=length(x);
if N>m
x=[x,zeros(1,N-m)];
end
%Create matrix Q
n=0:N-1;
k=0:N-1;
Q=n'*k;
%twiddle factor wn
wn=exp(-i*20*pi/N);
%create Wk0
Wk=wn.^Q;
%Compute DFT
xk=Wk*x'

14
Prepared by Dr. Harish M S, AIT

%Magnitude & Phase Computation


mag=abs(xk);
ph=angle(xk);
%plotting
subplot(311);
stem(n,x);
title('Sequence x[n]');
subplot(312);
stem(n,mag);
title('Magnitude of X(k)');
subplot(313);
stem(n,ph);
title('Phase of X(k)');

Result and conclusion:

15
Prepared by Dr. Harish M S, AIT

Expt No 6: Using the DFT and IDFT, compute the following for any two given sequences a)
Circular convolution b) Linear convolution.
Theory: Linear convolution: Linear Convolution is a mathematical operation used to express
the relation between input and output of an LTI system. It relates input, output and impulse
response of an LTI system. The important significance of convolution is, if we know the impulse
response h (n) of the system we can find the output y (n) for any arbitrary input signal x (n). The
linear convolution of two discrete- time signals x (n) and h (n is defined as
𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛) = ∑𝑘𝑘=−∞ 𝑥 (𝑘)ℎ(𝑛 − 𝑘) −∞ ≤ 𝑛 ≤ ∞
Linear convolution can be performed using DFT and IDFT method also. Let 𝑥(𝑛 ) be a discrete
time signal of length 𝑁1 . Let ℎ(𝑛 ) be a discrete time signal of length 𝑁2 . If we convolve these
two signals we get 𝑦(𝑛)i.e. 𝑦(𝑛) = 𝑥(𝑛) ∗ ℎ(𝑛). The length of 𝑦(𝑛)is 𝑁 = 𝑁1 + 𝑁2 − 1. Using
N-point DFT and IDFT we can perform the linear convolution.
Circular convolution: Circular convolution is also known as periodic convolution. In circular
convolution samples are shifted circularly, in linear convolution the samples are shifted linearly
left and right. Let 𝑥1 (𝑛) and 𝑥2 (𝑛) are finite duration sequences both of length N with DFT’s
𝑋1 (𝑘) and 𝑋1 (𝑘). Circular convolution of two given sequences 𝑥(𝑛) and ℎ(𝑛) is given by the
equation
𝑦(𝑛) = 𝐼𝐷𝐹𝑇{𝑌 (𝑘)} Where 𝑌(𝑘 ) = 𝑋1 (𝑘)𝑋2 (𝑘)
𝑦(𝑛) = 𝑥(𝑛) ⊛ ℎ(𝑛) = ∑𝑁−1
𝑚=0 𝑥 (𝑚 )ℎ((𝑛 − 𝑚))𝑁 , 0 ≤ 𝑛 ≤ 𝑁 − 1

MatLab program for linear convolution


clc;
close all;
x=input('Enter the samples of x(n)');
h=input('Enter the samples of h(n)');
N1=length(x);
N2=length(h);
N=N1+N2-1;
newx=[x zeros(1,N-N1)];
newh=[h zeros(1,N-N2)];
Xdft=fft(newx);
Hdft=fft(newh);
Ydft=Xdft.*Hdft;
y=ifft(Ydft);
disp('y(n)=x(n)*h(n)');
disp(y);

16
Prepared by Dr. Harish M S, AIT

MatLab program for circular convolution


clc;
close all;
x=input('Enter the samples of x(n)');
h=input('Enter the samples of h(n)');
N1=length(x);
N2=length(h);
N=max(N1,N2);
newx=[x zeros(1,N-N1)]
newh=[h zeros(1,N-N2)]
Xdft=fft(newx);
Hdft=fft(newh);
Ydft=Xdft.*Hdft;
y=ifft(Ydft);
disp('y(n)=x(n)*h(n)');
disp(y);

Result and conclusion:

17
Prepared by Dr. Harish M S, AIT

Expt No 7: Verification of Linearity property, circular time shift property & circular frequency
shift property of DFT.
Theory: In the context of the Discrete Fourier Transform (DFT), there are three important
properties we can verify:
1. Linearity Property
2. Circular Time Shift Property
3. Circular Frequency Shift Property
Let's define each property and then verify them using MATLAB.
1. Linearity Property:
The linearity property of the DFT states:
𝐷𝐹𝑇[𝑥1 (𝑛) + 𝑥2 (𝑛)] = 𝐷𝐹𝑇[𝑥1 (𝑛)] + 𝐷𝐹𝑇[𝑥2 (𝑛)] = 𝑋1 (𝑘) + 𝑋2 (𝑘)
This means the DFT of the sum of two signals is equal to the sum of their individual DFTs.
2. Circular Time Shift Property:
The circular time shift property of the DFT states that if a sequence 𝑥[𝑛] is circularly time-
shifted by m, the DFT of the shifted sequence is:

𝐷𝐹𝑇 [𝑥((𝑛 − 𝑚))𝑁 ] = 𝑋(𝑘)𝑒 −𝑗2𝜋𝑘𝑚/𝑁

Where, 𝑋[𝑘] is the DFT of the original sequence𝑥[𝑛] and ((𝑛 − 𝑚))𝑁 denotes a circular time
shift.
3. Circular Frequency Shift Property
The circular frequency shift property of the DFT states that if the DFT of a sequence is circularly
shifted in frequency by m, the result is:
𝑗2𝜋𝑚𝑛
𝐷𝐹𝑇 [𝑥(𝑛)𝑒 𝑁 ] = 𝑋((𝑘 − 𝑚))𝑁

Where, 𝑋[𝑘] is the DFT of the original sequence𝑥[𝑛] and ((𝑘 − 𝑚))𝑁 denotes a circular
frequency shift.
MATLAB program to verify Linearity property
clc;
close all;
x1=input('Enter the samples of x1(n)');
x2=input('Enter the samples of x2(n)');
N1=length(x1);
N2=length(x2);
N=max(N1,N2);

18
Prepared by Dr. Harish M S, AIT

newx1=[x1 zeros(1,N-N1)];
newx2=[x2 zeros(1,N-N2)];
X1=fft(newx1);
X2=fft(newx2);
x_sum=fft(newx1+newx2);
X1_plus_X2=X1+X2;
if abs(x_sum)==abs(X1_plus_X2)
disp('DFT satiesfies the linearity property')
else
disp('DFT doesnot satiesfies the linearity property')
end
MATLAB program to verify circular time shift property
clc;
close all;
x=input('Enter the samples of x(n)');
m=input('enter the value for sfift');
N=length(x);
X=fft(x);
n=0:N-1;
x_shifted=circshift(x,[0,m])
X_shifted=fft(x_shifted);
expected_time_shift=X.*exp(-1*j*2*pi*m*(0:N-1)/N);
if round(expected_time_shift)==round(X_shifted)
disp('DFT satiesfies the time shifting property')
else
disp('DFT doesnot satiesfies the time shifting property')
end
MATLAB program to verify circular frequency shift property
clc;
close all;
x=input('Enter the samples of x(n)');
m=input('enter the value for sfift');
N=length(x);
n=0:N-1;
X=fft(x);
expected_frequency_sfift=circshift(X,[0,m]);
x_frequency_shift=x.*exp(1*j*2*pi*m*n/N);
X_frequency_shift=fft(x_frequency_shift);
if round(X_frequency_shift)==round(expected_frequency_sfift)
disp('DFT satiesfies the frequency shifting property')
else
disp('DFT doesnot satiesfies the frequency shifting property')
end

Result and conclusion:

19
Prepared by Dr. Harish M S, AIT

Expt No 8: Develop decimation in time radix-2 FFT algorithm without using built-in
functions.
Theory: The Decimation-in-Time (DIT) Radix-2 Fast Fourier Transform (FFT) algorithm
recursively reduces the computation of the Discrete Fourier Transform (DFT) of size N into
smaller FFTs. This process divides the input into even and odd-indexed elements, significantly
speeding up the computation compared to a direct DFT calculation. Here is the MATLAB code
for implementing the Decimation-in-Time Radix-2 FFT algorithms without using any built-in
FFT functions.
MATLAB program to implement decimation in time radix-2 FFT algorithm
clc;
close all
x=[1 2 3 4 5 6 7 8];
N = length(x);
if mod(log2(N), 1)~= 0
error('Input length must be a power of 2');
end
if mod(log2(N), 1) ~= 0
error('Input length must be a power of 2');
end
% Bit-reversal reordering
X = bitrevorder(x);
% Iterative FFT computation
for s = 1:log2(N)
m = 2^s;
wm = exp(-2i * pi / m); % Twiddle factor
for k = 1:m:N
for j = 0:(m/2-1)
t = wm^j * X(k+j+m/2);
u = X(k+j);
X(k+j) = u + t; % FFT[k]
X(k+j+m/2) = u - t; % FFT[k + N/2]
end
end
end
disp('DFT of x(n) using Radix-2 DIT FFT algorithm is');
disp(X)

Result and conclusion:

20
Prepared by Dr. Harish M S, AIT

Expt No 9: Design and implementation of digital low pass FIR filter using a window to meet the
given specifications.
Theory: Filters are used to separate the desired signal from the unwanted disturbance. Two
important classes of digital filters based on their impulse response are
 Finite impulse response (FIR) filter
 Infinite impulse response (IIR) filter

Finite impulse response (FIR) filter: Finite impulse response filter (FIR) are described by
using the difference equation, as
𝑦(𝑛) = 𝑏0 𝑥(𝑛) + 𝑏1 𝑥(𝑛 − 1) + ⋯ . . 𝑏𝑀 𝑥 (𝑛 − 𝑀)
Apply z-transform on both sides we get
𝑌 (𝑧) = 𝑏0 𝑋 (𝑧) + 𝑏1 𝑋(𝑧)𝑧 −1 + ⋯ . . 𝑏𝑀 𝑋 (𝑧)𝑧 −𝑀
M
Y (z)
𝐻 (𝑧 ) = = b0 + b1 z −1 + ⋯ . . bM z −M = ∑ bk z −k
X (z)
k=0

This the general transfer function of FIR filters. FIR filter is also known all zero filter or non
recursive filter and they are inherently stable filter
Characteristics of practical frequency-selective filters
Filters are usually classified according to their frequency domain characteristic as LP, HP, BP, BS
filters. The ideal magnitude response characteristics of these filters are is as shown in below
figure. Theses ideal filters have a constant gain (usually taken as unity gain) in the passband and
zero gain in the stopband. The range of the frequencies where the frequency response takes the
value of one is called passband and the range of frequency where the frequency response is equal
to zero is called stopband of the filter.

21
Prepared by Dr. Harish M S, AIT

These ideal filters having non-casual impulse response and hence these filters are not used for
practical signal processing applications. In order to obtain stable practical, the ideal frequency
response are relaxed by including a transition band between the passband and stopband is as
shown in below figure. And also a small amount of ripple in both passband and stopband are
introduced are known as tolerance.
Let 𝛿1 be the ripple in passband and magnitude varies between 1 ± 𝛿1 in passband. Let 𝛿2 be the
ripple in the stopband. To accommodate a large dynamic range in the graph of frequency
response of the filter, it is a common practice to use logarithmic scale for the magnitude |𝐻(𝜔)|.
Consequently the ripple in the passband is 𝑎𝑝 = 20 log10 𝛿1 decibels and in the stopband 𝑎𝑠 =
20 log10 𝛿2 decibels.
In any filter design problem we specify (1) the maximum passband ripple, (2) the maximum
stopband ripple, (3) the passband edge frequency𝜔𝑝 , and (4) the stopband edge frequency𝜔𝑠 .
Based on these specifications, we can select the filter coefficients {𝑎𝑘 } and {𝑏𝑘 }.

22
Prepared by Dr. Harish M S, AIT

Design of FIR filters using windows


Design of FIR filter using window is a simplest method. This method generally begins with the
desired frequency response specification 𝐻𝑑 (𝜔) and corresponding impulse response ℎ𝑑 (𝑛). For
example, if the desired frequency response of the LPF is characterized by
|𝜔|≤𝜔𝑐
𝑒 −𝑗𝜔𝛼
𝐻𝑑 (𝜔) = { , the magnitude and phase response as shown in below figure.
0 𝜔𝑐 ≤ |𝜔| ≤ 𝜋

The ℎ𝑑 (𝑛) is related to 𝐻𝑑 (𝜔) by Fourier transform relation


1 𝜋
ℎ𝑑 (𝑛 ) = ∫ 𝐻 (𝜔)𝑒 𝑗𝜔𝑛 𝑑𝜔
2𝜋 −𝜋 𝑑

23
Prepared by Dr. Harish M S, AIT

1 𝜔𝑐 −𝑗𝜔𝛼 𝑗𝜔𝑛
= ∫ 𝑒 𝑒 𝑑𝜔
2𝜋 −𝜔𝑐

1 𝜔𝑐 𝑗𝜔(𝑛−𝛼) 1 𝑒 𝑗𝜔(𝑛−𝛼) 𝜔𝑐 𝑠𝑖𝑛𝜔𝑐 (𝑛 − 𝛼)


= ∫ 𝑒 𝑑𝜔 = ( )−𝜔𝑐 =
2𝜋 −𝜔𝑐 2𝜋 𝑗(𝑛 − 𝛼) 𝜋(𝑛 − 𝛼)
𝑠𝑖𝑛𝜔𝑐 (𝑛 − 𝛼)
ℎ 𝑑 (𝑛 ) = if 𝑛 ≠ 𝛼
𝜋(𝑛 − 𝛼)
𝑠𝑖𝑛𝜔𝑐 (𝑛 − 𝛼)
if 𝑛 ≠ 𝛼 −∞≤𝑛≤∞
∴ ℎ𝑑 (𝑛) = { 𝜋(𝑛 − 𝛼)
𝜔𝑐
if 𝑛 = 𝛼
𝜋
The impulse response ℎ𝑑 (𝑛) of the desired filter is non-causal and infinitely long. The finite
duration impulse response ℎ(𝑛) is obtained by multiplying ℎ𝑑 (𝑛) with finite duration window
function 𝑤(𝑛) of length 𝑀 i.e.
ℎ ( 𝑛 ) = ℎ 𝑑 ( 𝑛 ) 𝑤( 𝑛 ) 0≤𝑛 ≤ 𝑀−1
If the window is rectangular window defined as
1 0≤𝑛≤𝑀−1
𝑤 (𝑛 ) = {
0 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Shape of the different windows such as Rectangular, Hanning, Hamming, and Blackman are
shown in below figure.

24
Prepared by Dr. Harish M S, AIT

Depending upon the minimum stopband attenuation, following window are used to design FIR
filter as mentioned in below table.
Table: Transition width and attenuation of different window functions
Name of the Transition Minimum Window function
window width stopband w(n), 0 ≤𝑛 ≤𝑀−1
attenuation
Rectangular 4𝜋 -13 dB w(n) = 1 0≤𝑛≤𝑀−1
∆𝜔 ≥
𝑀
Bartlett 8𝜋 -27 dB 2[𝑛 −
𝑀−1
]
∆𝜔 ≥ 2
𝑀 𝑤(𝑛) = 1 − 0≤𝑛≤𝑀−1
𝑀−1
Hamming 8𝜋 -32dB 2𝜋𝑛
∆𝜔 ≥ w(n) = 0.54 − 0.46cos( ) 0≤𝑛
𝑀 𝑀−1
≤𝑀−1
Hanning 8𝜋 -43dB 1 2𝜋𝑛
∆𝜔 ≥ w(n) = (1 − cos ( ) 0 ≤𝑛 ≤ 𝑀−1
𝑀 2 𝑀−1
Blackman ∆𝜔 -58dB 2𝜋𝑛
𝑤(𝑛) = 0.42 − 0.5 cos ( )
12𝜋 𝑀−1
≥ 4𝜋𝑛
𝑀 + 0.08 cos ( )
𝑀−1
0≤𝑛≤𝑀−1

Design steps
1. Select the suitable window depending upon the minimum stopband attenuation in dB
2. Using the transition width ∆𝑤 of the corresponding window, determine the order of the
2𝜋𝑘
filter M using the formula 𝑀 ≥ .
∆𝑤

3. Specify the ideal frequency response 𝐻𝑑 (𝜔).


4. Determine ℎ𝑑 (𝑛) using 𝐻𝑑 (𝜔).
5. Determine the impulse response of the designed filter h(n) using window function w(n)
i.e. ℎ(𝑛) = ℎ𝑑 (𝑛)w(n).
MATLAB program to design low pass FIR filter using windows
Design an linear phase FIR LPF filter using various windows for the following
specification

25
Prepared by Dr. Harish M S, AIT

 Passband edge frequency 1.3 kHz


 Stopband edge frequency 1.9 kHz
 Minimum stopband attenuation >40dB
 Sampling frequency 10 kHz

MATLAB program to design FIR low pass filter


clc;
close all;
fp=input('enter the passband frequency ');
fs=input('enter the stopband frequency ');
f=input('sampling frequency ');
as=input('stop band attenuation ');
ap=input('pass band attenuation ');
wp=2*pi*fp/f;
ws=2*pi*fs/f;
tw=ws-wp;
if(as>=13)
n=ceil(4*pi/tw);
elseif(13<=as<58)
n=ceil(8*pi/tw);
elseif(as<=58)
n=ceil(12*pi/tw);
end
n1=n+1;
if(rem(n,2)==0)
n=n1;
end
wc=wp+tw/2;
exp=0.001;
alpha=(n-1)/2;
n2=0:1:n-1;
hd=sin(wc*(n2-alpha+exp))./(pi*(n2-alpha+exp));
if(as>13)
wn=boxcar(n);
elseif(27<=as<32)
wn=bartlett(n);
elseif(32<=as<43)
wn=hanning(n);
elseif(43<=as<58)
wn=hamming(n);
elseif(as>58)
wn=blackman(n);
end

26
Prepared by Dr. Harish M S, AIT

hn=hd.*wn';
freqz(hn);
wc
n

Result and conclusion:

27
Prepared by Dr. Harish M S, AIT

Expt No 10: Design and implementation of digital high pass FIR filter using a window to meet
the given specifications.
MATLAB program to design high pass FIR filter using windows
Design a linear phase FIR HPF filter using various windows for the following
specification

 Passband edge frequency 1.9 kHz


 Stopband edge frequency 1.3 kHz
 Minimum stopband attenuation >40dB
 Sampling frequency 10 kHz
MATLAB program to design FIR high pass filter
clc;
clear all;
fs=input('enter the stopband frequency ');
fp=input('enter the passband frequency ');
f=input('enter the sampling frequency ');
as=input('enter the stop band attenuation in dB ');
ap=input('enter the pass band attenuation in dB ');
wp=2*pi*fp/f;
ws=2*pi*fs/f;
tw=wp-ws;
if(as>=13)
n=ceil(4*pi/tw);
elseif(13<=as<58)
n=ceil(8*pi/tw);
elseif(as<=58)
n=ceil(12*pi/tw);
end
n1=n+1;
if(rem(n,2)==0)
n=n1;
end
wc=wp+tw/2;
exp=0.001;
alpha=(n-1)/2;
n2=0:1:n-1;
hd=(sin(pi*(n2-alpha+exp))-sin(wc*(n2-alpha+exp)))./(pi*(n2- alpha+exp));
if(as>13)
wn=boxcar(n);
elseif(27<=as<32)
wn=bartlett(n);
elseif(32<=as<43)

28
Prepared by Dr. Harish M S, AIT

wn=hanning(n);
elseif(43<=as<58)
wn=hamming(n);
elseif(as>58)
wn=blackman(n);
end
hn=hd.*wn';
freqz(hn);
wc
n

Result and conclusion:

29
Prepared by Dr. Harish M S, AIT

Expt No11: Design and implementation of digital IIR Butterworth low pass filter to meet the
given specifications.
Theory: Infinite impulse response filter (IIR) are described by using the difference equation, as
𝑦(𝑛) = 𝑏0 𝑥(𝑛) + 𝑏1 𝑥(𝑛 − 1) + ⋯ . . 𝑏𝑀 𝑥 (𝑛 − 𝑀) − 𝑎1 𝑦(𝑛 − 1) − 𝑎2 𝑦(𝑛 − 2) − ⋯ … − 𝑎𝑁 𝑦(𝑛
− 𝑁)
Apply z-transform on both sides we get
𝑌(𝑧) = 𝑏0 𝑋 (𝑧) + 𝑏1 𝑋(𝑧)𝑧 −1 + ⋯ . . 𝑏𝑀 𝑋 (𝑧)𝑧 −𝑀 − 𝑎1 𝑌(𝑧)𝑧 −1 − 𝑎2 𝑌 (𝑧)𝑧 −2 − ⋯ …
− 𝑎𝑁 𝑌(𝑧)𝑧 −𝑁
Y (z) b0 + b1 z −1 + ⋯ . . bM z −M ∑M
k=0 bk z
−k
𝐻(𝑧) = = =
X(z) 1 + a1 z −1 + a2 z −2 + ⋯ … + aN z −N 1 + ∑N
k=1 ak z
−k

Where 𝑎𝑘 and 𝑏𝑘 are coefficient of the filter and 𝐻(𝑧) is the transfer function of the IIR filter.
We know analog filter design is a well developed field. The analog filters are designed using
Butterworth approximation or Chebyshev approximation. In the first step the required digital
filter specifications are converted into analog lowpass filter specifications. With these filter
specifications design the prototype analog lowpass filter is using Butterworth approximation
with cutoff frequency of 1 radian per second, let the transfer function is denoted as 𝐻𝑝 (𝑠). This
analog prototype filter is converted into practical analog filter is denoted as 𝐻𝑎 (𝑠). Finally,
digital IIR filter function 𝐻(𝑧) can easily obtained from practical filter function 𝐻𝑎 (𝑠 ) using
mapping from s-plane to z-plane. To obtain the IIR digital filter function 𝐻(𝑧 ) from 𝐻𝑎 (𝑠 ) we
were using the following method.
1. Bilinear transformation (BLT)
2. Approximation of derivatives
3. Impulse invariance
4. Matched z-transform
MATLAB program to design low pass IIR filter using Butterworth approximation
Design an IIR LPF filter using Butterworth approximation and bilinear transformation
for the following specification
 Stopband attenuation 4dB
 Passband attenuation 40dB
 Stopband frequency 5000rad/sec
 Passband frequency 2000rad/sec
 Take sampling frequency 10000rad/sec

30
Prepared by Dr. Harish M S, AIT

MATLAB program to design IIR low pass filter


clc;
clear all;
fs=input('Enter sampling frequency in Hz ');
wp=input('Enter passband frequency in rad/sec');
ws=input('Enter stopband frequency in rad/sec');
as=input('Enter stopband attenuation in dB');
ap=input('Enter the passband attenuation in dB');
[N,wc]=buttord(wp/fs,ws/fs,ap,as,'s');
[num,den]=butter(N,wc,'s');
freqs(num,den);
[b,a]=bilinear(num,den,fs);
disp('order n=');N
disp('cutoff frequency wc=');wc
disp ('Numerator coefficients b=');b
disp ('Denominator coefficients a=');a

Result and conclusion:

31
Prepared by Dr. Harish M S, AIT

Expt No 12: Design and implementation of digital IIR Butterworth high pass filter to meet the
given specifications.
MATLAB program to design high pass IIR filter using Butterworth approximation
Design an IIR HPF filter using Butterworth approximation and bilinear transformation
for the following specification
 Stopband attenuation 40dB
 Passband attenuation 4dB
 Stopband frequency 2000rad/sec
 Passband frequency 5000rad/sec
 Take sampling frequency 10000rad/sec

MATLAB program to design IIR hign pass filter


clc;
clear all;
fs=input('Enter sampling frequency in Hz ');
wp=input('Enter passband frequency in rad/sec');
ws=input('Enter stopband frequency in rad/sec');
as=input('Enter stopband attenuation in dB');
ap=input('Enter the passband attenuation in dB');
[N,wc]=buttord(wp,ws,ap,as,'s');
[num,den]=butter(N,wc,'high','s');
freqs(num,den);
[b,a]=bilinear(num,den,fs);
disp('order n=');N
disp('cutoff frequency wc=');wc
disp ('Numerator coefficients b=');b
disp ('Denominator coefficients a=');a

Result and conclusion:

32

You might also like