Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

DSP FILE

AIM: Generation of Elementary Signals


CODE: //generation of unit-impulse signal
clear;
close;
clc;
L=5;
n=-L:L;
x=[zeros(1,L) 1 zeros(1,L)];
subplot(2,3,1)
plot2d3(n,x)

scf;
//unit-step signal
close;
L=8;
n=-L:L;
x=[zeros(1,L),ones(1,L+1)]
subplot(2,3,2)
plot2d3(n,x)

scf;
//ramp-signal
close;
L=8;
n=-L:L;
x=[zeros(1,L),0:L];
a=gca();
a.y_location="middle"
subplot(2,3,3)
plot2d3(n,x)

scf;
//exponential-signal
close;
L=8;
n=0:L;
a=0.7;
x=a^n;
subplot(2,3,4)
plot2d3(n,x)

scf;
//sine-cosine-signal
close;
L=8;
n=0:L;
x=sin(n);
subplot(2,3,5)
plot2d3(n,x)
AIM: Time-shifting and time-reversal
CODE:
// Time-shifting of a discrete time sequence
clear;close;clc;
x=input('Enter the sequence');
nx=input('Enter the time domain information of sequence');
k=input('enter the shift value');
nx1=nx+k;
subplot(2,1,1);
plot2d3(nx,x)
subplot(2,1,2);
plot2d3(nx1,x)

//Write a Scilab program to verify the Time Reversal


clear ;
clc ;
close ;
n = -3:3;
x =[0 1 2 5 3 6 0];
subplot (2,1,1);
plot2d3 (n,x);
xlabel ( 'n >' );
ylabel ( 'x(n) >' );
title('Original Discrete Time Sequence');
subplot (2,1,2);
plot2d3 (-n,x);
xlabel ( 'n >' );
ylabel ( 'x(n) >' );
title ('Time Reversal of x(n)');

AIM: Convolution using 'conv' function


// program for linear convolution
clc ;
clf ;
clear all;
x = input ( "Enter the first sequence" ) ;
y = input ( "Enter the second sequence" ) ;
n = convol (x , y ) ;
subplot (2 ,2 ,1) ;
plot2d3 ( x );
title ( "first sequenc" ) ;
xlabel ( "n−−−−−−>" ) ;
ylabel ( "amp−−−−>" ) ;
subplot (2 ,2 ,2) ;
plot2d3 ( y );
title ( "second sequence" ) ;
xlabel ( "n−−−−−−>" ) ;
ylabel ( "amp−−−−>") ;
subplot (2 ,2 ,3) ;
plot2d3 ( n );
title ( "convolved sequence" ) ;
xlabel ( "n−−−−−−>" ) ;
ylabel ( "amp−−−−>" ) ;
disp ( "The convoluted sequence") ;
disp ( n );

AIM: Convolution without using 'conv' function


clc ;
clear ;
close ;

x=input('ENTER THE FIRST SEQUENCE x(n): ');


m=length(x);
h=input('ENTER THE SECOND SEQUENCE h(n): ');
n=length(h);

//x(n)
p=0:1:m-1;

subplot(3,2,1),
a = gca();
a.x_location = "origin";
a.y_location = "origin";
plot2d3('gnn',p,x) //plot for x(n)
title('i/p sequencce x(n) is:');
xlabel('-->n');

x=[x,zeros(1,n)];

//h(n)
q=0:1:n-1;

subplot(3,2,2),
a = gca();
a.x_location = "origin";
a.y_location = "origin";
plot2d3('gnn',q,h) //plot for h(n)
title('i/p sequencce h(n) is:');
xlabel('-->n');

h=[h,zeros(1,m)];

//convoluted signal
y=zeros(1,m+n-1);
for i=1:m+n-1
y(i)=0;
for j=1:m+n-1
if(j<i+1)
y(i)=y(i)+x(j)*h(i-j+1);
end
end
end

r=0:1:m+n-2;

subplot(3,1,3)
a = gca ();
a.x_location = "origin";
a.y_location = "origin";
plot2d3('gnn',r,y) //plot of convoluted signal
title('convolution of x(n) & h(n) is :');
xlabel('-->n');
disp ( "The convoluted sequence") ;
disp ( y );

AIM: Correlation of two sequences


//Program 6 - To find the correlation of signals

//In convolution, we shift and multiply but here we dont flip

//Resulting seq will have length = sum of length of both the sequences - 1

//Starting point = nx initial point - ny last point

//Ending point = last pt of nx - first value of ny

clc ;
clear ;
close ;
x = input('Enter the Input Sequence =') // x=[1 2 3 1 ]
m = length(x);
xl = input('Enter the lower index of Input Sequence= ' )// 0
xh = xl +m -1;
n = xl:1:xh;
subplot(2,2,1);
a=gca();
a.x_location ='origin';
a.y_location ='origin';
plot2d3('gnn',n,x) ;
title('Input Sequence x[n]');
xlabel('Samples n');
ylabel('Amplitude');

h=input ('Enter the Impulse response Sequence = ') ; //h=[1 2 1 1 ]


l=length(h);
hl=input('Enter the lower index of impulse response sequence = ') ; // 0
hh = hl +l -1;
g = hl:1:hh ;
subplot(2 ,2 ,2) ;
a = gca() ;
a.x_location ='origin';
a.y_location ='origin';
plot2d3 ('gnn',g,h) ;
title ('Impulse Response Sequence h[n]') ;
xlabel ('Samples n') ;
ylabel ('Amplitude') ;

z = xcorr (x , h ) ;
disp('Correlation of Sequence y(n)= ');
disp(z);
nx = xl + xl ;
nh = xh + xh ;
r = nx : nh ;
subplot(2,2,4);
a=gca();
a.x_location = 'origin';
a.y_location = "origin";
plot2d3 ('gnn',r,z) ;
title ('Output of Correlation of Sequence');
xlabel('Samples n');
ylabel('Amplitude');

AIM: Correlation in RADAR

//
applicati
on of
correlati
on-RADAR
clc ;
clear ;
close ;

x =[0 1 2 3 2 1 0]; // Triangle pulse transmitted by radar


n =[ -3 -2 -1 0 1 2 3]; // Index of Triangular Pulse
D =10; // Delay amount
nd = n+ D ; // Index of Delayed Signal
y = x ; // Delayed Signal

scf () ;
subplot (2 ,1 ,1) ;
plot2d3(n,x,0.1);
title ( 'Original Transmitted Signal','color','red','fontsize',4) ;
xlabel ("Index","fontsize",2,"color","blue") ;
ylabel ("Amplitude","fontsize",2,"color","blue") ;
subplot (2 ,1 ,2) ;
plot2d3(nd,y,0.1);
title ( 'DelayedSignal','color','red','fontsize',4) ;
xlabel ("Index","fontsize",2,"color","blue") ;
ylabel ("Amplitude","fontsize",2,"color","blue") ;

w = rand (1 , length (x ) ) ; // Noise Generation


nw = nd ;
scf () ;
plot2d3(nw,w,0.1);
title ( 'Noisy Signal','color','red','fontsize',4) ;
xlabel ("Index","fontsize",2,"color","blue") ;
ylabel ("Amplitude","fontsize",2,"color","blue") ;

R = y + w; // Original Signal + Noise


nr = nw ; // Index of received signal at RADAR

nr_fold = flipdim ( nr,2 ) ;


R_fold = flipdim (R,2 ) ;
nmin =min( n ) + min ( nr_fold ) ; // Lowest index of y(n)
nmax =max( n ) + max ( nr_fold ) ; // Highest index of y(n)
n_received = nmin : nmax ;
Received_Presence = xcorr (x , R_fold ) ; // Convolution of Original signal
and Received Signal in the Presence of Object(Equivalent to Correlation) //
scf () ;
subplot (2 ,1 ,1) ;
plot2d3(n_received , Received_Presence ,0.1);
title ( 'Correlation in the Presence of Object','color','red','fontsize',4)
;
xlabel ("Index","fontsize",2,"color","blue") ;
ylabel ("Correlation Value","fontsize",2,"color","blue") ;

R = w ; // only Noise Signal


nr = nw ;

nr_fold = flipdim ( nr,2 ) ;


R_fold = flipdim (R,2 ) ;
nmin =min( n ) + min ( nr_fold ) ; // Lowest index of y(n)
nmax =max( n ) + max ( nr_fold ) ; // Highest index of y(n)
n_received = nmin : nmax ;
Received_Absence = xcorr (x , R_fold ) ; // Convolution of Original
transmitted signal and Received Signal in the Absence of Object(Equivalent
to Correlation) //

subplot (2 ,1 ,2) ;
plot2d3(n_received , Received_Absence ,0.1);
title ( 'Correlation in the Absence of
Object','color','red','fontsize',4) ;
xlabel ("Index","fontsize",2,"color","blue") ;
ylabel ("Correlation Value","fontsize",2,"color","blue");
AIM: FFT
//FFT of a Sequence
clc;
clear all;
close;
x = input("Enter the sequence = ")
X = fft(x)
disp("The FFT is = ", X)
plot(X)

AIM: Discrete Fourier Transform


// Caption : Program to find the spectral information of discrete time signal
clc ;
close ;
clear ;
xn = input ( "Enter the real input discrete sequence x[n]=") ;
N = length ( xn ) ;
XK = zeros (1 , N ) ;
IXK = zeros (1 , N ) ;
// Code block to find the DFT of the Sequence
for K = 0: N -1
for n = 0: N -1
XK ( K +1) = XK ( K +1) + xn ( n +1) *exp( - %i *2* %pi * K * n /N ) ;
end
end
[phase,db] = phasemag(XK)
disp ( "Discrete Fourier Transform X( k )= ", XK )
disp ( " Magnitude Spectral Sample s= " ,abs( XK ))
disp ( " Phase Spectral Sample s= ", phase ,)
n = 0:N -1;
K = 0:N -1;
subplot (2 ,2 ,1)
a = gca () ;
a.x_location = "origin";
a.y_location = "origin";
plot2d3 ( "gnn" ,n , xn )
xlabel ( " Time I n d e x n−−−−> " )
ylabel ( " Ampli tude xn−−−−> " )
title ( " D i s c r e t e I n p u t S e q u e n c e " )
subplot (2 ,2 ,2)
a = gca () ;
a.x_location = "origin";
a.y_location = "origin";
plot2d3 ( "gnn" ,K ,abs( XK ) )
xlabel ( " F r e q u e n c y Sample I n d e x K−−−−> " )
ylabel ( " |X(K)|−−−−> " )
title ( " Magni tude Spec t rum " )
subplot (2 ,2 ,3)
a = gca () ;
a.x_location = "origin";
a.y_location = "origin";
plot2d3 ( "gnn" ,K , phase )
xlabel ( " F r e q u e n c y Sample I n d e x K−−−−> " )
ylabel ( "<X(K) i n r a di a n s −−−−> " )
title ( " Phase Spec t rum " )
[]
AIM: Inverse Discrete Fourier Transform
for n = 0: N -1
for K = 0: N -1
IXK ( n +1) = IXK ( n +1) + XK ( K +1) * exp ( %i *2* %pi * K *n/ N ) ;
end
end
IXK = IXK / N;
ixn = real(IXK) ;
subplot (2 ,2 ,4)
a = gca () ;
a.x_location = "origin";
a.y_location = "origin";
plot2d3 ( "gnn",[0:N-1] , ixn )
xlabel ( " Discrete Time Index n −−−−> " )
ylabel ( " Amplitude x [ n]−−−−> " )
title ( " IDFT s e q u e n c e " )

AIM: Circular Convolution


[]//PROGRAMTOCOMPUTECIRCULARCONVOLUTIONOFTWOSEQUENCESUSINGBASICEQUATION
clc;
clear;
close;
x=input("Ent[]ertheinputsequence=")
//x=[1122]
m=length(x);
xl=input("Enterthelowerindexofinputsequence=")//0
xh=xl+m-1;
n=xl:1:xh;
subplot(3,1,1);
a=gca();
a.x_location="origin";
a.y_location="origin";
a.foreground=5;
a.font_color=5;
a.font_style=5;
plot2d3("gnn",n,x);
title("InputSequencex[n]");
xlabel("Samplesn");
ylabel("Amplitude");
h=input("Entertheimpulseresponsesequence=");//h=[1234]
l=length(h);
hl=input("Enterthelowerindexofimpulseresponse=");//0
hh=hl+l-1;
g=hl:1:hh;
subplot(3,1,2);
a=gca();
a.x_location="origin";
a.y_location="origin";
a.foreground=5;
a.font_color=5;
a.font_style=5;
plot2d3("gnn",g,h);
title("ImpulseResponseSequenceh[n]");
xlabel("Samplesn");
ylabel("Amplitude");

//formakinglengthofbothsignalsequal
N = max(m,l) ;
p=m-l;
if(p>=0) then
h =[h, zeros(1 , p)];
else
x =[x, zeros(1 , -p)];
end
for i=1:N
y(i)=0;
for j=1:N
k=i-j+1;
if(k<=0)
k= k+N ;
end
y(i)=y(i)+(x(j)*h(k)) ;
end
end
disp("Circularconvolutionbyequationisy[n]:");
disp(y);
nx=xl+hl;
r=nx:length(y)-1;
subplot(3,1,3);
a=gca();
a.x_location="origin";
a.y_location="origin";
a.foreground=5;
a.font_color=5;
a.font_style=5;
plot2d3("gnn",r,y);
title("OutputResponseSequenceofCircularConvolutiony[n]usingBasicEquation");
xlabel("Samplesn");
ylabel("Amplitude");
AIM: FIR filter design using window method
clc ;
clear ;
xdel ( winsid () ) ;
fc = input (" Enter Analog cut off freq in Hz=")
fs = input (" Enter Analog sampling Freq . in Hz=")
M = input (" Enter o r d e r of f i l t e r =")
w = (2* %pi ) *( fc / fs );
disp (w , "Dig i t a l cut off freq u e n c y i n r a d i a n s . c y c l e s /s am pl e s " ) ;
wc = w/ %pi ;
disp ( wc , " Normalized digital cut off frequency in cycles/ samples " ) ;
[wft,wfm,fr]=wfir("lp",M+1,[wc/2,0],"re",[0,0]) ;
disp ( wft , "Impulse Response of LPF FIR Filter: h [ n]=" );
// P l o t t i n g t h e Magni tude R e s po n s e of LPF FIR F i l t e r
subplot (2 ,1 ,1)

plot (2* fr , wfm )


xlabel ( " No rmali z e d D i g i t a l Freq u e n c y w−−−> " )
ylabel ( " Magni tude |H(w)|= " )
title ( " Magni tude R e s po n s e of FIR LPF " )
xgrid (1)
subplot (2 ,1 ,2)
plot ( fr * fs , wfm )
xlabel ( " Analog Freq u e n c y i n Hz f −−−> " )
ylabel ( " Magni tude |H(w)|= " )
title ( " Magni tude R e s po n s e of FIR LPF " )
xgrid (1)
AIM: IIR filter design
clc ;
clear ;
xdel ( winsid () ) ;
fc = input ( " Enter cut off freq in Hz f c = " )
fs = input ( " Enter sampling freq in Hz f s = " )
N = input ( " Enter order of Butterworth filter N = " )
Fp = 2* fc / fs ;
// Pa s s band e dg e f r e q u e n c y i n c y c l e s /sam pl e s
[ Hz ]= iir(N , "lp" , "butt" ,[ Fp /2 ,0] ,[0 ,0])
// d i g i t a l I IR B u t t e r wo r t h F i l t e r
[ Hw , w ] = frmag ( Hz ,256) ;
subplot (2 ,1 ,1)
plot (2* w , abs ( Hw ) );
xlabel ( " No rmali z e d D i g i t a l F r e q u e n c y w−−−> " )
ylabel ( " Magnitude |H(w)|= " )
title ( " Magnitude R e s po n s e of I IR LPF " )
xgrid (1)

subplot (2 ,1 ,2)
plot (2* w * fs , abs ( Hw ) ) ;
xlabel ( " Analog F r e q u e n c y i n Hz f −−−> " )
ylabel ( " Magnitude |H(w)|= " )
title ( " Magnitude R e s po n s e of I IR LPF " )
xgrid (1)

You might also like