Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSP Lab Programs1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

STEPS FOR DSP PROCESSOR PROGRAMS :

1. Open the Vi Debugger software and create a new project.

2.Type the program and save with file name and with the extension .dpj

3. Click on assembly and select ‘Add File To Project’/Selec t your program in the place where you saved and open.

4. Click on the cmd file and select ‘Add File To Project’/my project-> click the file ‘Micro 5416’ and open.

5. Go to the project menu and save the project.

6. Go to the project menu and click on ‘Build’.

7. Go to the serial menu and click on ‘Open port’.

8. Go to the serial menu and click on ‘Load Program’.

9. Browse and select the name of the file to be executed.

10. Go to the serial menu and click on ‘Communication Window’.

11. Enter the input and click the ‘reset’ key on the kit and verify the output.

STEPS FOR SINE WAVE GENERATION

1. Open a vi-debugger software and open the new project


2. Save the project with the extension .dpj
3. Right click on assembly and command and add file to project from vi examples(asm programs , without
serial)
4. Build the project from project menu
5. Check whether the port is opened or not from serial menu
6. Load the program from serial menu
7. Once again load the program and change address 0000 to 200
8. select the sine wave program
9. open the communication window from serial menu
10. Give the input as SD A000
11. Execute the program as GO 0000 and observe the graph from CRO
DSP PROCESSOR PROGRAMS
1.ADDITION
;******************************************************************
INP1 .SET 0H
INP2 .SET 1H
OUT .SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
ADD INP2,A
STL A,OUT
HLT: B HLT

;******************************************************************
;Input:
;Data Memory:
; A000h 0004h
; A001h 0004h
;Output:
;Data Memory:
; A002h 0008h
2.SUBTRACTION
;******************************************************************
INP1 .SET 0H
INP2 .SET 1H
OUT .SET 2H
.mmregs
.text
START:
LD #140H,DP
RSBX CPL
NOP
NOP
NOP
NOP
LD INP1,A
SUB INP2,A
STL A,OUT
HLT: B HLT

;******************************************************************
;Input:
;Data Memory:
; A000h 0004h
; A001h 0002h
;Output:
;Data Memory:
; A002h 0002h
3.DIVISION
;******************************************************************
DIVID .SET 0H
DIVIS .SET 1H
OUT .SET 2H
.mmregs
.text
START:
STM #140H,ST0
RSBX CPL
RSBX FRCT
NOP
NOP
NOP
NOP
LD DIVID,A ;dividend to acc
RPT #0FH
SUBC DIVIS,A ;A / DIVIS -> A
STL A,OUT ;result in 9002h
HLT: B HLT
.END
;******************************************************************
;INPUT
;DATA MEMORY
; A000H 000AH
; A001H 0002H
;OUTPUT
; A002H 0005H
4.CIRCULAR CONVOLUTION FOR FOUR INPUTS
;******************************************************************
.mmregs
.text
START:
STM #0140H,ST0
RSBX CPL
RSBX FRCT
NOP
NOP
NOP
NOP
STM #0A020H,AR2
RPT #4H
ST #0H,*AR2+
STM #0A000H,AR0 ;x1(n)
STM #0A010H,AR1 ;x2(n)
STM #0A020H,AR2
STM #0A030H,AR3 ;output y(n)
STM #3H,AR4 ;counter for finding y(1) to y(n)
CALL ROT1 ;180 degree rotate for y(0)
CALL CONV ;routine for convolution
NEXTY:CALL ROT2 ;routine for 90 degree rotation
CALL CONV
BANZ NEXTY,*AR4-
HLT: B HLT
;routine for 180 degree rotation
ROT1:
STM #0A011H,AR0 ;exchange x1(1) and x1(3)
STM #0A013H,AR1
LD *AR0,A
LD *AR1,B
STL A,*AR1
STL B,*AR0
RET
;routine for 90 degree rotation
ROT2:
STM #0A013H,AR0
STM #0A012H,AR1
LD *AR0,A ;store x2(3) to acc(a)temporarily
STM #2H,BRC
RPTB ROT ;x2(2)->x2(3)
LD *AR1-,B ;x2(1)->x2(2)
ROT:
STL B,*AR0- ;x2(0)->x2(1)
STM #0A010H,AR0 ;acc(a)->x2(0)
STL A,*AR0
STM #0H,BRC
RET
CONV:
STM #0A000H,AR0
STM #0A010H,AR1
LD #0H,A
STM #3H,BRC
RPTB CON
LD *AR0+,T
;multiply and add loop
CON:
MAC *AR1+,A
STL A,*AR3+ ;store the result in AR3 and increment AR3
RET

;******************************************************************
;INPUT X1(n) DATA MEMORY
; 0A000 0002
; 0A001 0001
; 0A002 0002
; 0A003 0001

;INPUT X2(n) DATA MEMORY


; 0A010 0001
; 0A011 0002
; 0A012 0003
; 0A013 0004

;OUTPUT Y(n) DATA MEMORY


; 0A030 000E
; 0A031 0010
; 0A032 000E
; 0A033 0010

;******************************************************************

5.LOW-PASS FILTER
;******************************************************************
;Filter type : FIR-LPF
;Window type : Rectangular window
;Sampling frequency : 41khz
;Cut-off frequency : 4khz
;No. of taps : 52
.mmregs
.text
START:
STM #01h,ST0
RSBX CPL
RSBX FRCT
NOP
NOP
STM #150H,AR1
LD #0H,A
RPT #34H
STL A,*AR1+
LOOP:
PORTR 06,0
CHK_BUSY:
; PORTR 07,0
; BITF 0,#20H
; BC CHK_BUSY,TC
PORTR 04,0
LD 0,A
AND #0FFFH,A
XOR #0800H,A
SUB #800H,A
STM #150H,AR1
STL A,*AR1
STM #183H,AR2
LD #0H,A
RPT #33H
MACD *AR2-,TABLE,A
STH A,1,0H
LD 0H,A
ADD #800H,A
STL A,1H
PORTW 1H,04H
B LOOP
;fs = 41khz ; fc = 4khz ; N = 52
TABLE:
.word 01FH
.word 010EH
.word 01ABH
.word 01B4H
.word 0117H
.word 0H
.word 0FECDH
.word 0FDEEH
.word 0FDC2H
.word 0FE6EH
.word 0FFCDH
.word 016FH
.word 02C0H
.word 0333H
.word 0274H
.word 097H
.word 0FE19H
.word 0FBCBH
.word 0FA9BH
.word 0FB53H
.word 0FE50H
.word 0362H
.word 09C5H
.word 01048H
.word 01599H
.word 01895H
.word 01895H
.word 01599H
.word 01048H
.word 09C5H
.word 0362H
.word 0FE50H
.word 0FB53H
.word 0FA9BH
.word 0FBCBH
.word 0FE19H
.word 097H
.word 0274H
.word 0333H
.word 02C0H
.word 016FH
.word 0FFCDH
.word 0FE6EH
.word 0FDC2H
.word 0FDEEH
.word 0FECDH
.word 0H
.word 0117H
.word 01B4H
.word 01ABH
.word 010EH
.word 01FH
6.HIGH PASS FIR FILTER
;******************************************************************
;Sampling freq : 43khz
;Cut-off freq : 2khz
;N : 52
;Filter type : High pass filter
;Window type : Rectangular

;Program Description:
;1. Make all the x(n) zero initially
;2. Read the data from the adc.
;3. Store the adc data in x(0)
;4. Make the pointer to point the x(n_end)
;5. Perform the convolution of x(n) and the coefficients h(n) using
; MACD instruction.
;6. Send the convolution output to the dac
;7. Repeat from step 2.

.mmregs
.text
START:
STM #01h,ST0 ;intialize the data page pointer
RSBX CPL ;Make the processor to work using DP
RSBX FRCT ;reset the fractional mode bit
NOP
NOP
;*****loop to make all x(n) zero initially*****
STM #150H,AR1 ;initialize ar1 to point to x(n)
LD #0H,A ;make acc zero
RPT #34H
STL A,*AR1+ ;make all x(n) zero
;*****to read the adc data and store it in x(0)*****
LOOP:
PORTR 06,0 ;start of conversion
CHK_BUSY:
; PORTR 07,0 ;check for busy
; BITF 0,#20H
; BC CHK_BUSY,TC
PORTR 04,0 ;read the adc data
LD 0,A
AND #0FFFH,A ;AND adc data with 0fffh for 12 bit adc
XOR #0800H,A ;recorrect the 2's complement adc data
SUB #800H,A ;remove the dc shift
STM #150H,AR1 ;initialize ar1 with x(0)
STL A,*AR1 ;store adc data in x(0)
STM #183H,AR2 ;initialize ar2 with x(n_end)
;*****start of convolution*****
LD #0H,A ;sum is 0 initially
RPT #33H
MACD *AR2-,TABLE,A ;convolution process
STH A,1,0H
LD 0H,A
ADD #800H,A ;add the dc shift to the convolution output
STL A,1H
PORTW 1H,04H ;send the output to the dac
B LOOP

TABLE:
.word 0FCEFH
.word 62H
.word 0FD50H
.word 14AH
.word 0FE1BH
.word 28FH
.word 0FF11H
.word 3E5H
.word 0FFD1H
.word 4ECH
.word 0FFF5H
.word 54FH
.word 0FF28H
.word 4DAH
.word 0FD38H
.word 398H
.word 0FA2EH
.word 1DDH
.word 0F627H
.word 55H
.word 0F131H
.word 4BH
.word 0EA6DH
.word 568H
.word 0D950H
.word 459EH
.word 459EH
.word 0D950H
.word 568H
.word 0EA6DH
.word 4BH
.word 0F131H
.word 55H
.word 0F627H
.word 1DDH
.word 0FA2EH
.word 398H
.word 0FD38H
.word 4DAH
.word 0FF28H
.word 54FH
.word 0FFF5H
.word 4ECH
.word 0FFD1H
.word 3E5H
.word 0FF11H
.word 28FH
.word 0FE1BH
.word 14AH
.word 0FD50H
.word 62H
.word 0FCEFH
7.BAND PASS FIR FILTER
;******************************************************************
;Sampling freq : 43khz
;Cut-off freq1 : 2khz
;Cut-off freq2 : 4khz
;N : 52
;Filter type : Band pass filter
;Window type : Rectangular

;Program Description:
;1. Make all the x(n) zero initially
;2. Read the data from the adc.
;3. Store the adc data in x(0)
;4. Make the pointer to point the x(n_end)
;5. Perform the convolution of x(n) and the coefficients h(n) using
; MACD instruction.
;6. Send the convolution output to the dac
;7. Repeat from step 2.

.mmregs
.text
START:
STM #01h,ST0 ;intialize the data page pointer
RSBX CPL ;Make the processor to work using DP
RSBX FRCT ;reset the fractional mode bit
NOP
NOP
;*****loop to make all x(n) zero initially*****
STM #150H,AR1 ;initialize ar1 to point to x(n)
LD #0H,A ;make acc zero
RPT #34H
STL A,*AR1+ ;make all x(n) zero
;*****to read the adc data and store it in x(0)*****
LOOP:
PORTR 06,0 ;start of conversion
CHK_BUSY:
;PORTR 07,0 ;check for busy
; BITF 0,#20H
; BC CHK_BUSY,TC
PORTR 04,0 ;read the adc data
LD 0,A
AND #0FFFH,A ;AND adc data with 0fffh for 12 bit adc
XOR #0800H,A ;recorrect the 2's complement adc data
SUB #800H,A ;remove the dc shift
STM #150H,AR1 ;initialize ar1 with x(0)
STL A,*AR1 ;store adc data in x(0)
STM #183H,AR2 ;initialize ar2 with x(n_end)
;*****start of convolution*****
LD #0H,A ;sum is 0 initially
RPT #33H
MACD *AR2-,TABLE,A ;convolution process
STH A,1,0H
LD 0H,A
ADD #800H,A ;add the dc shift to the convolution output
STL A,1H
PORTW 1H,04H ;send the output to the dac
B LOOP

TABLE:
.word 208H
.word 257H
.word 218H
.word 143H
.word 0H
.word 0FE9EH
.word 0FD7AH
.word 0FCE7H
.word 0FD08H
.word 0FDD1H
.word 0FEECH
.word 0FFE4H
.word 3DH
.word 0FFA1H
.word 0FDFCH
.word 0FB8FH
.word 0F8ECH
.word 0F6D4H
.word 0F608H
.word 0F713H
.word 0FA21H
.word 0FEE6H
.word 4A7H
.word 0A60H
.word 0EF8H
.word 1187H
.word 1187H
.word 0EF8H
.word 0A60H
.word 4A7H
.word 0FEE6H
.word 0FA21H
.word 0F713H
.word 0F608H
.word 0F6D4H
.word 0F8ECH
.word 0FB8FH
.word 0FDFCH
.word 0FFA1H
.word 3DH
.word 0FFE4H
.word 0FEECH
.word 0FDD1H
.word 0FD08H
.word 0FCE7H
.word 0FD7AH
.word 0FE9EH
.word 0H
.word 143H
.word 218H
.word 257H
.word 208H

8.BAND REJECT FIR FILTER


;******************************************************************
;Sampling freq : 43khz
;Cut-off freq1 : 2khz
;Cut-off freq2 : 4khz
;N : 52
;Filter type : Band reject filter
;Window type : Rectangular

;Program Description:
;1. Make all the x(n) zero initially
;2. Read the data from the adc.
;3. Store the adc data in x(0)
;4. Make the pointer to point the x(n_end)
;5. Perform the convolution of x(n) and the coefficients h(n) using
; MACD instruction.
;6. Send the convolution output to the dac
;7. Repeat from step 2.

.mmregs
.text
START:
STM #01h,ST0 ;intialize the data page pointer
RSBX CPL ;Make the processor to work using DP
RSBX FRCT ;reset the fractional mode bit
NOP
NOP
;*****loop to make all x(n) zero initially*****
STM #150H,AR1 ;initialize ar1 to point to x(n)
LD #0H,A ;make acc zero
RPT #34H
STL A,*AR1+ ;make all x(n) zero
;*****to read the adc data and store it in x(0)*****
LOOP:
PORTR 06,0 ;start of conversion
CHK_BUSY:
; PORTR 07,0 ;check for busy
; BITF 0,#20H
; BC CHK_BUSY,TC
PORTR 04,0 ;read the adc data
LD 0,A
AND #0FFFH,A ;AND adc data with 0fffh for 12 bit adc
XOR #0800H,A ;recorrect the 2's complement adc data
SUB #800H,A ;remove the dc shift
STM #150H,AR1 ;initialize ar1 with x(0)
STL A,*AR1 ;store adc data in x(0)
STM #183H,AR2 ;initialize ar2 with x(n_end)
;*****start of convolution*****
LD #0H,A ;sum is 0 initially
RPT #33H
MACD *AR2-,TABLE,A ;convolution process
STH A,1,0H
LD 0H,A
ADD #800H,A ;add the dc shift to the convolution output
STL A,1H
PORTW 1H,04H ;send the output to the dac
B LOOP

TABLE:
.word 0FEB9H
.word 14EH
.word 0FDA1H
.word 155H
.word 0FE1BH
.word 282H
.word 0FEAFH
.word 2ACH
.word 0FD35H
.word 8DH
.word 0F9D9H
.word 0FE07H
.word 0F7CCH
.word 0FEE2H
.word 0FA2FH
.word 4BAH
.word 1AH
.word 25CH
.word 420H
.word 1008H
.word 89H
.word 0D61H
.word 0F3F2H
.word 0AF9H
.word 0DB7EH
.word 045DFH
.word 045DFH
.word 0DB7EH
.word 0AF9H
.word 0F3F2H
.word 0D61H
.word 89H
.word 1008H
.word 420H
.word 25CH
.word 1AH
.word 4BAH
.word 0FA2FH
.word 0FEE2H
.word 0F7CCH
.word 0FE07H
.word 0F9D9H
.word 8DH
.word 0FD35H
.word 2ACH
.word 0FEAFH
.word 282H
.word 0FE1BH
.word 155H
.word 0FDA1H
.word 14EH
.word 0FEB9H

9.SAWTOOTH WAVE GENERATION


;******************************************************************
DATA .SET 0H
.mmregs
.text
START:
STM #140H,ST0 ;initialize the data page pointer
RSBX CPL ;make the processor to work using DP
NOP
NOP
NOP
NOP
REP:
ST #0H,DATA ;initialize the value as 0h
INC:
LD DATA,A
ADD #1H,A ;increment the value
STL A,DATA
PORTW DATA,04H ;send the value to the dac
CMPM DATA,#0FFFH ;repeat the loop until the value becomes 0fffh
BC INC,NTC
B REP ;repeat the above
10.SINE WAVE GENERATION
;******************************************************************
PROGRAM DESCRIPTION:
; The sine table values are stored in the program memory. Accumulator A
;is initially loaded with the starting address of the sine table. The
;reada instruction, reads the content of the address which is stored in acc.
;That data is sent to the dac and the acc. content is incremented to point
;to the next value in the sin table.
DATA .SET 0H
ADD .SET 1H
TABLE .SET 200H
.mmregs
.text
START:
STM #140H,ST0 ;initialize the data page pointer
RSBX CPL ;make the processor to work using DP
NOP
NOP
NOP
NOP
REP:
LD #TABLE,A ;Load the address first value of the sine
;table to the accumulator
STM #372,AR1 ;no. of sine table values
LOOP:
READA DATA ;read the sine table value
PORTW DATA,04H ;send it to the dac
ADD #1H,A ;increment the sine table address which
;is in the accumulator
BANZ LOOP,*AR1- ;repeat the loop until all 372 values has
;been sent
B REP ;repeat the above
11.SQUARE WAVE GENERATION
;******************************************************************
DATA .SET 0H
.mmregs
.text
START:
STM #140H,ST0 ;initialize the data page pointer
RSBX CPL ;make the processor to work using DP
NOP
NOP
NOP
NOP
REP:
ST #0H,DATA ;send 0h to the dac
CALL DELAY ;delay for some time
ST #0FFFH,DATA ;send 0fffh to the dac
CALL DELAY ;delay for some time
B REP ;repeat the same
DELAY:
STM #0FFFH,AR1
DEL1:
PORTW DATA,04H
BANZ DEL1,*AR1-
RET
12.TRIANGULAR WAVE GENERATION
;****************************************************************** DATA .SET 0H
.mmregs
.text
START:
STM #140H,ST0 ;initialize the data page pointer
RSBX CPL ;make the processor to work using DP
NOP
NOP
NOP
NOP
REP:
ST #0H,DATA ;initialize the value as 0h
INC:
LD DATA,A ;increment the value
ADD #1H,A
STL A,DATA
PORTW DATA,04H ;send the value to the dac
CMPM DATA,#0FFFH ;repeat the loop until the value becomes 0fffh
BC INC,NTC
DEC:
LD DATA,A ;decrement the value
SUB #1H,A
STL A,DATA
PORTW DATA,04H ;send the value to the dac
CMPM DATA,#0H
BC DEC,NTC ;repeat the loop until the value becomes 0h
B REP ;repeat the above
MATLAB PROGRAMS

1.UPSAMPLING

n=0:1:5;
x=[1 2 3 4 5]
y=upsample(x,2);
disp(y);
subplot(2,2,1);
plot(y);
title('up sampling');
subplot(2,2,2);
y=downsample(x,2);
disp(y);
plot(y);
title('down sample');

2.POLYPHASE DECOMPOSITION

%Analog signal

t=-0.005:0.00005:0.005;
xa=exp(-1000*abs(t));
%Discrete time signal
Fs=input('Enter the sampling frequency(Fs)= ');
Ts=1/Fs;
n=-25:1:25;
xn=exp(-1000*abs(n*Ts));
%Discrete time fourier transform
N=500;
k=0:1:N;
w=(2*pi*k/N);
X=xn*exp(-1i*n'*w);
X=abs(X);
%omega range from -Wmax to Wmax
W=[-fliplr(w),w(2:N+1)];
%X over -Wmax to Wmax interval
X=[fliplr(X),X(2:N+1)];
subplot(2,1,1);
plot(t*1000,xa,'k');
grid;
hold on;
stem(n*Ts*1000,xn,'k');
xlabel('time in sec');
ylabel('x(n)');
title('discrete time signal');
gtext('Ts=1msec');
hold off;

3.CALCULATING IMPULSE AND STEP RESPONSES FOR A GIVEN DIFFERENCE EQUATION AND
ALSO PLOTTING FREQUENCY RESPONSE

a=[1 -0.4];
b=[0.5 1.5];
c=dimpulse(b,a);
d=dstep(b,a);
disp('The impulse response is');c
disp('The step response is');d
subplot(3,1,1);
stem(c);
subplot(3,1,2);
stem(d);
w=-pi:0.01:pi;
h=freqz(b,a,w);
m=20*log10(abs(h));
subplot(3,1,3);
plot(w/pi,m);
IIR FILTER PROGRAMS
1.DIGITAL LOWPASS BUTTERWORTH FILTER USING BILINEAR

clc;
close all;
clear all;
wp=input('Digital passband freq in rad/sec: ');
ws=input('Digital stopband freq in rad/sec: ');
gp=input('Passband gain: ');
gs=input('Stopband gain: ');
T=input('Sampling interval: ');
fs=1/T % Sampling freq
ap=-20*log10(gp) % Passbad ripple or attenuation Alpha_p in db
as=-20*log10(gs) % Stopband attenutaion Alpha_s in db
omp=(2/T)*tan(wp/2) % Analog passband freq in rad/sec
oms=(2/T)*tan(ws/2) % Analog stopband freq in rad/sec
[N,wc]=buttord(omp,oms,ap,as ,'s') % order & cutoff freq
[b,a]=butter(N,wc,'s') ;
HS=tf(b,a) % LPF transfer function H(S)
[num,den]=bilinear(b,a,1/T) ;
HZ=tf(num,den,T) % Digital transfer function H(Z)
freqz(num,den);

ANS:
Digital passband freq in rad/sec: 0.35*pi
Digital stopband freq in rad/sec: 0.7*pi
Passband gain: 0.6
Stopband gain: 0.1
Sampling interval: 0.1
fs = 10
ap = 4.4370
as = 20
omp = 12.2560
oms = 39.2522
N= 2
wc = 12.4439
Transfer function:
154.8
--------------------
s^2 + 17.6 s + 154.8
Transfer function:
0.1708 z^2 + 0.3415 z + 0.1708
------------------------------
z^2 - 0.5407 z + 0.2237
Sampling time: 0.1
2.DIGITAL HIGHPASS BUTTERWORTH FILTER USING BILINEAR

clc;
close all;
clear all;
wp=input('Digital passband freq in rad/sec: ');
ws=input('Digital stopband freq in rad/sec: ');
gp=input('Passband gain: ');
gs=input('Stopband gain: ');
T=input('Sampling interval: ');
fs=1/T % Sampling freq
ap=-20*log10(gp) % Passbad ripple or attenuation Alpha_p in db
as=-20*log10(gs) % Stopband attenutaion Alpha_s in db
omp=(2/T)*tan(wp/2) % Analog passband freq in rad/sec
oms=(2/T)*tan(ws/2) % Analog stopband freq in rad/sec
[N,wc]=buttord(omp,oms,ap,as ,'s') % order & cutoff freq
[b,a]=butter(N,wc,’high’,'s') ;
HS=tf(b,a) % LPF transfer function H(S)
[num,den]=bilinear(b,a,1/T) ;
HZ=tf(num,den,T) % Digital transfer function H(Z)
freqz(num,den);

ANS
Digital passband freq in rad/sec: 0.35*pi
Digital stopband freq in rad/sec: 0.7*pi
Passband gain: 0.6
Stopband gain: 0.1
Sampling interval: 0.1
fs = 10
ap = 4.4370
as = 20
omp = 12.2560
oms = 39.2522
N= 2
wc = 12.4439
Transfer function:
s^2
--------------------
s^2 + 17.6 s + 154.8
Transfer function:
0.4411 z^2 - 0.8822 z + 0.4411
------------------------------
z^2 - 0.5407 z + 0.2237
Sampling time: 0.1
3.DIGITAL BANDPASS BUTTERWORTH FILTER USING BILINEAR

clc;
close all;
clear all;
wp=input('Digital passband freq in rad/sec: ');
ws=input('Digital stopband freq in rad/sec: ');
gp=input('Passband gain: ');
gs=input('Stopband gain: ');
T=input('Sampling interval: ');
fs=1/T % Sampling freq
ap=-20*log10(gp) % Passbad ripple or attenuation Alpha_p in db
as=-20*log10(gs) % Stopband attenutaion Alpha_s in db
omp=(2/T)*tan(wp/2) % Analog passband freq in rad/sec
oms=(2/T)*tan(ws/2) % Analog stopband freq in rad/sec
[N,wc]=buttord(omp,oms,ap,as ,'s') % order & cutoff freq
[b,a]=butter(N,wc,’bandpass’,'s') ;
HS=tf(b,a) % LPF transfer function H(S)
[num,den]=bilinear(b,a,1/T) ;
HZ=tf(num,den,T) % Digital transfer function H(Z)
freqz(num,den);

ANS

Digital passband freq in rad/sec: [0.4*pi 0.6*pi]


Digital stopband freq in rad/sec: [0.1*pi 0.8*pi]
Passband gain: 0.6
Stopband gain: 0.1
Sampling interval: 0.001
fs = 1000
ap = 4.4370
as = 20
omp = 1.0e+003 * 1.4531 2.7528
oms = 1.0e+003 * 0.3168 6.1554
N= 2
wc = 1.0e+003 * 1.3094 3.0548
Transfer function:
3.046e006 s^2
------------------------------------------------------
s^4 + 2468 s^3 + 1.105e007 s^2 + 9.873e009 s + 1.6e013

Transfer function:
0.1053 z^4 - 1.242e-015 z^3 - 0.2107 z^2 - 7.567e-016 z + 0.1053
----------------------------------------------------------------
z^4 + 1.388e-015 z^3 + 0.8958 z^2 + 7.216e-016 z + 0.3172

Sampling time: 0.001


4.DIGITAL LOWPASS BUTTERWORTH FILTER USING IMPULSE INVARIANCE
TRANSFORMATION

clc;
close all;
clear all;
wp=input('Digital passband freq in rad/sec: ');
ws=input('Digital stopband freq in rad/sec: ');
gp=input('Passband gain: ');
gs=input('Stopband gain: ');
T=input('Sampling interval: ');
fs=1/T % Sampling freq
ap=-20*log10(gp) % Passbad ripple or attenuation Alpha_p in db
as=-20*log10(gs) % Stopband attenutaion Alpha_s in db
omp=wp/T % Analog passband freq in rad/sec
oms=ws/T % Analog stopband freq in rad/sec
[N,wc]=buttord(omp,oms,ap,as ,'s') % order & cutoff freq
[b,a]=butter(N,wc,'s') ;
HS=tf(b,a) % LPF transfer function H(S)
[num,den]=impinvar(b,a,1/T) ;
HZ=tf(num,den,T) % Digital transfer function H(Z)
freqz(num,den);

ANS
Digital passband freq in rad/sec: 0.3*pi
Digital stopband freq in rad/sec: 0.75*pi
Passband gain: 0.707
Stopband gain: .2
Sampling interval: 1
fs = 1
ap = 3.0116
as = 13.9794
omp = 0.9425
oms = 2.3562
N= 2
wc = 1.0645
Transfer function:
1.133
---------------------
s^2 + 1.505 s + 1.133
Transfer function:
0.4848 z
-----------------------
z^2 - 0.6876 z + 0.2219

Sampling time: 1
5.DIGITAL LOWPASS CHEBYSHEV FILTER USING BILINEAR

clc;
close all;
clear all;

wp=input('Digital passband freq in rad/sec: ');


ws=input('Digital stopband freq in rad/sec: ');
gp=input('Passband gain: ');
gs=input('Stopband gain: ');
T=input('Sampling interval: ');
fs=1/T % Sampling freq
ap=-20*log10(gp) % Passbad ripple or attenuation Alpha_p in db
as=-20*log10(gs) % Stopband attenutaion Alpha_s in db
omp=(2/T)*tan(wp/2) % Analog passband freq in rad/sec
oms=(2/T)*tan(ws/2) % Analog stopband freq in rad/sec
[N,wc]=cheb1ord(omp,oms,ap,as ,'s') % order & cutoff freq
[bn,an]=cheby1(N,ap,1,'s') ;
HaS=tf(bn,an) % Normalized analog transfer function H(S)
[b,a]=cheby1(N,ap,wc,'s') ;
HS=tf(b,a) % LPF transfer function H(S)
[num,den]=bilinear(b,a,1/T) ;
HZ=tf(num,den,T) % Digital transfer function H(Z)
freqz(num,den);

ANS
Digital passband freq in rad/sec: .2*pi
Digital stopband freq in rad/sec: .32*pi
Passband gain: .8
Stopband gain: .2
Sampling interval: 1
fs = 1
ap = 1.9382
as = 13.9794
omp = 0.6498
oms = 1.0995
N= 3
wc = 0.6498

Transfer function:
0.3333
----------------------------------
s^3 + 0.7489 s^2 + 1.03 s + 0.3333

Transfer function:
0.09147
-------------------------------------
s^3 + 0.4867 s^2 + 0.4351 s + 0.09147

Transfer function:
0.008386 z^3 + 0.02516 z^2 + 0.02516 z + 0.008386
-------------------------------------------------
z^3 - 2.274 z^2 + 1.967 z - 0.6263

Sampling time: 1

6.Design of LPF using bilinear transformation and Impulse invariance Method

clc;
close all;
clear all;

fc=150;
fs=1300;
T=1/fs;
wc=(2*pi*fc)/fs;
N=input('enter the order of the filter');

% Magnitude Response of LPF using bilinear transformation


figure(1);
omp1=(2/T)*tan(wc/2);
[b1,a1]=butter(N,omp1,'s') ;
HS1=tf(b1,a1) % LPF transfer function H(S)
[num1,den1]=bilinear(b1,a1,1/T) ;
HZ1=tf(num1,den1,T) % Digital transfer function H(Z)
freqz(num1,den1);

% Magnitude Response of LPF using Impulse invariance Method


figure(2);
omp2=wc/T;
[b2,a2]=butter(N,omp2,'s') ;
HS2=tf(b2,a2) % LPF transfer function H(S)
[num2,den2]=bilinear(b2,a2,1/T) ;
HZ2=tf(num2,den2,T) % Digital transfer function H(Z)
freqz(num2,den2);
FIR FILTER PROGRAMS

1.FIR LOWPASS AND HIGHPASS USING HAMMING WINDOW

fc=300;
fs=2000;

n = input('order: ');
n=mod(n,2)+n;
wc=2*fc/fs;

figure(1);
b=fir1(n,wc,hamming(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');

figure(2);
b=fir1(n,wc,'high',hamming(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');

2.FIR LOWPASS AND HIGHPASS USING BLACKMANN WINDOW

fc=300;
fs=2000;
t=0:1/fs:0.2;
n = input('order: ');
n=mod(n,2)+n;
wc=2*fc/fs;
figure(1);
b=fir1(n,wc,blackman(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');

figure(2);
b=fir1(n,wc,'high',blackman(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of highpass
filter');

3.FIR BANDPASS USING RECTANGULAR WINDOW

fc1=100;
fc2=450;
fs=1000;

n = input('order: ');
n=mod(n,2)+n;

wc1=2*fc1/fs;
wc2=2*fc2/fs;
wn=[wc1 wc2 ];

b=fir1(n,wn,'bandpass',boxcar(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');

4.FIR BANDSTOP USING RECTANGUALR AND HANNING WINDOW

clc;
close all;
clear all;

fc1=200;
fc2=450;
fs=1000;

n = input('order: ');
n=mod(n,2)+n;

wc1=2*fc1/fs;
wc2=2*fc2/fs;
wn=[wc1 wc2 ];

figure(1);
b=fir1(n,wn,'stop',boxcar(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');
figure(2);
b=fir1(n,wn,'stop',hanning(n+1));
freqz(b,1,512)
xlabel('freq in hz' );ylabel('gain in db');title('freq response of lowpass
filter');

You might also like