Вы находитесь на странице: 1из 13

DSP PROGRAMS IN C

AIM THEORY

1. Linear Convolution
To verify Linear Convolution. Convolution is a formal mathematical operation, just as multiplication, addition, and

integration. Addition takes two numbers and produces a third number, while convolution takes two signals and produces a third signal. Convolution is used in the mathematics of many fields, such as probability and statistics. In linear systems, convolution is used to

describe the relationship between three signals of interest: the input signal, the impulse response, and the output signal. In this equation, x1(k), x2 (n-k) and y(n) represent the input to and output from the system at time n. Here we could see that one of the input is shifted in time by a value every time it is multiplied with the other input signal. Linear Convolution is quite often used as a method of implementing filters of various types. ALGORITHM Step 1 Declare three buffers namely Input buffer, Temporary Buffer, Output Buffer. Step 2 Get the input from the CODEC, store it in Input buffer and transfer it to the first location of the Temporary buffer. Step 3 Make the Temporary buffer to point to the last location. Step 4 Multiply the temporary buffer with the coefficients in the data memory and accumulate it with the previous output. Step 6 Repeat the steps from 2 to 5. Step 5 Store the output in the output buffer. PROGRAM

#include<stdio.h> int x[15],h[15],y[15]; main() { int i,j,m,n; printf("\n enter value for m"); scanf("%d",&m); printf("\n enter value for n"); printf("Enter values for i/p\n"); for(i=0;i<m;i++) scanf("%d",&x[i]); printf("Enter Values for n \n"); for(i=0;i<n;i++)

scanf("%d",&h[i]); for(i=m;i<=m+n-1;i++) x[i]=0; for(i=n;i<=m+n-1;i++) h[i]=0; { for(i=0;i<m+n-1;i++) y[i]=0; for(j=0;j<=i;j++) { y[i]=y[i]+(x[j]*h[i-j]); }} for(i=0;i<m+n-1;i++) } Result: enter value for m4 enter value for n4 1234 Enter Values for n 1234 The Value of output y[0]=1 The Value of output y[1]=4 The Value of output y[2]=10 The Value of output y[3]=20 The Value of output y[4]=25 The Value of output y[5]=24 The Value of output y[6]=16 Enter values for i/p printf("\n The Value of output y[%d]=%d",i,y[i]);

2. Circular Convolution AIM


To verify Circular Convolution. Circular convolution is another way of finding the convolution sum of two input

THEORY

signals. It resembles the linear convolution, except that the sample values of one of the input signals is folded and right shifted before the convolution sum is found. Also note that circular convolution could also be found by taking the DFT of the two input signals and finding the product of the two frequency domain signals. The Inverse DFT of the product would give the output of the signal in the time domain which is the circular convolution DFT of higher point, which ever signals levels to. For eg. If one of the signal is of length 256 output. The two input signals could have been of varying sample lengths. But we take the and the other spans 51 samples, then we could only take 256 point DFT. So the output of IDFT would be containing 256 samples instead of 306 samples, which follows N1+N2 1 where N1 & N2 are the lengths 256 and 51 respectively of the two inputs. Thus the output being a distorted version of the correct signal. This process is called circular convolution. PROGRAM: /* prg to implement circular convolution */ #include<stdio.h> 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*/ int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30]; which should have been 306 samples long is fitted into 256 samples. The 256 points end up

{ 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++) } OUTPUT:567 Enter the second sequence Enter the first sequence printf("%d \t",y[i]);

7854 OUTPUT ;- the circular convolution is 94 110 122 106

3. CORRELATION
AIM: To convert Circular Convolution Correlation is measures of the degree to which two sequences are similar .There are THEORY:

two types of Correlation 1. Cross correlation 2.Auto correlation Cross Correlation:- given two real valued sequences x1(n) of finite energy , the cross correlation of x1(n) and x2(n) is a sequence rxy (1) defined as rxy( 1 )= #include<stdio.h> int m,n,X[30],RXY[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); scanf("%d",&n); printf("enter the length of the second sequence\n"); printf("enter the first sequence\n"); for(i=0;i<m;i++) scanf("%d",&X[i]); printf("enter the secound sequence\n"); for(j=0;j<n;j++) scanf("%d",&Y[j]); for(i=n;i<m+n-1;i++) X[i]=0; Y[i]=0; a=m; else a=n { for(l=0;l<a;l++) RXY[l]=0; for(n=0;n<a;n++) { RXY[l]+=X[n+l]*X2[n]; }} for(i=m;i<n+m-1;i++) if(m>n)

printf("the correlation is\n"); for(i=0;i<n;i++) } enter the length of the first sequence 5 enter the first sequence 5,5,5,5,5,5 5,5,5,5,5,5 enter the secound sequence the correlation is 125,100,75,50,25 5 enter the length of the second sequence printf("%d\t",RXY[i]);

4. DISCRETE FOURIER TRANSFORM


AIM To find the Fast Fourier Transform for the realtime samples. The Transform Domain Technique involves the transformation of the time domain THEORY

signal into a frequency domain one. The available methods of implementing the transformation are .Discrete Fourier Transform Fast Fourier Transform

PROGRAM: #include<stdio.h> #include<math.h> #define pi 3.1415 #define PTS 64 float X[PTS]; main() {

float xr[PTS],xi[PTS],k,n,N=PTS; float XR[PTS],XI[PTS]; for(i=0;i<PTS-1;++) { xr[i]=sin(2*pi*10*i/64.0); xi[i]=0; } { for (k=0;k<N;k++) Xr[k]=0; Xi[k]=0; { XR[k]+=(xr[n]*cos(2*pi*k*n/N))+(xi[n]*sin(2*pi*k*n/N)); XI[k]+=(xi[n]*sin(2*pi*k*n/N))-(xr[n]*cos(2*pi*k*n/N)); for (n=0; <N; n++)

} X[k]=sqrt((XR[k]*XR[k])+(XI[k]*XI[k])); printf("%f\n",X[k]); }

5.FIR FILTER
AIM: . To design and implement a low pass FIR filter using windowing technique.

THEORY: A Finite Impulse Response (FIR) filter is a discrete linear time-invariant system whose output is based on the weighted summation of a finite number of past inputs. An FIR transversal filter structure can be obtained directly from the equation for discrete-time convolution.

In this equation, x(k) and y(n) represent the input to and output from the filter at time n. h(n-k) is the transversal filter coefficients at time n. These coefficients are generated by using FDS (Filter Design Software or Digital filter design package). Windows: #include <stdio.h> #include <math.h> #define pi 3.1415 int N,n; float h[64]; { void main() printf("enter the number of samples\n"); scanf("%d",&N); for(n=0;n<(N-1);n++) { h[n]=0.0; h[n]= 0.42+0.5*cos(2*pi*n/(N-1))+0.08*cos(4*pi*n/(N-1)); printf("h[%d]=%f\n",n,h[n]); } } PROGRAM: #include <stdio.h> #define pi 3.1415 int N,n; float h[64]; void main() #include <math.h>

{ printf("enter the number of samples\n"); scanf("%d",&N); { for(n=0;n<(N-1);n++) h[n]=0.0; h[n]=1-(4*n/(float)(n-1)) ; } } printf("h[%d]=%f\n",n,h[n]);

6. IIR FILTER
AIM: To design and implement a low pass IIR filter using windowing technique. THEORY: The IIR filter can realize both the poles and zeroes of a system because it has a

rational transfer function, described by polynomials in z in both the numerator and the denominator:

The difference equation for such a system is described by the following:

bk and ak are the filter coefficients. These filter coefficients are generated using FDS (Filter Design software or Digital Filter design package). LPF: #include<stdio.h> #include<math.h> #define pi 3.1415 float Xmod[64],Xr[64],Xi[64]; float xr[64],xi[64],h[100],y[64]; int n,k,i,N,w,wc; void main() { { xr[k]=sin((2*pi*10*k)/64)+sin((2*pi*50*k)/64); xi[k]=0.0; } for(k=0;k<=63;k++) { Xr[k]=0; for(n=0;n<=63;n++) {

for(k=0;k<=63;k++)

Xr[k]+=xr[n]*cos((2*pi*k*n)/64.0)+xi[n]*sin((2*pi*k*n)/64.0); Xi[k]+=xi[n]*cos((2*pi*k*n)/64.0)-xr[n]*sin((2*pi*k*n)/64.0); } } Xmod[k]=sqrt(Xr[k]*Xr[k]+Xi[k]*Xi[k]); printf("enter the order of the filter\n"); scanf("%d",&w); printf("enter the cutoff frequency\n"); scanf("%d",&wc); for(w=0;w<100;w++) { h[w]=1/sqrt(1+pow(w/(float)wc,2*N)); printf("h[%d]=%f\n",w,h[w]); } { for(k=0;k<=63;k++) y[k]=Xmod[k]*h[k]; printf("y[%d]=%f\n",k,y[k]); }}

Вам также может понравиться