DSP Manual
DSP Manual
BOOK
For Academic Session Fall 2018
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
2|Page
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
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
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
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);
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
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.
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
Objective:
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
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
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
11 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
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
∆=0.5
13 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
∆=0.1
14 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
∆=0.05
15 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
∆=0.005
16 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
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.
Objective:
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.
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
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.
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.
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
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.
Objective:
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
25 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
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
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
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);
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.
Objective:
30 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
To
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=−∞
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
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
y(n) -3 -5 -5 -4 8 10 41 41 43 32 20 39 12 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
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
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
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=−∞
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
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
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=−∞
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
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
40 | P a g e
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology
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.
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