0% found this document useful (0 votes)
221 views43 pages

DSP Manual

This document provides instructions for a laboratory session on digital signal processing. It discusses: 1) Verifying properties of discrete cosine signals using MATLAB, including periodicity, aliasing, and highest rate of oscillation. 2) Implementing an analog to digital converter in MATLAB Simulink. This involves sampling an analog signal to create a discrete-time signal, and quantizing the signal so it can be represented with a finite number of bits. 3) Sampling must be at least twice the highest frequency component per the sampling theorem. Quantization introduces error as the continuous valued signal is mapped to discrete values.

Uploaded by

Maryam Muneeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
221 views43 pages

DSP Manual

This document provides instructions for a laboratory session on digital signal processing. It discusses: 1) Verifying properties of discrete cosine signals using MATLAB, including periodicity, aliasing, and highest rate of oscillation. 2) Implementing an analog to digital converter in MATLAB Simulink. This involves sampling an analog signal to create a discrete-time signal, and quantizing the signal so it can be represented with a finite number of bits. 3) Sampling must be at least twice the highest frequency component per the sampling theorem. Quantization introduces error as the continuous valued signal is mapped to discrete values.

Uploaded by

Maryam Muneeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

PRACTICAL WORK

BOOK
For Academic Session Fall 2018

Digital Signal Processing (EE-394)


For
TE Electrical

Name: SAMEEHA ARIF


Roll Number: EE-219 Year: TE
Section: F
Batch: 2017-18

Department of Electrical Engineering


N.E.D. University of Engineering & Technology, Karachi
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 01

Objective:
Verify the properties of discrete cosine using MATLAB

Discrete Cosine:
A discrete-time cosine signal is a version of analog cosine signal which is sampled at a given rate
which converts the analogue time to discrete time period. It is expressed as
x(n) = A cos(ωn + θ) (1.0)
Where;
n is an integer variable, called the sample number, ranges from -∞ to ∞,
A is the amplitude of the sinusoid,
ω is the frequency in radians per sample, and
θ is the phase shift in radians.
If we replace ω = 2πƒ, equation (1.0) becomes:
x(n) = A cos(2πƒn + θ) (1.1)
here, ƒ is frequency in cycles per sample.
The discrete-time cosines are characterized by following properties:
1. It is periodic only if its frequency ƒ is a rational number.
A discrete time signal is periodic only if
x(n+N) = x(n) for all n
Where N is the smallest value for which this equation is true. This is called the fundamental
period.
From above equation, we can conclude that:
cos[2πƒ0(N+n) + θ] = cos(2πƒ0n + θ)
This above relation is only true if
2πƒ0N = 2kπ
Or, equivalently
K
ƒ0 = (1.2)
N

1|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Let us test the discrete cosines while changing the fundamental frequency.

Plotting Command:
n = 0:1:20;
y1=cos(2*pi*(1/sqrt(5))*n);
subplot(211)
stem(n,y1);
y2=cos(2*pi*(1/5)*n);
subplot(212)
stem(n,y2);
Code 01: Plotting periodic and aperiodic discrete signal

Plot 1(a) : Aperiodic Discrete Cosine

Plot1(b): Periodic Discrete Cosine


n = 0:1:20;
y1=cos(2*pi*(5/9.2)*n);
subplot(211)
stem(n,y1);
y2=cos(2*pi*(5/9)*n);
subplot(212)
stem(n,y2);
Code 02: Plotting periodic and aperiodic discrete signal

2|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 2(a) : Aperiodic Discrete Cosine

Plot 2(b) : Aperiodic Discrete Cosine

It can be seen from the above output(s) that if the fundamental frequency, ƒ0, is a rational
number, it gives a periodic signal, whereas, if there is even a slightest of change in the frequency,
ƒ0, which makes the frequency irrational, then the signal no more, remains periodic.
2. Discrete Cosines whose frequencies are separated by an integer multiple of 2π
are identical (alias of fundamental signal).
Let us consider the signal cos(ωon + θ). According to the statement above,
cos[(ωo+2π)n + θ)] = cos(ωon + θ) (1.3)

Mathematical Proof:
cos[(ωo+2π)n + θ)] = cos[(ωo+2π)n]cosθ - sin[(ωo+2π)n]sinθ …. (a)
Cosθ[cos(ωon)cos(2πn) – sin(ωon)sin2πn]- sinθ[sin(ωon)cos(2πn) + cos(ωon)sin2πn] …. (b)

3|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

‫؞‬cos(2πn) = 1 and sin(2πn) = 0


Cosθ[cos(ωon).1- sin(ωon).0] – sinθ [sin(ωon).1 + cos(ωon).0] .… (c)
Cosθcos(ωon) - sinθsin(ωon) = cos(ωon + θ) …. (d)

Plotting Command:
n = 0:1:20;
y1=cos(2*pi*(5/9)*n);
subplot(211)
stem(n,y1);
y2=cos(2*pi*(5/9+1)*n);
subplot(212)
stem(n,y2);
Code 03: Plotting discrete signal and its alias

Plot 3(a) : Discrete Cosine with fundamental frequency

Plot3(b): Alias of fundamental discrete cosine

4|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

n = 0:1:20;
y1=cos(2*pi*(3/10)*n);
subplot(211)
stem(n,y1);
y2=cos(2*pi*(3/10 + 1)*n);
subplot(212)
stem(n,y2);

Code 04: Plotting discrete signal and its alias

Plot 4(a) : Discrete Cosine with fundamental frequency

Plot 4(b) : Aperiodic Discrete Cosine

We can see from above results that if we add 2π in fundamental period, we will get the same
signal, as seen by the mathematical and graphical proof. The signal whose frequency is separated
by an integer multiple of 2π is called as alias of the signal with its corresponding fundamental
frequency.
5|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

3. The highest rate of oscillation in a discrete time cosine is attained when |ƒ0|
=1/2.
To illustrate this property, we take fundamental frequency from a range of 0 to 1/2.

Plotting Command:
n = 0:1:20;
y1=cos(2*pi*(0)*n);
subplot(221)
stem(n,y1);
y2=cos(2*pi*(1/8)*n);
subplot(222)
stem(n,y2);
y3=cos(2*pi*(1/4)*n);
subplot(223)
stem(n,y3);
y4=cos(2*pi*(1/2)*n);
subplot(224)
stem(n,y4);
Code 05: Highest rate of oscillation in discrete cosine

Plot 5 : highest rate of oscillation when f=1/2

n = 0:1:20;

6|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

y1=cos(2*pi*(1)*n);
subplot(221)
stem(n,y1);
y2=cos(2*pi*(7/8)*n);
subplot(222)
stem(n,y2);
y3=cos(2*pi*(3/4)*n);
subplot(223)
stem(n,y3);
y4=cos(2*pi*(3/2)*n);
subplot(224)
stem(n,y4);
Code 6: Frequencies greater than 1/2.

Plot 6: discrete cosines with frequency greater than 1/2.

We can see from the above results that as the fundamental frequency increases from 0 to ½, the
rate of oscillations also increases and is max when ƒ0 is 1/2. If we increase frequency from 1/2, it
will form an alias of its corresponding fundamental frequency. Alias of any signal depends upon
the sampling frequency of the signal.

7|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 02

Objective:

Using MATLAB Simulink, implement analog to digital converter.

Analog to Digital Converter:

To process analog signals by digital means, it is first necessary to convert them


into digital form, that is, convert them into a sequence of numbers having finite
precision. This procedure is called Analog to Digital Conversion, and the
corresponding devices are called analog to digital converters .

Analog Signal Digital Signal

There are two most basic operations which are needed to convert analog signal to
digital:

1. Sampling:

8|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

This is the conversion1 of a continuous-time signal into discrete-time signal


obtained by taking samples of continuous-time signal at discrete-time instants.
Thus, if x a (t) is the input to the sampler, the output is x a (nT) = x(n), where T is
the sampling interval.

While sampling any analog signal with different frequencies, the sampling
frequency should be at least twice the highest analog frequency component. This
theorem is known as sampling theorem.

F s ≥ 2 Fmax

2. Quantization:

Since, we have limited number of memory locations in our system’s memory and each memory
has finite amount of space thus we need to quantize our continuous valued signal in order to
process it from a digital system. Quantization is the process which converts discrete-time
continuous-valued signal into a discrete-time discrete-valued signal. The difference between the
un-quantized sample x(n) and the quantized sample xq(n) is called the quantization error.

e q(n)=x q (n) – x( n)

Quantization Error

9|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

The values allowed in digital signal are called the quantization levels, whereas
the distance ∆ between two successive quantization levels is called the
quantization step size or resolution. We can conclude from the above graph, that
the error can never be greater than half of resolution.

−∆ ∆
≤ eq ( n ) ≤
2 2

Now, we will convert an analog signal to digital signal in Simulink.

The above circuit is used to convert an analog sine wave to digital sine wave. The signal has
been first sampled at a time period, T, and then the discrete time period signal is quantized and
we get a digital signal at output. The components used in this circuit is described below:
1. Signal Input (Sine Wave):
The Sine Wave block outputs an analog sinusoidal waveform. The block can operate in time-
based or sample-based mode. We will operate it in time-based type and will change the
frequency to 2π(10). We simulate model for a time period of 0.1.

10 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 1: Sine Wave Input


2. Discrete-time Signal (Zero-hold Signal):
Convert an input signal with a continuous sample time to an output signal with a discrete sample
time. The Zero-Order Hold block holds its input for the sample period you specify. The sampling
period for the above model is 0.02s. The block takes the sample of Sine wave after every 0.02s
and holds it until the next sample is taken.

11 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 2: Sampled Signal

3. Quantizer:
The Quantizer block discretizes the input signal using a quantization algorithm. The block uses a
round-to-nearest method to map signal values to quantized values at the output that are defined
by the Quantization interval. A smooth input signal can take on a stair-step shape after
quantization.
This equation mathematically describes the round-to-nearest method:
y=q∗round(u /q)
where y is the quantized output, u is the input, and q is the Quantization interval.
Once the signal is being sampled, quantizer rounds of the analog value to discrete values making
the signal digital.
4. Sum Block:
It is used to find the difference between the quantized and un-quantized signal. This difference is
known as error as discussed above.
Now, we check if reducing the quantization step size has any effect on the error. The following
outputs represents the quantized signal and the error, respectively.

12 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

∆=1

Plot 3(a): Quantized Signal

Plot 3(b): Error Signal

∆=0.5

13 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 4(a): Quantized Signal

Plot 4(b): Error Signal

∆=0.1

14 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 5(a): Quantized Signal

Plot 5(b): Error Signal

∆=0.05

15 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 6(a): Quantized Signal

Plot 6(b): Error Signal

∆=0.005

16 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plot 7(a): Quantized Signal

Plot 7(b): Error Signal

We can conclude from the above outputs that if we decrease the quantization step size, the error
also decreases, since, now the value will be rounding off nearest to the value which was obtained
in the sampled signal. The error reduced from 0.5 to 0.002 as the quantization level reduced from
1 to 0.005. Also, we can see that none of the error exceeds half of its corresponding resolution.

Laboratory Session No. 03


17 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Objective:

Quantify features of dynamic signals

Dynamic Signals:

If a system depends upon the past and future value of the signal at any instant of the time then it
is known as dynamic system. Unlike static systems, these are not memory less systems. They
store past and future values. Therefore, they require some memory.

 All time shifting cases signals are dynamic signals.


 In case of time scaling too, all signals are dynamic signals.
 Integration cases signals are dynamic signals.
We take a signal as speech to see the features of dynamic signal.

Plot 7(b): Simulink Model

From Multimedia File:

The From Multimedia File block reads audio samples, video frames, or both from a multimedia
file. The sample rate that the block uses depends on the audio and video sample rate.

18 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

File name
Specify the name of the multimedia file from which to read. The block determines the
type of file (audio and video, audio only, or video only) and provides the associated
parameters. It reads a built in speech if the user doesn’t choose any file manually.
Samples per audio channel
Specify number of samples per audio channel. This parameter becomes available for files
containing audio. We take 1 sample so that our audio remains original.
To Workspace:

The To Workspace block writes input signal data to a workspace. During simulation, the block
writes data to an internal buffer. When you pause the simulation or the simulation completes,
that data is written to the workspace. Data is not available until the simulation pauses or stops.

19 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Variable Name:

Specify the name of the variable for the saved data. The data will be saved in the
workspace with the name of ‘Speech’, as specified.

Decimation:

Specify the decimation factor, n, which writes data at every nth time that the block
executes. The default value causes the block to write data at every time step.

Scope displays the output wave from the multimedia source file.

20 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plotting Command:
Figure
plot(Speech);
l=length(Speech)
count=0;
X=[];
eng=[];
for a=1:2450
x=Speech(1+count:45+count);
count=45*a;
X=[X x];
eng= [eng x'*x];
end
figure
plot(eng);
Code 01: Energy Levels of Small Samples

Plot 01 : Output of Speech Signal


The Speech signal is divided in 2450 portions and each portion contains 45 number of samples.
The energy signal is made up of those 2450 portions.

Energy Signal:

21 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Any signal whose life is finite, is known as Energy Signal. In other words, signals whose energy
is finite and can be measured are classified as energy signals while signals whose energy is
infinite are classified as power signals.

We will quantify our signal to measure its characteristics. Energy of discrete time signal can be
represented as:

2
E= ∑ [x ( n ) ]
n=−∞

Since, energy signal is dynamic i.e. it is not constant throughout, so, it is better if we treat it in
dynamic value. We will divide the ‘Speech’ signal in small samples and will evaluate energy of
each sample. Such type of processing is known as Short time processing.

Plot 02 : Energy Signal


We can see from the above Energy Signal that the highest amount of Energy is achieved when
the data of Speech Signal had its samples close to each other and with highest amplitude. This
arrangement can be seen when n ranges from 21000 to 30000. And when we sampled this part,
the energy obtained is highest. While, the energy level decreases as the strength of the Speech
signal decreases with an increase in number of samples ‘n’.

22 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Zero Crossing:

A zero-crossing is a point where the sign of a mathematical function changes (e.g. from positive
to negative), represented by a intercept of the axis (zero value) in the graph of the function.
Counting zero-crossings is also a method used in speech processing to estimate the fundamental
frequency of speech.

Let us check zero crossing in a simple Sine wave:

Plotting Command:
t=0:0.0001:0.1;
x=sin(2*pi*50*t);
plot(t,x);
zero_count=0;
for a=1:length(x)-1
if ((x(a)*x(a+1)) <0)
zero_count=zero_count+1;
end
end
zero_count
Code 02: Zero Crossing of Sine Wave

Plot 03 : Output of Sine Wave


>> lab1

zero_count =

10
Output of Code 02
Let us test zero crossing on the speech signal:

23 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plotting Command:

zero_count=0;
for a=1:length(Speech)-1
if ((Speech(a)*Speech(a+1)) <0)
zero_count=zero_count+1;
end
end
zero_count
Code 03: Zero Crossing of Speech Signal
>> lab2

zero_count =

8652
Output of Code 03

The above code iterates for every number of samples in the speech signal. If the two consecutive
numbers crosses zero, a count is increased. We multiply the two consecutive numbers and if the
multiple is negative (this happens when one of the numbers is positive and other is negative,
which means that x axis line has been crossed), the zero-crossing is counted.

Laboratory Session No. 04

Objective:

To convert analog systems into discrete system representation

Theory:
Consider a series RL circuit excited by a voltage source x ( t ) as shown below,

24 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Applying KVL we get,


di ( t )
L + Ri ( t )=x ( t ) (1)
dx
For a source a source free RL circuit the above expression will become,
di ( t )
L + Ri ( t )=0 (2)
dx
By using first principle, the derivate of inductor current in a discrete time domain can be
evaluated as,
f ( x )−f ( a )
f ' ( a )=lim (3)
x→ a x−a
or,
di [ ( n−1 ) T ] i ( nT )−i [ ( n−1 ) T ]
=lim
dt T→0 nT −( n−1 ) T
(4)
From eq. (2), the derivative at the instant ( n−1 ) T can be evaluated as,
di [ ( n−1 ) T ] −Ri [ ( n−1 ) T ]
=
dt L
(5)
Equating eq. (4) and (5) we get,
i ( nT )−i [ ( n−1 ) T ] −Ri [ ( n−1 ) T ]
lim =
T→0 nT −( n−1 ) T L
(6)
By choosing sampling T close to zero we can evaluate the difference equation i ( n ) as,
R
(
i ( n )= 1−T
L )
∗i ( n−1 ) (7)

25 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Where, nT and ( n−1 ) T are replaced by n and ( n−1 ) respectively.


Derivation

Consider a series RL circuit excited by a voltage source 1 V as shown below,

Applying KVL we get,


di(t )
L + Ri ( t )=1
dx
; di
t=(n−1)=lim i(n)−i ¿ ¿ ¿
dt T→0

i ( n )−i(n−1)
L + Ri ( n ) =1
T
i (n) i ( n−1 )
L +L + Ri ( n ) =1
T T
i ( n−1 ) L L
i ( n )=1+ L
T { }
/ R+
T
{a=
T
}

1+ a∗i ( n−1 )
i ( n )=
R+ a

This is an expression for inductor current i(n), in discrete time domain.


Now, let us test our circuit at different sampling time period for following values of R, L and
input voltage.
R=20Ω

L=2 H

i init =0 A

x ( t )=discretestepinput

26 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

n=anysuitablerange

Simulink Model

Figure 1 : RL Circuit Model


Simulink Graph

Figure 2 : Current Graph on Scope

27 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Plotting Command:

R=20;
L=2;
T=0.1;
i=0;
a=L/T;
t=0:0.02:0.98;
for n=2:50;
i(n) = ((1+a*i(n-1))/(R+a));
i;
end
stem (t ,i);
Code 01: Time period = 0.1

Output of Code 01

Plotting Command:

R=20;

28 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

L=2;
T=0.05;
i=0;
a=L/T;
t=0:0.02:0.98;
for n=2:50;
i(n) = ((1+a*i(n-1))/(R+a));
i;
end
stem (t ,i);

Code 02: Time period = 0.05

Output of Code 02

Plotting Command:

R=20;
L=2;
T=0.025;

29 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

i=0;
a=L/T;
t=0:0.02:0.98;
for n=2:50;
i(n) = ((1+a*i(n-1))/(R+a));
i;
end
stem (t ,i);
Code 03: Time period = 0.025

Output of Code 03
It can be clearly seen from the above outputs that when we decrease the sampling time period,
the graph is almost similar to current graph obtained from the Simulink model with minimal
error. Hence, we conclude that decreasing sampling time period to a suitable range can help us
approximate values accurately.

Laboratory Session No. 05

Objective:

30 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

To

a) Implement convolution sum in MATLAB


b) Implement and highlight the significance of correlation and
correlation-sequence

CONVOLUTION SUM:
Convolution can be used to determine the output a system produces for a given input signal. It
can be shown that a linear time invariant system is completely characterized by its impulse
response. The sifting property of the discrete time impulse function tells us that the input signal
to a system can be represented as a sum of scaled and shifted unit impulses. Thus, by linearity, it
would seem reasonable to compute of the output signal as the sum of scaled and shifted unit
impulse responses. That is exactly what the operation of convolution accomplishes. Hence,
convolution can be used to determine a linear time invariant system's output from knowledge of
the input and the impulse response.
Any Discrete-time input x(n) can be expressed as a sum of scaled unit impulses.

x ( n )= ∑ x ( k ) δ(n−k )
k=−∞

By linearity and time invariance, the output of the system is the scaled sum of outputs due to
each unit impulse

y ( n )=h [ n ]∗x [ n ]= ∑ x ( k ) h(n−k )
k=−∞

This is called the discrete-time convolution sum.

Now, let us see an example of convolution sum applied to discrete time input and impulse
function.

Given
h(n) = {-3,1,2, 4,1,0, 4,0,3}
x(n) = {1 2 3 5 4 6}
In order to find y(n) we will perform convolution,

y ( n )= ∑ x ( k ) h(n−k )
k=−∞

31 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Performing Convolution using Convolution Table:

Consider the following table:

n -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9

x(0)* h(k) -3 1 2 4 1 0 4 0 3

x(1)* h(k-1) -6 2 4 8 2 0 8 0 6

x(2)* h(k-2) -9 3 6 12 3 0 12 0 9

x(3)* h(k-3) -15 5 10 20 5 0 20 0 15

x(4)* h(k-4) -12 4 8 16 4 0 16 0 12

x(5)* h(k-5) -18 6 12 24 6 0 24 0 18

y(n) -3 -5 -5 -4 8 10 41 41 43 32 20 39 12 18

After convolution the output response of system will be;


y(n) = {-3 -5 -5 -4 8 10 41 41 44 28 29 36 15 18}

MATLAB Code:
clear all;
close all;
clc;
h = [-3 1 2 4 1 0 4 0 3]; % impulse response
org_h = 5; % Sample number where origin exists
nh = [0 : length(h)-1]- org_h + 1;
x = [1 2 3 5 4 6]; % input sequence
org_x = 1; % Sample number where origin exists
nx = [0 : length(x)-1]- org_x + 1;
y = conv(h,x);
ny = [nh(1)+ nx(1) : nh(end)+nx(end)];
figure,
subplot(3,1,1),
stem(nh,h);

32 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

xlabel('Time index n'); ylabel('Amplitude');


xlim([nh(1)-1 nh(end)+1]);
title('Impulse Response h(n)'); grid;
subplot(3,1,2),
stem(nx,x);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Input Signal x(n)'); grid;
subplot(3,1,3)
stem(ny,y);
xlabel('Time index n'); ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Output Obtained by Convolution'); grid;
print(gcf, 'myfile.png', '-dpng', '-r300');
Code 01: For convolution of input signal & impulse response
Output Obtained by Convolution
45

40

35

30

25
Amplitude

20

15

10

-5
-5 0 5 10
Time index n
Figure 1(a): Stem figure of Convolution signal
Stem Output Graph:

33 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Impulse Response h(n)


4

2
Amplitude

-2

-4
-5 -4 -3 -2 -1 0 1 2 3 4 5
Time index n
Input Signal x(n)
6
Amplitude

0
-1 0 1 2 3 4 5 6
Time index n
Output Obtained by Convolution
60

40
Amplitude

20

-20
-5 0 5 10
Time index n

Figure 1(b): For convolution of input signal & impulse response

CORRELATION:
A mathematical tool used to find the degree of similarity between two signals (or patterns) as a
function of time shift (or lag) between them. A signal operation similar to signal convolution, but
with completely different physical meaning, is signal correlation. The signal correlation
operation can be performed either with one signal (autocorrelation) or between two different
signals (cross-correlation).
Auto Correlation:
If the sequence is correlated with itself, then its autocorrelation. Sometimes, autocorrelation is
used to find if the sequence is repeating itself. Peak autocorrelation occurs when the sequence
completes its one period.

rxx ( l )= ∑ x ( n ) x (n−l)
n=−∞

Given
x(n)= {1 2 3 4 5}
In order to find y(n) we will perform auto-correlation:
34 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology


rxx ( l )= ∑ x ( n ) x (n−l)
n=−∞

Performing Auto-Correlation using Correlation Table:

Consider the following table:

L n -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 rxx

- x(n) 1 2 3 4 5 -

-4 y(n+4) 1 2 3 4 5 5

-3 y(n+3) 1 2 3 4 5 14

-2 y(n+2) 1 2 3 4 5 26

-1 y(n+1) 1 2 3 4 5 40

0 y(n) 1 2 3 4 5 55

1 y(n-1) 1 2 3 4 5 40

2 y(n-2) 1 2 3 4 5 26

3 y(n-3) 1 2 3 4 5 14

4 y(n-4) 1 2 3 4 5 5

After correlation the output response of system will be;


y(n) = {5 14 26 40 55 40 26 14 5}
The above results show that the x(n) is maximum similar to x(n) without having any delay, since,
the maximum amplitude lies at 0.

MATLAB Code:

35 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

clear all;
close all;
clc;
x = [1 2 3 4 5]; % impulse response
org_x = 3; % Sample number where origin exists
nx = [0 : length(x)-1]- org_x + 1;
y = [1 2 3 4 5]; % input sequence
org_y = 3; % Sample number where origin exists
ny = [0 : length(y)-1]- org_y + 1;
r = xcorr(x,y);
nr = [nx(1)+ ny(1) : nx(end)+ny(end)];
figure,
subplot(3,1,1),
stem(nx,x);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Input Signal x(n)'); grid;
subplot(3,1,2),
stem(ny,y);
xlabel('Time index n'); ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Input Signal y(n)'); grid;
subplot(3,1,3)
stem(nr,r);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nr(1)-1 nr(end)+1]);
title('Output Obtained by Correlation'); grid;
figure,
stem(nr,r);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nr(1)-1 nr(end)+1]);
title('Output Obtained by Correlation'); grid;
Code 02: For Auto Correlation of a signal

36 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Figure 2(a): Stem figure of Correlation Sequence


Stem Output Graph:

Figure 2(b): For Auto Correlation of a Signal


Cross Correlation:
If two different sequences are correlated, then its cross correlation.

37 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology


rxy ( l )= ∑ x ( n ) y (n−l)
n=−∞

The above formula calculates the similarity of signal y(n) w.r.t x(n). For this, x(n) remains fixed
while y(n) is shifted.

ryx ( l )= ∑ y ( n ) x (n−l)
n=−∞

Likewise, the above formula calculates the similarity of signal x(n) w.r.t y(n). For this, y(n)
remains fixed while x(n) is shifted.
Given
x(n) = {1 2 3 4 5}
y(n) = {5 4 3 2 1}
In order to find y(n) we will perform cross-correlation:

rxy ( l )= ∑ x ( n ) y (n−l)
n=−∞

Performing Cross-Correlation using Correlation Table:

Consider the following table:

l n -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 rxx

- x(n) 1 2 3 4 5 -

-4 y(n+4) 5 4 3 2 1 1

-3 y(n+3) 5 4 3 2 1 4

-2 y(n+2) 5 4 3 2 1 10

-1 y(n+1) 5 4 3 2 1 20

0 y(n) 5 4 3 2 1 35

1 y(n-1) 5 4 3 2 1 44

38 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

2 y(n-2) 5 4 3 2 1 46

3 y(n-3) 5 4 3 2 1 40

4 y(n-4) 5 4 3 2 1 25

After correlation the output response of system will be;


y(n) = {1 4 10 20 35 44 46 40 25}
The above results show that the x(n) is maximum similar to x(n) without having any delay, since,
the maximum amplitude lies at 0.

MATLAB Code:
clear all;
close all;
clc;
x = [1 2 3 4 5]; % impulse response
org_x = 3; % Sample number where origin exists
nx = [0 : length(x)-1]- org_x + 1;
y = [5 4 3 2 1]; % input sequence
org_y = 3; % Sample number where origin exists
ny = [0 : length(y)-1]- org_y + 1;
r = xcorr(x,y);
nr = [nx(1)+ ny(1) : nx(end)+ny(end)];
figure,
subplot(3,1,1),
stem(nx,x);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nx(1)-1 nx(end)+1]);
title('Input Signal x(n)'); grid;
subplot(3,1,2),
stem(ny,y);
xlabel('Time index n'); ylabel('Amplitude');
xlim([ny(1)-1 ny(end)+1]);
title('Input Signal y(n)'); grid;
subplot(3,1,3)
stem(nr,r);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nr(1)-1 nr(end)+1]);
title('Output Obtained by Correlation'); grid;
figure,

39 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

stem(nr,r);
xlabel('Time index n'); ylabel('Amplitude');
xlim([nr(1)-1 nr(end)+1]);
title('Output Obtained by Correlation'); grid;
Code 03: For correlation of two different signals

Figure 3(a): Stem figure of Cross Correlation Sequence

Stem Output Graph:

40 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Figure 3(b): For Cross Correlation of two different Signals

Since, the maximum degree occurs at l = 2 which shows that x(n) will be maximum similar to 2
unit delayed version of y(n).
x(n) ≈ y(n-2)
Just as convolution, correlation uses two signals to produce a third. The third signal is called the
correlation of two input signals. The amplitude of each sample in the correlation signal is a
measure of how much the received signal resembles the target signal, at that location. In other
words, the value of correlation is maximized when the target signal is aligned with the same
features in the received signal.
Correlation is the optimal technique for detecting a known waveform in random noise. That is,
the peak is higher above the noise using correlation that can be produced by any other linear
system.

Laboratory Session No. 07

41 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Objective:

To verify Fourier series analysis equation. Extend the idea of Fourier series
analysis equation for aperiodic signals.

42 | P a g e

You might also like