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

CYCLE -II

Code Composer Studio Programs


// Linear convolution program in c language using CCStudio

#include<stdio.h>
int x[15],h[15],y[15];
main()
{
int i,j,m,n;
printf("\n enter first sequence length m:");
scanf("%d",&m);
printf("\n enter second sequence length n:");
scanf("%d",&n);
printf("Enter i/p sequence for x(n):\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf("Enter i/p sequence for h(n): \n");
for(i=0;i<n; i++)
scanf("%d",&h[i]);
// padding of zeors
for(i=m;i<=m+n-1;i++)
x[i]=0;
for(i=n;i<=m+n-1;i++)
h[i]=0;
/* convolution operation */
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]);
}
}
//displaying the o/p
printf("Output (Linear Convolution) sequence is:\n ");
for(i=0;i<m+n-1;i++)
printf("y[%d]=%d\t",i,y[i]);
}
enter first sequence length m:4
enter second sequence length n:3
Enter i/p sequence for x(n):
1234
Enter i/p sequence for h(n):
231
Output (Linear Convolution) sequence is:
y[0]=2y[1]=7 y[2]=13 y[3]=19 y[4]=15 y[5]=4
/* program to implement circular convolution */

#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j, k,x2[30],a[30];
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*/
{
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("%d \t",y[i]);
}

enter the length of the first sequence


4
enter the length of the second sequence
3
enter the first sequence
1234
enter the second sequence
231
the circular convolution is
17 11 13 19
3. FIR filters
#include<stdio.h>
#include<math.h>
#define pi 3.1415
int n,N,c;
float wr[64],wt[64];
void main()
{
printf("\n enter no. of samples,N= :");
scanf("%d",&N);
printf("\n enter choice of window function\n 1.rect \n 2. triang \n c= :");
scanf("%d",&c);
printf("\n elements of window function are:");
switch(c)
{
case 1:
for(n=0;n<=N-1;n++)
{
wr[n]=1;
printf(" \n wr[%d]=%f",n,wr[n]);
}
break;
case 2:
for(n=0;n<=N-1;n++)
{
wt[n]=1-(2*(float)n/(N-1));
printf("\n wt[%d]=%f",n,wt[n]);
}
break;
}}

Output:
enter no. of samples,N= :64

enter choice of window function


1.rect
2. triang
c= :1

elements of window function are:


wr[0]=1.000000
wr[1]=1.000000
wr[2]=1.000000
wr[3]=1.000000
wr[4]=1.000000
wr[5]=1.000000
wr[6]=1.000000
wr[7]=1.000000
wr[8]=1.000000
wr[9]=1.000000
wr[10]=1.000000
wr[11]=1.000000
wr[12]=1.000000
wr[13]=1.000000
wr[14]=1.000000
wr[15]=1.000000
wr[16]=1.000000
wr[17]=1.000000
wr[18]=1.000000
wr[19]=1.000000
wr[20]=1.000000
wr[21]=1.000000
wr[22]=1.000000
wr[23]=1.000000
wr[24]=1.000000
wr[25]=1.000000
wr[26]=1.000000
wr[27]=1.000000
wr[28]=1.000000
wr[29]=1.000000
wr[30]=1.000000
wr[31]=1.000000
wr[32]=1.000000
wr[33]=1.000000
wr[34]=1.000000
wr[35]=1.000000
wr[36]=1.000000
wr[37]=1.000000
wr[38]=1.000000
wr[39]=1.000000
wr[40]=1.000000
wr[41]=1.000000
wr[42]=1.000000
wr[43]=1.000000
wr[44]=1.000000
wr[45]=1.000000
wr[46]=1.000000
wr[47]=1.000000
wr[48]=1.000000
wr[49]=1.000000
wr[50]=1.000000
wr[51]=1.000000
wr[52]=1.000000
wr[53]=1.000000
wr[54]=1.000000
wr[55]=1.000000
wr[56]=1.000000
wr[57]=1.000000
wr[58]=1.000000
wr[59]=1.000000
wr[60]=1.000000
wr[61]=1.000000
wr[62]=1.000000
wr[63]=1.000000
enter no. of samples,N= :64

enter choice of window function


1.rect
2. triang
c= :2

elements of window function are:


wt[0]=1.000000
wt[1]=0.968254
wt[2]=0.936508
wt[3]=0.904762
wt[4]=0.873016
wt[5]=0.841270
wt[6]=0.809524
wt[7]=0.777778
wt[8]=0.746032
wt[9]=0.714286
wt[10]=0.682540
wt[11]=0.650794
wt[12]=0.619048
wt[13]=0.587302
wt[14]=0.555556
wt[15]=0.523810
wt[16]=0.492063
wt[17]=0.460317
wt[18]=0.428571
wt[19]=0.396825
wt[20]=0.365079
wt[21]=0.333333
wt[22]=0.301587
wt[23]=0.269841
wt[24]=0.238095
wt[25]=0.206349
wt[26]=0.174603
wt[27]=0.142857
wt[28]=0.111111
wt[29]=0.079365
wt[30]=0.047619
wt[31]=0.015873
wt[32]=-0.015873
wt[33]=-0.047619
wt[34]=-0.079365
wt[35]=-0.111111
wt[36]=-0.142857
wt[37]=-0.174603
wt[38]=-0.206349
wt[39]=-0.238095
wt[40]=-0.269841
wt[41]=-0.301587
wt[42]=-0.333333
wt[43]=-0.365079
wt[44]=-0.396825
wt[45]=-0.428571
wt[46]=-0.460317
wt[47]=-0.492064
wt[48]=-0.523810
wt[49]=-0.555556
wt[50]=-0.587302
wt[51]=-0.619048
wt[52]=-0.650794
wt[53]=-0.682540
wt[54]=-0.714286
wt[55]=-0.746032
wt[56]=-0.777778
wt[57]=-0.809524
wt[58]=-0.841270
wt[59]=-0.873016
wt[60]=-0.904762
wt[61]=-0.936508
wt[62]=-0.968254
wt[63]=-1.000000
//iirfilters
#include<stdio.h>
#include<math.h>
int i,w,wc,c,N;
float H[100];
float mul(float, int);
void main()
{
printf("\n enter order of filter ");
scanf("%d",&N);
printf("\n enter the cutoff freq ");
scanf("%d",&wc);
printf("\n enter the choice for IIR filter 1. LPF 2.HPF ");
scanf("%d",&c);
switch(c)
{
case 1:
for(w=0;w<100;w++)
{
H[w]=1/sqrt(1+mul((w/(float)wc),2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
case 2:
for(w=0;w<=100;w++)
{
H[w]=1/sqrt(1+mul((float)wc/w,2*N));
printf("H[%d]=%f\n",w,H[w]);
}
break;
}}
float mul(float a,int x)
{
for(i=0;i<x-1;i++)
a*=a;
return(a);
}
RESULT
enter order of filter 2

enter the cutoff freq 50

enter the choice for IIR filter 1. LPF 2.HPF 1


enter order of filter 2

enter the cutoff freq 50

enter the choice for IIR filter 1. LPF 2.HPF 2


FFT

#include <math.h>
#define PTS 64 //# of points for FFT
#define PI 3.14159265358979

typedef struct {float real,imag;} COMPLEX;

void FFT(COMPLEX *Y, int n); //FFT prototype


float iobuffer[PTS]; //as input and output buffer
float x1[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer
short flag = 0; //set to 1 by ISR when iobuffer full
COMPLEX w[PTS]; //twiddle constants stored in w
COMPLEX samples[PTS]; //primary working buffer

main()
{
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants
w[i].imag =-sin(2*PI*i/(PTS*2.0)); //Im component of twiddle constants
}

for (i = 0 ; i < PTS ; i++) //swap buffers


{

iobuffer[i] = sin(2*PI*10*i/64.0);/*10- > freq,


64 -> sampling freq*/
samples[i].real=0.0;
samples[i].imag=0.0;
}

for (i = 0 ; i < PTS ; i++) //swap buffers


{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0; //imag components = 0

FFT(samples,PTS); //call function FFT.c


for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real
+ samples[i].imag*samples[i].imag);
}

} //end of main
//#define PTS 64 //# of points for FFT
//typedef struct {float real,imag;} COMPLEX;
//extern COMPLEX w[PTS]; //twiddle constants stored in w

void FFT(COMPLEX *Y, int N) //input sample array, # of points


{
COMPLEX temp1,temp2; //temporary storage variables
int i,j,k; //loop counter variables
int upper_leg, lower_leg; //index of upper/lower butterfly leg
int leg_diff; //difference between upper/lower leg
int num_stages = 0; //number of FFT stages (iterations)
int index, step; //index/step through twiddle constant
i = 1; //log(base2) of N points= # of stages
do
{
num_stages +=1;
i = i*2;
}while (i!=N);
leg_diff = N/2; //difference between upper&lower legs
step = (PTS*2)/N; //step between values in twiddle.h
for (i = 0;i < num_stages; i++) //for N-point FFT
{
index = 0;
for (j = 0; j < leg_diff; j++)
{
for (upper_leg = j; upper_leg < N; upper_leg += (2*leg_diff))
{
lower_leg = upper_leg+leg_diff;
temp1.real = (Y[upper_leg]).real + (Y[lower_leg]).real;
temp1.imag = (Y[upper_leg]).imag + (Y[lower_leg]).imag;
temp2.real = (Y[upper_leg]).real - (Y[lower_leg]).real;
temp2.imag = (Y[upper_leg]).imag - (Y[lower_leg]).imag;
(Y[lower_leg]).real = temp2.real*(w[index]).real
-temp2.imag*(w[index]).imag;
(Y[lower_leg]).imag = temp2.real*(w[index]).imag
+temp2.imag*(w[index]).real;
(Y[upper_leg]).real = temp1.real;
(Y[upper_leg]).imag = temp1.imag;
}
index += step;
}
leg_diff = leg_diff/2;
step *= 2;
}
j = 0;
for (i = 1; i < (N-1); i++) //bit reversal for resequencing data
{
k = N/2;
while (k <= j)
{
j = j - k;
k = k/2;
}
j = j + k;
if (i<j)
{
temp1.real = (Y[j]).real;
temp1.imag = (Y[j]).imag;
(Y[j]).real = (Y[i]).real;
(Y[j]).imag = (Y[i]).imag;
(Y[i]).real = temp1.real;
(Y[i]).imag = temp1.imag;
}
}
return;
}

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