Saturday, 12 April 2014

                                        DSP PRACTICALS


Q1:Write a program in Matlab to implement linear convolution of two signals ?
Ans:-
     Code:

clc;
x1=[-1 0 1/2 5 ];
h1=[2 3 5];
m=6;
n=2;
x=[x1,zeros(1,n)];
h=[h1,zeros(1,m)];
for i=1:m+n-1
y(i)=0;
for j=1:m
    if(i-j+1>0)
        y(i)=y(i)+x(j)*h(i-j+1);
    else
    end
end
end
y;
subplot(2,2,1)
stem(x1);
xlabel('n');
ylabel('amplitude');
title('x1');
subplot(2,2,2)
stem(h1);
xlabel('n');
ylabel('amplitude');
title('h1');
subplot(2,2,3)
stem(y);
xlabel('n');
ylabel('y(n)');
title('covolution w/o using inbuilt function');
z=conv(x1,h1);
subplot(2,2,4)
stem(z);
xlabel('n');
ylabel('y(n)');
title('using inbuilt function');






Output:-




Q2:Write a program to implement circular convolution of two signals ?
Ans:-
   Code:
clc;
x=[1 3 5 ];
h= [2 4 6 7 ];
N1= length(x);
N2= length(h);
N=max(N1,N2);
x=[x zeros(1,N-N1)]; 
h=[h zeros(1,N-N2)]; 

for n=0:N-1;
    y(n+1)=0;
    for i=0:N-1
       j=mod(n-i,N);
       y(n+1)=y(n+1)+x(i+1)*h(j+1); 
    end
end

n=1:N;
disp('output sequence of circular convolution');
disp(y);
subplot(2,2,4)
stem(n,y);
xlabel('n');
ylabel('Y(n)');
title('circular convolution w/o using inbuilt function')
subplot(2,2,1)
stem(x);
xlabel('n');
ylabel('amplitude');
subplot(2,2,2)
stem(h);
xlabel('n');
ylabel('amplitude');
z=cconv(x,h,N);
subplot(2,2,3)
stem(z);
xlabel('n');
ylabel('Y(n)');
title('circular covolution using inbuilt function')

Output:-

Result/observation:I had learned to use the MATLAB tool and its various applications. 


Q:Design an low pass fir filter using hanning window of order 11 having following specification:
pass band cut off frequency=250 Hz
sampling frequency=1000 Hz
fp=250 Hz
Fs=1000 Hz

PROGRAM:
clc
clear all;
close all;
syms w
H=input('enter value of H');
N=input('order of filter');
a=input('enter lower limit');
b=input('enter upper limit');
hd=0;
for n=1:N
    hd(n)=(1/(2*pi))*int(H*exp(j*w*(n-1)),a,b)
end
h=hd.*rishhann(N)
figure(1)
plot(h)
figure(2)
freqz(h)
pause
a=6

FUNCTION WINDOW:
PROGRAM1:
function  [w] = rishhamm(N)
w=0;
for n=1:N
w(n)=.54-.46*cos(2*pi*(n-1)/(N-1))
end
end

PROGRAM 2:
function [w] = rishhann(N)
w=0;
for n=1:N
    w(n)=.5-.5*cos(2*pi*(n-1)/(N-1))
end
end
PROGRAM 3:
function [w] = rishrect(N)
w=0;
for n=1:N
    w(n)=1                        
end
end

OUTPUT:
enter value of Hexp(-5*j*w)
order of filter11
enter lower limit-1.57
enter upper limit1.57

hd =

    0.0637


hd =

    0.0637   -0.0003


hd =

    0.0637   -0.0003   -0.1061


hd =

    0.0637   -0.0003   -0.1061    0.0003


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997    0.3183


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997    0.3183    0.0003


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997    0.3183    0.0003   -0.1061


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997    0.3183    0.0003   -0.1061   -0.0003


hd =

    0.0637   -0.0003   -0.1061    0.0003    0.3183    0.4997    0.3183    0.0003   -0.1061   -0.0003    0.0637


w =

     0


w =

         0    0.0955


w =

         0    0.0955    0.3455


w =

         0    0.0955    0.3455    0.6545


w =

         0    0.0955    0.3455    0.6545    0.9045


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000    0.9045


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000    0.9045    0.6545


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000    0.9045    0.6545    0.3455


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000    0.9045    0.6545    0.3455    0.0955


w =

         0    0.0955    0.3455    0.6545    0.9045    1.0000    0.9045    0.6545    0.3455    0.0955         0


h =

         0   -0.0000   -0.0367    0.0002    0.2879    0.4997    0.2879    0.0002   -0.0367   -0.0000         0




AIM: Program to find cross corelation.

 Q:To do the reverse of : x=[1 3 2 5]
                                        y=[5 3 2 1]
              ans: Program:
                     
       x=[1 3 2 5];
       l=length(x)
        for i=1:l
         y(i)=x(l)
         l=l-1;
          end
        Output:

           l =

                 4


           y =

                5


           y =

               5     2


          y =

              5     2     3


           y =

              5     2     3     1



           Q:To find out cross co relation of:x=[1 3 2 7]
                                                              h=[1 2 3 7]

             ans:x=[1 2 3 4]
                  h=[1 3 4 2]
                  z=xcorr(x,h)
                  k=length(h)
                  y=0
                 for i=k:-1:1
                 y(k-i+1)=h(i)
                 end
                 h=y
                n1=length(x)
                n2=length(h)
                n=n1+n2-1
                x=[x zeros(1,n-n1)]
                h=[y zeros(1,n-n2)]
               for i=1:1:n
               y(i)=0
              for j=1:1:n
              if(i-j+1>0)
              y(i)=y(i)+x(j)*h(i-j+1)
           
         end
     end
  end

            subplot(2,2,1)
            stem(x)
            subplot(2,2,2)
            stem(h)
            subplot(2,2,3)
            stem(z)
            subplot(2,2,4)
            stem(y)
            subplot(2,2,3)


output:


x =

     1     2     3     4


h =

     1     3     4     2


z =

    2.0000    8.0000   17.0000   27.0000   27.0000   15.0000    4.0000


k =

     4


y =

     0


y =

     2


y =

     2     4


y =

     2     4     3


y =

     2     4     3     1


h =

     2     4     3     1


n1 =

     4


n2 =

     4


n =

     7


x =

     1     2     3     4     0     0     0


h =

     2     4     3     1     0     0     0


y =

     0     4     3     1


y =

     2     4     3     1


y =

     2     0     3     1


y =

     2     4     3     1


y =

     2     8     3     1


y =

     2     8     0     1


y =

     2     8     3     1


y =

     2     8    11     1


y =

     2     8    17     1


y =

     2     8    17     0


y =

     2     8    17     1


y =

     2     8    17     7


y =

     2     8    17    19


y =

     2     8    17    27


y =

     2     8    17    27     0


y =

     2     8    17    27     0


y =

     2     8    17    27     2


y =

     2     8    17    27    11


y =

     2     8    17    27    27


y =

     2     8    17    27    27


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     3


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4


x =

     1     2     3     4


h =

     1     3     4     2


z =

    2.0000    8.0000   17.0000   27.0000   27.0000   15.0000    4.0000


k =

     4


y =

     0


y =

     2


y =

     2     4


y =

     2     4     3


y =

     2     4     3     1


h =

     2     4     3     1


n1 =

     4


n2 =

     4


n =

     7


x =

     1     2     3     4     0     0     0


h =

     2     4     3     1     0     0     0


y =

     0     4     3     1


y =

     2     4     3     1


y =

     2     0     3     1


y =

     2     4     3     1


y =

     2     8     3     1


y =

     2     8     0     1


y =

     2     8     3     1


y =

     2     8    11     1


y =

     2     8    17     1


y =

     2     8    17     0


y =

     2     8    17     1


y =

     2     8    17     7


y =

     2     8    17    19


y =

     2     8    17    27


y =

     2     8    17    27     0


y =

     2     8    17    27     0


y =

     2     8    17    27     2


y =

     2     8    17    27    11


y =

     2     8    17    27    27


y =

     2     8    17    27    27


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     0


y =

     2     8    17    27    27     3


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     0


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4


y =

     2     8    17    27    27    15     4




AIM: Representing signals and waveform generation using MATLAB.

Q1: Draw a unot step function with in the time range of -10 to 16?
Program: a=-10:1:16
y=[zeros(1,10) ones(1,13)]
plot(y)
title('unit step signal')
xlabe('time')
ylabel('amplitude')
subplot(2,2,3)

output :                                                        

Q2:Draw the signal for 2hz sine wave for first three second and for 1hz for next two second?
Program:  t1=0:.01:3
y1=sin(2*pi*2*t1)
t2=0:.01:2
y2=sin(2*pi*1*t2)
y=[y1 y2]
plot(y)

Output:
          

Q3:Draw signal;
X=5sin20t+2sin10t
Program:  t=0:.01:15
a=5*sin(20*t)
b=2*sin(10*t)
c=a+b
plot(t,c)
  
output:
Result/Observations:for the following questions implemented on the editors window of MATLAB i get the output and learned to use the various tools of matlab.

Q: Design a low pass FIR filter using fourier transform
order of filter=27
pass band edge frequency=300Hz
sampling frequency=1000Hz

PROGRAM:
clc
close all;
clear all;
syms w;
N=input('order of filter');
H=input('enter the value');
c=input('lower limit');
d=input('higher limit');
fp=300;
fs=1000;
wc=(2*pi*(fp/fs));
for n=1:N
    hd(n)=(1/2*pi)*int(H*exp(j*w*(n-1)),c,d);
end
plot(hd)

OUTPUT:
order of filter27
enter the valueexp(-13*j*w)
lower limit-1.8
higher limit1.8

Q:Implement the program for DFT?
ANS: Program
function[y]=raj_idft(x)
x=[7 6 9 6 1 0 0 5 7 0];
N=length(x);
y=zeros(1,N);
for n=1:1:N
    y(n)=0;
    for k=1:1:N
     y(n)=[y(n)+(x(k)*exp(j*2*pi*(k-1)*(n-1)/N))];
    end
end
y=y/N;
subplot(3,1,1);
stem(x);
b=abs(y);
subplot(3,1,2);
stem(y);
end
ans =

  Columns 1 through 5

   4.1000             1.2590 + 0.6968i  -1.2680 + 0.5343i   0.1410 + 0.4894i   0.9680 + 0.1988i

  Columns 6 through 10

   0.7000 - 0.0000i   0.9680 - 0.1988i   0.1410 - 0.4894i  -1.2680 - 0.5343i   1.2590 - 0.6968i










No comments:

Post a Comment