Sunday 20 October 2013

Digital Signal Processing lab interfacing programs

Digital Signal Processing lab interfacing programs...

To verify, Download C,C++ compiler from
https://www.facebook.com/groups/163904047131828/


alt+c   compile
alt+r   run
alt+f open file functions
alt+enter   exit full screen mode..

Note:1: Copy the programs and save as  filename.c  at  C:\TurboC++\Disk\TurboC3\BIN
          and directly run the programs from compiler.
         2:While running these progrms on CCStudio,Linux remove following commands from program.
                    a)#include<conio.h>
                    b)clrscr();
                    c)getch();

Expt no.1
Linear convolution


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{

float x[15],y[15],h[15];
int i,j,m,n;
clrscr();

printf("\n Enter the value of m:");
scanf("%d",&m);
printf("\n Enter the first sequence:");
for(i=0;i<m;i++)
scanf("%f",&x[i]);

printf("\n Enter the value of n:");
scanf("%d",&n);
printf("\n Enter the second sequence:");
for(i=0;i<n;i++)
scanf("%f",&h[i]);

for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("\n Linear convolution =%f\n",y[i]);
getch();

 }

Expt no.2
Circular convolution


#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int m,n,i,j,k;
float x[30],h[30],y[30],x2[30],a[30];
clrscr();
printf("Enter the length of the first sequence:\n");
scanf("%d",&m);
printf("Enter the first sequence:\n");
for(i=0;i<m;i++)
scanf("%f",&x[i]);
printf("Enter the length of the second sequence:\n");
scanf("%d",&n);
printf("Enter the second sequence:\n");
for(j=0;j<n;j++)
scanf("%f",&h[j]);
/*If length of both sequences are not equal*/
if(m-n!=0)
{
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];
for(j=1;j<n;j++) /*folding h(n) to h(-n)*/
a[j]=h[n-j];
/*Circular convolution*/
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
/*circular shift*/
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
/*displaying the result*/
printf(" the circular convolution is\n");
for(i=0;i<n;i++)
printf("%f ",y[i]);
getch();
}


Expt no.3
N point DFT

#include <stdio.h>
#include <math.h>
#include<conio.h>

void dft(int *x, short k, float *real, float *imaginary);
float pi = 3.141592654;
float real[10],imaginary[10]; //Output results
int index=0;
int N,x[10]={0};

void main()
{
 short i,j;
 clrscr();
 printf("\nEnter the length of the input sequence:");
 scanf("%d",&N);
 printf("\nEnter the input sequence:");
 for(i=0;i<N;i++)
 scanf("%d",&x[i]);
  printf("\n The %d point DFT of sequence is \n",N);
 for (j = 0; j < N; j++)
    {
     dft(x,j,real,imaginary);                 //call DFT function
    }
}

void dft(int *x, short k, float *real, float *imaginary)  //DFT function
{
 float sumRe = 0;                        //initialize real component
 float sumIm = 0;                        //initialize imaginary component
 int i = 0;
 float cs = 0;                         //initialize cosine component
 float sn = 0;                          //initialize sine component

 for (i = 0; i < N; i++)                 //for N-point DFT
    {
      cs = cos(2*pi*(k)*i/N);           //real component
      sn = sin(2*pi*(k)*i/N);           //imaginary component
      sumRe = sumRe + x[i]*cs;        //sum of real components
      sumIm = sumIm - x[i]*sn;        //sum of imaginary components
    }
    real[index]=sumRe;
    imaginary[index]=sumIm;
 index++;

 printf("\n%f+j*%f\n",sumRe,sumIm);
 getch();
}


Expt no.4
Impulse response

#include<stdio.h>
#include<conio.h>
#define Order 2
#define Len 5

float h[Len] = {0.0,0.0,0.0,0.0,0.0},sum;

void main()
{
int j,k;
float a[Order+1] = {0.5311, 0.8622, 1.1311};
float b[Order+1] = {.2009, -0.7478, 0.5722};
clrscr();
printf("Impulse response of the given system:\n");
for(j=0;j<Len;j++)
{
sum = 0.0;
for(k=1;k<=Order;k++)
{
if((j-k)>=0)
sum = sum+(b[k]*h[j-k]);
}
if(j<=Order)
{
h[j] = a[j]-sum;
}
else
{
h[j] = -sum;
  }
printf("%f ",h[j]);
}
getch();

}




No comments:

Post a Comment