DSP Notes
DSP Notes
OBJECTIVE: To verify sampling theorem for Nyquist rate, Under sampling and Over
sampling condition in both time and frequency domain using MATLAB.
THEORY:
Sampling: Is the process of converting a continuous time signal into a discrete time signal.
It is the first step in conversion from analog signal to digital signal.
Nyquist Rate Sampling: The Nyquist rate is the minimum sampling rate required to avoid
aliasing, equal to the highest modulating frequency contained within the signal. In other
words, Nyquist rate is equal to two sided bandwidth of the signal (Upper and lower
sidebands) i.e., fs = 2W.To avoid aliasing, the sampling rate must exceed the Nyquist rate.
i.e.fs>fN, where fN =2W.
ALGORITHM:
1. Select the frequency of analog signal f Hz
2. To generate sine wave of f Hz define a closely spaced time vector
3. Generate the sinusoid and plot the signal
4. Select the sampling frequency fs= 10f samples/sec. Generate a suitable time scale for
this sampling signal
5. Sample the analog signal at the instant specified by n.
6. Modify the time vector n used for discrete simulation
7. Reconstruct the analog signal from its discrete samples
8. Compare the analog and reconstructed signal
9. Repeat the values experiment for different values of f and verify reconstructed and
analog signal
Program:
clc;
clear all;
%define analog signal for comparison t=-100:01:100;
fm=0.02;
x=cos(2*pi*t*fm);
subplot(2,2,1);
plot(t,x);
xlabel('time in sec'); ylabel('x(t)');
title('continuous time signal');
%simulate condition for undersamplingi.e.,fs1<2*fm
fs1=0.02;
n=-2:2;
x1=cos(2*pi*fm*n/fs1);
subplot(2,2,2);
stem(n,x1); hold on
subplot(2,2,2);
plot(n,x1,':');
title('discrete time signal x(n) with fs<2fm');
xlabel('n');
ylabel('x(n)');
%condition for Nyquist plot
fs2=0.04;
n1=-4:4;
x2=cos(2*pi*fm*n1/fs2);
subplot(2,2,3);
stem(n1,x2);
hold on
subplot(2,2,3);
plot(n1,x2,':');
title('discrete time signal x(n) with fs>2fm');
xlabel('n');
ylabel('x(n)');
%condition for oversampling n2=-
50:50;
fs3=0.5;
x3=cos(2*pi*fm*n2/fs3);
subplot(2,2,4);
stem(n2,x3);
hold on
subplot(2,2,4);
plot(n2,x3,':');
xlabel('n');
ylabel('x(n)');
title('discrete time signal x(n) with fs=2fm');
Results:
Discussions on results
Page 4
AIM: To implement linear convolution of two given sequences
OBJECTIVE:
1. To find linear convolution of right sided sequence using inbuilt MATLAB function
“CONV” and its theoretical method to verify the result
2. To find linear convolution of both sided sequence using inbuilt MATLAB function
“CONV” and its theoretical method to verify the result
3. To find linear convolution of a given sequences using circular convolution.
Mathematical Formula:
The linear convolution of two continuous time signals x(t) and h(t) is defined by
Where x(n) is the input signal and h(n) is the impulse response of the system.
ALGORITHM:
1. Read the input sequence, x[n] and plot.
2. Read the impulse response of the system, h[n] and plot
3. Convolve the two sequences using conv command and plot the results.
Page 5
PROGRAM:LINEAR CONVOLUTION OF RIGHT SIDED SEQUENCES
clc; % clear screen
clear all; % clear workspace
close all; % close all figure windows
x1 = input('enter the first sequence x1(n) = '); % define first sequence
x2 = input('enter the second sequence x2(n) = '); % define second sequence
y = conv(x1,x2); % convolute first and second
sequences disp('Linear convolution of x1 and x2 is = ');
disp(y); % display the output
%graphical display
subplot(2,2,1); % divide the display screen in four sections and choose the first one to
display stem(x1); % plot the first sequence
xlabel('n'); % label x axis
ylabel('x1(n)'); % label y axis
title('plot of x1(n)'); % graph title
subplot(2,2,2);% divide the display screen in four sections and choose the first second
to display
stem(x2); % plot the second sequence
xlabel('n'); % label x axis
ylabel('x2(n)'); % label y axis
title('plot of x2'); % graph title
subplot(2,1,2);% divide the display screen in four sections and choose the first second
to display
stem(y); % plot the output sequence
xlabel('n'); % label x axis
ylabel('y(n)'); % label y axis
title('convolution output'); % graph title
OUTPUT:
Page 6
Linear convolution of x1 and x2 is = 5 35 100 200 200
Page 7
xlabel('n'); % label x axis
ylabel('y(n)'); % label y axis
title('convolution output'); % graph title
OUTPUT:
enter the first sequence x1(n) = [1 2 3 2 1 3 4]
enter the second sequence x2(n) = [2 -3 4 -1 0 1]
Linear convolution of x1 and x2 is =
2 1 4 2 6 9 3 2 15 -3 3 4
Page 8
end
disp('Linear Convolution using Circular Convolution = ');
disp(y); % display
output
subplot(2,2,1); %Graphical display of first input
sequence stem(xn);
xlabel('n');
ylabel('x(n)');
title('Plot of x1(n)');
subplot(2,2,2); %Graphical display of second input
sequence stem(hn);
xlabel('n');
ylabel('(h(n)');
title('Plot of x2(n)');
subplot(2,2,3); %Graphical display of output sequence stem(y);
xlabel('n');
ylabel('y(n)');
title('Linear Convolution Output');
OUTPUT:
OUTCOME:Linear convolution of the sequences is found and the results are verified in
MATLAB.
Page 9
EXPERIMENT NO- 3 :- CIRCULAR CONVOLUTION
AIM: Circular convolution of two given sequences
THEORY:Let x1(n) and x2(n) are finite duration sequences both of length N with
DFT’s X1(k) and X2(k). Convolution of two given sequences x1(n) and x2(n) is given
by the equation,
x3(n) = IDFT[X3(k)]where X3(k) = X1(k) X2(k)
ALGORITHM:
1. Read the first input sequence, x[n] and plot.
2. Read the second input sequence, h[n] and plot
3. Find the length of x[n] and y[n] , l1 and l2 respectively
4. Check if l1=l2. Proceed only if equal.
8. Find the sum of products of x[n] and cyclically folded and shifted h[n] to get circular
convoluted output.
9. Display and plot the output.
10
N = max(l1,l2); % Define the length of the output
xn = [xn, zeros(1,N-l1)]; % zero padding is done to make l1=l2.
hn = [hn, zeros(1,N-l2)]; % zero padding is done to make l1=l2.
for n=0:N-1; % loop to calculate circular
convolution y(n+1) = 0;
for k=0:N-1
i = mod((n-k),N);
y(n+1) =y(n+1)+hn(k+1)*xn(i+1);
end ;
end;
disp('Circular convolution in Time Domain = ');
disp(y); % display the output
subplot(2,2,1); % graphical plot the first input
sequence stem(xn);
xlabel('n');
ylabel('x(n)');
title('Plot of x(n)');
OUTPUT:
11
Circular convolution in Time Domain =
13 14 11 12
OUTCOME: Circular convolution of the sequences are found and the results are verified in
MATLAB
AIM: To compute n-point DFT of a given sequence and to plot magnitude and phase
spectrum.
OBJECTIVE:
1. To find the N point DFT of a given sequence using the MATLAB inbuilt function
“FFT” and to find Magnitude and Phase of DFT sequence using functions “ABS and
ANGLE”
2. To verify the result theoretically.
, k = 0,1, …. N-1
For k = 1,
X(0) = x(0) + x(1) + x(2) +
-jπ -j2π
-j3π
X(2) = x(0) + x(1) e + x(2) e + x(3)
e
X(2) = 1 + cos π – jsin π
For k = 3, X(2) = 1-1 = 0
13
-j3π/2 -j3π -j9π/2
= x(0) + x(1) e + x(2) e + x(3) e
X(3) = 1 + cos(3π/2) -
jsin(3π/2) X(3) = 1 + j
The DFT of the given
sequence is, X(k) = { 2, 1-j,
0, 1+j }
To find Magnitude of X(k):
Magnitude= (a +b )
Where a and b are real and imaginary parts
respectively To fine Phase of X (k):
-1
Phase=tan (b/a)
ALGORITHM:
OUTPUT:
enter the N point = 4
enter the input sequence x(n) =
[0 1 2 3] N point DFT of x(n) is
=
6.0000 -2.0000 + 2.0000i -2.0000 -2.0000 - 2.0000i
xlabel('n');
ylabel('|X(K)|');
title('Magnitude
spectrum');
subplot(2,2,3); % graphical plot of angle of
output signal stem(k,angle(Xk)*180/pi);
xlabel('n');
ylabel('<X(K)'
); title('Phase
spectrum');
%function file
function [Xk]
= dft(xn,N) n
= 0:1:N-1;
k = 0:1:N-1;
WN = exp(-
j*2*pi/N); nk
= n'*k;
WNnk =
WN.^nk;
Xk =
xn*WNnk;
OUTPUT:
enter N point = 4
enter the input sequence x(n) =
[1 4 2 7] N point DFT of x(n) =
14.0000 -1.0000 + 3.0000i -8.0000 - 0.0000i -1.0000 - 3.0000i
OUTCOME: DFT of the given sequence is found and the results are verified
using MATLAB.
It may be noted that the number of complex multiply and add operations required
by
the simple forms both the DFT and IDFT is of order N2. This is because there are N
data points to calculate, each of which requires N complex arithmetic operations. In
computer science jargon, we say they have algorithmic complexity O(N2). This is not
good news. If we can't do any better than this then the DFT will not be very useful
for the majority of practical DSP applications. Fortunately, there are a number of
different 'Fast Fourier Transform' (FFT) algorithms that enable us to do very much
better than this.
The DFT takes N2 operations for N points. Since at any stage the computation
required to combine smaller DFTs into larger DFTs is proportional to N, and there
are
log2(N) stages (for radix 2), the total computation is proportional to N log2(N).
Therefore, the ratio between a DFT computation and an FFT computation for the
same N is proportional
to N / log2(N). In cases where N is small this ratio is not very significant, but when
N becomes large, this ratio gets very large. (Every time you double N, the numerator
doubles, but the denominator only increases by 1.
In FFT, N point DFT is decomposed into two N/2 pt DFT's, N/2 pt DFT is
decomposed into N/4 pt DFT's and so on... Decomposition reduces calculations this
process continues till further decomposition is not possible. In radix-2 last level of
decomposition is when the length of signal becomes 2 pt. For minimum calculations
there must be maximum levels of decompositions. In Radix-2 algorithms, we get
maximum levels of decompositions and therefore a radix-2 algorithm requires less
calculation. Radix-2 algorithms are fast algorithms.
EXPT NO: 6
TO FIND THE FFT OF A GIVEN SEQUENCE
Algorithm:
PROGRAM 1
MATLAB code for N-Point DIF FFT algorithm
clc;
clear all;
close all;
X=input('Enter the sequence : ');
N=input('Enter the Point : ');
n=length(X);
x=[X zeros(1,N-n)];
M=log2(N);
for m=1:M
d=2^(M-m+1);
for l=1:d:(N-d+1)
for k=0:(d/2)-1
w=exp(-1i*2*pi*k/d);
z1=x(l+k);
z2=x(l+k+d/2);
x(l+k)=z1+z2;
x(l+k+d/2)=(z1-z2)*w;
end
end
end
y=bitrevorder(x);
disp(y)
subplot(3,1,1)
stem(abs(X));
title('Input Sequence');
subplot(3,1,2)
stem(abs(y));
title('Magnitude Response');
subplot(3,1,3)
stem(angle(y));
title('Phase Response');
PROGRAM 2
MATLAB code for N-Point DIT FFT algorithm
clc;
clear all;
close all;
x=input('Enter the sequence : ');
N=input('Enter the Point : ');
n=length(x);
x=[x zeros(1,N-n)];
y=bitrevorder(x);
M=log2(N);
for m=1:M
d=2^m;
for l=1:d:N-d+1
for k=0:(d/2)-1
w=exp(-1i*2*pi*k/d);
z1=y(l+k);
z2=y(l+k+d/2);
y(l+k)=z1+w*z2;
y(l+k+d/2)=z1-w*z2;
end
end
end
disp(y);
subplot(3,1,1)
stem(abs(x));
title('Input Sequence');
subplot(3,1,2)
stem(abs(y));
title('Magnitude Response');
subplot(3,1,3)
stem(angle(y));
title('Phase Response');
Output:
Output Waveform:
RESULT:
EXP.NO: 6
IMPLEMENTATION OF DECIMATION PROCESS
Algorithm:
Flow Chart:
PROGRAM:
Clc;
Clear
all;
Close
all;
D=input('enter the downsampling factor');
L=input('enter the length of the input signal');
f1=input('enter the frequency of first sinusodal');
f2=input('enter the frequency of second
sinusodal'); n=0:L-1;
x=sin(2*pi*f1*n)+sin(2*pi*f2*
n); y=decimate(x,D,'fir');
subplot(2,1,1);
stem(n,x(1:L));
title('input
sequence');
xlabel('time(n)');
ylabel('amplitude');
subplot(2,1,2)
m=0:(L/D)-1;
stem(m,y(1:L/D));
title('Decimated sequence');
xlabel('time(n)');
ylabel('amplitude');
INPUT:
enter the downsampling factor = 5
enter the length of the input signal = 100
enter the frequency of first sinusoidal = 0.01
enter the frequency of second sinusoidal = 0.03
Output Waveforms:
RESULT:
EXP. NO: IMPLEMENTATION OF
INTERPOLATION PROCESS
Algorithm:
PROGRAM:
Clc;
Clear
all;
Close
all;
L=input('enter the upsampling factor');
N=input('enter the length of the input signal'); % Length should be greater than
8 f1=input('enter the frequency of first sinusodal');
f2=input('enter the frequency of second
sinusodal'); n=0:N-1;
x=sin(2*pi*f1*n)+sin(2*pi*f2*
n); y=interp(x,L);
subplot(2,1,1)
stem(n,x(1:N))
title('input
sequence');
xlabel('time(n)');
ylabel('amplitude');
subplot(2,1,2)
m=0:N*L-1;
stem(m,y(1:N*L))
title('output sequence
'); xlabel('time(n)');
ylabel('amplitude');
INPUT:
enter the upsampling factor = 5 enter
the length of the input signal = 9
enter the frequency of first sinusoidal = 0.1
enter the frequency of second sinusoidal = 0.3
Output Waveforms:
RESULT:
VIVA QUESTIONS:
Exercise: