30
SNIST DEPARTMENT OF ECE DSP LAB DSP PROCESSOR PROGRAM FOR FFT: PROGRAM: #include <stdio.h> #include <math.h> void butterfly(double *,double *,double *,double *,double *,double *); int main( ) { int i; double x[8]={1,2,3,4,4,3,2,1}; //double x[8]={4,3,2,1,0,0,0,0}; double XR[8]; double XI[8]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; double WNR[4]={0.999969, 0.707092, 0.0, -0.707092}; double WNI[4]={0.0 , -0.707092,-0.999969,-0.707092}; double spectrum[8]; // store data in bit reversal order XR[0]=x[0]; XR[1]=x[4]; XR[2]=x[2]; XR[3]=x[6]; XR[4]=x[1]; III YEAR ECE-A 13311A0444 2015-16

DSP CYCLE 2

Embed Size (px)

DESCRIPTION

digital signal processing

Citation preview

Page 1: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

DSP PROCESSOR PROGRAM FOR FFT:

PROGRAM:

#include <stdio.h>

#include <math.h>

void butterfly(double *,double *,double *,double *,double *,double *);

int main( )

{

int i;

double x[8]={1,2,3,4,4,3,2,1};

//double x[8]={4,3,2,1,0,0,0,0};

double XR[8];

double XI[8]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};

double WNR[4]={0.999969, 0.707092, 0.0, -0.707092};

double WNI[4]={0.0 , -0.707092,-0.999969,-0.707092};

double spectrum[8];

// store data in bit reversal order

XR[0]=x[0];

XR[1]=x[4];

XR[2]=x[2];

XR[3]=x[6];

XR[4]=x[1];

XR[5]=x[5];

XR[6]=x[3];

XR[7]=x[7];

III YEAR ECE-A 13311A0444 2015-16

Page 2: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

// clrscr( );

// FIRST STAGE OF BUTTERFLY

butterfly(&XR[0],&XI[0],&XR[1],&XI[1],&WNR[0],&WNI[0]);

butterfly(&XR[2],&XI[2],&XR[3],&XI[3],&WNR[0],&WNI[0]);

butterfly(&XR[4],&XI[4],&XR[5],&XI[5],&WNR[0],&WNI[0]);

butterfly(&XR[6],&XI[6],&XR[7],&XI[7],&WNR[0],&WNI[0]);

// SECOND STAGE OF BUTTERFLY

butterfly(&XR[0],&XI[0],&XR[2],&XI[2],&WNR[0],&WNI[0]);

butterfly(&XR[1],&XI[1],&XR[3],&XI[3],&WNR[2],&WNI[2]);

butterfly(&XR[4],&XI[4],&XR[6],&XI[6],&WNR[0],&WNI[0]);

butterfly(&XR[5],&XI[5],&XR[7],&XI[7],&WNR[2],&WNI[2]);

// THIRD STAGE OF BUTTERFLY

butterfly(&XR[0],&XI[0],&XR[4],&XI[4],&WNR[0],&WNI[0]);

butterfly(&XR[1],&XI[1],&XR[5],&XI[5],&WNR[1],&WNI[1]);

butterfly(&XR[2],&XI[2],&XR[6],&XI[6],&WNR[2],&WNI[2]);

butterfly(&XR[3],&XI[3],&XR[7],&XI[7],&WNR[3],&WNI[3]);

for(i=0;i<8;i++)

{

XR[i]*=64.0;

XI[i]*=64.0;

}

for(i=0;i<8;i++)

{

printf("\n FFT XR[%d] = %lf XI[%d]= %lf",i,XR[i],i,XI[i]);

}

III YEAR ECE-A 13311A0444 2015-16

Page 3: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

return(0);

}

void butterfly(double *ar,double *ai,double *br,double *bi,double *wr,double *wi)

{

double tr,ti;

*ar/=4.0;

*ai/=4.0;

tr=*ar*2;

ti=*ai*2;

*br/=4.0;

*bi/=4.0;

*ar+=*br * *wr - *bi * *wi;

*ai+=*br * *wi + *bi * *wr;

*br= tr - *ar;

*bi= ti - *ai;

}

OUTPUT:

XR[0]=19.9999070; XI[0]=0.000000;

XR[1]=-5.828134; XI[1]=-2.414237;

XR[2]=0.000813; XI[2]=-0.000093;

XR[3]=-0.171618; XI[3]=-0.414175;

XR[4]=0.000248; XI[4]=0.000000;

III YEAR ECE-A 13311A0444 2015-16

Page 4: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 5: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FREQUENCY RESPONSE OF ANALOG LP/HP FILTERS

PROGRAM:

wp=input('enter the pass band cutoff frequency');

ws=input('enter the stop band cutt off frequency');

rp=input('enter pass band ripple in db');

rs=input('enter stop band ripple in db');

fs=input('enter the samling frequency');

w1=2*wp/fs;

w2=2*ws/fs;

[N,wn]=buttord(w1,w2,rp,rs,'s');

%[z,p,k]=butter(N,wn);

%[b,a]=zp2tf(z,p,k);

[b,a]=butter(N,wn,'s');

w=0:0.01:pi;

[h,omega]=freqs(b,a,w);

gain=20*log10(abs(h));

an=angle(h);

subplot(2,1,1);

title('mag res of lpf');

plot(omega/pi,gain);

ylabel('gain in db');

xlabel('normalized frequency');

subplot(2,1,2);

title('p res lpf');

plot(omega/pi,an);

xlabel('normalized frequency');

ylabel('phase in radians');

III YEAR ECE-A 13311A0444 2015-16

Page 6: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

OUTPUT:

enter the pass band cutoff frequency1500

enter the stop band cutt off frequency3000

enter pass band ripple in db10

enter stop band ripple in db40

enter the samling frequency7000

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 7: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

IIR LOW PASS FILTER USING BUTTERWORTH

APPROXIMATION

PROGRAM:

clc;

close all;

clear all;

fp=input('enter the pass band edge frequency');

fs=input('enter the stop band edge frequency');

f=input('enter the sampling frequency');

rp=input('enter the pass band riples');

rs=input('enter the stop band ripples');

wp=2*fp/f;

ws=2*fs/f;

[n,wn]=buttord(wp,ws,rp,rs);

[b,a]=butter(n,wn);

freqz(b,a);

OUTPUT:

enter the pass band edge frequency200

enter the stop band edge frequency500

enter the sampling frequency1200

enter the pass band riples10

enter the stop band ripples100

III YEAR ECE-A 13311A0444 2015-16

Page 8: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 9: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

IIR LOW PASS FILTER USING CHEBYSHEV TYPE 1

APPROXIMATION

PROGRAM:

clc;

close all;

clear all;

fp=input('enter the pass band edge frequency');

fs=input('enter the stop band edge frequency');

f=input('enter the sampling frequency');

rp=input('enter the pass band ripple');

rs=input('enter the stop band ripple');

wp=2*fp/f;

ws=2*fs/f;

[n,wn]=cheb1ord(wp,ws,rp,rs);

[b,a]=cheby1(n,rp,wn);

freqz(b,a);

OUTPUT:

enter the pass band edge frequency200

enter the stop band edge frequency500

enter the sampling frequency1200

enter the pass band ripple10

enter the stop band ripple100

III YEAR ECE-A 13311A0444 2015-16

Page 10: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 11: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

IIR LOW PASS FILTER USING CHEBYSHEV TYPE 2 APPROXIMATION

PROGRAM:

clc;

close all;

clear all;

fp=input('enter the pass band edge frequency');

fs=input('enter the stop band edge frequency');

f=input('enter the sampling frequency');

rp=input('enter the pass band ripple');

rs=input('enter the stop band ripple');

wp=2*fp/f;

ws=2*fs/f;

[n,wn]=cheb2ord(wp,ws,rp,rs);

[b,a]=cheby2(n,rp,wn);

freqz(b,a);

OUTPUT:

enter the pass band edge frequency200

enter the stop band edge frequency500

enter the sampling frequency1200

enter the pass band ripple10

enter the stop band ripple100

III YEAR ECE-A 13311A0444 2015-16

Page 12: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 13: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FIR FILTER USING WINDOWING TECHNIQUE

PROGRAM:

clc;

close all;

clear all;

fc=input('enter the cutoff frequency');

fs=input('enter the sampling frequency');

N=input('enter the order of the filter');

if(rem(N,2)==0)

N=N+1;

end

wn=2*fc/fs;

wr=boxcar(N+1);

b=fir1(N,wn,wr);

freqz(b,1);

OUTPUT:

enter the cutoff frequency300

enter the sampling frequency1000

enter the order of the filter20

III YEAR ECE-A 13311A0444 2015-16

Page 14: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 15: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FIR FILTER USING TRIANGULAR WINDOW:

PROGRAM:

clc;

close all;

clear all;

fc=input('enter the cutoff frequency');

fs=input('enter the sampling frequency');

N=input('enter the order of the filter');

if(rem(N,2)==0)

N=N+1;

end

wn=2*fc/fs;

wr=triang(N+1);

b=fir1(N,wn,wr);

freqz(b,1);

OUTPUT:

enter the cutoff frequency300

enter the sampling frequency1000

enter the order of the filter20

III YEAR ECE-A 13311A0444 2015-16

Page 16: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 17: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FIR FILTER USING KAISER WINDOW

PROGRAM:

clc;

close all;

clear all;

fc=input('enter the cutoff frequency');

fs=input('enter the sampling frequency');

N=input('enter the order of the filter');

B=15;

if(rem(N,2)==0)

N=N+1;

end

wn=2*fc/fs;

wr=kaiser(N+1,B);

b=fir1(N,wn,wr);

freqz(b,1);

OUTPUT:

enter the cutoff frequency300

enter the sampling frequency1000

enter the order of the filter20

III YEAR ECE-A 13311A0444 2015-16

Page 18: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 19: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FIR FILTER USING HAMMING WINDOW

PROGRAM: clc;

close all;

clear all;

fc=input('enter the cutoff frequency');

fs=input('enter the sampling frequency');

N=input('enter the order of the filter');

if(rem(N,2)==0)

N=N+1;

end

wn=2*fc/fs;

wr=hamming(N+1);

b=fir1(N,wn,wr);

freqz(b,1);

OUTPUT:enter the cutoff frequency300

enter the sampling frequency1000

enter the order of the filter20

III YEAR ECE-A 13311A0444 2015-16

Page 20: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 21: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

FIR FILTER USING HANNING WINDOW

PROGRAM:

clc;

close all;

clear all;

fc=input('enter the cutoff frequency');

fs=input('enter the sampling frequency');

N=input('enter the order of the filter');

if(rem(N,2)==0)

N=N+1;

end

wn=2*fc/fs;

wr=hanning(N+1);

b=fir1(N,wn,wr);

freqz(b,1);

OUTPUT:enter the cutoff frequency300

enter the sampling frequency1000

enter the order of the filter20

III YEAR ECE-A 13311A0444 2015-16

Page 22: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 23: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

CONVERSION OF ANALOG TO DIGITAL FILTERS

A)IMPULSE INVARIANCE METHOD:

PROGRAM:

clc;clear all;close all;[b,a] = butter(4,.3,'s');[bz,az] = impinvar(b,a,10);sys = tf(b,a);impulse(sys);hold on;impz(10*bz,az,[],10);

OUTPUT:

enter the pass band edge frequency200enter the stop band edge frequency500enter the sampling frequency1200enter the pass band riples3enter the stop band ripples100

GRAPH:

III YEAR ECE-A 13311A0444 2015-16

Page 24: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

B)BILINEAR TRANSFORM METHOD:

PROGRAM:

clc;clear all;close all;num = 1;tau = .1;den = [tau,1];fs = 1000;[numd,dend] = bilinear(num,den,fs);[h,f] = freqz(numd,dend,4096,fs);figure(1); clf();subplot(2,1,1); semilogx(f,20*log10(abs(h))); hold on;plot([.1,1000],[-3,-3],'r');grid on;ylim([-40,1]); ylabel('gain in db'); xlim([.1,fs/2]);subplot(2,1,2); semilogx(f,angle(h)* 180/pi);grid on;ylabel('phase in rad');xlim([.1,fs/2]); xlabel('frequency in hz');

III YEAR ECE-A 13311A0444 2015-16

Page 25: DSP CYCLE 2

SNIST DEPARTMENT OF ECE DSP LAB

GRAPH:

III YEAR ECE-A 13311A0444 2015-16