SP Lab Final
SP Lab Final
LAB MANUAL
(Regulation 2021)
BE –BIOMECICAL ENGINEERING
IVSEMESTER
LIST OF EXPERIMENTS
Ex No: Date:
Requirements:
PC with MATLAB
Algorithm:
Ex No: Date:
Aim:
Requirements
PC with MATLAB
Algorithm:
clear all;
close all;
clc;
numG = [1.5 1]; % Create G(s) as a ratio of numerator & denumerator
Result:
Thus a MATLAB program was written to analyse the stability of the system
EXP.NO: DATE:
Aim:
To write a MATLAB program to reconstruct signal from the samples and study the effect of aliasing
Requirement:
PC with MATLAB
Algorithm:
Program:
clc;
clear all;
close all;
Ts1=0.04;
Ts2=0.005;
t=[0:0.0001:0.5];
w=100;
x=sin(w*t);
% Sampling with aliasing
figure(1);
subplot(3,1,1);
plot(t,x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Continuous-Time Signal');
t1=[0:Ts1:0.5];
xs1=sin(w*t1);
subplot(3,1,2)
stem(t1,xs1);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Version of Ts1=0.04');
subplot(3,1,3)
plot(t,x,'LineWidth',3);
hold on; plot(t1,xs1,'r');
hold off;
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Version of Ts1=0.04 - Effect of aliasing');
legend('Original Signal','Aliased Signal with Ts1');
subplot(3,1,2)
stem(t2,xs2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Version of Ts2=0.005');
subplot(3,1,3)
plot(t,x,'LineWidth',3);
hold on ;
plot(t2,xs2,'ro');
hold off;
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Version of Ts2=0.005 - Effect of without aliasing');
legend('Original Signal','Without Aliasing Signal with Ts2');
EXP.N O: DATE:
Aim:
To write a MATLAB program to find the spectrum of a sinusoidal signal using FFT
Requirements:
PC wiyh MATLAB
Alogrithm:
Program:
Fs = 150
X = fft(x,nfft);
X = X(1:nfft/2);
figure(2);
plot(f,mx);
xlabel('Frequency (Hz)');
ylabel('Power');
Result:
Thus a MATLAB program was written to analyze the spectrum sinusoidal of the FFT
EXP.NO: DATE:
Aim:
To write a program to design the FIR low pass, high pass, band pass and band stop filters and obtain thefrequency
response of the filter using MATLAB
Requirements:
PC with MATLAB
Algorithm:
1. Start the program.
2. Get the cutoff frequency and order of the filter as input.
. 3. Find the ideal impulse response of the filter hd (n).
4. Select a window function w (n).
5. Find out the filter coefficients h (n) by multiplying hd (n) and w (n).
6. Compute the frequency response of the filter.
7. Repeat the process for high pass, band pass and band stop filters.
8. Execute the program, display the result and verify it.
9. Plot the output graph for hd(n),w(n),h(n)and frequency response of LPF,HPF,BPF and BSF
. 10.Stop the process
Output:
Filter coefficients for LPF= hd= -0.0470 0.1075 0.2967 0.3820 0.2967 0.1075 -0.0470
w= 0.0800 0.3100 0.7700 1.0000 0.7700 0.3100 0.0800
h= -0.0038 0.0333 0.2284 0.3820 0.2284 0.0333 -0.0038
disp(hd);
subplot(2,2,2);
stem(n,w);
xlabel('n--->');
ylabel('w(n)--->');
title('Hamming Window');
disp('w=');
disp(w);
subplot(2,2,3);
stem(n,h);
xlabel('n--->');
ylabel('h(n)--->');
title('Actual Impulse Response');
disp('h=');
disp(h);
subplot(2,2,4);
plot(P/pi, mag);
grid on;
xlabel('Normalized frequency--->');
ylabel('Magnitude in db--->');
title('Frequency Response ');
[M1,P]=freqz(h,1,2000);
mag=20*log10(abs(M1));
Output:
Filter coefficients for BPF=
hd=-0.0168 0.1580 0.0173 -0.2280 -0.0072 0.2546 -0.0072 -0.2280 0.0173 0.1580 -0.0168
w=0 0.0955 0.3455 0.6545 0.9045 1.0000 0.9045 0.6545 0.3455 0.0955 0
h=0 0.0151 0.0060 -0.1492 -0.0065 0.2546 -0.0065 -0.1492 0.0060 0.0151 0
subplot(2,2,1);
stem(n,hd);
xlabel('n--->');
ylabel('hd(n)--->');
title('Ideal Impulse Response');
disp('hd=');
disp(hd);
subplot(2,2,2);
stem(n,w);
xlabel('n--->');
ylabel('w(n)--->');
title('Hanning Window');
disp('w=');
disp(w);
subplot(2,2,3);
stem(n,h);
xlabel('n--->');
ylabel('h(n)--->');
title('Actual Impulse Response');
disp('h=');
disp(h);
subplot(2,2,4);
plot(P/pi, mag);
grid on;
xlabel('Normalized frequency--->');
ylabel('Magnitude in db--->');
title('Frequency Response ');
Output:
w= 1 1 1 1 1 1 1
[M1,P]=freqz(h,1,2000);
mag=20*log10(abs(M1));
subplot(2,2,1);
stem(n,hd);
xlabel('n--->');
ylabel('hd(n)--->');
title('Ideal Impulse Response');
disp('hd=');
disp(hd);
subplot(2,2,2);
stem(n,w);
xlabel('n--->');
ylabel('w(n)--->');
title('Hanning Window');
disp('w=');
disp(w);
subplot(2,2,3);
stem(n,h);
xlabel('n--->');
ylabel('h(n)--->');
title('Actual Impulse Response');
disp('h=');
disp(h);
subplot(2,2,4);
plot(P/pi, mag);
grid on;
xlabel('Normalized frequency--->');
ylabel('Magnitude in db--->');
title('Frequency Response ');
%PROGRAM FOR BAND STOP FILTER
clc; clear
all;close
all;M=7;
wc1=1.2;
wc2=2;
n=0:1:M-1;
hd=ideal_lp(pi,M)-ideal_lp(wc2,M)+ideal_lp(wc1,M);
w=rectwin(M);
h=hd.*w';
disp('Filter coefficients for BSF=');
[M1,P]=freqz(h,1,2000);
mag=20*log10(abs(M1));
subplot(2,2,1);
stem(n,hd);
xlabel('n--->');
ylabel('hd(n)--->');
title('Ideal Impulse Response');
disp('hd=');
disp(hd);
subplot(2,2,2);
stem(n,w);
xlabel('n--->');
ylabel('w(n)--->');
title('Rectangular Window');
disp('w=');
disp(w);
subplot(2,2,3);
stem(n,h);
xlabel('n--->');
ylabel('h(n)--->');
title('Actual Impulse Response');
disp('h=');
disp(h);
Ex : No: Date:
Algorithm:
Output:
Enter the Passband frequency w1=[0.2*pi]
Enter the Stopband frequency w2=[0.6*pi]
Enter the Passband ripple a1=[0.8]
Enter the Stopband ripple a2=[0.2]
l = 4.8990 , e = 0.7500
N=2, Wc = 0.7255
clc;
clear all;
close all;
%w1=0.2*pi;w2=0.6*pi;a1=0.8;a2=0.2;
[num,den]=butter(N,Wc,'s')
[b,a]=impinvar(num,den)
[mag,angle]=freqz(b,a,512);
magnitude=20*log10(abs(mag));
figure(1);
plot(angle/pi,magnitude);
grid on;
xlabel('frequency --->');
ylabel('magnitude --->');
[num1,den1]=butter(N1,Wc1,'high','s')
disp('HPF DIGITAL FILTER TRANSFER FUNCTION');
[b1,a1]=bilinear(num1,den1,1);
[mag1,angle1]=freqz(b1,a1,512);
magnitude=20*log10(abs(mag1));
figure(2);
plot(angle1/pi,magnitude);
grid on;
ylabel('magnitude --->');
subplot(2,2,4);
xlabel('Normalized frequency--->');
ylabel('Magnitude in db--->');
Result:
Thus a program to design the Butterworth low pass and high pass
filter using Matlabwas written and response of the filter was executed.
EXP.NO DATE:
EXP.NO DATE
MULTIRATE SIGNAL PROCESSING
Aim:
Requirements:
Algorithm:
Program:
AIM: program to verify the decimation of given sequence.
SOFTWARE: MATLAB
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
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
EXP.NO DATE:
Aim:
AIM: To verify the linear convolution operation Using DSK Code composer studio
Note:
1. To execute the above program follow “ procedure to work on code composer studio”
2. To view graphical output follow the above procedure.
AIM: To verify the circular convolution operation Using DSK Code composer studio
C Program to Implement Circular Convolution
#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30]; void main()
{
printf(" enter the length of the first sequence\n"); scanf("%d",&m);
printf(" enter the length of the second sequence\n"); scanf("%d",&n);
printf(" enter the first sequence\n"); for(i=0;i<m;i++) scanf("%d",&x[i]);
printf(" enter the second sequence\n"); for(j=0;j<n;j++)
scanf("%d",&h[j]);
if(m-n!=0) /*If length of both sequences are not equal*/
{
if(m>n) /* Pad the smaller sequence with zero*/
{
for(i=n;i<m;i++) h[i]=0;
n=m;
}
for(i=m;i<n;i++) x[i]=0;
m=n;
} y[0]=0;
a[0]=h[0];
➢Open Code Composer Studio; make sure the DSP kit is turned on.
➢Start a new project using ‘Project-new ‘ pull down menu, save it in a separate
directory(c:\ti\myprojects) with name cir conv.pjt.
➢Add the source files Circular Convolution.C.
➢to the project using ‘Project add files to project’ pull down menu.
➢Add the linker command file hello.cmd .
➢(Path: c:\ti\tutorial\dsk6713\hello1\hello.cmd)
➢Add the run time support library file rts6700.lib
➢(Path: c:\ti\c6000\cgtools\lib\rts6700.lib)
INPUT:
Eg: x[4]={3, 2, 1,0}
h[4]={1, 1, 0,0}
OUTPUT: y[4]={3, 5, 3,0}
RESULT:
The C program was written and verified successfully for linear & circular convolution operation Using
DSK Code composer studio.
AIM: To find the DFT of a sequence using FFT algorithm
C PROGRAM TO IMPLEMENT 4 POINT FFT :
Main.c (fft 256.c):
#include <math.h>
#define PTS 64 //# of points for FFT #define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX; void FFT(COMPLEX *Y, int n); //FFT prototype
float iobuffer[PTS]; //as input and output buffer float x1[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer short flag = 0; //set to 1 by ISR when
iobuffer full COMPLEX w[PTS]; //twiddle constants stored in w COMPLEX samples[PTS];
//primary working buffer main()
{
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants w[i].imag =-
sin(2*PI*i/(PTS*2.0)); //Im component of twiddle constants
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
iobuffer[i] = sin(2*PI*10*i/64.0);/*10- > freq, 64 -> sampling freq*/
samples[i].real=0.0; samples[i].imag=0.0;
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0; //imag components = 0 FFT(samples,PTS); //call function FFT.c
for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real
+ samples[i].imag*samples[i].imag);
}
} //end of main
C PROGRAM TO IMPLEMENT 8 POINT FFT :
#define PTS 64 //# of points for FFT
typedef struct {float real,imag;} COMPLEX;
extern COMPLEX w[PTS]; //twiddle constants stored in w
void FFT(COMPLEX *Y, int N) //input sample array, # of points
{
COMPLEX temp1,temp2; //temporary storage variables int i,j,k; //loop counter variables
int upper_leg, lower_leg; //index of upper/lower butterfly leg int leg_diff; //difference between
upper/lower leg
int num_stages = 0; //number of FFT stages (iterations) int index, step; //index/step through
twiddle constant i = 1; //log(base2) of N points= # of stages
do
{
num_stages +=1; i = i*2;
}while (i!=N);
leg_diff = N/2; //difference between upper&lower legs step
= (PTS*2)/N; //step between values in twiddle.h for (i = 0;i
< num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{
for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))
{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real; temp1.imag = (Y[upper_leg]).imag +
(Y[lower_leg]).imag; temp2.real = (Y[upper_leg]).real - (Y[lower_leg]).real; temp2.imag =
(Y[upper_leg]).imag - (Y[lower_leg]).imag; (Y[lower_leg]).real = temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag; (Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real; (Y[upper_leg]).real = temp1.real; (Y[upper_leg]).imag =
temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2; step *= 2;
}
j = 0;
for (i = 1; i < (N-1); i++) //bit reversal for resequencing data
{
k = N/2;
while (k <= j)
{
j = j - k; k = k/2;
}
j = j + k; if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag; (Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag; (Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}
C PROGRAM TO IMPLEMENT N POINT FFT :
#define PTS 64 //# of points for FFT
typedef struct {float real,imag;} COMPLEX;
extern COMPLEX w[PTS]; //twiddle constants stored in w
void FFT(COMPLEX *Y, int N) //input sample array, # of points
{
COMPLEX temp1,temp2; //temporary storage variables int i,j,k; //loop counter variables
int upper_leg, lower_leg; //index of upper/lower butterfly leg int leg_diff; //difference between
upper/lower leg
int num_stages = 0; //number of FFT stages (iterations) int index, step; //index/step through
twiddle constant i = 1; //log(base2) of N points= # of stages
do
{
num_stages +=1; i = i*2;
}while (i!=N);
leg_diff = N/2; //difference between upper&lower legs step
= (PTS*2)/N; //step between values in twiddle.h for (i = 0;i
< num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{
for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))
{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real;
temp1.imag = (Y[upper_leg]).imag + (Y[lower_leg]).imag; temp2.real = (Y[upper_leg]).real -
(Y[lower_leg]).real; temp2.imag = (Y[upper_leg]).imag - (Y[lower_leg]).imag; (Y[lower_leg]).real
= temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag; (Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real; (Y[upper_leg]).real = temp1.real; (Y[upper_leg]).imag =
temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2; step *= 2;
}
j = 0;
for (i = 1; i < (N-1); i++) //bit reversal for resequencing data
{
k = N/2; while (k <= j)
{
j = j - k; k = k/2;
}
j = j + k; if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag; (Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag; (Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}
RESULT: