You are on page 1of 19

DSP Programs

Ex.no:1 DATE:

GENERATION OF DISCRETE SIGNALS

AIM: To generate and plot all the given discrete elementary signals such as unit step, unit impulse, unit ramp, rectangular signal, exponential sequence, complex exponential sequence, exponential function, sinc function, random signal and Gaussian signal. ALGORITHM: Step 1: Start. Step 2: Get the necessary input from the user. Step 3: Compute each functions to get the discrete elementary signals. Step 4: Plot the signals with all necessary details. Step 5: Stop. PROGRAM: 1.UNIT STEP: %unit step x=0:15; y=[ones(1,16)]; stem (x,y) title('UNIT STEP') xlabel('SAMPLES n'); ylabel('y(n)'); 2.UNIT IMPULSE: x=[1 0 0 0 0]; y=0:1:4; stem (y,x); title('UNIT IMPULSE');

xlabel('SAMPLES n '); ylabel('y(n)'); 3.UNIT RAMP: %unit ramp x=0:10; m=1; c=0; y=m*x+c; stem (y,x); title('UNIT RAMP'); xlabel('SAMPLES n '); ylabel('y(n)'); 4.RECTANGULAR SIGNAL: x=-10:10; y=[zeros(1,5) ones(1,11) zeros(1,5)]; stem (x,y) axis([-10 10 0 2]); title('RECTANGULAR SIGNAL') xlabel('SAMPLES n '); ylabel('y(n)'); 5.EXPONENTIAL SEQUENCE: x=0:1:10; y=exp(x); stem (y) axis([0 10 0 8000]); title('EXPONENTIAL SEQUENCE') xlabel(SAMPLES n'); ylabel('y(n)'); 6.COMPLEX EXPONENTIAL SEQUENCE: x=1:6; y=1:6;

z=x+i*y; d=exp(z); stem (d) axis([-50 50 -150 10]) title('COMPLEX EXP') xlabel('SAMPLES n'); ylabel('y(n)'); 7.EXPONENTIAL FUNCTION: x=0:1:10; y=exp(x); stem (y) axis([0 10 0 8000]); title('EXPONENTIAL FUNCTION') xlabel('SAMPLES n'); ylabel('y(n)'); 8.SINC FUNCTION: for x=-30:1:30; y= sin(x)/ x; stem (x,y) title('SINC FUNCTION') xlabel('SAMPLES n'); ylabel('y(n)'); hold on; end 9.RANDOM SIGNAL: y=rand(1,20); stem(y) title('RANDOM SIGNAL') xlabel('SAMPLES n'); ylabel('y(n)'); 10.GAUSSIAN SIGNAL: x=25;

y=randn(x); stem(y); title('GAUSSIAN SIGNAL'); xlabel('SAMPLES n'); ylabel('y(n)'); RESULT: Thus discrete elementary signals were generated using Matlab.

EX.NO:2 LINEAR CONVOLUTION DATE: AIM: To compute linear convolution between two sequences using matlab. ALGORITHM: Step 1: start Step 2: get the first sequence x(n) through array. Step 3: get the second sequence h(n) through array. Step 4: get length of each sequence by using len() function. Step 5: then summation is did from zero to infinity. Step 6: in each summation product of x(n) sequence and h(n) sequence is computed and stored in the array y(n). Step 7: plot the output y(n) and getting its length of x and y-axis. Step 8: stop. PROGRAM: %linear convolution for the given input x=input('Enter the sequence x[n]:'); h=input('Enter the sequence h[n]:'); for n=2:1:(length(x)+length(h)) t=n-1;

sum=1; for k=1:1:length(h) if ((n>k)&&((n-k)<(length(x)+1))); sum=sum+(x(k)*h(n-k)); z(t)=sum; end end hold on; title('LINEAR CONVOLUTION'); xlabel('n'); ylabel('y(n)'); stem(t,z(t)); end

SAMPLE INPUT AND OUTPUT: Enter the sequence x[n]:[1 1 1 1 -1 -1 -1 -1] Enter the sequence h[n]:[0 1 2 3 4 3 2 1] Resultant sequence [1 2 4 7 11 12 10 5 -3 -8 -10 -9 -5 -2 0] CONCULSION: Thus linear convolution of twp given sequence is computed using matlab.

EX.NO:3 CIRCULAR CONVOLUTION DATE: (TIME DOMAIN)

AIM: To compute circular convolution between two input sequence using matlab. ALGORITHM: Step 1: start Step 2: get the first sequence x1(n) through array. Step 3: get the second sequence x2(n) through array. Step 4: get both sequence are of equal length. Step 5: now rotate the second sequence in anticlockwise direction then do product and summation of two to get the circular convolution. Step 6: display the output of circular convolution y(m). Step 7: stop.

PROGRAM: %circular convolution of two given sequence x1=input('Enter the first sequence x1[n]'); x2=input('Enter the second sequence x2[n]'); N=length(x1); display('Result of circular convolution'); for m=1:N sum=0; for k=1:N if((m-k)>=0) n=m-k+1; else n=m-k+N+1; end sum=sum+x1(k)*x2(n); end disp(sum); end

SAMPLE INPUT AND OUTPUT: Enter the first sequence x1[n][2 1 2 1] Enter the second sequence x2[n][1 2 3 4] Result of circular convolution 14 16 14 16

CONCLUSION: Thus circular convolution of two sequences was computed in matlab.

EX.NO:4 CIRCULAR CONVOLUTION DATE: (USING DFT) AIM: To compute circular convolution using frequency domain between two input sequence using matlab.

ALGORITHM: Step 1: start Step 2: get the first sequence x1(n) through array. Step 3: get the second sequence x2(n) through array. Step 4: check weather both the sequence has the equal number of elements in the array. Step 5: depending up on the condition number of zeros should be added.

Step 6: take fft for the obtained input datas. Step 7: multiply the obtained fft signal and store it in separate array. Step 8: take ifft for the output . Step 9: display and plot output value. Step 10:stop. PROGRAM: close all; clear all; x1=input('enter the first sequence'); lenx1=length(x1); x2=input('enter the second sequence'); lenx2=length(x2); if (lenx2 > lenx1) x1=[x1,zeros(1,lenx2-lenx1)]; else if (lenx2 < lenx1) x2=[x2,zeros(1,lenx1-lenx2)]; end end len=lenx1; y1=fft(x1); y2=fft(x2); for k=1:1:len; y3(k)=y1(k)*y2(k); end z=ifft(y3); disp('output sequence'); disp(z); subplot(1,2,1); title('real part'); stem(real(z)); subplot(1,2,2);

title('imaginary part'); stem(imag(z)); SAMPLE INPUT AND OUTPUT: Enter the first sequence [1 2 3 4] Enter the second sequence [1 2 3 4] Output sequence 26 28 26 20 CONCLUSION: Thus computed the circular convolution of two sequences using dft.

EX.NO:5 DATE:

SAMPLING

AIM: To compute the sampling for the given input sequence using matlab. ALGORITHM: Step 1: start Step 2: get the input sampling frequency. Step 3: generate the continuous signal for the specific frequency. Step 4: get, number of samples N. Step 5: generate the sampling for the frequency y=sin(Wc*n*ts). Step 6: obtain fft for the obtained output signal. Step 7: stop.

PROGRAM: %SAMPLING clear all; close all; fs=input('Enter the sampling frequency'); N=input('enter the number of samples'); ts=1/fs; n=1:1:N; y=sin(2*pi*10*n*ts); subplot(2,1,1); b=n-1; stem(b,y); title('signal to be sampled'); k=1:1:N; z=fft(y,32); a=k-1; subplot(2,1,2); stem(a,real(z)); title('sampled signal'); disp(real(z));

SAMPLE INPUT AND OUTPUT: Enter the sampling frequency [64] Enter the number of samples [32] Columns 1 through 8 -0.0000 0.0000 0.0000 -0.0000 -0.0000 13.3035 Columns 9 through 16 0.0000 -0.0000 0.0000

0.0000

0.0000

0 -0.0000

0.0000

0.0000

0.0000

Columns 17 through 24 0.0000 0.0000 0.0000

0.0000 -0.0000

0.0000 -0.0000

Columns 25 through 32 0.0000 0.0000 0.0000 13.3035 -0.0000 -0.0000

0.0000

0.0000

CONCLUSION: Thus computed the sampling of the given input signal using matlab.

EX.NO:6 FAST FOURIER TRANSFORM 8-POINT DATE:

AIM: To obtain Fast Fourier Transform(FFT) of 8-point for the given input sequence.

ALGORITHM: Step 1: start. Step 2: get the sequence x(n) and find its length. Step 3: calculate its twiddle factor by using complex function. Step 4: do bit reversal of the given sequence x(n). Step 5: compute first stage by applying proper twiddle factor. Step 6: sequentially compute for second and third stage. Step 7: the result is kept in order in the third stage and displayed. Step 8: stop. PROGRAM:

%fast Fourier transform x=input ('Enter the sequence x[n]'); N=length(x); %calculating twiddle factor for i=1:1:N/2 y(i)=complex(cos((2*pi*(i-1))/N),-sin((2*pi*(i-1))/N)); end display(y); %first stage for i=1:1:4 x1(i)=x(i)+(y(1)*x(i+4)); t=i+4; x1(t)=x(i)-(y(1)*x(i+4)); end display('Result of first stage');x1 %second stage for i=1:2 x2(i)=x1(i)+(y(1)*x1(i+2)); t=i+2; x2(t)=x1(i)-(y(1)*x1(i+2)); end for s=5:6 x2(s)=x1(s)+(y(3)*x1(s+2)); d=s+2; x2(d)=x1(s)-(y(3)*x1(s+2)); end display('Result of second stage');x2 %third stage v=1;x=1;s=1; for i=1:1:4 x3(i)=x2(s)+(y(v)*x2(s+1)); v=v+1; if (v==2)||(v==4)

s=s+4; elseif v==3 s=s-2; end end v=1;x=1;s=1; for i=5:1:8 x3(i)=x2(s)-(y(v)*x2(s+1)); v=v+1; if (v==2)||(v==4) s=s+4; elseif v==3 s=s-2; end end display('Result of third stage');x3

SAMPLE INPUT AND OUTPUT: Enter the sequence x[n] [1 2 3 0 0 0 0 0] Twiddle factor y= 1 0.707-0.707i 0 1i -0.707-0.707i Result of first stage x1 = 1 2 3 0 1 2 3 0

Result of second stage

x2 = 4 2 -2 2 1- 3i 2 1+ 3i 2 Result of third stage x3 = 6 2.41-4.41i -2-2i -0.41+1.58i 2 -0.41-1.58i -2+2i 2.41+4.41i

RESULT: Thus the fast fourier transform of 8-point has been written and executed successfully using matlab.

Ex. no: 7 FIR FILTER DESIGN USING WINDOWS DATE:

AIM: To design a Linear Phase FIR Filter using Windows.

ALGORITHM: Step 1: Start. Step 2: Obtain the desired filter function Hd(n) by computing inverse Fourier transform of the desired frequency response Hd(w). Step 3: Obtain the various window functions w(n). Step 4: Obtain the filter response using the formula h(n) =hd(n)*w(n). Step 5: Plot the various frequency response. PROGRAM:

clear all; close all; for n=-10:1:10 if n==0 t=1; else t=(sin(pi*n*0.15))/(pi*n); end hd(n+11)=.15*(exp(j*.15*pi*n)*t); stem(n,hd(n+11)); title('DESIRED RESPONSE') xlabel('SAMPLES n'); ylabel('Hd(n)'); hold on; end %hamming figure; for n=-10:1:10 y(n+11)=0.54+0.46*cos(2*pi*(n)/21); subplot(2,3,1); stem(n,y(n+11)); title('HAMMING WINDOW'); xlabel('SAMPLES n'); ylabel('Whm(n)'); hold on; end for n=-10:1:10 h1(n+11)=hd(n+11)*y(n+11); subplot(2,3,4); stem(n,h1(n+11)); title('FILTER RESPONSE') xlabel('SAMPLES n'); ylabel('h(n)');

hold on; end %hanning for n=-10:1:10 z(n+11)=0.5+0.5*cos(2*pi*(n)/21); subplot(2,3,2); stem(n,z(n+11)); title('HANNING WINDOW'); xlabel('SAMPLES n'); ylabel('Whn(n)'); hold on; end for n=-10:1:10 h2(n+11)=hd(n+11)*z(n+11); subplot(2,3,5); stem(n,h2(n+11)); title('FILTER RESPONSE') xlabel('SAMPLES n'); ylabel('h(n)'); hold on; end %rectangle for n=-10:1:10 a(n+11)=1; subplot(2,3,3); stem(n,a(n+11)); title('RECTANGULAR WINDOW'); xlabel('SAMPLES n'); ylabel('Wr(n)'); hold on; end for n=-10:1:10 h3(n+11)=hd(n+11)*a(n+11);

subplot(2,3,6); stem(n,h3(n+11)); title('FILTER RESPONSE') xlabel('SAMPLES n'); ylabel('h(n)'); hold on; end CONCLUSION: Thus designed linear phase FIR filters using Windows.

Ex.no.8: IIR FILTER DESIGN DATE:

AIM: To design a low pass Butterwort filter for the given specifications.

ALGORITHM: Step 1: Start Step 2: Get the necessary input details from the user. Step 3: Compute the required details by using the appropriate functions. Step 4: Plot the frequency and phase response of the output. Step 5: Stop.

PROGRAM: %IIR filter design

clear all; close all; clc; fp=input('passband frequency '); fs=input('stopband frequency '); ap=input('passband ripple '); as=input('stopband attenuation '); F=input('sampling frequency '); wp=2*pi*fp; ws=2*pi*fs; [n,wn]=buttord(wp,ws,ap,as,'s'); disp('order of filter is'); disp(n) disp('cutoff frequency'); disp(wn); [num,den]=butter(n,wn,'s'); [b,a]=bilinear(num,den,F); disp('b='); disp(b); disp('a=') disp(a); freqz(b,a,512,F);

SAMPLE INPUT AND OUTPUT: Passband frequency 200 Stopband frequency 300 Passband ripple 1.25 Stopband ripple 15 Sampling frequency 2000

OUTPUT: Order of filter is 6 Cutoff frequency 1.4173e+003 b= 0.0005 0.0031

0.0077

0.0103

0.0077

0.0031

0.0005

a= 1.0000 -3.3775 5.1136 -4.3287 2.1386 -0.5805 0.0673 RESULT: Thus designed a low pass Butterworth filter using Bilinear transformation

You might also like