DSP LAB
DSP LAB
1.1 Objective:
To verify the sampling theorem using octave software.
1.2 Software Required:
Octave software
1.3 Pre-Requisite:
Sampling theorem.
1.4 Introduction:
• Sampling is a process of converting a continuous time signal (analog signal)
x(t) into a discrete time signal x[n], which is represented as a sequence of numbers. (A/D
converter)
• Converting back x[n] into analog (resulting in reconstruction. (D/A converter)
• Some techniques for reconstruction-
o ZOH ( zero order hold) interpolation results in a staircase waveform, is implemented
by OCTAVE plotting function stairs(n,x),
o FOH (first order hold) where the adjacent samples are joined by straight lines is
implemented by OCTAVE plotting function plot(n,x),
• For x(t ) to be exactly the same as x(t), sampling theorem in the generation of x(n) from x(t)
is used. The sampling frequency fs determine the spacing between samples.
• Aliasing-A high frequency signal is converted to a lower frequency, results due to under
sampling. Though it is undesirable in ADCs, it finds practical applications in stroboscope and
sampling oscilloscopes.
1.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
1.6 Program:
tfinal=0.05;
t=0:0.00005:tfinal;
fd=input('Enter analog frequency');
%define analog signal for comparison
xt=cos(2*pi*fd*t);
%simulate condition for under sampling i.e., fs1<2*fd
fs1=1.3*fd;
%define the time vector
n1=0:1/fs1:tfinal;
%Generate the under sampled signal
xn=cos(2*pi*n1*fd);
%plot the analog & sampled signals
subplot(3,1,1);
plot(t,xt,'b',n1,xn,'r*-');
title('undersampling plot');
%condition for Nyquist plot
fs2=2*fd;
n2=0:1/fs2:tfinal;
xn=cos(2*pi*fd*n2);
subplot(3,1,2);
plot(t,xt,'b',n2,xn,'r*-');
title('Nyquist plot');
%condition for oversampling
fs3=5*fd;
n3=0:1/fs3:tfinal;
xn=cos(2*pi*fd*n3);
subplot(3,1,3);
plot(t,xt,'b',n3,xn,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('amplitude');
legend('analog','discrete');
1.7 Results :
Enter analog frequency 200
1.8 Discussions:
1. From the under-sampling plot observe the aliasing effect. The analog signal is of 200Hz
(T=0.005s). The reconstructed (from under sampled plot) is of a lower frequency. The alias
frequency is computed as
fd - fs1 = 200-1.3*200 = 200-260= -60Hz
This is verified from the plot. The minus sign results in a 180˚ phase shift.
(For example: 3kHz & 6kHz sampled at 5kHz result in aliases of -2kHz (3k-5k) & 1kHz
(6k-5k) respectively)
2. Sampling at the Nyquist rate results in samples sin(πn) which are identically zero, i.e., we are
sampling at the zero crossing points and hence the signal component is completely missed. This
can be avoided by adding a small phase shift to the sinusoid. The above problem is not seen
in cosine waveforms (except cos(90n)). A simple remedy is to sample the analog signal at a
rate higher than the Nyquist rate.
3. The over sampled plot shows a reconstructed signal almost similar to that of the analog signal.
5. What is aliasing?
2.1 Objective:
To verify the properties of DFT.
2.4 Introduction:
a) Linear convolution
• The output y[n] of a LTI (linear time invariant) system can be obtained by
convolving the input x[n] with the system’s impulse response h[n].
the sequences is infinite (say, h[n] = (0.9)n u[n] ), we can analytically evaluate the
• The conv function assumes that the two sequences begin at n=0 and is invoked by
y=conv(x,h).
• Even if one of the sequences begin at other values of n, say n=-3,or n=2; then we need
Where the index <n-k>N implies circular shifting operation and <-k>N implies folding the
sequence circularly
• Steps for circular convolution are the same as the usual convolution, except all index
calculations are done "mod N" = "on the wheel".
Plot f [m] and h [−m] as shown in Fig. 2.2. (use f(m) instead of x(k))
o Multiply the two sequences
o Add to get y[m]
o "Spin" h[−m] n times Anti Clock Wise (counter-clockwise) to get h[n-m].
• x[n] and h[n] can be both finite or infinite duration sequences. If infinite sequences,
they should be periodic, and the N is chosen to be at least equal to theperiod. If they are
finite sequences N is chosen as >= to max(xlength, hlength). Whereas in linear
convolution N>= xlength+hlength-1.
2.5 Procedure:
1. Turn on PC.
2. Open the octave software already installed in a PC.
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
2.6 Program :
subplot (2,1,1);
stem(y);
xlabel('time index n');
ylabel('amplitude ');
title('convolution output');
subplot(2,2,3);
stem(x1);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x1');
subplot(2,2,4);
stem(x2);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x2');
b). Circular convolution of two given sequences
x=[1 2 3 4];
h=[1 2 3 4];
N=length(x);
%Compute the output
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0 i=i+N;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end
disp('circular convolution of x & h is y=');
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
clc;
close all;
clear all;
x1=input('enter the first sequence=');
x2=input('enter the second sequence=');
n=input('enter the no of points of the dft=');
subplot(3,1,1);
stem(x1,'filled');
title('plot of first sequence');
subplot(3,1,2)
stem(x2,'filled');
title('plot of second sequence');
n1=length(x1);
n2=length(x2);
m=n1+n2-1;
x=[x1 zeros(1,n2-1)];
y=[x2 zeros(1,n1-1)];
x_fft=fft(x, m);
y_fft=fft(y, m);
dft_xy=x_fft.*y_fft
y=ifft(dft_xy, m);
disp('the linear convolution result is');
disp(y);
subplot(3,1,3);
stem(y,'filled');
title('plot of linearly convoluted sequence');
if (N3>=0)
h=[h,zeros(1,N3)];
else
x=[x,zeros(1,N3)];
end
%compute the output
for r=1:N
y(r)=0;
for (s=1:N)
k=r-s+1;
%calculation of x index
if (k<=0) k=k+N;
end
%end of ‘if’
y(r)=y(r)+x(s)*h(k);
end %end of inner ‘for loop’
end %end of outer ‘for loop’
disp ('circular convolution of x & h is y=');
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
title ('Circular convolution output y(n)')
2.7 Results :
a) Linear convolution of two given sequences:
Columns 1 through 4:
Columns 5 through 7:
1 3 4 5
1 -4 6 3
2.8 Discussion:
3.1 Objective:
To verify Auto and cross correlation of two given sequences and their properties using octave
software.
related and in a precise quantitative way how much they are related. A measure of similarity
between a pair of energy signals x[n] and y[n] is given by the cross
• The parameter ‘l’ called ‘lag’ indicates the time shift between the pair. Autocorrelation sequence
of x[n] is given by
• At zero lag, i.e., at l=0, the sample value of the autocorrelation sequence has its maximum
And compile A time shift of a signal does not change its autocorrelation sequence. For
example, let y[n]=x[n-k]; then ryy[l] = rxx[l] i.e., the autocorrelation of x[n] and y[n] are the
same regardless of the value of the time shift k. This can be verified with a sin e
and cosinesequences of same amplitude and frequency will have identical autocorrelation
functions.
• For power signal the autocorrelation sequence is given by
Cross correlation :
• Cross Correlation has been introduced in the last experiment. Comparing the
equations for the linear convolution and cross correlation we find that
• The ordering of the subscripts xy specifies that x[n] is the reference sequence that
remains fixed in time, whereas the sequence y[n] is shifted w.r.t x[n]. If y[n] is the reference
sequence then
3.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
7. Open the octave software already installed in a PC
8. In octave open the new script file.
9. Save the file as any name with .m extension.
10. Type the program and save it. the program using run option or F5.
11. After compilation give the input and observe the output, Graph.
3.6 Program:
Auto correlation:
n = -5:5;
N=10;
%Generate the square sequence
x = ones(1,11);
%Compute the correlation sequence
r = conv(x,fliplr(x));
disp('autocorrelation sequence r=');
disp(r);
%plot the sequences
subplot(2,1,1);
stem(n,x);
title('square sequence');
subplot(2,1,2);
k = -N:N;
stem(k, r);
title('autocorrelation output');
xlabel('Lag index');
ylabel('Amplitude');
disp('auto-correlation r=');
disp(r);
% to calculate the Energy of the input signal
e=sum(x.^2);
disp('Maximum value of energy =');
disp(e);
disp('Maximum value of energy is at the zero lag index');
disp('The auto-correlated sequence is a even sequence');
Cross correlation :
3.7 Results
Auto correlation:
Autocorrelation sequence r = 1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000
10.0000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
3.0000 2.0000 1.0000
Maximum value of energy = 11
Maximum value of energy is at the zero lag index
The auto-correlated sequence is a even sequence
Cross correlation:
Type the reference sequence = [1 -2 6 1]
Type the second sequence = [1 2 3 4]
Cross correlation output is =
4 -5 20 19 13 8 1
3.8 Discussions:
Calculations:-
xcorr(y)
4.1 Objective:
To obtain the impulse response/step response/steady state response/response to an
arbitrary input of a system described by the given difference equation
o With x[n]= δ[n], an impulse, the computed output y[n] is the impulse response.
• The difference equation containing past samples of output, i.e., y[n-1], y[n-2], etc leads to
a recursive system, whose impulse response is of infinite duration (IIR). For such systems
the impulse response is computed for a large value of n, say n=100 (to approximate n=∞).
The OCTAVE function filter is used to compute the impulse response/ step response/
response to any given x[n]. Note: The filter function evaluates the convolution of an
infinite sequence (IIR) and x[n], which is not possible with conv function (remember conv
requires both the sequences to be finite).
• The difference equation having only y[n] and present and past samples of input (x[n], x[n-
k]), represents a system whose impulse response is of finite duration (FIR). The response of
FIR systems can be obtained by both the ‘conv’ and ‘filter’ functions. The filter function
results in a response whose length is equal to that of the input x[n], whereas the output
sequence from conv function is of a longer length
(xlength + hlength-1).
4.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC.
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, graph.
4.6 Program:
N=input('Length of response required=');
b=[2]; %x[n] coefficient
a=[1,0.5]; %y coefficients
%impulse input
x=[1,zeros(1,N-1)];
%time vector for plotting
n=0:1:N-1;
%impulse response
h=filter(b,a,x);
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('impulse input');
xlabel('n');
ylabel('δ(n)');
subplot(2,1,2);
stem(n,h);
title('impulse response');
xlabel('n');
ylabel('h(n)');
4.7 Results :
Length of response required=8
4.8 Discussions:
Calculations:-
4. What is frequency response? Give equation for first order system and second order system?
a. Impulse response
b. Step response
4. Suppose we have a system with transfer function H(z) = 1 / ((z – 1.1)*(z – 0.9)). Is the
system stable or unstable?
5. What is BIBO stability? What is the condition to be satisfied foe stability?
5.1 Objective:
To compute N point DFT of a given sequence and to plot magnitude and phase spectrum.
5.3 Pre-Requisite:
Fundamentals of time domain and frequency domain signals.
5.4 Introduction:
Discrete Fourier Transform is a powerful computation tool which allows us to evaluate the
Fourier Transform X(ejw) on a digital computer or specially designed digital hardware. Since
X(ejw) is continuous and periodic, the DFT is obtained by sampling one period of the Fourier
Transform at a finite number of frequency points. Apart from determining the frequency
content of a signal, DFT is used to perform linear filtering operations in the frequency domain.
5.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
5.6 Program:
clc;
clear all;
xn=input('enter the sequence x(n)');
N=input('enter the number of points of computation ');
y=fft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
title('Input Sequence x(n)');
subplot(2,2,2)
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3);
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
z=ifft(y,N);
mag1=real(z);
subplot(2,2,4);
stem(mag1);
title('signal sequence constituted from spectrum');
5.7 Results
enter the sequence x(n) [1 1 1 1 0 0 0 0]
Columns 1 through 6
Columns 7 through 8
0 1.0000 + 2.4142
5.8 Discussions:
Calculations:-
6.1 Objective:
Determination of Linear convolution and Circular convolution of two sequences using
6.3 Pre-Requisite:
Properties of DFT
6.4 Introduction:
• By multiplying two N-point DFTs in the frequency domain, we get the circular
convolution in the time domain.
y[n] = x[n] h[n] ⎯N− D⎯FT→Y (k) = X (k) H (k )
6.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
6.6 Program:
a) Linear
clc;
close all;
clear all;
x1 = input('Enter the first
sequence: ');
x2 = input('Enter the second
sequence: ');
n = input('Enter the number of
points of dft: ');
y1 = fft(x1,n);
disp('DFT of first sequence: ');
disp(y1);
mag1 = abs(y1);
phase1 = angle(y1);
figure(1);
subplot(2,2,1);
stem(mag1);
xlabel('k');
ylabel('Magnitude');
title('Magnitude of X1(k)');
subplot(2,2,2);
stem(phase1);
xlabel('k');
ylabel('Phase');
title('Phase angle of X1(k)');
y2 = fft(x2,n);
disp('DFT of second sequence: ');
disp(y2);
mag2 = abs(y2);
phase2 = angle(y2);
subplot(2,2,3);
stem(mag2);
xlabel('k');
ylabel('Magnitude');
title('Magnitude of X2(k)');
subplot(2,2,4);
stem(phase2);
xlabel('k');
ylabel('Phase');
title('Phase angle of X1(k)');
y = y1.*y2;
z = ifft(y,n);
mag = real(z);
figure(2);
stem(mag);
title('Linear convolution
output');
disp('The Linear convolution
result is: ');
disp(z);
b)Circular
clc;
close all;
clear all;
x1 = input('Enter the first sequence: ');
x2 = input('Enter the second sequence: ');
n = input('Enter the number of points of dft: ');
figure(1);
subplot(3,1,1);
stem(x1,'filled');
title('Plot of the first sequence');
subplot(3,1,2);
stem(x2,'filled');
title('Plot of the second sequence');
n1 = length(x1);
n2 = length(x2)
m = n1+n2-1;
x = [x1,zeros(1,n2-1)];
y = [x2,zeros(1,n1-1)];
x_fft = fft(x,m);
y_fft = fft(y,m);
dft_xy = x_fft.*y_fft;
y = ifft(dft_xy,m);
disp('The linear convolution result is: ');
disp(y);
subplot(3,1,3);
stem(y,'filled');
title('Plot of linearly convoluted signal');
6.7 Result:
Linear
Enter the first sequence: [1 2 3 5]
Enter the second sequence: [3 4 5 6]
Enter the number of points of dft: 8
DFT of first sequence:
Columns 1 through 4:
Columns 5 through 8:
Columns 5 through 8:
Column 8:
-4.4409e-15
Circular
n2 = 4
4 13 29 57 62 59 40
>>
6.8 Discussions:
Calculations:
5. How many points are required for DFT when performing linear convolution?
1. How does the result of circular convolution differ from linear convolution without zero-
adding?
3. Why is the frequency domain approach (DFT and IDFT) efficient for convolution?
4. Were the results of linear and circular convolution consistent with theoretical
expectations?
5. How can errors in convolution results using DFT and IDFT be minimized?
7.1 Objective:
To design and implement a FIR filter for given specifications.
7.2 Software Required:
Octave software
7.3 Pre-Requisite:
Design of FIR filter
7.4 Introduction:
There are two types of systems – Digital filters (perform signal filtering in time domain)and
spectrum analyzers (provide signal representation in the frequency domain). The design of
a digital filter is carried out in 3 steps- specifications, approximations and implementation.
For OCTAVE the Normalized cut-off frequency is in the range 0 and 1, where
= Wc / π
Step 2: Compute the Impulse Response h(n) of the required FIR filter using the given Window
type and the response type (lowpass, bandpass, etc). For example given a rectangular window,
order N=20, and a high pass response, the coefficients (i.e., h[n] samples) of the filter are
computed using the OCTAVE inbuilt command ‘fir1’ as h =fir1(N, wc , 'high', boxcar(N+1));
Note: In theory we would have calculated h[n]=hd[n]×w[n], where hd[n] is the desired
impulse response (low pass/ high pass,etc given by the sinc function) and w[n] is the
window coefficients. We can also plot the window shape as stem(boxcar(N)).
Plot the frequency response of the designed filter h(n) using the freqz function and o
Method 2: Given the pass band (wp in radians) and Stop band edge (ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
Step 1: Select the window depending on the stop-band attenuation required. Generally if
As>40 dB, choose Hamming window. (Refer table )
Step 2: Compute order N based on the edge frequencies as Transition bandwidth = tb=ws-
wp; N=ceil (6.6*pi/tb);
Step 3: Compute the digital cut-off frequency Wc as Wc=(wp+ws)/2. Now compute the
normalized frequency in the range 0 to 1 for OCTAVE as wc=Wc/pi;
Note: In step 2 if frequencies are in Hz, then obtain radian frequencies (for computation of
tb and N) as wp=2*pi*fp/fs, ws=2*pi*fstop/fs, where fp, fstop and fs are the passband,
stop band and sampling frequencies in Hz
Step 4: Compute the Impulse Response h(n) of the required FIR filter using N, selected
window, type of response(low/high,etc) using ‘fir1’ as in step 2 of method 1.
7.5 Procedure:
1. Turn on PC
2. Open the octave software already installed in a PC.
3. In octave open the new script file.
4. Save the file as any name with .m extension.
5. Type the program and save it. And compile the program using run option or F5.
6. After compilation give the input and observe the output, Graph.
7.6 Program:
RECTANGULAR WINDOW :
HAMMING WINDOW
BARTLETT WINDOW
HANNING WINDOW
b=fir1(n,wp,y);
[h,o]= freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,1); plot(o/pi,m)
ylabel('gain in db');
xlabel('(a)normalised freq');
grid on;
7.9 Results:
Rectangular window:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
Graph:
Hamming window :
enter the passband ripple:0.05 enter
the stopband ripple:0.001 enter the
passband frequency:1200 enter the
stopband frequency:1700 enter the
sampling frequency:9000
Graph:
Bartlett window :
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
Graph:
Hanning window :
Graph:
7.10 Discussions:
Calculations:-
8.1 Objective:
To design and implement an IIR filter for given specifications.
8.2 Software Required:
Octave software
8.3 Pre-Requisite:
Design of IIR filter
8.4 Introduction:
There are two methods of stating the specifications as illustrated in previous program. Inthe
first program, the given specifications are directly converted to digital form and the designed filter
is also implemented. In the last two programs the butterworth and chebyshev filters are designed
using bilinear transformation (for theory verification).
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the IIR
filter type (butterworth, cheby1, cheby2).
• Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz Wc =
2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians. For OCTAVE the Normalized cut-off
frequency is in the range 0 and 1, where 1 corresponds to fs/2 (i.e.,fmax)). Hence to use
the OCTAVE commands wc = fc / (fs/2) =400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then normalized frequency is computed as wc
= Wc / π
• Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter and the
response type (lowpass, bandpass, etc) using the appropriate butter, cheby1, cheby2
command. For example given a butterworth filter, order N=2, and a high pass response,
the coefficients [b,a] of the filter are computed using the OCTAVE inbuilt command
• Butterworth LPF :
Clc;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on;
• Butterworth HPF :
clc;
clear all; close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on
Graph:
Garaph:
8.8 Discussions:
Calculation
4. Explain how to find output of digital IIR filter in real time applications?
1. What are the functions used in OCTAVE for designing a digital Butterworth and
Chebyshev low pass filter using BLT?
2. Draw typical responses of Chebyshev filter for order odd & even?
4. How to find output of IIR filter for real time input signal?