SP Manual 2023-24
SP Manual 2023-24
DEPARTMENT VISION
DEPARTMENT MISSION
Laboratory Instructor
1. Mr. Byrareddy A B
Course Objectives:
1. Simulate discrete time signals and verify of sampling theorem.
2. Compute convolution, correlation and verify its properties.
3. Find solutions to difference equations and determine the response to impulse,
step, and sinusoidal inputs.
4. Compute DFT using inbuilt functions and analyse the properties.
5. Compute and display the filtering operations and compare with the theoretical
values.
6. Familiarity with DSP kits and implement basic operations of signals & systems.
9. Design and implementation of a digital IIR filter (Low pass and High pass)
to meet given specifications.
CO1: Write a code to carryout various basic operations on discreate signals and
verify them using MATLAB/OCTAVE software.
CO2: Simulate the programs and execute them on the DSP Starter Kit using Code
Composer Studio Software tool.
CO3: Write the report for the conducted experiment.
CO4: Write open ended experiment to analyse 1-D /2-D signals.
PART-B
1. Obtain the Linear convolution of two sequences.
2. Compute Circular convolution of two sequences.
3. Compute the N-point DFT of a given sequence.
4. Determine the Impulse response of first order /second order system.
PART-C
Open end experiment: Creation of GUI based signal generator with time
domain and frequency domain plots.
MATLAB
As of 2017, MATLAB has roughly one million users across industry and
academia. MATLAB users come from various backgrounds of engineering, science,
and economics. There are various toolboxes in MATLAB like signal processing
toolbox, image processing toolbox, neural network toolbox, statistics toolbox,
financial toolbox, Bioinformatics toolboxes.
The Matlab screen has 4 important windows along with Editor (where programs
can be written and stored). These are Command window where we type
commands and outputs/error messages are seen, Command history window
where past commands are remembered(we can re-run any command by clicking
on the commands),Workspace window which stores all the variables used and
Current Directory window shows the files in current directory.
In this semester students are required to register in Math works website using
their official email ID and download the MATLAB software from the following link.
https://www.mathworks.com/academia/tah-portal/bms-institute-of-technology-
31463638.html
PART A
EXPERIMENT NO 1
Verification of Sampling Theorem
Theory:
● 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 is the process of
reconstruction. (D/A converter)
● Techniques for reconstruction-(i) ZOH (zero order hold)
interpolation results in a staircase waveform, is implemented by
MATLAB plotting function stairs (n, x), (ii) FOH (first order hold)
where the adjacent samples are joined by straight lines is
implemented by MATLAB plotting function plot (n, x),(iii) spline
interpolation, etc.
● For 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
determines the spacing between samples.
Algorithm:
1. Input the desired frequency fmax (for which sampling theorem is to
be verified).
2. Generate an analog signal xt of frequency fmax for comparison.
3. Generate over sampled, Nyquist & Under sampled discrete time
signals.
4. Plot the waveforms and hence prove sampling theorem.
MATLAB Implementation:
MATLAB Program:
tfinal=0.05;
t=0:0.00005: tfinal;
fmax=input('Enter analog frequency');
%Condition forNyquistplot
fs2=2*fmax;
n2=0:1/fs2:tfinal;
xn2=sin(2*pi*fmax*n2);
subplot(3,1,2);
plot(t,xt,'b',n2,xn2,'r*-');
title('Nyquist plot');
%Condition for Oversampling
fs3=10*fmax;
n3=0:1/fs3:tfinal;
xn3=sin(2*pi*fmax*n3);
subplot(3,1,3);
plot(t,xt,'b',n3,xn3,'r*-');
title('Oversampling plot');
xlabel('time');
ylabel('amplitude');
legend('analog','discrete')
Result:
Enter analog frequency 200
Output Waveforms
Plots of a sampled sine wave of 200Hz
Inference:
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 fmax-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. The Fig1.2 shows the result due to a cosine signal
(x1=cos(2*pi*fmax*n1);
3. The over sampled plot shows a reconstructed signal almost similar
to that of the analog signal. Using low pass filtering the wave form
can be further smoothened.
VIVA Questions
1. Define plot, stem, and subplot in MATLAB.
2. List all the functions used in the above program.
3. Briefly explain sampling theorem.
4. What is aliasing?
EXPERIMENT NO 2
A. Linear Convolution of Two Given Sequences
Theory:
● 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 convolution sum is
Algorithm:
1. Input the two sequences as x1, x2
2. Convolve both to get output y.
3. Plot the sequences.
MATLAB Implementation:
MATLAB recognizes index 1 to positive maximum. Index 0 is also
not recognized. The timing information for a sequence is provided by
another vector, say n=-3:5; creates a vector with values from -3 to 5 with
an increment of 1.
During plotting, the time vector size and the sequence size should be
the same, i.e., the number of elements in the sequence x1 and in its time
vector n1 should be the same. Similarly for x2 and y.
MATLAB Program:
Level 1 Program (both sequences are right sided)
%Mainpart of computation
x1=input('Enter first sequence')
x2=input('Enter second sequence')
y=conv(x1, x2);
n1=0:length(x1)-1;
n2=0:length(x2)-1;
n=0:length(y)-1;
disp('linear con of x1 & x2 is y=');
disp(y);
%Graphical display part
subplot (2,1,1);
stem(n,y);
xlabel('time index n');
ylabel('amplitude ');
title ('convolution output');
subplot(2,2,3);
stem(n1,x1);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x1');
subplot(2,2,4);
stem(n2,x2);
xlabel('time index n');
ylabel('amplitude ');
title('plot of x2');
Result
Enter first sequence [1 2 3 4]
x1 = 1 2 3 4
Enter second sequence [2 3 2 4]
x2 = 2 3 2 4
Output Waveforms
title('plot of x2');
Result
x1 =1 2 3 2 1 3 4
n1 =-3 -2 -1 0 1 2 3
x2 =2 -3 4 -1 0 1
n2 =-1 0 1 2 3 4
linear con of x1 & x2 is y= 2 1 4 2 6 9 3 2 15 -3 3 4
Output waveforms
Properties of Convolution:
a). Commutative property
clear all;
close all;
x1=[1 2 4 4];
x2=[1 2 7 2];
y1=conv(x1,x2);
y2=conv(x2,x1);
if y1==y2
disp('Commutative property is satisfied');
else
disp('Commutative property is not satisfied');
end
b). Associative property
clear all;
close all;
x1=[1 2 3 4];
x2=[1 2 1 2];
x3=[3 4 2 1];
y1=conv(x1,x2);
y2=conv(y1,x3);
y3=conv(x2,x3);
y4=conv(x1,y3);
if y2==y4
disp('Associative property is satisfied');
else
disp('Associative property is not satisfied');
end
c). Distributive property
clear all;
close all;
x1=[1 2 3 4 ];
x2=[1 2 1 2];
x3=[3 4 2 1];
y1=x2+x3;
y2=conv(x1,y1);
y3=conv(x1,x2)+conv(x1,x3);
if y2==y3
disp('distributive property is satisfied');
else
disp('distributive property is not satisfied');
end
VIVA Questions :
1. Explain how linear convolution can be obtained.
2. Explain different properties of convolution
3. Give the different methods for performing convolution
.
● To compute this equation, the linear convolution involves the
following operations:
● Steps for circular convolution are the same as the usual convolution,
except all index calculations are done "mod N" = "on the wheel".
o Plot f [m] and h [−m] as shown in Fig. 4.1.
(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 Clockwise
(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 the period. If they are finite sequences N is chosen as >= to
max(xlength, hlength). Whereas in linear convolution N>=
xlength+hlength-1.
● Say x[n]={-2,3,1,1} and N = 5, then the x[-1] and x[-2] samples
are plotted at x[N-1] = x[-4] and x[N-2] =x[-3] places. Now x[n] is
entered as x[n] = {1, 1, 0,-2,3}.
MATLAB Implementation:
MATLAB recognizes index 1 to be positive maximum. Index 0 is
not recognized. Hence in the below program wherever y, x and h
sequences are accessed, the index is added with +1. The modulo index
calculation for circular convolution is carried out using the function -
MOD Modulus (signed remainder after division). MOD( x, y) is x -
y.*floor(x./y) if y ~= 0. By convention, MOD(x,0) is x. The input x and y
must be real arrays of the same size, or real scalars. MOD( x, y) has the
same sign as y while REM( x, y) has the same sign as x. MOD( x, y) and
REM( x, y) are equal if x and y have the same sign, but differ by y if x and
y have different signs.
MATLAB Program:
Program to find circular convolution of two sequences by
padding zeroes
clc;
clear all;
close all;
g=input('Enter the first sequence');
h=input('Enter the second sequence');
N1=length(g);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if(N3>=0)
h=[h,zeros(1,N3)];
else
g=[g,zeros(1,-N3)];
end
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N); %calculation of x index
if i<0
i=i+N;
end %end of ‘if’
y(n+1)=y(n+1)+h(k+1)*g(i+1);
end %end of inner ‘for loop’
end %end of inner ‘for loop’
disp('The resultant signal is');y
figure(1);
subplot(3,1,1);
stem(g);
title('The first sequence is');
grid on;
subplot(3,1,2);
stem(h);
title('The second sequence is');
grid on;
subplot(3,1,3);
stem(y);
title('The circularly convolved sequence is');
grid on;
Result:
Enter the first sequence [1 1 1 1]
Enter the second sequence [1 2 3 4]
The resultant signal is
y =10 10 10 10
Output Waveform
VIVA Questions :
1. Write A Program to find the circular convolution of two sequences
using Matrix Method
2. Explain how circular convolution can be obtained.
EXPERIMENT NO 3
Solving a Given Difference Equation
Theory:
MATLAB Implementation:
MATLAB has an inbuilt function ‘filter’ to solve difference equations
numerically, given the input and difference equation coefficients (b, a).
y=filter (b,a,x)
Given Problem
1) Difference equation y(n) - y(n-1) + 0.9y(n-2) = x(n);
Calculate impulse response h(n) and also step response at n=0,…..,100
2) Plot the steady state response y[n] to x[n]=cos(0.05πn)u(n), given
y[n]-0.8y[n-1]=x[n]
MATLAB Program:
%To find Impulse Response
N=input ('Length of response required=');
b=[1]; %x[n] coefficient
a=[1,-1,0.9]; %y coefficients
%impulse input
h=[1,zeros(1,N-1)];
%time vector for plotting
n=0:1:N-1;
%impulse response
y=filter (b,a,h);
Result:
Length=100
Output Waveform
stem(n,h);
title('step input');
xlabel('n');
ylabel('u(n)');
subplot(2,1,2);
stem(n,y);
title('step response');
xlabel('n');
ylabel('y(n)');
Result:
Length=100
Output Waveform
Output Waveform
VIVA Questions
1. Explain the Matlab function filter
2. How to find various responses using filter function
EXPERIMENT NO 4
Theory:
● Correlation is mathematical technique which indicates whether 2
signals are 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 correlation sequence rxy[l] defined by
● The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.
● Autocorrelation sequence of x[n] is given by
εx) i.e., .
Algorithm:
1. Input the sequence as x.
2. Use the ‘xcorr’ function to get auto correlated output r.
3. Plot the sequences.
MATLAB Implementation:
MATLAB has the inbuilt function XCORR(A), when A is a vector,
is the auto-correlation sequence. If A is of length M vector (M>1), then the
xcorr function returns the length 2*M-1 auto-correlation sequence. The
zeroth lag of the output correlation is in the middle of the sequence at
element M.
XCORR(...,MAXLAG) computes the (auto/cross) correlation over
the range of lags: -MAXLAG to MAXLAG, i.e., 2*MAXLAG+1 lags. If
missing, default is MAXLAG = M-1.
[C,LAGS] = XCORR(...) returns a vector of lag indices (LAGS).
MATLAB Programs
%Computation of Autocorrelation of a rectangular Sequence
x = input ('Type in the reference sequence = ');
% Compute the correlation sequence
n = length(x)-1;
r = conv(x, fliplr(x));
disp('autocorrelation sequence r=');
disp(r);
%plot the sequences
subplot (2,1,1)
stem (x);
title ('square sequence');
subplot
(2,1,2)
k = -n: n;
stem (k, r);
title('autocorrelation output');
xlabel('Lag index');
ylabel('Amplitude');
Result:
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
Output Waveform
Algorithm:
1. Input the sequence as x and y.
2. Use the ‘xcorr’ function to get cross correlated output r.
3. Plot the sequences.
MATLAB Implementation:
MATLAB has the inbuilt function XCORR: Say C = XCORR(A,B), where
A and B are length M vectors (M>1), returns the length 2*M-1
cross-correlation sequence C. If A and B are of different length, the
shortest one is zero-padded. Using convolution to implement correlation,
the instruction is FLIPLR Flip matrix in left/right direction. FLIPLR(X)
returns X with row preserved and columns flipped in the left/right
direction. X = 1 2 3 becomes 3 2 1.
MATLAB Programs
% Computation of Cross-correlation Sequence using folded sequence
and convolution
x = input('Type in the reference sequence = ');
y = input('Type in the second sequence = ');
% Compute the correlation sequence
n1 = length(x)-1;
n2 = length(y)-1;
r = conv(x,fliplr(y));
disp(‘Cross correlation output is =’);
disp(r);
k = (-n1):n2;%time vector for plotting
stem(k,r);
xlabel('Lag index');
ylabel('Amplitude');
Result:
Type in the reference sequence = [1 3 -2 1 2 -1 4 4 2]
Type in the second sequence = [2 -1 4 1 -2 3]
Cross correlation output is =
3 7 -11 14 13 -15 28 6 -2 21 12 12 6 4
Output Waveforms
Inference:
Strong peak of 15 at lag = -2 implies the delay between xi and x2 is 2.
Also peak =15=energy of xi (1+22+32+ (-1)2) implies that both xi and x2
are strongly correlated.
%consider the below sequences
xr=[1,2,3,-1];
x2=[3,-1,1,2];
xcorr(xr,x2) ans ={2,5,7,2,2,10,-3}
xcorr(x2,xr) ans ={-3,10,2,2,7,5,2}
Inference:
Strong peak of 10 at lag = 2 implies the delay between xr and x2 is 2, but
since 10<15, it implies that xr and x2 are uncorrelated slightly (may be due
to noise, etc). is verified.
EXPERIMENT NO 5
Computation of N point DFT of a Given Sequence and to Plot
Magnitude and Phase Spectrum.
if fs=8 kHz and N=8 point DFT, then in the resulting spectrum, k=1
corresponds to 1 kHz frequency. For the same fs and x[n], if N=80
point DFT is computed, then in the resulting spectrum, k=1
corresponds to 100Hz frequency. Hence, the resolution in frequency
is increased.
● Since , increasing N to 8 from 80 for the same x[n] implies
x[n] is still the same sequence (<8), the rest of x[n] is padded with
zeros. This implies that there is no further information in time
domain, but the resulting spectrum has higher frequency resolution.
This spectrum is known as ‘high density spectrum’ (resulting from
zero padding x[n]). Instead of zero padding, for higher N, if more
number of points of x[n] are taken (more data in time domain), then
the resulting spectrum is called a ‘high resolution spectrum’.
Algorithm:
1. Input the sequence for which DFT is to be computed.
2. Input the length of the DFT required (say 4, 8, >length of the
sequence).
3. Compute the DFT using the ‘fft’ command.
4. Plot the magnitude & phase spectra.
MATLAB Implementation:
MATLAB has an inbuilt function ‘FFT’ which computes the Discrete
Fourier transform.
FFT(X) is the discrete Fourier transform (DFT) of vector X. For length
N input vector x, the DFT is a length N vector X, with elements N.
FFT(X,N) is the N-point FFT, padded with zeros if X has less than N
points and truncated if it has more.
The magnitude spectrum is computed using the function ABS Absolute
value.
ABS(X) is the absolute value of the elements of X. When X is complex,
ABS(X) is the complex modulus (magnitude) of the elements of X.
The phase spectrum is computed using the function ANGLE Phase angle.
ANGLE (H) returns the phase angles, in radians, of a matrix with complex
elements.
Matlab Program:
N-point FFT and IFFT of a given sequence
clc;
clear all;
close all;
x=input ('Enter the sequence x (n)');
N=length(x);
y=fft(x,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(x);
grid on;
title('Input Data Sequence x(n)');
subplot(2,2,3);
stem(mag);
grid on;
title('Magnitude Plot');
subplot(2,2,2);
stem(phase);
grid on;
title('Phase plot');
z=ifft(y,N);
mag1=real(z);
subplot(2,2,4);
stem(mag1);
grid on;
title('Signal Sequence constituted from Spectrum');
Result:
Enter the sequence x(n) [0.5 0.5 0.5 0.5 0 0.5 0.5 0.5]
3.5000 0.5000 -0.5000 0.5000 -0.5000 0.5000 -0.5000
0.5000
Output waveform
title(‘phase plot’);
clc:
xk=input(‘enter the sequence x(k)’);
N=input(‘enter the number of points of computation’);
y=idft(xk,N);
disp(y);
amp=abs(y);
ph=angle(y);
subplot(2,1,1)
stem(amp);
xlabel(‘MAGNITUDE’);
ylabel(‘K’);
title(‘magnitude plot’);
stem(ph*180/pi);
xlabel(‘phase’);
ylabel(‘K’);
title(‘phase plot’);
Result:
Enter the sequence Xk=[4, 1-2.4142i ,0,1-0.4142i,0, 1.0000+0.4142i ,
0,1+2.4142i]
x(n)=[1 1 1 1 0 0 0 0]
Inputs -
Run1: Give 4 point sequence and find 4 point DFT
Run2: Give 8 point sequence and find 8 point DFT
VIVA Questions:
1. Explain DFT
2. What are the methods of finding DFT
3. Explain differences between DFT and FFT
4. Explain how functions can be declared in MATLB.
5. Explain MATLAB function fft(x)
6. How can user create a MATLAB function
EXPERIMENT NO 6
A. Verification of DFT properties (Linearity and Parseval’s
Theorem)
Matlab code for Linearity properly &Parseval's Theorem
i) Verificaton of Parseval's Theorem
Program
x1=[1 2 3 4]
X1=fft(x1,4);
en1=sum(x1.^2);
en2=(1/4)*(sum(abs(X1.^2)))
if en1==en2
disp('ParsevalTheoreem Proved')
disp('Energy =')
disp(en2)
end
Result
x1 =1 2 3 4
en2 = 30
Parseval Theorem Proved
Energy =30
Program
x1=[1 2 3 4]
x2=[1 1 1 1]
a1=a2=1;
term1=a1.*x1+a2.*x2;
LHS=fft(term1,4);
term2=fft(a1.*x1);
term3=fft(a2.*x2);
RHS=term2+term3;
if(LHS==RHS)
disp('Lineraity Property Proved')
else
disp('Non Linear')
end
Result
x1=1 2 3 4
x2 =1 1 1 1
Program
x1=[zeros(1,8),ones(1,16),zeros(1,8)]
xk=fft(x1)
xk2=fftshift(xk);
subplot(2 ,1, 1)
stem(x1)
subplot(2 ,1, 2)
plot(abs(xk2));
Result
x=-pi:0.05:pi;
x1=sinc(x)
xk=fft(x1)
xk2=fftshift(xk);
subplot(2 ,1, 1)
stem(x1)
subplot(2 ,1, 2)
plot(abs(xk2));
Result
EXPERIMENT NO 7
Theory:
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.
N=20, and a high pass response, the coefficients (i.e., h[n] samples)
of the filter are computed using the MATLAB 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 observe the type of response (lowpass / highpass
/bandpass).
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 stopband attenuation
required. Generally if As>40 dB, choose Hamming window. (Refer
table )
● Step 2: Compute the 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
MATLAB as
wc=Wc/pi;
MATLAB IMPLEMENTATION
FIR1 Function
B = FIR1(N,Wn) designs an N'th order low pass FIR digital filter and
returns the filter coefficients in length N+1 vector B. The cut-off frequency
Wn must be between 0 <Wn< 1.0, with 1.0 corresponding to half the
sample rate. The filter B is real and has linear phase, i.e., even symmetric
coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1.
If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N
band pass filter with pass band W1< W < W2. B = FIR1(N,Wn,'high')
Matlab Program:
1 RECTANGULAR WINDOW
clc;
clear all;
close all;
lp=input('Enter the pass band ripple:');
ls=input('Enter the stop band ripple:');
fp=input('Enter the pass band frequency:');
fs=input('Enter the stop band frequency:');
f=input('Enter the sampling frequency:');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if (rem(n,2)~=0)
n1=n;
n=n-1;
end
y=boxcar(n1);% or y=rect(n1);
% Low Pass Filter
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 Frequency-->');
grid on;
% High Pass Filter
b=fir1(n,wp,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m);
ylabel('Gain in db-->');
xlabel('(b)Normalised Frequency-->');
grid on;
Result
Output waveform
2 HAMMING WINDOW
clc;
clear all;
close all;
lp=input('Enter the passband ripple:');
ls=input('Enter the stopband ripple:');
fp=input('Enter the passband frequency:');
fs=input('Enter the stopband frequency:');
f=input('Enter the sampling frequency:');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if (rem(n,2)~=0)
n1=n;
n=n-1;
end
y=hamming(n1);
plot(o/pi,m);
ylabel('Gain in db-->');
xlabel('(b)Normalised Frequency-->');
grid on;
Result:
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
Output waveform
3 BARTLET
clc;
clear all;
close all;
lp=input('Enter the passband ripple:');
ls=input('Enter the stopband ripple:');
fp=input('Enter the passband frequency:');
fs=input('Enter the stopband frequency:');
subplot(2,2,2);
plot(o/pi,m);
ylabel('Gain in db-->');
xlabel('(b)Normalised Frequency-->');
grid on;
Result:
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
Output Waveform
4 HANNING
clc;
clear all;
close all;
lp=input('Enter the passband ripple:');
ls=input('Enter the stopband ripple:');
m=20*log10(abs(h));
subplot(2,2,2);
plot(o/pi,m);
ylabel('Gain in db-->');
xlabel('(b)Normalised Frequency-->');
grid on;
Result
Enter the passband ripple: 0.03
Enter the stopband ripple: 0.01
Enter the passband frequency: 1400
Enter the stopband frequency: 2000
Enter the sampling frequency: 8000
Output Waveform
6 KAISER
clc;
clearall;
closeall;
lp=input('Enter the passband ripple:');
ls=input('Enter the stopband ripple:');
Result:
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
Enter the beta value: 5.8
Output Waveform
VIVA Questions:
1. What is a Filter? What are its specifications?
2. Mention the types of filters.
3. Compare analog and digital filters.
4. Define a FIR filter.
5. What are the important properties of FIR filter?
6. What are the window techniques used?
7. Why the Kaiser window is important?
8. How do you calculate the length of Kaiser Window?
9. What are the functions used in designing a FIR filter in Matlab
EXPERIMENT NO 8
Design and Implementation of IIR filter to meet given specifications.
Theory:
There are two methods of stating the specifications as illustrated in
previous program. In the 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 MATLAB 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 MATLAB
commands
wc = fc / (fs/2) = 400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then the 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 MATLAB inbuilt command
‘butter’ as [b,a] =butter(N, wc , 'high');
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: Since the frequencies are in radians divide by π to obtain
normalized frequencies to get wp=Wp/pi and ws=Ws/pi
If the frequencies are in Hz (note: in this case the sampling
frequency should be given), then obtain normalized frequencies as
wp=fp/(fs/2), ws=fstop/(fs/2), where fp, fstop and fs are the
passband, stop band and sampling frequencies in Hz
● Step 2: Compute the order and cut off frequency as
[N, wc] = BUTTORD(wp, ws, Rp, Rs)
● Step 3: Compute the Impulse Response [b,a] coefficients of the
required IIR filter and the response type as [b,a] =butter(N, wc ,
'high');
IMPLEMENTATION OF THE IIR FILTER
1. Once the coefficients of the IIR filter [b,a] are obtained, the next
step is to simulate an input sequence x[n], say input of 100, 200 &
400 Hz (with sampling frequency of fs), each of 20/30 points.
Choose the frequencies such that they are >, < and = to fc.
2. Filter the input sequence x[n] with Impulse Response, to obtain the
output of the filter y[n] using the ‘filter’ command.
3. Infer the working of the filter (low pass/ high pass, etc).
MATLAB IMPLEMENTATION
BUTTORD Butterworth filter order selection.
[N, Wn] = BUTTORD (Wp, Ws, Rp, Rs) returns the order N of the lowest
order digital Butterworth filter that loses no more than Rp dB in the
passband and has at least Rs dB of attenuation in the stopband. Wp and
Ws are the passband and stopband edge frequencies, normalized from 0 to
1 (where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3
dB frequency") to use with BUTTER to achieve the specifications.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an
analog filter, in which case Wp and Ws are in radians/second.
When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in
BUTTORD.
MATLAB PROGRAMS
Butterworth Filters
1.Butterworth LPF
clc;
clear all;
close all;
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);disp(n);disp(wn)
[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;
Result:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000
Output Wave form
2 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); disp(n);disp(wn)
[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
Result:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000
CHEBYSHEV FILTERS
1 Chebyshev LPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
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]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
xlabel('(a) normalised frequency-->');
subplot(2,1,2);plot(om/pi,an);
xlabel('(b) normalised frequency-->');
ylabel('phase in radians-->');
Result:
enter the passband ripple 0.2
enter the stopband ripple 45
enter the passband frequency 1300
enter the stopband frequency 1500
enter the sampling frequency 10000
Output Waveform
2 Chebyshev HPF
clc;
close all;
clear all;
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
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]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
xlabel('(a) normalised frequency-->');
subplot(2,1,2);plot(om/pi,an);
xlabel('(b) normalised frequency-->');
ylabel('phase in radians-->');
Result:
enter the passband ripple 0.3
enter the stopband ripple 60
enter the passband frequency 1500
enter the stopband frequency 2000
enter the sampling frequency 9000
Output Waveform
VIVA Questions:
1. Define an IIR filter.
2. Compare Impulse invariance and bilinear transformations.
3. How do you convert analog filter prototype to a digital filter?
4. What are the functions used in MATLAB for designing a digital
Butterworth and Chebyshev low pass filter using BLT?
5. Draw typical responses of Chebyshev filter for order odd & even.
6. Compare FIR & IIR filters.
PART II
CCS INTRODUCTION
Procedure
1. Double click on CCS icon
Type the project name (x.pjt) or (USN) & store in my projects folder
( Select type of file as: C/C++ file before saving) & add this C file to
your project
f. Display Buffer size 🡪 verify given output data size and change
EXPERIMENT NO 1
Linear convolution for the given sequences
Aim: To perform linear convolution for the given sequences
The linear convolution sum is
C Program
# include<stdio.h>
# include<math.h>
float y[10];
main()
{
//the two sequences can be of different lengths, no need to zero pad for this
program
float h[4] = { 2,2,2,2};
float x[4] ={1,2,3,4};
intxlen=4;
inthlen=4;
int N=xlen+hlen-1;
intk,n;
for(n=0;n<N;n++) //outer loop for y[n] array
{ y[n]=0;
for(k=0;k<hlen;k++) //inner loop for computing each y[n] point
{ if (((n-k)>=0) & ((n-k)<xlen))
y[n]=y[n]+h[k]*x[n-k]; //compute output
} //end of inner for loop
printf("%f\t", y[n]);
EXPERIMENT NO 2
Circular convolution of two given sequences
C Program
# include<stdio.h>
# include<math.h>
float y[10]; //output sequence
main()
{ //input the two sequences, if of uneven lengths zero pad the smaller one
such that both are of same size
float x[5]={1,2,3,4,5};
float h[5]={2,1,3,4,5};
int N=5; // N=max of xlen and hlen//
intk,n,i;
for(n=0;n<N;n++) //outer loop for y[n] array
{ y[n]=0;
for(k=0;k<N;k++) //inner loop for computing each y[n]
point
{ i=(n-k)%N; //compute the index modulo N
if(i<0) //if index is <0, say x[-1], then convert to
x[N-1]
i=i+N;
y[n]=y[n]+h[k]*x[i]; //compute output
EXPERIMENT NO 3
Computation of N- Point DFT of a given sequence
C Program
#include <stdio.h>
#include <math.h>
main()
{float y[16]; //for 8 point DFT to store real & imaginary
float x[4]={1,3,2,5}; //input only real sequence
float w;
int n,k,k1,N=8,xlen=4;
for(k=0;k<2*N;k=k+2)
{y[k]=0; y[k+1]=0; //initialize real &image parts
k1=k/2; //actual k index
for(n=0;n<xlen;n++)
{w=-2*3.14*k1*n/N; //careful about minus sign
y[k]=y[k]+x[n]*cos(w);
y[k+1]=y[k+1]+x[n]*sin(w);
}
printf("%f+j%f \n",y[k],y[k+1]);
}
}//end of main
MATLAB verification
>> x=[1 3 2 5];
>>fft(x,8) 11.0000 -0.4142 - 7.6569i -1.0000 + 2.0000i
2.4142 - 3.6569i -5.0000 2.4142 + 3.6569i -1.0000 - 2.0000i
-0.4142 + 7.6569i
EXPERIMENT NO 4
Impulse response of the given system
Aim: To find the Impulse response of the given first order / second
order system
Theory:
A linear constant coefficient difference equation representing a second
order system is given by
Algorithm
1. Input the coefficients b0 b1 b2, a1 a2 (Say the coefficients from a 2nd
order Butterworth filter. Note coefficient of y[n] is a0=1 always)
2. Generate the impulse input of size n (1st element=1, rest all=0)
3. Compute the output as
a. y[n]-1/9y[n-2]=x[n-1]
b. y[n]-3/4y[n-1]+1/8y[n-2]=x[n]+1/2x[n-1]
Open Ended Experiment
Mini Project
Students are expected to create a GUI based signal generator using
MATLAB. The signal generator will have three buttons to select three
signals ie., sine wave, square wave and triangular wave. Upon the
selection the wave form in time domain and frequency domain needs to be
displayed graphically. There should be a control to change the amplitude
and frequency of the signals.
Questions:
1. Why do we need SP processors?
2. What is the difference between Fixed and floating point
processors?
3. Explain the architectural features of DSP processor.
4. How do perform signed arithmetic on DSP processor?
5. What are the advantages and disadvantages of DSP processor?