Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
LABORATORY MANUAL
DIGITAL SIGNAL PROCESSING
B.TECH
5TH SEMESTER
DEPARTMENT OF ELECTRICAL ENGINEERING
JHARKHAND RAI UNIVERSITY
KAMRE, RANCHI
List of Experiments
1. To represent basic signals (unit step, unit impulse, ramp, exponent, Sine and
Cosine)
2. OCTAVE Program to generate sum of sinusoidal signals. 3. To develop a program for discrete convolution. 4. To develop a program for discrete Correlation. 5. To verify Linear Convolution. 6. To verify Circular Convolution. 7. To find the DFT/ IDFT of a sequence without using the inbuilt function.
8. To determine the N-point FFT of a given Sequence.
EXPERIMENT NO. 1
AIM:-TO REPRESENT BASIC SIGNALS(UNIT STEP, UNIT IMPULSE, RAMP,
EXPONENT, SINE AND COSINE)
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
THEORY:-
The command most often used for plotting is plot, which creates linear plots of vectors and
matrices; plot(t,y) plots the vector t on the x-axis versus vector y on the y-axis. For discrete-
time signals, use the command stem which plots each point with a small open circle and
a straight line. To plot y[k] versus k, type stem(k,y). To plot two or more graphs on the
same set of axes, use the command plot (t1,y1,t2,y2),which plots y1 versus t1 and y2
versus t2. To plot more than one graph on the screen, use the command subplot(mnp)
which partitions the screen into an mxn grid where p determines the position of the
particular graph counting the upper left corner as p=1. The auto scaling of the axes can
be done by using the axis command after the plotting command:
axis([xminxmaxyminymax]);
where xmin, xmax, ymin, and ymax are numbers corresponding to the limits you desire for
the axes. To return to the automatic scaling, simply type axis. There are options on the line
type and the color of the plot which are obtained using plot (t,y,'option'). The linetype
options are '-' solid line (default), '--' dashed line, '-.' dot dash line, ':' dotted line. The points
in y can be left unconnected and delineated by a variety of symbols: + .* o x.
PROGRAM:-
** for unit step, unit impulse, ramp and exponent
clc;
clear all;
close all;
n=[0:0.5:20];
x=input('enter value of x');
y=exp(x*n);
subplot(2,2,1);
stem(n,y);
xlabel('x');
ylabel('exponent');
title('expo1');
%clc;
%clear all;
%close all;
n=[0:0.5:20];
a=input('enter value of a');
y=(a).^n;
subplot(2,2,2);
stem(n,y);
xlabel('a');
ylabel('exponent');
title('expo2');
%clc;
%clear all;
%close all; n=[0:0.5:20];
subplot(2,2,3);
stem(n,n);
xlabel('x');
ylabel('y');
title('unit ramp');
%clc;
%clear all;
%close all;
t=[-3:1:2];
n1=[zeros(1,3),ones(1,1),zeros(1,2)];
subplot(2,2,4);
stem(t,n1);
xlabel('x');
ylabel('y');
title('unit impulse');
**for sine and cosine
clc;
clear all;
close all;
t=[0:0.1:pi];
y=cos(2*pi*t);
subplot(2,2,1);
stem(t,y); xlabel('t');
ylabel('coz');
title('cos(2pit)');
%clc;
%clear all;
%close all;
t=[0:0.1:pi];
y=sin(2*pi*t);
subplot(2,2,2);
stem(t,y);
xlabel('t');
ylabel('sin');
title('sin(2pit)');
OUTPUT:-
RESULT:- BASIC SIGNALS(UNIT STEP, UNIT IMPULSE, RAMP, EXPONENT, SINE
AND COSINE) HAS BEEN REPRESENTED USING OCTAVE.
EXPERIMENT NO. 2
Aim: To generate sum of sinusoidal signals.
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
Theory:
Sinusoidal sequence:
X (n) = cos (w0n +θ ),
Where θ is the phase in radians. A OCTAVE function cos (or sin) is used to generate sinusoidal
sequences. Signal addition: This is a sample-by-sample addition given by
{X1 (n)} + {x2(n)} = {x1(n) + x2(n)}
It is implemented in OCTAVE by the arithmetic operator ‘’+’’. However, the lengths of x1 (n) and x2 (n)
must be the same. If sequences are of unequal lengths, or if the sample positions are different for equal length
sequences, then we cannot directly use the operator + . We have to first augment x1 (n) and x2 (n) so that they
have the same position vector n (and hence the same length). This requires careful attention to OCTAVE’s
indexing operations. In particular, logical operation of intersection ‘’&’’ relational operations like ‘’<=’’ and
‘’==’’ and the find function are required to make x1(n) and x2 (n) of equal length.
Program:
%Generation of sum of sinusoidal sequences
n=-4:0.5:4;
y1=sin (n)
subplot (2, 2, 1)
stem (n, y1,'filled')
grid on;
xlabel ('-->Samples');
ylabel ('--->Magnitude');
title ('SINUSOIDAL SIGNAL1');
y2=sin (2*n)
subplot (2, 2, 2)
stem (n, y2,'filled')
grid on;
xlabel ('-->Samples');
ylabel ('--->Magnitude');
title ('SINUSOIDAL SIGNAL2');
y3=y1+y2
subplot (2, 1, 2);
stem (n, y3,'filled');
xlabel ('-->Samples');
ylabel ('--->Magnitude'); title ('SUM OF SINUSOIDAL SIGNALS');
Output:
Result: Sum of sinusoidal signals is generated.
EXPERIMENT NO. 3
AIM:-TO DEVELOP A PROGRAM FOR DISCRETE CONVOLUTION.
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
THEORY:
In mathematics and, in particular, functional analysis, convolution is a mathematical operation on
two functions f and g, producing a third function that is typically viewed as a modified version of one
of the original functions, giving the area overlap between the two functions as a function of the
amount that one of the original functions is translated. It has applications that include probability,
statistics, computer vision, image and signal processing, electrical engineering, and
differential equations. the circular convolution can be defined for periodic functions (that is,
functions on the circle), and the discrete convolution can be defined for functions on the set
of integers. The convolution of f and g is written f∗g, using an asterisk or star. It is defined as
the integral of the product of the two functions after one is reversed and shifted.
PROGRAM:-
clc,
clear all; close all; x=[1 2 3 5]; h=[2 3 5 6]; y=conv(x,h); subplot(3,1,1); stem(x); xlabel('x');
ylabel('amplitude'); title('x sequence'); subplot(3,1,2); stem(h); xlabel('h');
ylabel('amplitude'); title('h sequence'); subplot(3,1,3); stem(y); xlabel('y'); ylabel('amplitude'); title('y sequence');
OUTPUT:-
RESULT:- A PROGRAM FOR DISCRETE CONVOLUTION HAS BEEN DEVELOPED IN OCTAVE.
EXPERIMENT NO. 4
AIM:-TO DEVELOP A PROGRAM FOR DISCRETE CORRELATION
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
THEORY:-
Correlation quantifies the strength of a linear relationship between two variables. When there is no
correlation between two variables, then there is no tendency for the values of the variables to increase
or decrease in tandem. Two variables that are uncorrelated are not necessarily independent, however,
because they might have a nonlinear relationship. It has applications that computes and plots the
sample autocorrelation function (ACF) of a univariate, stochastic time series . The autocorrelation
function of a random signal describes the general dependence of the values of the samples at one
time on the values of the samples at another time. The cross correlation function however
measures the dependence of the values of one signal on another signal. r=corr2(A,B) computes the
correlation coefficient between A and B, where A and B are matrices or vectors of the same size.
PROGRAM:-
clc,
clear all; close all; x=[1 2 3 5]; h=[2 3 5 6]; y=xcorr(x,h); subplot(3,1,1); stem(x); xlabel('x');
ylabel('amplitude'); title('x sequence'); subplot(3,1,2); stem(h); xlabel('h'); ylabel('amplitude'); title('h sequence'); subplot(3,1,3); stem(y);
xlabel('y'); ylabel('amplitude'); title('y sequence');
OUTPUT:-
RESULT:- A PROGRAM FOR DISCRETE CORRELATION HAS BEEN DEVELOPED.
EXPERIMENT NO. 5
Aim: To compute the linear convolution of two discrete sequences.
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
Theory: Consider two finite duration sequences x (n) and h (n), the duration of x
(n) is n1 samples in the interval 0 ≤ n ≤ (n1 − 1) . The duration of h (n) is n2 samples; that is h (n) is non – zero only in the interval 0 ≤ n ≤ (n2 − 1) . The linear or a periodic convolution of x (n) and h (n) yields the sequence y (n) defined as,
Clearly, y (n) is a finite duration sequence of duration (n1+n2 -1) samples. The
convolution sum of two sequences can be found by using following steps
Step1: Choose an initial value of n, the starting time for evaluating the output sequence y (n). If x (n) starts
at n=n1 and h (n) starts at n= n2 then n = n1+ n2-1 is a good choice.
Step2: Express both sequences in terms of the index m.
Step3: Fold h (m) about m=0 to obtain h (-m) and shift by n to the right if n is positive and left if n is negative to obtain h (n-m).
Step4: Multiply two sequences x (n-m) and h (m) element by element and sum the products to get y (n).
Step5: Increment the index n, shift the sequence x (n-m) to right by one sample and repeat step4.
Step6: Repeat step5 until the sum of products is zero for all remaining values of n.
Program:
%Linear convolution of two sequences
a=input('enter the input sequence1=');
b=input('enter the input sequence2=');
n1=length (a)
n2=length (b)
x=0:1:n1-1;
subplot (2, 2, 1), stem(x, a);
title ('INPUT SEQUENCE1');
xlabel ('---->n');
ylabel ('---->a (n)');
y=0:1:n2-1;
subplot (2, 2, 2), stem(y, b);
title ('INPUT SEQUENCE2');
xlabel ('---->n');
ylabel('---->b(n)');
c=conv (a, b)
n3=0:1:n1+n2-2;
subplot (2, 1, 2), stem (n3, c);
title ('CONVOLUTION OF TWO SEQUENCES');
xlabel ('---->n');
ylabel ('---->c (n)');
Output:
Result: Linear convolution of two discrete sequences is computed.
EXPERIMENT NO. 6
Aim: To compute the circular convolution of two discrete sequences.
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
Theory: Two real N-periodic sequences x(n) and h(n) and their circular or periodic convolution sequence
y(n) is also an N- periodic and given by,
Circular convolution is some thing different from ordinary linear convolution operation, as this involves the
index ((n-m))N, which stands for a ‘modula-N’ operation. Basically both type of convolution involve same six
steps. But the difference between the two types of convolution is that in circular convolution the folding and
shifting (rotating) operations are performed in a circular fassion by computing the index of one of the
sequences with ‘modulo-N’ operation. Either one of the two sequences may be folded and rotated without
changing the result of circular convolution. That is,
If x (n) contain L no of samples and h (n) has M no of samples and that L > M, then perform circular
convolution between the two using N=max (L,M), by adding (L-M) no of zero samples to the sequence h (n),
so that both sequences are periodic with number. Two sequences x (n) and h (n), the circular convolution of these two sequences can be found by using the following steps.
1. Graph N samples of h (n) as equally spaced points around an outer circle in counterclockwise direction.
2. Start at the same point as h (n) graph N samples of x (n) as equally spaced points around an inner circle in clock wise direction.
3. Multiply corresponding samples on the two circles and sum the products to produce output.
4. Rotate the inner circle one sample at a time in counter clock wise direction and go to step 3 to obtain the next value of output.
5. Repeat step No.4 until the inner circle first sample lines up with first sample of the exterior circle once again.
Program:
%circular convolution
a=input('enter the input sequence1=');
b=input('enter the input sequence2=');
n1=length (a)
n2=length (b)
n3=n1-n2
N=max (n1, n2);
if (n3>0)
b= [b, zeros (1, n1-n2)]
n2=length (b);
else
a= [a, zeros (1, N-n1)]
n1=length (a);
end;
k=max (n1, n2); a=a';
b=b';
c=b;
for i=1: k-1
b=circshift (b, 1);
c=[c, b];
end;
disp(c);
z=c*a;
disp ('z=')
disp (z)
subplot (2, 2, 1);
stem (a,'filled');
title ('INPUT SEQUENCE1');
xlabel ('---->n');
ylabel ('---->Amplitude')
subplot (2, 2, 2);
stem (b,'filled');
title ('INPUT SEQUENCE2');
xlabel ('---->n');
ylabel ('---->Amplitude');
subplot (2, 1, 2);
stem (z,'filled');
title ('CONVOLUTION OF TWO SEQUENCES');
xlabel ('---->n');
ylabel ('---->Amplitude');
Output:
z=
7
5
7
9
Result: Circular convolution of two discrete sequences is computed.
EXPERIMENT NO. 7
AIM: To find the DFT/IDFT of a sequence without using the inbuilt functions
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
PROGRAM:
%program to find the DFT/IDFT of a sequence without using the inbuilt functions clc close all; clear all; xn=input('Enter the sequence x(n)'); %Get the sequence from user ln=length(xn); %find the length of the sequence xk=zeros(1,ln); %initilise an array of same size as that of input sequence
ixk=zeros(1,ln); %initilise an array of same size as that of input sequence %code block to find the DFT of the sequence %-----------------------------------------------------------
for k=0:ln-1 for n=0:ln-1
xk(k+1)=xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/ln)); end
end %------------------------------------------------------------ %code block to plot the input sequence %------------------------------------------------------------ t=0:ln-1; subplot(221); stem(t,xn); grid ylabel ('Amplitude'); xlabel ('Time Index'); title('Input Sequence'); %--------------------------------------------------------------- magnitude=abs(xk); % Find the magnitudes of individual DFT points %code block to plot the magnitude response %------------------------------------------------------------ t=0:ln-1; subplot(222); stem(t,magnitude);
grid ylabel ('Amplitude'); xlabel ('K'); title ('Magnitude Response'); %------------------------------------------------------------
phase=angle(xk); % Find the phases of individual DFT
points %code block to plot the magnitude sequence %%------------------------------------------------------------
t =0: ln -1; subplot(223); stem(t,phase); grid ylabel ('Phase'); xlabel ('K'); title('Phase Response');
% Code block to find the IDFT of the sequence
for n=0:ln-1 for k=0:ln-1 ixk(n+1)=ixk(n+1)+(xk(k+1)*exp(i*2*pi*k*n/ln));
end end
ixk=ixk./ln; %----------------------------------------------------------- - %code block to plot the input sequence
%------------------------------------------------------------
t=0:ln-1 ; subplot(224); stem(t,xn); grid; ylabel ('Amplitude'); xlabel ('Time Index'); title ('IDFT sequence');
Expected Output Waveform:
Result: The DFT/IDFT of a sequence has been found without using the inbuilt functions.
EXPERIMENT NO. 8
Aim: To determine the N-point FFT of a given sequence.
EQUIPMENT REQUIRED:
P – IV Computer
Windows
OCTAVE Software
Theory: The Fast Fourier transform is a highly efficient procedure for computing the DFT of a finite series
and requires less no of computations than that of direct evaluation of DFT. It reduces the computations by
taking the advantage of the fact that the calculation of the coefficients of the DFT can be carried out
iteratively. Due to this, FFT computation technique is used in digital spectral analysis, filter simulation,
autocorrelation and pattern recognition. The FFT is based on decomposition and breaking the transform into
smaller transforms and combining them to get the total transform. FFT reduces the computation time required
to compute a discrete Fourier transform and improves the performance by a factor 100 or more over direct
evaluation of the DFT. The fast fourier transform algorithms exploit the two basic
multiplications required to perform DFT from N2
to N/2 log2N. In other words, for N=1024, this implies
about 5000 instead of 106
multiplications – a reduction factor of 200. FFT algorithms are based on the
fundamental principal of decomposing the computation of discrete fourier transform of a sequence of length
N into successively smaller discrete fourier transform of a sequence of length N into successively smaller
discrete fourier transforms. There are basically two classes of FFT algorithms. They are decimation-in-time
and decimation-in-frequency. In decimation-in-time, the sequence for which we need the DFT is successively
divided into smaller sequences and the DFTs of these subsequences are combined in a certain pattern to
obtain the required DFT of the entire sequence. In the decimation-in-frequency approach, the frequency
samples of the DFT are decomposed into smaller and smaller subsequences in a similar manner.
Radix – 2 DIT – FFT algorithm
1. The number of input samples N=2M
, where, M is an integer.
2. The input sequence is shuffled through bit-reversal.
3. The number of stages in the flow graph is given by M=log2 N.
4. Each stage consists of N/2 butterflies.
5. Inputs/outputs for each butterfly are separated by 2m-1
samples, where m represents the stage index, i.e., for first stage m=1 and for second stage m=2 so on.
6. The no of complex multiplications is given by N/2 log2 N.
7. The no of complex additions is given by N log2 N.
8. The twiddle factor exponents are a function of the stage index m and is given by
9. The no of sets or sections of butterflies in each stage is given by the formula 2M-m
.
10. The exponent repeat factor (ERF), which is the number of times
the exponent sequence associated with m is repeated is given by 2M-m.
Radix – 2 DIF – FFT Algorithm
1. The number of input samples N=2M
, where M is number of stages.
2. The input sequence is in natural order.
3. The number of stages in the flow graph is given by M=log2 N.
4. Each stage consists of N/2 butterflies.
5. Inputs/outputs for each butterfly are separated by 2M-m
samples, Where m represents the stage index i.e., for first stage m=1 and for second stage m=2 so on.
6. The number of complex multiplications is given by N/2 log2 N.
7. The number of complex additions is given by N log2 N.
8. The twiddle factor exponents are a function of the stage index m and is given by
9. The number of sets or sections of butterflies in each stage is given by the formula 2m-1.
10. The exponent repeat factor (ERF), which is the number of times the exponent sequence associated with
m repeated is given by 2m-1
.
For decimation-in-time (DIT), the input is bit-reversed while the output is in natural order. Whereas,
for decimation-in-frequency the input is in natural order while the output is bit reversed order.
The DFT butterfly is slightly different from the DIT wherein DIF the complex multiplication takes place
after the add-subtract operation.
Both algorithms require N log2 N operations to compute the DFT. Both algorithms can be done in-place
and both need to perform bit reversal at some place during the computation.
Program:
% N-point FFT algorithm
N=input ('enter N value=');
Xn=input ('type input sequence=');
k=0:1: N-1;
L=length (Xn)
if (N<L)
error ('N MUST BE>=L');
end;
x1= [Xn zeros(1, N-L)]
for c=0:1:N-1;
for n=0:1:N-1;
p=exp (-i*2*pi*n*c/N);
x2(c+1, n+1) =p;
end;
Xk=x1*x2';
end;
MagXk=abs (Xk);
angXk=angle (Xk);
subplot (2, 1, 1);
stem (k, MagXk);
title ('MAGNITUDE OF DFT SAMPLES');
xlabel ('---->k');
ylabel ('-->Amplitude');
subplot (2, 1, 2);
stem (k, angXk);
title ('PHASE OF DFT SAMPLES');
xlabel ('---->k');
ylabel ('-->Phase');
disp (‘abs (Xk) =');
disp (MagXk)
disp ('angle=');
disp (angXk)
Output:
Result: N-point FFT of a given sequence is determined.