Embedded LAB MANUAL
Embedded LAB MANUAL
Name : ………………………………………………………………………………………
USN : ………………………………………………………………………………………
Batch: ………………………………………………………………………………………
EMBEDDED CONTROLLER LAB 18ECL46
CONTENTS
III Experiments
PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding Page No. 11-17
largest element in an Array
Arithmetic Instructions - Addition/subtraction, Page No. 18-25
2.
multiplication
3. Counters Page No. 26-30
11. Design a Latch and Flip-Flop to compare the synthesis report Page No. 74-75
of D, SR, JK.
Page No. 81-83
IV Viva Questions
INSTITUTE
Vision
To emerge as one of the finest technical institutions of higher learning, to develop engineering
professionals who are technically competent, ethical and environment friendly for betterment
of the society.
Mission
Accomplish stimulating learning environment through high quality academic instruction, innovation
and industry-institute interface.
DEPARTMENT
Vision
Emerge as premier department developing high quality Telecommunication Engineering
professionals with ethics and eco friendliness for the betterment of the society.
Mission
Impart quality education in Telecommunication Engineering by facilitating:
Conducive learning environment and research activities
Good communication skills, leadership qualities and ethics
Strong Industry-Institute interaction
Ground Rules
Maintain silence
DO’s
DON’Ts
Do not use cell phones, iPods, and any similar devices in labs
CYCLE OF EXPERIMENTS
CYCLE 1 – PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an Array.
3. Counters.
CYCLE 2 – VLSI(CADENS)
6. Draw the schematic and Layout of a CMOS Inverter using library files.
7. Draw the schematic and Layout of a CMOS 2 input NAND gate using library files.
8. Draw the schematic and layout of common source amplifier with PMOS current mirror load.
11. Design a Latch and Flip-Flop to compare the synthesis report of D, SR, JK.
1 Conduct an open ended experiment to design a simple Calculator / Elevator using 8051
microcontroller.
FEATURES OF 8051
1.ALU
3.Registers
PART I
PROGRAMMING
EXPERIMENT NO.1
OBJECTIVE:
TO TRANSFER A BLOCK OF DATA BYTES FROM SOURCE MEMORY TO DESTINATION
MEMORY USING 8051.
PROGRAM:
BACK: MOV A,@R0 // Get the data from source memory pointer
MOV @R1,A // Store the data into destination memory pointer
END
MEMORY WINDOW:
Before execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 00 00 00 00 00 00
After execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 AB 3D 44 55 00
1. 2 BLOCK EXCHANGE
OBJECTIVE:
TO EXCHANGE TWO BLOCKS OF DATA BYTES USING 8051
PROGRAM:
MOV R0,#50H // Initialize the source memory pointer
MOV R1,#60H // Initialize the destination memory pointer
After execution:
D:0x50H: 06 07 08 09 10 00
D:0X60H: 01 02 03 04 05 00
OBJECTIVE:
TO FIND THE LARGEST/SMALLEST ELEMENT IN AN ARRAY USING 8051
PROGRAM TO FIND THE LARGEST NUMBER:
MOV R0,#50H //Initialize the source memory pointer
MOV R2,#05H //Initialize Iteration counter
MOV B, @R0 /* Use B Register to store largest value and initialize it to the first
value*/
BACK: MOV A,@R0 /* Get the data from source memory pointer and Load into
accumulator*/
CJNE A,B,LOOP /* Compare the data if not equal, go to relative
address(LOOP)*/
LOOP: JC LOOP1 // If carry generates, go to relative address LOOP1
MOV B,A // Store larger value into B-register
INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to
relative address and
repeat the same process until count become zero.*/
After execution:
D:0x50h: 22 AB 3D 44 55 00
D:0x60h: AB 00 00 00 00 00
After execution:
D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 00 00 00 00 00
1.4 SORTING
OBJECTIVE:
TO ARRANGE N 8-BIT NUMBERS IN ASCENDING ORDER.
PROGRAM:
MOV R2, #05H // Initialize the iteration counter
Before execution:
D:0x50H: 06 04 03 07 02 01
After execution:
D:0x50H: 01 02 03 04 06 07
OBJECTIVE:
Before execution:
D:0x50H: 06 04 03 07 02 01
After execution:
D:0x50H: 07 06 04 03 02 01
WORKSHEET
Write an ALP to generate eight Fibonacci numbers using 8051
The first term must be zero and second term must be one
Add the current term and previous term, store in the next term
MOVC A,@R0
MOV A,@R0
MOV A,@R1
MOV A,@R2
MOVC A,@R0+DPTR
MOV @R3,A
MOV A,40H
MOV A,#40H
MOV 40H,A
MOV 40H,#0A
A= B= R0 = R2 =
EXPERIMENT NO.2
ARITHMETIC INSTRUCTIONS - ADDITION/SUBTRACTION, MULTIPLICATIO.
OBJECTIVE:
TWO UNDERSTAND THE ARITHMETIC OPERATIONS AND PERFORM 8/16 BIT
ADDITION/ SUBTRACTION AND MULTIPLICATION
PROGRAM:
MOV R0,#51H //Initialize input1 memory pointer
MOV R1,#61H /* Initialize input2 memory pointer and store output also same
*/
MOV R2,#02H // Initialize iteration count
CLR C
BACK: MOV A,@R0 /*Get lower bytes data in first iteration, upper bytes data in
second iteration, add them with carry and store in memory pointer2.*/
ADDC A,@R1
MOV @R1,A
DEC R0 // Increment memory pointer1 & 2 to get upper bytes
DEC R1
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, go to relative
address and repeat the same process until count become zero.*/
JNC FINISH
MOV @R1,#01H
FINISH:SJMP $
END
MEMORY WINDOW
Before execution:
D:0x50H: FD07 00 00 00 00
D:0X60H: FF5F 00 00 00 00
After execution:
D:0x50H: FD07 00 00 00 00
D:0X5FH: 01 FC 6600 00 00
D:0x50H: FA F4 00 00 00 00
D:0X60H: 02 F5 00 00 00 00
After execution:
D:0x50H: FA F4 00 00 00 00
D:0X60H: F7 FF 00 00 00 00
Eg. 0025 – 0AF6 = FFF52F (ANSWER IS NEGATIVE)
Before execution:
D:0x50H: 0025 00 00 00 00
D:0X60H: 0A F6 00 00 00 00
After execution:
D:0x50H: 0025 00 00 00 00
D:0x5FH: FF F52F 00 00 00
RESULT :
REGISTER VALUES:
R6 R7 = FF FF
R4 R5 = FF FF
R0 R1 R2 R3 = FF FE 00 01
EXPERIMENT NO.3
COUNTERS
OBJECTIVES:
TWO UNDERSTAND THE SIMULATION OF BINARY/BCD UP/DOWN COUNTERS AND
TO KNOW THE CONCEPTS OF SUBROUTINES
RESULT:
During execution:
D:0x50H : (FFH to 00H ) (FFH to 00H) 00 00 00 00
RESULT
During Execution: D:0x50H :(99H to 00H) (99H to 00H) 00 00 00 00
Assignment:
Following is a delay subroutine. Find how much delay this subroutine provides. Insert
the delay appropriately inside the BCD COUNTER program so that each count should
DELAY SUBROUTINE:
DELAY: NOP
MOV R2,#25
DJNZ R4,$
DJNZ R3,HI
DJNZ R2,H0
RET
EXPERIMENT NO.4
BOOLEAN & LOGICAL INSTRUCTIONS (BIT MANIPULATIONS)
OBJECTIVES:
TWO UNDERSTAND THE BASIC LOGICAL BIT AND BYTE OPERATIONS
4. 1. EXAMPLES FOR LOGICAL BYTE OPERATIONS
ORG 00H
MOV R0, #34H
MOV A, R0
ANL A, #0FH //and logical operation
MOV P1, A
MOV A, R0
ORL A, #0FH //or logical operations
MOV P1, A
MOV A, R0
XRL A, #0FH //exclusive or logical operations
MOV P1, A
MOV A, R0
CPL A //complement logical operations
MOV P1, A
MOV A, R0
CLR A //clear logical operations
MOV P1, A
MOV A, R0
RR A //rotate right logical operations
RR A
RR A
RR A
MOV P1, A
MOV A, R0
RL A //rotate left logical operations
RL A
RL A
RL A
MOV P1, A
MOV A, R0
SETB C
RRC A //rotate right with carry logical operations
RRC A
RRC A
RRC A
MOV P1, A
MOV A, R0
RLC A //rotate left with carry logical operations
RLC A
RLC A
RLC A
MOV P1, A
SJMP $
END
SJMP $
END
4. 4 BOOLEAN EXPRESSIONS
1. WRITE PROGRAMS TO REALIZE SOME BOOLEAN EXPRESSIONS
TABLE : 1
BIT ADDRESSIBLE MEMORY LOCATIONS AND CORRESPONGING ADDRESSES
// VARIABLE DECLARATION
X EQU 10H
Y EQU 11H
Z EQU 12H
NOT_X EQU 20H
NOT_Y EQU 21H
NOT_Z EQU 22H
MOV X, C
CPL C
MOV NOT_X, C
MOV C, P1.1
MOV Y, C
CPL C
MOV NOT_Y, C
MOV C, P1.2
MOV Z, C
CPL C
MOV NOT_Z, C
//EXPRESSION OUTPUT = X+ YZ
CLR C
MOV C, Y
ANL C, Z
ORL C, X
MOV OUTPUT, C
//EXPRESSION OUTPUT =!X+ !Y+ !Z
CLR C
MOV C, NOT_X
ORL C, NOT_Y
ORL C, NOT_Z
MOV OUTPUT, C
//EXPRESSION OUTPUT =!(X+Y)+ Z
CLR C
MOV C, X
ORL C, Y
CPL C
ORL C, Z
MOV OUTPUT, C
//EXPRESSION OUTPUT =!(X+Y+Z)
MOV C, X
ORL C, Y
ORL C, Z
CPL C
MOV OUTPUT, C
NOTE:
Give minimum 4 input combinations and verify the results for each expression
EXPERIMENT NO.5
CODE CONVERSION
OBJECTIVES:
TWO PERFORM CONVERSIONS OF DIFFERENT NUMBER SYSTEM
REPRESENTATIONS BY MAKING USE OF LOGICAL INSTRUCTIONS.
5. 1 BCD – ASCII
; register a has packed BCD
; Program to convert BCD to '2' ASCII numbers
ORG 0H
MOV A, #29H //mov 29 to A(packed BCD)
MOV R2, A //copy of A (BCD DATA)in R2
ANL A, #0FH //mask the upper nibble A=09h
ORL A, #30H //maske it as ASCII to A=39h(ASCII char 09h
MOV R6,A //save it in R6
Department of ETE, BMSIT & M. 41
| Page
EMBEDDED CONTROLLER LAB 18ECL46
5. 2. ASCII – DECIMAL
ORG 0
MOV R5, #"4"// LOADING R5 WITH ASCII EQUIVALENT OF 4 (IE, 34)
MOV R6, #"7"// LOADING R6 WITH ASCII EQUIVALENT OF 7 (IE, 37)
MOV A, R5
ANL A, #0FH // MASK LOWER NIBBLE
MOV R5, A
MOV A, R6
ANL A, #0FH // MASK UPPER NIBBLE
MOV R6, A
MOV A, R5
RL A
RL A
RL A
RL A
ORL A, R6 // PACKING
MOV P1,A
SJMP $
END
5. 3. DECIMAL – ASCII
(Conversion of Decimal number 0-9 to corresponding ASCII equivalent)
ORG 00H
MAIN: MOV R0,#RAM_ADDR
MOV R1,#ASC_RSULT
MOV R2,#0AH
BACK: MOV A,@R0 //stored decimal values
ORL A,#30H //are fetched ,they are ‘OR’ed with
MOV @R1,A //30 and results are placed at memory
INC R0 //pointed by R1 pointer
INC R1
DJNZ R2,BACK
MOV R1,#ASC_RSULT
PART II
VLSI (CADENCE)
VIVA QUESTIONS
1. Upon reset, all ports of the 8051 are configured as _____________ (output, input).
2. Which ports of the 8051 have internal pull-up resistors?
3. Which ports of the 8051 require the connection of external pull-up resistors in order to
be used for I/O? Show the drawing for the connection.
4. In the 8051, explain why we must write "1" to a port in order for it to be used for input.
5. Explain why we need to buffer the switches used as input in order to avoid damaging the
8051 port.
6. How does the LCD distinguish data from instruction codes when receiving information at
its data pin?
7. To send the instruction code 01 to clear the display, we must make RS = ___.
8. To send letter 'A' to be displayed on the LCD, we must make RS = ____.
9. What is the purpose of the E line? Is it an input or an output as far as the LCD is
concerned?
10. When is the information (code or data) on the LCD pin latched into the LCD?
11. Indicate the direction of pins WR, RD, and INTR from the point of view of the 8051.
12. Give the three steps for converting data and getting the data out of the ADC804. State the
status of the CS, RD, INTR, and WR pins in each step.
13. Assume that Vref/2 is connected to 1.28 V. Find the following.
13.1. step size
13.2. maximum range for Vin
13.3. D7 D0 values if Vin = 1.2 V
13.4. Vin if D7 D0 = 11111111
13.5. Vin if D7 D0 = 10011100
14. Assume that Vref/2 is connected to 1.9 V. Find the following.
14.1. step size
14.2. maximum range for Vin
14.3. D7 D0 values if Vin = 2.7 V
14.4. Vin if D7 D0 = 11111111
14.5. Vin if D7 D0 = 11011101
15. The ADC804 is a(n) ____-bit converter.
16. To get step size of 2 mV, what is the value for Vref/2?
17. What is a transducer?
18. What is the form of the transducer output?
19. What is preprocessing of transducer signals to be fed into an ADC called?
20. The LM35 and LM34 produce a _______ mV output for every degree of change in
temperature.
21. The LM35/LM34 is a ____________ (linear, nonlinear) device. Discuss the advantages of
linear devices and of nonlinear devices.
22. Explain signal conditioning and its role in data acquisition.
23. What is the maximum frequency that can be generated using Mode 1 if the crystal
frequency is 11.0592 MHz? Show your calculation.
24. What is the maximum frequency that can be generated using Mode 2 if the crystal
frequency is 11.0592 MHz? Show your calculation.
25. What is the lowest frequency that can be generated using Mode 1 if the crystal frequency
is 11.0592 MHz? Show your calculation.
26. What is the lowest frequency that can be generated using Mode 1 if the crystal frequency
is 11.0592 MHz? Show your calculation.
27. In mode 1, when is TFx set to high?
28. In mode 2, when is TFx set to high?
29. The 8051 TxD and RxD signals ________ (are, are not) TTL-compatible.
30. In this lab, what is the role of the MAX233 (MAX232) chip?
31. With XTAL=11.0592 MHz, what is the maximum baud rate for the 8051?
32. Show how to achieve the maximum baud rate
33. What is the role of TI and RI?
34. True or false. The 8051 can transfer data in full-duplex.
35. For full duplex, what are the absolute minimum signals needed between the 8051 and the
PC? Give their names.
36. What is a step angle? Define steps per revolution.
37. If a given stepper motor has a step angle of 5 degrees, find the number of steps per
revolution.
38. Give the four sequences for counter clockwise if it starts with 10011001 (binary).
39. Using the "RL A" instruction, show the fourstep sequences if the initial step is 0011
(binary).
40. Give the number of times the fourstep sequence must be applied to a stepper motor to
make a 100degree move if the motor has a 5degree step angle. Also fill in the
characteristics for your motor below.
40.1. Step angle _______Degree of movement per 4step sequence ________
40.2. Steps per revolution _____ Number of rotor teeth ______________
40.3. What is the purpose of generating the truth table for a given keyboard?
41. What is the purpose of grounding each row in keyboard interfacing?
42. What is the input to the microcontroller from column if no key is pressed?
43. True or false. In our N x M matrix keypad program we cannot press two keys at the same
time.
44. In your program in, how is the key press detected?
45. In your program in, how is a key press identified?
46. Explain the role of the C/T bit in the TMOD register.
47. How is the 8051 used as an event counter to count an external event?
48. If timer/counter 0 is used as an event counter, what is the maximum count for the
following modes.
48.1. Mode 1
48.2. Mode 2
49. Indicate which pin is used for the following.
49.1. timer/counter 0
49.2. timer/counter 1
50. If timer/counter 0 is used in mode 1 to count an external event, explain when TF0 is set
to high.
51. If timer/counter 1 is used in mode 2 to count an external event, explain when TF0 is set
to high.
52. Indicate the direction of pins ALE, SC, EOC, and OE from the point of view of the
ADC808/809.
53. Give the steps for converting data and getting the data out of the ADC809. State the status
of the SC and EOC pins in each step.
54. Give the role of signals ALE, A, B, and C in selecting the ADC channel.
55. In the ADC809 assume that Vref is connected to 2.56 V. Find the following.
55.1. step size
55.2. maximum range for Vin
55.3. D7 D0 values if Vin = 1.2 V
55.4. Vin if D7 D0 = 11111111
55.5. Vin if D7 D0 = 10011100
56. In the ADC809 assume that Vref is connected to 5V. Find the following.
56.1. step size
56.2. maximum range for Vin
56.3. D7 D0 values if Vin = 2.7 V
56.4. Vin if D7 D0 = 11111111
56.5. Vin if D7 – D0 = 11011101
57. In connecting ADC808/809 to an 8051, indicate the direction of pins ALE, SC, EOC, and
OE from the point of view of the 8051.
58. Define the following terminology in DAC.
58.1. resolution
58.2. fullscale voltage output
58.3. settling time
59. For your circuit, find Vout for the following inputs.
59.1. 11001100
59.2. 10001111
60. To get a smaller step size, we need DAC with ________ (more, less) data bit inputs.
61. In Figure 13-7 of the textbook, assume that R = 2.5 K ohms. Calculate Vout for the
following binary inputs.
61.1. 11000010
61.2. 01000001
61.3. 00101100
61.4. 11111111
62. Name all of the interrupts in the 8051 and their vector table addresses.
63. In timer mode 1, indicate when TF0 causes the interrupt.
64. In timer mode 2, indicate when TF0 causes the interrupt.
65. On reset, INT0 (and INT1) are _________ (edge, level) triggered.
20
15
10
clc;
clear all;
close all;
y=x*h;
disp('circular convolution');
disp(y);
stem(y);
xlabel('n');
ylabel('y(n)');
grid on;
RESULT:
30
25
clc
n=length(x)
c=zeros(n,'double')
for i=1:n
c(i,1)=x(i)
end
p=1; k=1;
for j=2:n
t=1;
for i=1:n
if i<j
c(i,j)=c((n-p)+t,1)
t=t+1
else
c(i,j)=c(i-k,1)
end
end
p=p+1
k=k+1
end
cnv=c*h'
clc;
clear all;
N1=length(x);
N2=length(h);
N=max(N1,N2);
N3=N1-N2;
if (N3>=0)
h=[h,zeros(1,N3)];
else
x=[x,zeros(1,N3)];
end
for r=1:N
y(r)=0;
for (s=1:N)
if (k<=0)
k=k+N;
y(r)=y(r)+x(s)*h(k);
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
Result:
x=
2 3 4 5
h=
1 1 1
The linear convolution is the convolution process for the discrete time
systems. The convolution of discrete time (DT) signals possesses the following
properties:
The commutative property states that the order in which two sequences
are convolved is not important. Mathematically, the commutative property is x(n)*h(n) =
h(n)*x(n).
From a systems point of view, this property states that a system with a unit
sample response h(n) and input x(n) behaves in exactly the same way as a system with
unit sample response x(n) and an input h(n). This is illustrated in Fig. 2 (a) .
From a systems point of view, the associative property states that if two systems
with unit sample responses h1(n) and h2(n) are connected in cascade as shown in Fig. 2
(b), an equivalent system is one that has a unit sample response equal to the convolution
of hI (n) and h2(n): heq[n] =h1[n]*h2[n].
Main Program
clc;
clear all;
close all;
inputs x=[1 2 3
4]; h1=[2 5 6 7];
h2=[1 2 3 4];
if y1==y2
else
end
if y3==y4
else
%z1 = conv(x,h1);
%z2= conv(x,h2);
if y5==y6
else
end
Result :
Main Program
clc;
clear all;
close all;
inputs x=[1 2 3
4]; h1=[2 5 6 7];
h2=[1 2 3 4];
Commutative Property
y1=circonv(x,h1);
y2=circonv(h1,x);
if y1==y2
%Associative Property
y3= circonv(x,circonv(h1,h2));
y4=circonv(circonv(x,h1),h2);
if y3==y4
else
%Distributive Property
y5=circonv (x,(h1+h2));
if y5==y6
else
end
function[y]=circonv(x,h)
N=length(x);
for n=0:N-1;
y(n+1)=0;
for k=0:N-1;
i=mod((n-k),N);
if i<0
i=i+1;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end
Note : Write the function file separately and save in the same
folder with file name as: function name with dot m (.m)
extension.
Result :
Viva Questions :
Write A Program to find the circular convolution of two sequences using Matrix
Method
EXPERIMENT NO. 3
Aim: To obtain autocorrelation of the given sequence and verify its properties.
Theory:
Correlation is mathematical technique which indicates whether 2 signals are related and in
a precise quantitative way how much they are related. A measure of similarity between
a pair of energy signals x[n] and y[n] is given by the cross
The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.
∞
The autocorrelation sequence is an even function i.e., rxx [l] = rxx [−l]
At zero lag, i.e., at l=0, the sample value of the autocorrelation sequence has its
maximum value (equal to the total energy of the signal εx) i.e.,
∞
This is verified in Fig. 5.1, where the autocorrelation of the rectangular pulse (square)
has a maximum value at l=0. All other samples are of lower value. Also the maximum
value = 11 = energy of the pulse [12+12+12..].
A time shift of a signal does not change its autocorrelation sequence. For example,
let y[n]=x[n-k]; then ryy[l] = rxx[l] i.e., the autocorrelation of x[n] and y[n] are the same
regardless of the value of the time shift k. This can be verified with a sine and cosine
sequences of same amplitude and frequency will have identical autocorrelation
functions.
N −1
1
period N it is rxx [l] =
N
∑ x[n]x[n − l]; l = 0,±1,±2,... and this rxx[l] is also
n=0
periodic with N. This is verified in Fig. 5.3 where we use the periodicity property of
the autocorrelation sequence to determine the period of the periodic signal y[n] which
is x[n] (=cos(0.25*pi*n)) corrupted by an additive uniformly distributed random noise
of amplitude in the range [-0.5 0.5]
Algorithm:
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR(A), when A is a vector, is the auto-
correlation sequence. If A is of length M vector (M>1), then the xcorr function returns the
length 2*M-1 auto-correlation sequence. The zeroth lag of the output correlation is in the
middle of the sequence at element M.
OCTAVE Programs
= -5:5;
N=10;
title('square sequence');
subplot(2,1,2)
disp('auto-correlation r=');
disp(r);
= -5:5;
N=10;
xlabel('Lag index');
ylabel('Amplitude');
disp('auto-correlation r=');
disp(r);
Result:
The output plot is in Fig.5.1
Autocorrelation sequence r = 1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000
10.0000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
3.0000 2.0000 1.0000
Maximum value of energy = 11
Exercise problem: Modify the above program for a given input x(n) =[1 2 3 4]
n = 1:N;
d = rand(1,N) - 0.5;
[ry,lag]=xcorr(y);
stem(y)
stem(lag, r);
Inference 2: The plot of noise corrupted signal y[n] (Fig. 5.3) does not show any noticeable
periodicity, but its autocorrelation sequence has distinct peaks at lags that are multiples of
8,indicating thaty[n] is a periodic with N=8 (cos(0.25*pi*n)=cos(wn) implies w=2*pi*f;
N= 1/f = 2/0.25 =8). Also notice the strong peak at zero lag. From Fig.5.2, the
autocorrelation of the noise component has a very strong peak only at zero lag. The
amplitudes are considerably smaller at other values of the lag as the sample values (of noise
generated by rand function) are uncorrelated with each other.
Cross Correlation has been introduced in the last experiment. Comparing the
equations for the linear convolution and cross correlation we find that
r∞ − l] ∞
n] y[−(l − n)] = x[l] * y[−l] . i.e.,
=∑ x
y
= ∑ x[ convolv
x[n]y[n
[ ing the
l
]
n=−∞ n=−∞
The cross correlation of two sequences x[n] and y[n]=x[n-k] shows a peak at the
value of k. Hence cross correlation is employed to compute the exact value of the
delay k between the 2 signals. Used in radar and sonar applications, where the
received signal reflected from the target is the delayed version of the transmitted
signal (measure delay to determine the distance of the target).
The ordering of the subscripts xy specifies that x[n] is the reference sequence that
remains fixed in time, whereas the sequence y[n] is shifted w.r.t
x[n]. If y[n] is the reference sequence then ryx [l] = rxy [−l] . Hence ryx[l] is
xi=[1,2,3,-1,0,0];
x2=[0,0,1,2,3,-1];
%=xr[n-2],i.e., k=2
>> xcorr(xi,x2)
ans ={−1,1,5,15,5,1,−1,0,0,0,0}
↑
Inference:
Also peak =15=energy of xi (1+22+32+(-1)2) implies that both xi and x2 are strongly
correlated.
xr=[1,2,3,-1];
x2=[3,-1,1,2];
Inference:
Strong peak of 10 at lag = 2 implies the delay between xr and x2 is 2, but since 10<15, it
implies that xr and x2 are uncorrelated slightly (may be due to noise,etc). ryx [l] = rxy [−l] is
verified.
Algorithm:
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR: Say C = XCORR(A,B), where A and B are
length M vectors (M>1), returns the length 2*M-1 cross-correlation sequence C. If A and
B are of different length, the shortest one is zero-padded. Using convolution to implement
correlation, the instruction is FLIPLR Flip matrix in left/right direction. FLIPLR(X)
returns X with row preserved and columns flipped in the left/right direction. X = 1 2 3
becomes 3 2 1.
OCTAVE Programs
n1 = length(y)-1;
n2 = length(x)-1;
r = conv(x,fliplr(y));
disp(r);
stem(k,r);
xlabel('Lag index');
ylabel('Amplitude');
Result:
4 -5 20 19 13 8 1
20
15
10
Amplitude
0
-10 -2 -1 0 1 2 3
-3
-5
Lag
index
n1 = length(y)-1;
n2 = length(x)-1; r =
xcorr(x,y);
%[r,lag]=xcorr(x,y);
disp('Cross correlation output is =');
disp(r);
N=max(n1,n2) ; k
= (-N):N;
stem(k,r); %stem(lag,r);
xlabel('Lag index');
ylabel('Amplitude');
Columns 1 through 9
Columns 10 through 17
Note: For sequences with different lengths, the length of the output using xcorr is
2*max(n1,n2)-1 = 2* 9-1=17 whereas using conv program, the length of output is n1+n2-
1.
Viva Questions
rand(1,N) - 0.5
fliplr(y)
xcorr(y)
EXPERIMENT NO. 4
Theory:
A difference equation with constant coefficients describes a LTI system. For example
the difference equation y[n] + 0.8y[n-2] + 0.6y[n-3] = x[n] + 0.7x[n-1] + 0.5x[n-
2] describes a LTI system of order 3. The coefficients 0.8, 0.7, etc are all constant
i.e., they are not functions of time (n). The difference equation y[n]+0.3ny[n-
1]=x[n] describes a time varying system as the coefficient 0.3n is not constant.
The difference equation can be solved to obtain y[n], the output for a given input x[n]
by rearranging as y[n] = x[n] + 0.7x[n-1]+0.5x[n-2]- 0.8y[n-2]-0.6y[n-3] and
solving.
With x[n]= δ[n], an impulse, the computed output y[n] is the impulse
response.
The difference equation containing past samples of output, i.e., y[n-1], y[n-2], etc leads
to a recursive system, whose impulse response is of infinite duration (IIR). For such
systems the impulse response is computed for a large value of n, say n=100 (to
approximate n=∞). The OCTAVE function filter is used to compute the impulse
response/ step response/ response to any given x[n]. Note: The filter function
evaluates the convolution of an infinite sequence (IIR) and x[n], which is not
possible with conv function (remember conv requires both the sequences to be
finite).
The difference equation having only y[n] and present and past samples of input (x[n],
x[n-k]), represents a system whose impulse response is of finite
duration (FIR). The response of FIR systems can be obtained by both the ‘conv’
and ‘filter’ functions. The filter function results in a response whose length is equal
to that of the input x[n], whereas the output sequence from conv function is of a
longer length (xlength + hlength-1).
Algorithm:
If IIR response, then input the length of the response required (say 100, which can be
made constant).
Plot the input sequence & impulse response (and also step response, etc if required).
OCTAVE Implementation:
OCTAVE has an inbuilt function ‘filter’ to solve difference equations numerically, given
the input and difference equation coefficients (b,a).
y=filter(b,a,x)
where x is the input sequence, y is the output sequence which is of same length as x.
Given a difference equation a0y[n]+a1y[n-1]+a2y[n-2]=b0x[n]+b2x[n-2], the coefficients
are written in a vector as b=[b0 0 b2] and a=[a0 a1 a2]. Note the zero in b (x[n-1] term is
missing).
For impulse response x[n] = {1,0,0,0,....} the number of zeros = the length of the IIR
↑
For step response x[n] = {1,1,1,1,1,....} the number of ones = the length of the IIR
↑
Similarly for any given x[n] sequence, the response y[n] can be calculated.
Problem 1.
Find the impulse response h(n), step response, response of the system to the input given by
x(n) = 2n u(n) of an LTI system described by a linear constant coefficient Difference
equation : y(n) +0.5 y(n-1) = 2x(n)
Plot the steady state response y[n] for the input x[n]=cos(0.05πn)u(n).
OCTAVE Program
a=[1,0.5]; %y coefficients
%impulse input
x=[1,zeros(1,N-1)];
n=0:1:N-1;
%impulse response
h=filter(b,a,x);
subplot(2,1,1);
stem(n,x);
title('impulse input');
xlabel('n');
ylabel('δ(n)');
subplot(2,1,2);
stem(n,h);
title('impulse response');
xlabel('n');
ylabel('h(n)');
Department of ETE, BMSIT & M. 96 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
Result:
impulse input
?(n)
0.5
1 2 3 4 5 6 7
00
n
impulse response
2
1
h(n)
0
- 0 1 2 3 4 5 6 7
1
a=[1,0.5]; %y coefficients
subplot(2,1,1);
stem(n,x);
title('step input');
xlabel('n');
ylabel('u(n)');
subplot(2,1,2);
stem(n,y);
title('step response');
xlabel('n');
ylabel('y(n)');
Result:
step input
u(n)
0.5
1 2 3 4 5 6 7 8 9
00
step response
2
1.5
y(n)
1
0.5
0 1 2 3 4 5 6 7 8 9
0
a=[1,0.5]; %y coefficients
subplot(2,1,1);
stem(n,x);
title('input sequence');
xlabel('n');
ylabel('x(n)');
subplot(2,1,2);
stem(n,y);
title('system response');
xlabel('n');
ylabel('y(n)');
Result:
y=
input sequence
20
15
10
x(n) 5
system response
30
20
y(n
)
10
0.5 1 1.5 2 2.5 3 3.5 4
00
subplot(2,1,1);
stem(n,x);
title('input sequence');
xlabel('n');
ylabel('x(n)');
subplot(2,1,2);
stem(n,y);
xlabel('n');
ylabel('y(n)');
Result:
Length of response required=25
y=
Columns 1 through 10
2.0000 0.9754 1.4144 1.0748 1.0806 0.8739 0.7386 0.5387 0.3487
0.1385
Columns 11 through 20
-0.0693 -0.2782 -0.4789 -0.6685 -0.8413 -0.9936 -1.1213 -1.2214 -1.2914 -1.3297
Columns 21 through 25
-1.3352 -1.3078 -1.2482 -1.1579 -1.0391
0
x(n)
-0.5
5 10 15 20 25
-10
n
steady state response
y(n)
2 -1
1
5 10 15 20 25
0 -20
Use [h,t]=impz(b,a,N) to find the impulse response and find the difference between y~=h’
(=0).
Using impz, we need not specify the impulse input x[n], just the coefficients and length N
that we desire (refer experiment 2).
Find the first 8 samples of the complete response y[n] for the difference equation
y[n] = 1 [x[n] + x[n −1] + x[n − 2]]+ 0.95y[n −1] − 0.9025y[n − 2];
3 n≥0;
where
⎛ πn
⎞
x[n] = cos⎜ ⎟u[n]
⎝ 3⎠
Solution: Use OCTAVE function filter(b,a,x,xic), where xic are the initial conditions
computed as xic=filtic(b,a,Y,X), where Y=[y(-1) y(-2)…y(N-1)] & X=[x(-1) x(-2)…x(-
M)] are the initial condition arrays. If x[n]=0 for n<0, then X need not be specified.
xic=filtic(b,a,Y,X);
1.3596 5.0281
Questions
Impulse response
Step response
EXPERIMENT NO. 5
Algorithm:
Input the length of the DFT required (say 4, 8, >length of the sequence).
OCTAVE Implementation:
The commands and functions that comprise the new function must be put in a file whose
name defines the name of the new function, with a filename extension of '.m'. At the top
of the file must be a line that contains the syntax definition for the new function. For
example, the existence of a file on disk called dft.m with body as follows
function[Xk] =dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=xn*WNnk;
defines a new function called dft that calculates the DFT of a sequence xn. The variables
within the body of the function are all local variables
In the Main body of the program call this function to calculate the DFT.
The magnitude spectrum is computed using the function abs (Absolute value).
abs(x) is the absolute value of the elements of x. When x is complex, abs(x) is the
complex modulus (magnitude) of the elements of x.
The phase spectrum is computed using the function angle (Phase angle). angle (h)
returns the phase angles, in radians, of a matrix with complex elements.
OCTAVE PROGRAMS --
Program:
clc;
clear all;
y=fft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
subplot(2,2,2)
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
z=ifft(y,N);
mag1=real(z);
subplot(2,2,4);
stem(mag1);
RESULT:
Columns 1 through 6
Columns 7 through 8
0 1.0000 + 2.4142i
2 4 6 8 2 4 6
00 00
K
phase plot signal sequence constituted from spec
2 1
1
phase 0 0.5
-1
2 4 6 8 0 2 4 6
-20 0
clc;
clear all;
y=dft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
subplot(2,2,2);
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Note : Write the function file separately and save in the same folder with file
function[xk]=dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
xk=xn*WNnk;
RESULT:
Columns 1 through 3
Columns 4 through 6
1.0000 - 0.4142i 0 - 0.0000i 1.0000 + 0.4142i
Columns 7 through 8
I magnitude plot
n 4
p
u
tM
3
SA
eG
qNI 2
uT
eU 1
nD
cE 0 2 4 6 8
e 0
x K
(
n
)
1
0.5
0 0 2 4 6 8
phase plot
2
ph 0
as
e
-2
2 4 6 8
-40
clc;
clear all;
XK=input('enter the sequence X(K) = ');
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,1,1);
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,1,2)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Write the function file separately and save in the same folder with file name as:
function[xn]=idft(XK,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
xn=(XK*WNnk)/N;
RESULT:
Columns 1 through 3
Column 4
0 + 0.0000i
MAGNIT
UDE
K
phase plot
magnitude plot
2
2
1.5
1.5
1
1
0.5
0.5
01
01
1.5 2 2.5 3 3.5 4
1.5 2 2.5 3 3.5 4
K
Fig.8.3 IDFT
Inputs -
Run 3: Give a Sinusoidal Signal with a frequency 250 Hz with fs = 8 KHz and sample it
for 64 points and perform 64 point DFT on it.
Run 4: Give a Sinusoidal Signal with a frequency 250 Hz and 500 Hz with fs = 8 KHz
and sample it for 64 points and perform 64 point DFT on it.
Viva Questions
Explain DFT
EXPERIMENT NO. 6
Linearity
Parseval’s Theorem
Convolution Property
Linearity Property :
Linearity Property
clc;
clear all;
close all;
LINEARITY PROPERTY
x1=[1,2,3,4];
x2=[1,1,1,1];
a1=1;
a2=1;
LHS=fft(term1);
term2= fft(a1.*x1);
term3 = fft(a2.*x2);
if (LHS==RHS)
disp('LHS=');
disp(LHS);
disp('RHS=');
disp(RHS);
else
end
Result :
LHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
RHS=
Parsevals Property
clc;
clear all;
close all;
Parseval's theoerm
x=[1,2,3,4];
XK =fft (x);
en1 = sum(x.^2);
if (en1==en2)
disp('Energy=');
Department of ETE, BMSIT & M. 126 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
disp(en2);
end
Result :
Energy = 30
Theory:
By multiplying two N-point DFTs in the frequency domain, we get the circular
convolution in the time domain.
Algorithm:
Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time domain.
x1=[1 2 3 4];
x2= [1 2 3 4];
N=max(length(x1),length(x2));
%obtain DFTs
X1= fft(x1,N);
Department of ETE, BMSIT & M. 128 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
X2= fft(x2,N);
y= X1.*X2;
yn= ifft(y,N);
disp(yn);
%plot
stem(yn);
Result:
circular convolution of x1 &x2 is yn= 26 28 26 20
Circular convolution output y(n)
30
25
20
15
10
01
[3 1 5 2 4]';
= exp(2*pi/N*sqrt(-1));
= fft(x);
Department of ETE, BMSIT & M. 130 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
G = W.^(-m*k) .* X;
g = ifft(G);
disp(g);
Result :
2.0000 - 0.0000i
4.0000 + 0.0000i
3.0000 - 0.0000i
1.0000 - 0.0000i
5.0000 + 0.0000i
clc;clear all;
close all;
N=5;
n = 0:N-1;
x = [3 6 2 4 7]';
X = fft(x);
= X(mod(-n,N)+1); g =
ifft(G);
Result :
N=5;
n = 0:N-1;
i = sqrt(-1);
Gk = conj(Xk(mod(-n,N)+1)); g
= ifft(Gk);
disp('X(n) =');
disp(x);
disp ('X*(n)=');
disp(g);
Result :
X(n) =
X*(n)=
N=5;
n = 0:N-1;
i = sqrt(-1);
Gk = conj(Xk(mod(-n,N)+1)); g
= ifft(Gk);
disp('X(n) =');
disp(x);
disp ('X*(n)=');
disp(g);
Result :
X(n) =
9.0000i
X*(n)=
9.0000i
x1 = [3 7 5 2 4 1 5]';
Xk1 = fft(x1);
disp(' circular symmetries present in the DFT of a real odd signal= ');
disp(y1);
x2 = [3 7 5 2 4 1 3 2]';
Xk2 = fft(x2);
Department of ETE, BMSIT & M. 136 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
disp(' circular symmetries present in the DFT of a real even signal= ');
disp(y2);
Result :
circular symmetries present in the DFT of circular symmetries present in the DFT of
a real odd signal= a real even signal=
27.0000 0 27.0000 0
3.7409 -4.5956 3.2426 -6.2426
-1.3351 -1.7780 -1.0000 -4.0000
-5.4058 4.2094 -5.2426 -2.2426
-5.4058 -4.2094 3.0000 0
-1.3351 1.7780 -5.2426 2.2426
3.7409 4.5956 -1.0000 4.0000
3.2426 6.2426
OCTAVE Program:
clc;
clear all;
close all;
x2= fftshift(X1K);
subplot(2,1,1); stem(x1);
title('time domain');
subplot(2,1,2); plot(abs(x2));
title('frequency domain');
Result :
X1K=fft(x1);
x2=fftshift(X1K);
subplot(2,1,1);
stem(x1);
title('time domain');
subplot(2,1,2); plot(abs(x2));
title('frequency domain');
Viva Questions
EXPERIMENT NO. 7
SPECIFICATIONS
Theory: There are two types of systems – Digital filters (perform signal filtering in time
domain) and spectrum analyzers (provide signal representation in the frequency domain).
The design of a digital filter is carried out in 3 steps- specifications, approximations and
implementation.
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the window.
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with π
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz
For OCTAVE the Normalized cut-off frequency is in the range 0 and 1, where 1
corresponds to fs/2 (i.e.,fmax)). Hence to use the OCTAVE commands
Note: if the cut off frequency is in radians then the normalized frequency is
computed as
wc = Wc / π
Step 2: Compute the Impulse Response h(n) of the required FIR filter using the given
Window type and the response type (lowpass, bandpass, etc). For example given a
rectangular window, order N=20, and a high pass response, the coefficients (i.e.,
h[n] samples) of the filter are computed using the
w[n] is the window coefficients. We can also plot the window shape as
stem(boxcar(N)).
Plot the frequency response of the designed filter h(n) using the freqz function and
observe the type of response (lowpass / highpass /bandpass).
Method 2: Given the pass band (wp in radians) and Stop band edge (ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
Note: In step 2 if frequencies are in Hz, then obtain radian frequencies (for computation of
tb and N) as wp=2*pi*fp/fs, ws=2*pi*fstop/fs, where fp, fstop and fs are the passband,
stop band and sampling frequencies in Hz
Step 4: Compute the Impulse Response h(n) of the required FIR filter using N, selected
window, type of response(low/high,etc) using ‘fir1’ as in step 2 of method 1.
Once the coefficients of the FIR filter h[n] are obtained, the next step is to simulate an
input sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of
fs), each of 20/30 points. Choose the frequencies such that they are >, < and = to
fc.
Convolve input sequence x[n] with Impulse Response, i.e., x (n)*h (n) to obtain the
output of the filter y[n]. We can also use the ‘filter’ command.
Infer the working of the filter (low pass/ high pass, etc).
OCTAVE IMPLEMENTATION
FIR1 Function
B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter
coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn <
1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has
linear phase, i.e., even symmetric coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1.
0<W<W1,W1<W<W2,...,WN<W<1.
FREQZ Digital filter frequency response. [H,W] = FREQZ(B,A,N) returns the N-point
complex frequency response vector H and the N-point frequency vector W in
radians/sample of the filter whose numerator and denominator coefficients are in vectors
B and A. The frequency response is evaluated at N points equally spaced around the upper
half of the unit circle. If N isn't specified, it defaults to 512.
For FIR filter enter A=1 and B = h[n] coefficients. Appropriately choose N as 128, 256,
etc
M M
=FIR1(N,Wn,boxcar)
Bartlett 8Π 6.1Π 25db
M M
B=FIR1(N,Wn,bartlett)
Hanning 8Π 6.2Π 44db
M M
B=FIR1(N,Wn,hanning)
Hamming 8Π 6.6Π 53db B=
M M
FIR1(N,Wn)
B=FIR1(N,Wn,blackman)
RECTANGULAR WINDOW
50 50
ga 0 gain in 0
in db
in
db -50 -50
20 5
0 0
db -20 db -5
in in
gai -40 gai -10
n n
-60 -15
0.5 1 0.5 1
-800 -200
HAMMING WINDOW
[h,o]=freqz(b,1,256);
Department of Telecommunication, BMSIT Page 72
b=fir1(n,wp,y); m=20*log10(abs(h));
[h,o]= freqz(b,1,256); subplot(2,2,4);
m=20*log10(abs(h)); plot(o/pi,m)
subplot(2,2,1); ylabel('gain in db');
plot(o/pi,m) xlabel('(d)normalised freq');
ylabel('gain in db'); grid on;
xlabel('(a)normalised freq');
grid on;
RESULT:
50 50
0 in 0
db
gain
in db -50
ga
-100 -50
in
0 5
0
db -50 db -5
in in
-100
ga gai -10
in n
-15
0.5 1 0.5 1
-1500 -200
BARTLETT WINDOW
RESULT:
0 0
ga -10
in
-10 in
gain in db -20
db
-20
-30 0 0.5 1
-30
(b)normalised freq
-40
0 0.5 1 (a)normalised
freq
0 2
-10 0
in in db
db -20 -2
gai gain
n
-30 -4
0.5 1 0.5 1
-400 -60
HANNING WINDOW
n1=n+1; m=20*log10(abs(h));
if(rem(n,2)) subplot(2,2,3);
N=0; plot(o/pi,m)
n1=n; ylabel('gain in db');
n=n-1; xlabel('(c)normalised freq');
end grid on;
y=hanning(n1);
b=fir1(n,wp,y); b=fir1(n,wn,'stop',y);
RESULT:
in in 0
db db
-50
0 5
-50 0
in in db
db
-5
ga -100 gain
in
-10
0.5 1 0.5 1
-1500 -150
50 50
0 ga 0
in
in in
db db -50
-50
gai
n
-100 -100 0 0.5 1
0.5 1
-1500
0 2
-20 0
db -40 in
db
in -2
Questions
EXPERIMENT NO. 8
SPECIFICATIONS
Theory: There are two methods of stating the specifications as illustrated in previous
program. In the first program, the given specifications are directly converted to digital form
and the designed filter is also implemented. In the last two programs the butterworth and
chebyshev filters are designed using bilinear transformation (for theory verification).
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the IIR filter
type (butterworth, cheby1, cheby2).
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with
corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz, fs=8000Hz Wc
= 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians. For OCTAVE
the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds to fs/2
(i.e.,fmax)). Hence to use the OCTAVE commands wc = fc / (fs/2) = 400/(8000/2) =
0.1
Note: if the cut off frequency is in radians then normalized frequency is computed as
wc = Wc / π
Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter and the
response type (lowpass, bandpass, etc) using the appropriate butter, cheby1, cheby2
command. For example given a butterworth filter, order N=2, and a high pass response,
the coefficients [b,a] of the filter are computed using the
Method 2: Given the pass band (Wp in radians) and Stop band edge (Ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
Step 2: Compute the order and cut off frequency as [N, wc] = BUTTORD(wp, ws,
Rp, Rs)
Step 3: Compute the Impulse Response [b,a] coefficients of the required IIR filter and
the response type as [b,a] =butter(N, wc , 'high');
Once the coefficients of the IIR filter [b,a] are obtained, the next step is to simulate an
input sequence x[n], say input of 100, 200 & 400 Hz (with sampling frequency of
fs), each of 20/30 points. Choose the frequencies such that they are >, < and = to
fc.
Filter the input sequence x[n] with Impulse Response, to obtain the output of the filter
y[n] using the ‘filter’ command.
Infer the working of the filter (low pass/ high pass, etc).
OCTAVE IMPLEMENTATION
BUTTORD Butterworth filter order selection. [N, Wn] = BUTTORD(Wp, Ws, Rp, Rs)
returns the order N of the lowest order digital Butterworth filter that loses no more than Rp
dB in the passband and has at least Rs dB of attenuation in the stopband. Wp and Ws are
the passband and stopband edge frequencies, normalized from 0 to 1 (where 1 corresponds
to pi radians/sample). For example,
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB frequency")
to use with BUTTER to achieve the specifications. [N, Wn] = BUTTORD(Wp, Ws, Rp,
Rs, 's') does the computation for an analog filter, in which case Wp and Ws are in
radians/second. When Rp is chosen as 3 dB, the Wn in BUTTER is equal to Wp in
BUTTORD.
BUTTER Butterworth digital and analog filter design. [B,A] = BUTTER(N,Wn) designs
an Nth order lowpass digital Butterworth filter and returns the filter coefficients in length
N+1 vectors B (numerator) and A (denominator). The
coefficients are listed in descending powers of z. The cutoff frequency Wn must be 0.0 <
Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn is a two-element vector,
Wn = [W1 W2], BUTTER returns an order 2N bandpass filter with passband W1 < W <
W2. [B,A] = BUTTER(N,Wn,'high') designs a highpass filter. [B,A] =
BUTTER(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. BUTTER(N,Wn,'s'),
BUTTER(N,Wn,'high','s') and BUTTER(N,Wn,'stop','s') design analog Butterworth filters.
In this case, Wn is in [rad/s] and it can be greater than 1.0.
OCTAVE PROGRAMS
BUTTERWORTH FILTERS
1.Butterworth LPF
clc;
clear all;
close all;
format long
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on;
RESULT:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
200
d 0
b-
->
in
G -200
ai
n
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-4000
(a)Normalised frequency--
>
4
-- 2
>
ra
di 0
a
ns
in
P -2
h
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b)Normalised frequency--
>
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on
RESULT:
Enter the passband attenuation: 0.5
200
d 0
b
--
>
in
G -200
ai
n
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-4000
(a)Normalised frequency-->
4
-- 2
>
ra
di 0
a
n
s
in
P -2
h
a
s
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b)Normalised frequency-->
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'bandpass');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
subplot(2,1,2);plot(om/pi,an);
ylabel('phase in radians-->');
RESULT:
enter the passband ripple 0.3
enter the stopband ripple 40
0
d -200
b-
-> -400
in
g -600
ai
n
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-8000
--> 2
ra
dia 0
ns
in
ph -2
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b) normalised frequency-->
clear all;
format long
w2=2*ws/fs;
[n]=buttord(w1,w2,rp,rs);
wn=[w1,w2];
[b,a]=butter(n,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(om/pi,m); ylabel('gain in
db'); xlabel('(a) normalised
freq'); subplot(2,1,2);
plot(om/pi,an); xlabel('(b)
normalised freq'); ylabel('phase
in radians');
RESULT:
enter the passband ripple 0.4
-- 2
>
ra
di 0
a
n
s
in
p -2
h
a
s
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b) normalised frequency-->
CHEBYSHEV FILTERS
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn);
w=0:0.01:pi;
Department of ETE, BMSIT & M. 182 |
Page
EMBEDDED CONTROLLER LAB 18ECL46
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
subplot(2,1,2);plot(om/pi,an);
ylabel('phase in radians-->');
RESULT:
enter the passband ripple 0.2
enter the stopband ripple 45
0
db -200
-->
in
ga -400
in
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-6000
4
--> 2
ra
di 0
an
s
in
ph -2
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b) normalised frequency-->
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
subplot(2,1,2);plot(om/pi,an);
ylabel('phase in radians-->');
RESULT:
--> 2
ra
dia 0
ns
in
ph -2
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b) normalised frequency-->
w2=2*ws/fs;
[n]=cheb1ord(w1,w2,lp,ls); wn=[w1
w2] [b,a]=cheby1(n,lp,wn,'bandpass');
w=0:0.01:pi; [h,om]=freqz(b,a,w);
m=20*log10(abs(h)); an=angle(h);
figure(1);
subplot(2,1,1);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an); xlabel('(b)Normalised
frequency-->'); ylabel('Phase in radians-
->');
grid on
Result:
wn = 0.40000000000000 0.50000000000000
0
d -200
b-
->
in
G -400
ai
n
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-6000
(a)Normalised frequency-->
4
--> 2
ra
di 0
an
s
in
Ph -2
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b)Normalised frequency-->
w2=2*ws/fs;
[n]=cheb1ord(w1,w2,lp,ls);
wn=[w1 w2]
[b,a]=cheby1(n,lp,wn,'stop');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
plot(om/pi,m);
grid on;
ylabel('Gain in db-->');
xlabel('(a)Normalised frequency-->');
subplot(2,1,2);
plot(om/pi,an);
xlabel('(b)Normalised frequency-->');
ylabel('Phase in radians-->');
grid on;
RESULT:
wn = 0.36666666666667 0.73333333333333
0
d -100
b-
->
in
G -200
ai
n
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-3000
(a)Normalised frequency-->
4
--> 2
ra
dia 0
ns
in
Ph -2
as
e
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-40
(b)Normalised frequency-->
Viva Questions:
Draw typical responses of Chebyshev filter for order odd & even.
PART II
EXPERIMENTS USING DSP
PROCESSOR
INTRODUCTION to CCS
Procedure
🡪
Create a new project: project New
Type the project name (x.pjt) & store in myprojects folder
🡪
To open an existing project: project open
🡪
Files to be added to the project: project Add Files to project
C:\CCStudio_v3.1\C5400\cgtools\lib\rts.lib
C:\CCStudio_v3.1\tutorial\dsk5402\datedisplay\mainapplication.cmd
🡪 🡪 🡪
File New source file same as xx.c
( Select type of file as: C/C++ file before saving) & add this C file to your project
🡪
Project Build. (obtain Build successful)
🡪
To execute ; File load program (select pjtname.out file)
To Run : Debug
🡪 Run
Observe the output on the stdout window or waveforms using graph or CRO
🡪
To view waveforms View Graph changes in graph property dialog box to be made
🡪
Display type Dual (to observe 2 waveforms)
Title
🡪 User defined
🡪
Start address – upper display x (user defined variable array names used in C
program. Note: x, y should be made global to be used by graph)
🡪
Start address – lower display y
Acquisition Buffer size
🡪 60 (depends on the array size)
Display Buffer size 60
🡪
DSP data type – 32-bit Floating pt
🡪 🡪
Auto scale ON (if off choose max yvalue=1)
EXPERIMENT NO. 9
C Program
# include<stdio.h>
# include<math.h>
main()
{
float h[4] = { 2,2,2,2}; float x[4] ={1,2,3,4};
float y[10];
int xlen=4; int hlen=4;
int N=xlen+hlen-1;
int k,n;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<hlen;k++)
//inner loop for computing each y[n] point
{
if (((n-k)>=0) & ((n-k)<xlen))
y[n]=y[n]+h[k]*x[n-k]; //compute output
} //end of inner for loop
printf("%f\t", y[n]);
} //end of main
Result
EXPERIMENT NO. 10
C Program
include<stdio.h>
include<math.h>
main()
float x[5]={1,2,3,4,5};
float h[5]={2,1,3,4,5};
float y[10]; //output sequence
int N=5; // N=max of xlen and hlen//
int k,n,i;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<N;k++) //inner loop for computing each y[n] point
{
i=(n- //compute the index modulo N
k)%N;
if(i<0) //if index is <0, say x[-1], then convert to x[N-1]
i=i+N;
y[n]=y[n]+h[k]*x[i]; //compute output
} //end of inner for loop
printf("%f\t",y[n]);
} //end of outer for loop
} //end of main
Result
EXPERIMENT NO. 11
Theory:
The N point DFT of discrete time signal x[n] is given by the equation
N-1 − j 2πkn
X (k ) = ∑x[n]e N ; k = 0,1,2,....N -1
n =0
⎝ N ⎠
C Program
#include <stdio.h>
#include <math.h>
main()
int n,k,k1,N=8,xlen=4;
for(k=0;k<2*N;k=k+2)
for(n=0;n<xlen;n++)
y[k]=y[k]+x[n]*cos(w);
y[k+1]=y[k+1]+x[n]*sin(w);
printf("%f+j%f \n",y[k],y[k+1]);
}//end of main
EXPERIMENT NO. 12
SYSTEM
Aim: To find the Impulse response of the given first order / second order system
Theory:
Since the difference equation contains past samples of output, i.e., y[n-1], y[n-2], its
impulse response is of infinite duration (IIR). For such systems the impulse response is
computed for a large value of N, say N=100 (to approximate n=∞).
Impulse response implies x[n]= δ[n], an impulse, and the initial conditions x[-1], x[-2], y[-
1] and y[-2] are zero.
OCTAVE)
Algorithm
Input the coefficients b0 b1 b2, a1 a2 (Say the coefficients from a 2nd order
butterworth filter. Note coefficient of y[n] is a0=1 always)
y[n] = −a1 y[n −1] − a2 y[n − 2] + b0 x[n] + b1 x[n −1] + b2 x[n − 2];
Program #include
<stdio.h> float
x[60],y[60]; main()
{
for(i=1;i<N;i++)
x[i]=0; //generate
output
for(j=0;j<N;j++)
y[j]=b0*x[j];
if(j>0) y[j]=y[j]+b1*x[j-1]-
a1*y[j-1]; if ((j-1)>0)
y[j]=y[j]+b2*x[j-2]-a2*y[j-2];
printf("%f \t",y[j]);
}//end of main
Note: For a first order system just enter a2=0 and b2=0.
Result (on std out)
0.067500 0.212053 0.282012 0.234804 0.151967 0.076771
0.025017 -0.003096 -0.013866 -0.014571 -0.010931 -
0.006479 -0.002893 -0.000632 0.000471 0.000800 0.000720
0.000492 0.000266 0.000100
EXPERIMENT NO. 13
SPECIFICATIONS
Aim: Realization of an FIR filter ( any type ) to meet given specifications. The input
Pre-requisite: Input is given from the signal generator of amplitude 1 v p-p and
frequency > 20 Hz. TMS kit: Plug in the signal generator o/p to line in and line out of
TMS to CRO
C Program
#include "FIRcfg.h"
#include "dsk6713.h"
#include "dsk6713_aic23.h"
0.220534,0.262448,0.220534,0.144802,0.063010,0.000000,-0.031505,-0.033416, -
0.018003, 0 ,0.010502, 0.011139,0.005728,0.000000, -0.002423,-0.001591, 0.0};
static short in_buffer[100];
DSK6713_AIC23_Config config = { \
*/\
*/ \
0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */ \
0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */ \
0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */ \
0x0043, /* 7 DSK6713_AIC23_DIGIFDigital audio interface format */ \
};
{ DSK6713_AIC23_CodecHandle hCodec;
Uint32 l_input, r_input,l_output, r_output;
while(1)
l_output=(Int16)FIR_FILTER(&filter_Coeff
,l_input);
r_output=l_output;
return(output);
Result: The output plot is observed on the CRO. This behaves as a highpass filter. See
the effect of filtering by giving a sinusoidal input to DSP kit and slowly varying its
frequency in the range from 0 to 3.5 kHz. What is the cutoff frequency of the filter?
Change the filter co-efficients for different types of window & Cut-off frequencies (refer
FIR filter design part from experiment 11 in part A-OCTAVE)