0% found this document useful (0 votes)
43 views217 pages

Embedded LAB MANUAL

The document discusses the 8051 microcontroller family and provides details about the 8051, 8052, and 8031 microcontrollers. It describes the features of the 8051 microcontroller including its memory types, ports, timers, and crystal oscillator. The rest of the document outlines the cycle of experiments to be performed with the 8051 microcontroller in an embedded controller laboratory.

Uploaded by

Pinak Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views217 pages

Embedded LAB MANUAL

The document discusses the 8051 microcontroller family and provides details about the 8051, 8052, and 8031 microcontrollers. It describes the features of the 8051 microcontroller including its memory types, ports, timers, and crystal oscillator. The rest of the document outlines the cycle of experiments to be performed with the 8051 microcontroller in an embedded controller laboratory.

Uploaded by

Pinak Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 217

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Avalahalli, Yelahanka, Bengaluru-64II

DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGINEERING


(Accredited by NBA-Tier II)

ACADEMIC YEAR 2023-24

Embedded Controller Laboratory (21ECL58B) MANUAL


V Semester ETE

Name : ………………………………………………………………………………………

USN : ………………………………………………………………………………………

Batch: ………………………………………………………………………………………
EMBEDDED CONTROLLER LAB 18ECL46

CONTENTS

I Cycle of Experiments Page No. 6

II Introduction to 8051 Microcontroller Page No. 7-9

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

4. Boolean & Logical Instructions (Bit manipulations) Page No. 31-38


Code conversion: BCD – ASCII; ASCII – Decimal; Decimal - Page No. 41-44
5.
ASCII;
6. Draw the schematic and Layout of a CMOS Inverter using Page No. 45-47
library files.
7. Draw the schematic and Layout of a CMOS 2 input NAND gate Page No. 66-67
using library files.
Draw the schematic and layout of common source amplifier Page No.
8.
with PMOS current mirror load.
9. Design an 4Bit UP/DOWN Counter Asynchronous Reset Page No. 68-70
Counter
• Write Verilog Code
• Verify the Functionality using Test-bench
• Synthesize the Gate Level Netlist by setting Area and
Timing Constraints and also find the Critical Path and
Maximum Frequency of Operation
10. Perform the above for 32Bit UP/DOWN Counter Page No. 71-73

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

Department of ETE, BMSIT & M. 2 | Page


EMBEDDED CONTROLLER LAB 18ECL46

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

Program Educational Objectives (PEOs):


After three to four years of graduation our graduates will:
PEO 1: Excel as Professionals in Telecommunication, Electronics and IT related fields.
PEO 2: Engage in life-long learning.
PEO 3: Maintain ethical norms, exhibit good communication skills and leadership qualities.

Program Specific Outcomes (PSOs):


At the end of graduation our graduates will be able to:
PSO 1: Analyze and Design Communication Systems
PSO 2: Analyze and implement signal processing applications.
PSO 3: Design and implement embedded systems.
Course Outcomes (Cos):
Students will be able to:
CO 1: Enhance programming skill using Assembly Language & execute programs in 8051 for
solving simple problems that manipulate input data using different instructions of 8051,
also interface different input and output devices to 8051.
CO 2: Write a report for the conducted experiment
CO 3: Conduct an open ended experiment to design a simple Calculator / Elevator using 8051
microcontroller.

Department of ETE, BMSIT & M. 3 | Page


EMBEDDED CONTROLLER LAB 18ECL46

CO-PO/PSO Mapping Table


P P P P P P P P P P P P PSO PSO PSO
O O O O O O O O O O O O 1 2 3
1 2 3 4 5 6 7 8 9 1 1 1
0 1 2
CO1 3 2
CO2 1
CO3 2 1 2

Ground Rules

Keep the lab clean and tidy.

Do come to the lab in time.

Maintain silence

Keep your belongings in appropriate place provided to you.

Work only on system allotted for you.

DO’s

Come to the lab in time

Submit your observation book before starting the experiment

Submit your record for valuation in time

Shut down the computers after completing your experiments

Handle the hardware kit with care

DON’Ts

Do not keep your bags on the computer table

Avoid unnecessary talking while doing the experiment

Do not use cell phones, iPods, and any similar devices in labs

Do not loiter around in the lab

Do not exchange your hardware kits with another group.

Department of ETE, BMSIT & M. 4 | Page


EMBEDDED CONTROLLER LAB 18ECL46

CYCLE OF EXPERIMENTS

CYCLE 1 – PROGRAMMING
1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an Array.

2. Arithmetic Instructions - Addition/subtraction, multiplication.

3. Counters.

4. Boolean & Logical Instructions (Bit manipulations).

5. Code conversion: BCD – ASCII; ASCII – Decimal; Decimal – ASCII.

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.

9. Design an 4Bit UP/DOWN Counter Asynchronous Reset Counter


• Write Verilog Code
• Verify the Functionality using Test-bench
• Synthesize the Gate Level Netlist by setting Area and Timing Constraints and also find the
Critical Path and Maximum Frequency of Operation.

10. Perform the above for 32Bit UP/DOWN Counter.

11. Design a Latch and Flip-Flop to compare the synthesis report of D, SR, JK.

OPEN ENDED EXPERIMENT

1 Conduct an open ended experiment to design a simple Calculator / Elevator using 8051
microcontroller.

Department of ETE, BMSIT & M. 5 | Page


EMBEDDED CONTROLLER LAB 18ECL46

Introduction to Microcontroller 8051

The most universally employed set of microcontrollers come from the


8051 family. 8051 Microcontrollers persist to be an ideal choice for a
huge group of hobbyists and experts. In the course of 8051, the
humankind became eyewitness to the most ground- breaking set of
microcontrollers. The original 8051 microcontroller was initially
invented by Intel. The two other members of these 8051 families are-

8052-This microcontroller has 3 timers & 256 bytes of RAM. Additionally,


it has all the features of the traditional 8051 microcontroller. 8051
microcontroller is a subset of 8052 microcontroller.
8031 - This microcontroller is ROM less, other than that it has all the
features of a traditional 8051 microcontroller. For execution an
external ROM of size 64K bytes can be added to its chip.

8051 microcontroller brings into play 2 different sorts of memory such


as - NV- RAM, UV - EPROM and Flash.

8051 is the basic microcontroller to learn embedded systems projects.

FEATURES OF 8051

8051 microcontroller is an eight-bit microcontroller


launched It is available in 40 pin DIP (dual inline package). It has
4kb of ROM (on- chip programmable space) and 128 bytes of
RAM space which is inbuilt, if desired 64KB of external memory can be
interfaced with the microcontroller. There are four parallel 8 bits ports
which are easily programmable as well as addressable.
An on- chip crystal oscillator is integrated in the microcontroller
which has crystal frequency of 12MHz. In the microcontroller there is a
serial input/output port which has 2 pins. Two timers of 16 bits are also
incorporated in it; these timers can be employed as timer for internal
functioning as well as counter for external functioning.

The microcontroller comprises of 5 interrupt sources namely


Serial Port Interrupt, Timer Interrupt 1, External Interrupt 0, Timer
Interrupt 0, External Interrupt 1.

The programming mode of this micro-controller includes GPRs


Department of ETE, BMSIT & M. 6|
Page
EMBEDDED CONTROLLER LAB 18ECL46

(general purpose registers), SFRs (special function registers) and SPRs


(special purpose registers).

INTERNAL ARCHITECHURE OF 8051 MICRO-


CONTROLLER

1.ALU

All arithmetic and logical functions are carried out by


the ALU. Addition, subtraction with carry, and multiplication come
under arithmetic operations. Logical AND, OR and exclusive OR (XOR)
come under logical operations.

Department of ETE, BMSIT & M. 7|


Page
EMBEDDED CONTROLLER LAB 18ECL46

2.Program Counter (PC)

A program counter is a 16-bit register and it has no internal address.


The basic function of program counter is to fetch from memory the
address of the next instruction to be executed. The PC holds the address
of the next instruction residing in memory and when a command is
encountered, it produces that instruction. This way the PC increments
automatically, holding the address of the next instruction.

3.Registers

Registers are usually known as data storage devices. 8051


microcontroller has 2 registers, namely Register A and Register B.
Register A serves as an accumulator while Register B functions as a
general purpose register. These registers are used to store the output of
mathematical and logical instructions. The operations of addition,
subtraction, multiplication and division are carried out by Register A.
Register B is usually unused and comes into picture only when
multiplication and division functions are carried out by Register A.
Register A also involved in data transfers between the
microcontroller and external memory. PIN DIAGRAM OF 8051
MICRO-CONTROLLER

Department of ETE, BMSIT & M. 8|


Page
EMBEDDED CONTROLLER LAB 18ECL46

Fig. Pin Diagram of 8051

Department of ETE, BMSIT & M. 9|


Page
EMBEDDED CONTROLLER LAB 18ECL46

PART I
PROGRAMMING

Department of ETE, BMSIT & M. 10


| Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO.1

DATA TRANSFER. - BLOCK MOVE, EXCHANGE, SORTING, FINDING LARGEST


ELEMENT IN AN ARRAY

1.1 DATA TRANSFER

OBJECTIVE:
TO TRANSFER A BLOCK OF DATA BYTES FROM SOURCE MEMORY TO DESTINATION
MEMORY USING 8051.

PROGRAM:

MOV R0,#50H // Initialize the source memory pointer


MOV R1,#60H // Initialize the destination memory pointer

MOV R2, #05H // Initialize Iteration counter

BACK: MOV A,@R0 // Get the data from source memory pointer
MOV @R1,A // Store the data into destination memory pointer

INC R0 // Increment the source memory pointer

INC R1 // Increment the destination 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.

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

Department of ETE, BMSIT & M. 11


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

MOV R2,#05H // Initialize Iteration counter


BACK: MOV A,@R0 // Get the data from source memory pointer and Load // into
Accumulator
XCH A,@R1 // Exchange data between Accumulator and // destination
memory pointer
MOV @R0,A // Store the data into source memory pointer
INC R0 // Increment the source memory pointer
INC R1 // Increment the destination 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*/
END
MEMORY WINDOW:
Before execution:
D:0x50H: 01 02 03 04 05 00
D:0X60H: 06 07 08 09 10 00
Department of ETE, BMSIT & M. 12
| Page
EMBEDDED CONTROLLER LAB 18ECL46

After execution:
D:0x50H: 06 07 08 09 10 00
D:0X60H: 01 02 03 04 05 00

1.3 LARGEST/SMALLEST ELEMENT IN AN ARRAY USING 8051

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.*/

Department of ETE, BMSIT & M. 13


| Page
EMBEDDED CONTROLLER LAB 18ECL46

SJMP NEXT // Go to NEXT


LOOP1: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.*/
NEXT: MOV 60H,B /*Store the largest value into memory location 60H.*/
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: AB 00 00 00 00 00

PROGRAM TO FIND THE SMALLEST NUMBER:


MOV R0,#50H //Initialize the source memory pointer
MOV R2,#05H //Initialize Iteration counter
MOV B, @R0 /* Use B Register to store smallest 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: JNC LOOP1 // If carry generates, go to relative address LOOP1
MOV B,A // Store smaller value into B-register
INC R0 // Increment the source memory pointer

Department of ETE, BMSIT & M. 14


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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.*/
SJMP NEXT // Go to NEXT
LOOP1: 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.*/
NEXT: MOV 60H,B /*Store the smallest value into memory location 60H.*/
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 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

Department of ETE, BMSIT & M. 15


| Page
EMBEDDED CONTROLLER LAB 18ECL46

DEC R2 // Decrement the iteration count


BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP /* Compare if not equal go to relative address (LOOP)*/
LOOP: JC LOOP1 /* If carry generates, go to relative address (LOOP1)*/
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK // Decrement inner loop count if not zero go to back
DJNZ R2, BACK1 // Decrement outer loop count if not zero go to back1
END
MEMORY WINDOW:

Before execution:
D:0x50H: 06 04 03 07 02 01

After execution:
D:0x50H: 01 02 03 04 06 07

Department of ETE, BMSIT & M. 16


| Page
EMBEDDED CONTROLLER LAB 18ECL46

OBJECTIVE:

TO ARRANGE N 8-BIT NUMBERS IN DESCENDING ORDER.


PROGRAM:
MOV R2, #05H // Initialize the iteration counter
DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1
MOV R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP // Compare if not equal go to relative address (LOOP)
LOOP: JNC LOOP1 // If carry generates, go to relative address (LOOP1)
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK /* Decrement inner loop count, if not zero go to back*/
DJNZ R2, BACK1 /* Decrement outer loop count, if not zero go to back1*/
END
MEMORY WINDOW:

Before execution:
D:0x50H: 06 04 03 07 02 01

After execution:
D:0x50H: 07 06 04 03 02 01

Department of ETE, BMSIT & M. 17


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Repeat the same processes until count become zero.

Write an ALP to check the given string of data is palindrome or not

The output will be 01 if it is palindrome.

The output will be FF if it is not palindrome

Explain the difference between the following two instructions:

MOVC A,@R0

MOV A,@R0

Circle the invalid instructions.

MOV A,@R1

MOV A,@R2

MOVC A,@R0+DPTR

MOV @R3,A

Explain the difference between the following two instructions:

MOV A,40H

MOV A,#40H

Explain the difference between the following two instructions:

MOV 40H,A

MOV 40H,#0A

Give the RAM address for the following registers.


Department of ETE, BMSIT & M. 18
| Page
EMBEDDED CONTROLLER LAB 18ECL46

A= B= R0 = R2 =

PSW = SP = DPL = DPH =

Department of ETE, BMSIT & M. 19


| Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO.2
ARITHMETIC INSTRUCTIONS - ADDITION/SUBTRACTION, MULTIPLICATIO.

OBJECTIVE:
TWO UNDERSTAND THE ARITHMETIC OPERATIONS AND PERFORM 8/16 BIT
ADDITION/ SUBTRACTION AND MULTIPLICATION

2.1 WRITE AN ALP TO PERFORM 16 BIT ADDITION

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

Department of ETE, BMSIT & M. 20


| Page
EMBEDDED CONTROLLER LAB 18ECL46

After execution:
D:0x50H: FD07 00 00 00 00
D:0X5FH: 01 FC 6600 00 00

2. 2 WRITE AN ALP TO PERFORM 16 BIT SUBTRACTION


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.
SUBB 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 POSITIVE
MOV @R1,#0FFH
JMP FINISH
POSITIVE: MOV @R1,#00H
FINISH: SJMP $
END
Eg. FAF4 - 02F5 = F7FF (ANSWER IS POSITIVE)
MEMORY WINDOW
Before execution:

Department of ETE, BMSIT & M. 21


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of ETE, BMSIT & M. 22


| Page
EMBEDDED CONTROLLER LAB 18ECL46

2. 3 WRITE AN ALP TO PERFORM MULTIPLICATION (16-bit by 16-bit)


First number will be in R6 and R7 while second number will be in R4 and R5. The
result will be in R0, R1, R2 and R3.
Eg:
R6 R7 = 15 FD
R4 R5 = A2 4B
R0 R1 R2 R3 = 0DF08B1F
PROGRAM:
MOV R6,#0FFH //FFFF X FFFF = FFFE 0001
MOV R7,#0FFH // input the multiplicand
MOV R4,#0FFH // input the multiplier
MOV R5,#0FFH
//Multiply R5 by R7
Department of ETE, BMSIT & M. 23
| Page
EMBEDDED CONTROLLER LAB 18ECL46

MOV A,R5 // Move the R5 into the Accumulator


MOV B,R7 // Move R7 into B
MUL AB // Multiply the two values
MOV R2,B // Move B (the high-byte) into R2
MOV R3,A // Move A (the low-byte) into R3
//Multiply R5 by R6
MOV A,R5 // Move R5 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R2 // Add the low-byte into the value already in R2
MOV R2,A // Move the resulting value back into R2
MOV A,B // Move the high-byte into the accumulator
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R1,A // Move the resulting answer into R1
MOV A,#00h // Load the accumulator with zero
ADDC A,#00h // Add zero (plus the carry, if any)
MOV R0,A // Move the resulting answer to R0.
//Multiply R4 by R7
MOV A,R4 // Move R4 into the Accumulator
MOV B,R7 // Move R7 into B

MUL AB // Multiply the two values


ADD A,R2 // Add the low-byte into the value already in R2

MOV R2,A // Move the resulting value back into R2


MOV A,B // Move the high-byte into the accumulator
ADDC A,R1 // Add the current value of R1 (plus any carry)
MOV R1,A // Move the resulting answer into R1.

Department of ETE, BMSIT & M. 24


| Page
EMBEDDED CONTROLLER LAB 18ECL46

MOV A,#00h // Load the accumulator with zero


ADDC A,R0 // Add the current value of R0 (plus any carry)
MOV R0,A // Move the resulting answer to R1.
//Multiply R4 by R6
MOV A,R4 // Move R4 back into the Accumulator
MOV B,R6 // Move R6 into B
MUL AB // Multiply the two values
ADD A,R1 // Add the low-byte into the value already in R1
MOV R1,A // Move the resulting value back into R1
MOV A,B // Move the high-byte into the accumulator
ADDC A,R0 // Add it to the value already in R0 (plus any carry)
MOV R0,A // Move the resulting answer back to R0
// answer is now in R0, R1, R2, and R3
SJMP $
END

RESULT :
REGISTER VALUES:
R6 R7 = FF FF
R4 R5 = FF FF
R0 R1 R2 R3 = FF FE 00 01

EXPERIMENT NO.3

Department of ETE, BMSIT & M. 25


| Page
EMBEDDED CONTROLLER LAB 18ECL46

COUNTERS
OBJECTIVES:
TWO UNDERSTAND THE SIMULATION OF BINARY/BCD UP/DOWN COUNTERS AND
TO KNOW THE CONCEPTS OF SUBROUTINES

3. 1 WRITE A PROGRAM TO REALIZE A BINARY UP COUNTER

ORG 0000H // Organization of code memory from 0000h


CLR 50H // Clear upper byte counter
CLR 51H // Clear lower byte counter
UP: ACALL DELAY /* Call the subroutine to provide delay between two
countervalue*/
// LOWER BYTE COUNTER
MOV A, 51H // Get the current lower counter
ADD A, #01H // Add 01h with previous value to get next counter
MOV 51H,A // Store the counter in lower byte
JNZ UP /* If lower count value not zero, go to relative address
(UP)*/
//UPPER BYTE COUNTER
MOV A,50H /* If lower byte reaches zero, get the current upper
counter*/
ADD A,#01H // Add 99h to previous value to get next counter
MOV 50H,A // Store the counter in upper byte
JNZ UP /* If upper count value not zero, go to relative address
(UP)*/
SJMP UP // Repeat this counter until stop running
//Provide delay between two counter value
DELAY: MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
Department of ETE, BMSIT & M. 26
| Page
EMBEDDED CONTROLLER LAB 18ECL46

JNZ L2 // If it is not zero,go to relative address (L2)


RET // Return to main program
END
RESULT :
During execution:D:0x50H: (00 to FFH) (00 to FFH) 00 00 00 00

3. 1 WRITE A PROGRAM TO REALIZE BINARY DOWN COUNTER

MOV 50H,#0FFH // Initialize upper byte of HEX counter with FFH


MOV 51H,#0FFH // Initialize lower byte of HEX counter with FFH
UP: ACALL DELAY // Call the subroutine to provide delay
//between two Counter value
// LOWER BYTE COUNTER
DEC 51H // Decrement lower byte counter
MOV A, 51H // Store the lower byte counter into //accumulator
JNZ UP // If lower count value not zero, go to //relative address
(UP)
// UPPER BYTE COUNTER
DEC 50H //Decrement upper byte counter
SJMP UP // Repeat this counter until stop running
// Provide delay between two counter value
DELAY:MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative // address(L2)
RET // Return to main program
END
Department of ETE, BMSIT & M. 27
| Page
EMBEDDED CONTROLLER LAB 18ECL46

RESULT:
During execution:
D:0x50H : (FFH to 00H ) (FFH to 00H) 00 00 00 00

3. 2 WRITE A PROGRAM TO REALIZE A BCD COUNTER

WRITE AN ALP TO PERFORM BCD UP COUNTER


ORG 0000H // Organization of code memory from 0000h
CLR 50H // Clear upper byte counter
CLR 51H // Clear lower byte counter
UP: ACALL DELAY // Call the subroutine to provide delay between
//two counter value

// LOWER BYTE COUNTER


MOV A, 51H // Get the current lower counter
ADD A, #01H // Add 01h to previous value to get next counter
DA A // Convert hex value to decimal
MOV 51H, A // Store the counter in lower byte
JNZ UP // If lower count value not zero, go to //relative address (UP) // UPPER
BYTE COUNTER
MOV A, 50H // If lower byte reaches zero, get the current //upper
counter

Department of ETE, BMSIT & M. 28


| Page
EMBEDDED CONTROLLER LAB 18ECL46

ADD A, #01H // Add 99h to previous value to get next counter


DA A // Convert hex value to decimal
MOV 50H, A // Store the counter in upper byte
JNZ UP // If upper count value not zero, go to //relative address
(UP)
SJMP UP // Repeat this counter until stop running
// Provide delay between two counter value
DELAY: MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative address (L2)
RET // Return to main program
END
RESULT
During execution: D:0x50H :(00 to 99H) (00 to 99H) 00 00 00 00

3.3 WRITE AN ALP TO PERFORM BCD DOWN COUNTER


ORG 0000H // Organization of code memory from 0000h
MOV 50H, #99H //Initialize upper byte of counter with 99h
MOV 51H, #99H //Initialize lower byte of counter with 99h
UP: ACALL DELAY /* Call the subroutine to provide delay between
two counter value*/
// LOWER BYTE COUNTER
MOV A, 51H // Get the current lower counter
ADD A, #99H /* Add 99h to previous value to get next counter*/
DA A // Convert hex value to decimal
Department of ETE, BMSIT & M. 29
| Page
EMBEDDED CONTROLLER LAB 18ECL46

MOV 51H,A // Store the counter in lower byte


JNZ UP /* If lower count value not zero, go to relative address (UP)*/
// UPPER BYTE COUNTER
MOV A, 50H // If lower byte reaches zero, get the current
//upper counter
ADD A, #99H /* Add 99h to previous value to get next counter*/
DA A // Convert hex value to decimal
MOV 50H,A // Store the counter in upper byte
JNZ UP /* If upper count value not zero, go to relative address (UP)*/
SJMP UP // Repeat this counter until stop running
// provide delay between two counter value
DELAY:MOV DPTR, #04FFH // Initialize the memory pointer
L2: INC DPTR // Increment the memory pointer
MOV A, DPL // Add higher byte and lower byte address
ORL A, DPH
JNZ L2 // If it is not zero, go to relative
//address(L2)
RET // Return to main program
END

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

happen after twice the delay

Department of ETE, BMSIT & M. 30


| Page
EMBEDDED CONTROLLER LAB 18ECL46

DELAY SUBROUTINE:

DELAY: NOP

MOV R2,#25

H0: MOV R3,#255

HI: MOV R4,#255

DJNZ R4,$

DJNZ R3,HI

DJNZ R2,H0

RET

Department of ETE, BMSIT & M. 31


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of ETE, BMSIT & M. 32


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of ETE, BMSIT & M. 33


| Page
EMBEDDED CONTROLLER LAB 18ECL46

4. 2 EXAMPLES OF LOGICAL BIT OPERATIONS


SETB 01H //set bit addressable memory 01h
CLR 01H //clear bit addressable memory 01h
CPL 01H //compliment bit addressable memory 01h
MOV C, 01H /*move bit addressable memory location 01h's content to
carry*/
MOV 07FH,C //move carry to bit addressable memory location 01h
SETB 01H
ANL C, 01H /*logically and bit addressable memory 01h content with carry*/
ORL C, 01H /*logically or bit addressable memory 01h content with carry*/

SJMP $
END

4.3. TWO OUT OF FIVE CODE


This is a program will check the data available at port 1 is a “2_out_of_5_code” or not.
If yes it will send f0 to the port0 otherwise it will send 0f to port 0.
This program demonstrates the use of some LOGICAL instruction and
CONDITIONAL/UNCONDITIONAL JUMPS
Initializing ports and counter.
MOV P1, #0FFH

Department of ETE, BMSIT & M. 34


| Page
EMBEDDED CONTROLLER LAB 18ECL46

MOV P0, #00H


MOV R2, #05H
/*Initially Checking whether any of the last three Most Significant Bits are high or not.
If it is high it is not a “2_OUT_OF_5_CODE”*/
MAIN: MOV A, P1
MOV R0, A
ORL A, #01FH
CJNE A, #01FH, NOTOF
/*If all the last three MSB’s are zero then checking for how many 1’s are present in the
remaining five bits.*/
CHECK:MOV A, R0
AGAIN:RRC A
JNC LOOP
INC R1
LOOP: DEC R2

CJNE R2, #00, AGAIN


/*After completing five times rotation, R1contains the number of 1’s in last five bits. If
the number is two it isa“2_OUT_OF_5_CODE”.*/
CJNE R1, #02, NOTOF
MOV P0, #0F0H //Set upper Nibble of P0 high if it is a //“2_OUT_OF_5_CODE”
SJMP HERE
NOTOF:MOV P0, #00FH //Set lower Nibble of P0 high if it is not a
HERE: SJMP HERE //“2_OUT_OF_5_CODE”
END

Department of ETE, BMSIT & M. 35


| Page
EMBEDDED CONTROLLER LAB 18ECL46

4. 4 BOOLEAN EXPRESSIONS
1. WRITE PROGRAMS TO REALIZE SOME BOOLEAN EXPRESSIONS

TABLE : 1
BIT ADDRESSIBLE MEMORY LOCATIONS AND CORRESPONGING ADDRESSES

BIT BYTE BIT BYTE BIT BYTE BIT BYTE

Department of ETE, BMSIT & M. 36


| Page
EMBEDDED CONTROLLER LAB 18ECL46

00 20.0 10 21.0 20 24.0 30 26.0


01 20.1 11 22.1 21 24.1 31 26.1
02 20.2 12 22.2 22 24.2 32 26.2
03 20.3 13 22.3 23 24.3 33 26.3
04 20.4 14 22.4 24 24.4 34 26.4
05 20.5 15 22.5 25 24.5 35 26.5
06 20.6 16 22.6 26 24.6 36 26.6
07 20.7 17 22.7 27 24.7 37 26.7
08 20.0 18 22.0 28 25.0 38 27.0
09 21.1 19 23.1 29 25.1 39 27.1
0A 21.2 1A 23.2 2A 25.2 3A 27.2
0B 21.3 1B 23.3 2B 25.3 3B 27.3
0C 21.4 1C 23.4 2C 25.4 3C 27.4
0D 21.5 1D 23.5 2D 25.5 3D 27.5
0E 21.6 1E 23.6 2E 25.6 3E 27.6
0F 21.7 1F 23.7 2F 25.7 3F 27.7

// VARIABLE DECLARATION
X EQU 10H
Y EQU 11H
Z EQU 12H
NOT_X EQU 20H
NOT_Y EQU 21H
NOT_Z EQU 22H

OUTPUT EQU P0.0


//INITIALIZING PORTS P1 AS INPUT AND P2 AS OUTPUT PORT

MOV P1, #0FFH


MOV P0, #00H
//INTIALIZING X,Y,Z AND ITS COMPLEMETS AFTER READING THE VALUES FROM
PORT 1
MOV C, P1.0

Department of ETE, BMSIT & M. 37


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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+Y+Z


CLR C
MOV C, X
ORL C, Y
ORL C, Z
MOV OUTPUT, C

//EXPRESSION OUTPUT = XYZ


CLR C
MOV C, X
ANL C, Y
ANL C, Z
MOV OUTPUT, C

Department of ETE, BMSIT & M. 38


| Page
EMBEDDED CONTROLLER LAB 18ECL46

//EXPRESSION OUTPUT = XY+ Z


CLR C
MOV C, X
ANL C, Y
ORL C, Z
MOV OUTPUT, 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

Department of ETE, BMSIT & M. 39


| Page
EMBEDDED CONTROLLER LAB 18ECL46

ORL C, Y
ORL C, Z
CPL C
MOV OUTPUT, C

//EXPRESSION OUTPUT = XYZ + !(XY)Z + !Y!Z


CLR C
MOV C, X
ANL C, Y
ANL C, Z
MOV 00H, C
MOV C, X
ANL C, Y
CPL C
ANL C, Z
MOV 01H, C
MOV C, NOT_Y
ANL C, NOT_Z
ORL C, 01H
ORL C, 00H
MOV OUTPUT, C

HERE: SJMP HERE


END

NOTE:
Give minimum 4 input combinations and verify the results for each expression

Assignment: Realize the function WX+WYX + YZ


Department of ETE, BMSIT & M. 40
| Page
EMBEDDED CONTROLLER LAB 18ECL46

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

MOV A, R2 //A=29, get original data


ANL A, #0F0H //mask the lower nibble A=20h
RR A //rotate right
RR A //rotate right
RR A //rotate right
RR A //rotate right A=02h
ORL A, #30H //A=32h, ASCII char 2
MOV R2,A //save ASCII to char in R2
MOV P1,A
MOV P2,R6
SJMP $
END

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

Department of ETE, BMSIT & M. 42


| Page
EMBEDDED CONTROLLER LAB 18ECL46

5. 3. DECIMAL – ASCII
(Conversion of Decimal number 0-9 to corresponding ASCII equivalent)

RAM_ADDR EQU 40H //initializing addresses


ASC_RSULT EQU 50H //for storing data and result

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

MOV R2,#0AH //results are verified on


NEXT: MOV A,@R1 //PORT 1
MOV P1,A
INC R1
DJNZ R2,NEXT
SJMP $
END

Department of ETE, BMSIT & M. 43


| Page
EMBEDDED CONTROLLER LAB 18ECL46

PART II
VLSI (CADENCE)

Department of ETE, BMSIT & M. 44


| Page
EMBEDDED CONTROLLER LAB 18ECL46

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.

Department of ETE, BMSIT & M. 45 | Page


EMBEDDED CONTROLLER LAB 18ECL46

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).

Department of ETE, BMSIT & M. 46 | Page


EMBEDDED CONTROLLER LAB 18ECL46

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.

Department of ETE, BMSIT & M. 47 | Page


EMBEDDED CONTROLLER LAB 18ECL46

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.

Department of ETE, BMSIT & M. 48 | Page


EMBEDDED CONTROLLER LAB 18ECL46

66. On reset, which interrupt has the highest priority?


67. True or False. There is only a single interrupt for the serial data transfer.

Department of ETE, BMSIT & M. 49 | Page


EMBEDDED CONTROLLER LAB 18ECL46

Department of ETE, BMSIT & M. 50 | Page


EMBEDDED CONTROLLER LAB 18ECL46

Department of ETE, BMSIT & M. 51 | Page


EMBEDDED CONTROLLER LAB 18ECL46

Circular convolution output y(n)


30
25

20

15

10

0 0.5 1 1.5 2 2.5 3


0

Fig 4.1 Circular Convoluted signal

Program 2: Circular convolution using Matrix Method

clc;

clear all;
close all;

x=input('enter the first sequence');


h=input('enter the second sequence');

y=x*h;
disp('circular convolution');

disp(y);
stem(y);

xlabel('n');
ylabel('y(n)');

grid on;

RESULT:

Note: The second sequence is entered in matrix(circulated) form

enter the first sequence[1 2 3 4]

enter the second sequence[1 2 3 4;4 1 2 3;3 4 1 2;2 3 4 1]


circular convolution
26 28 26 20
y(n)

Department of ETE, BMSIT & M. 52 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

30

25

0 1.5 2 2.5 3 3.5 4


1
n
68.

Fig 4.2 Circular convolution using matrix method

Department of Telecommunication Page 22

Department of ETE, BMSIT & M. 53 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Program 2 : Alternate Method (Generalized Case of matrix method)

clc

x=input('enter the first input sequence');

h=input('enter the second input sequence');

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'

Program 3: Circular Convolution Program using Zero Padding Method

Department of ETE, BMSIT & M. 54 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

clc;

clear all;

x=input('Enter first sequence ')

h=input('Enter second sequence ')

N1=length(x);

N2=length(h);

N=max(N1,N2);

N3=N1-N2;

if (N3>=0)

Department of Telecommunication Page 23

Department of ETE, BMSIT & M. 55 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

h=[h,zeros(1,N3)];

else

x=[x,zeros(1,N3)];

end

%compute the output

for r=1:N

y(r)=0;

for (s=1:N)

k=r-s+1; %calculation of x index

if (k<=0)

k=k+N;

end %end of ‘if’

y(r)=y(r)+x(s)*h(k);

end %end of inner ‘for loop’

end %end of outer ‘for loop’

disp('circular convolution of x & h is y=');

disp(y);

%plot

n1=0:N-1;

stem(n1,y);

title('Circular convolution output y(n)');

Result:

Enter first sequence [2 3 4 5]

x=

2 3 4 5

Enter second sequence [1 1 1]

Department of ETE, BMSIT & M. 56 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

h=

1 1 1

circular convolution of x & h is y=


11 10 9 12

Department of Telecommunication Page 24

Department of ETE, BMSIT & M. 57 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Circular convolution output y(n)


12
10

0.5 1 1.5 2 2.5 3


00

Fig 4.3 Circular convoluted output using zero padding method

2.3 Commutative, distributive and associative property of linear convolution.

The linear convolution is the convolution process for the discrete time
systems. The convolution of discrete time (DT) signals possesses the following
properties:

Commutative Property: x(n)*h(n) = h(n)*x(n).

Associative Property: x(n)*[h1(n) *h2(n)] = [x(n)*h1(n)] *h2(n)

Distributive Property: x(n)*[h1(n) + h2(n)] = x(n)*h1(n) + x(n)*h2(n)

The definitions and interpretations of these properties are summarized below.

(1) Commutative Property

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) .

Department of ETE, BMSIT & M. 58 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 25

Department of ETE, BMSIT & M. 59 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

(2) Associative Property :

The convolution operator satisfies the associative property, which is


x(n)*[h1 (n) *h2(n)] = [x(n)*h1 (n)] *h2(n)

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].

(3) Distributive Property :

The distributive property of the convolution operator states that


x(n)*[h1(n) + h2(n)] = x(n)*h1(n) + x(n)*h2(n). From a systems point of view,
this property asserts that if two systems with unit sample responses h 1(n) and
h2(n) are connected in parallel, as illustrated in Fig.2(c)), an equivalent system
is one that has a unit sample response equal to the sum of h1(n) and h2(n):
heq[n] =h1[n]+h2[n].

Figure 2. The interpretation of convolution properties from a systems point of view.

Department of ETE, BMSIT & M. 60 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 26

Department of ETE, BMSIT & M. 61 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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 : x(n) * h1(n) =h1(n) * x(n)

y1=conv(x,h1); % y1 (n) = x(n) * h1(n)

y2=conv(h1,x); % y2 (n) = h1(n) * x(n)

if y1==y2

disp('Commutative Property is proved');

else

disp('Commutative Property is not proved');

end

%Associative Property: x(n) * [h1(n) * h2(n)] = [x(n) * h1(n)] * h2(n) y3=


conv(x,conv(h1,h2)); % y3[n] = x(n) * [h1(n) * h2(n)]
y4=conv(conv(x,h1),h2); % y4[n] = [x(n) * h1(n)] * h2(n)

if y3==y4

disp('Associative property is proved');

else

disp('associative property is not proved');


end

%Distributive Property : x(n) *[ h1(n) + h2(n)] = [x(n) * h1(n)] + [x(n) *


h2(n) ]

y5=conv(x,(h1+h2)); % y5(n) = x(n) *[ h1(n) + h2(n)]

Department of ETE, BMSIT & M. 62 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%z1 = conv(x,h1);

%z2= conv(x,h2);

y6=(conv(x,h1)+conv(x,h2)); % y6(n) = [x(n) * h1(n)] + [x(n) * h2(n) ]

Department of Telecommunication Page 27

Department of ETE, BMSIT & M. 63 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

if y5==y6

disp('distributive property proved');

else

disp('distributive property not proved');

end

Result :

Commutative Property is proved


Associative property is proved

distributive property proved

2.4 Commutative, distributive and associative property of Circular convolution.

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

disp('Commutative Property is proved');


else

disp('Commutative Property is not proved');


end

Department of ETE, BMSIT & M. 64 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%Associative Property

y3= circonv(x,circonv(h1,h2));

y4=circonv(circonv(x,h1),h2);

if y3==y4

Department of Telecommunication Page 28

Department of ETE, BMSIT & M. 65 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

disp ('Associative property is proved');

else

disp ('associative property is not proved');


end

%Distributive Property

y5=circonv (x,(h1+h2));

y6= ((circonv (x,h1))+(circonv (x,h2)));

if y5==y6

disp ('distributive property proved');

else

disp ('distributive property not proved');

end

Function file to compute Circular Convolution

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

Department of ETE, BMSIT & M. 66 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

folder with file name as: function name with dot m (.m)
extension.

Department of Telecommunication Page 29

Department of ETE, BMSIT & M. 67 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result :

Commutative Property is proved

Associative property is proved

distributive property proved

Viva Questions :

Explain how linear convolution can be obtained.

Explain different properties of convolution

What is the length of the output sequence of linear convolution

Give the different methods for to find convolution

Write A Program to find the circular convolution of two sequences using Matrix
Method

Explain how circular convolution can be obtained.

Explain different properties of circular convolution

List out the difference between linear and circular convolution.

Why is zero padding used in circular convolution .

Give the different methods for performing circular convolution

Department of ETE, BMSIT & M. 68 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 30

Department of ETE, BMSIT & M. 69 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 3

AUTO AND CROSS CORRELATION OF TWO SEQUENCES AND

VERIFICATION OF THEIR PROPERTIES

3.1 Autocorrelation of the given sequence and verify its properties

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

correlation sequence rxy[l] defined by rxy [l] = ∑ x[n]y[n − l]; l = 0,±1,±2,... .


n=−∞

The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.

∙ Autocorrelation sequence of x[n] is given by rxx [l] = ∑ x[n]x[n − l]; l = 0,±1,±2,...


n=−∞

Some of the properties of autocorrelation are enumerated below

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.,

rxx [l] ≤ rxx [0] = ε x = ∑ x 2 [n] .


n=−∞

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.

Department of ETE, BMSIT & M. 70 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

o For power signalstheautocorrelation sequenceis given by


k
1
rxx [l] = ∑ x[n]x[n − l]; l = 0,±1,±2,... and for periodic signals with
lim
k →∞
2k 1
+ n=−k

Department of Telecommunication Page


31

Department of ETE, BMSIT & M. 71 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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:

Input the sequence as x.

Use the ‘xcorr’ function to get auto correlated output r.

Plot the sequences.

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.

XCORR(...,MAXLAG) computes the (auto/cross) correlation over the range of lags: -


MAXLAG to MAXLAG, i.e., 2*MAXLAG+1 lags. If missing, default is MAXLAG = M-
1.

[C,LAGS] = XCORR(...) returns a vector of lag indices (LAGS).

OCTAVE Programs

Computation of Autocorrelation of rectangular Sequence [with out in-built function]

= -5:5;
N=10;

%Generate the square sequence x


= ones(1,11);

Department of ETE, BMSIT & M. 72 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%Compute the correlation sequence r


= conv(x,fliplr(x));
disp('autocorrelation sequence r=');
disp(r);

%plot the sequences


subplot(2,1,1)
stem(n,x);

Department of Telecommunication Page 32

Department of ETE, BMSIT & M. 73 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

title('square sequence');

subplot(2,1,2)

= -N:N; stem(k, r);


title('autocorrelation output');
xlabel('Lag index');
ylabel('Amplitude');

disp('auto-correlation r=');
disp(r);

to calculate the Energy of the input signal


e=sum(x.^2);

disp('Maximum value of energy =');


disp(e);

disp('Maximum value of energy is at the zero lag index');


disp('The auto-correlated sequence is a even sequence');

Computation of Autocorrelation of rectangular Sequence [with in-built function]

= -5:5;
N=10;

%Generate the square sequence x


= ones(1,11);

%Compute the correlation sequence


r=xcorr(x); disp('autocorrelation
sequence r='); disp(r);

%plot the sequences


subplot(2,1,1) stem(n,x);
title('square sequence');
subplot(2,1,2)

k = -N:N; stem(k, r);


title('autocorrelation output');

Department of ETE, BMSIT & M. 74 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

xlabel('Lag index');
ylabel('Amplitude');

disp('auto-correlation r=');

Department of Telecommunication Page 33

Department of ETE, BMSIT & M. 75 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

disp(r);

to calculate the Energy of the input signal


e=sum(x.^2);

disp('Maximum value of energy =');


disp(e);

disp('Maximum value of energy is at the zero lag index');


disp('The auto-correlated sequence is a even sequence');

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

Maximum value of energy is at the zero lag index The


auto-correlated sequence is a even sequence

Fig.5.1 Auto-correlated sequence for rectangular pulses

Inference: Following autocorrelation properties are verified

Max peak of 11 (=energy of the pulse) at zero lag (l=0)

Department of ETE, BMSIT & M. 76 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

The autocorrelation sequence is an even function

Department of Telecommunication Page 34

Department of ETE, BMSIT & M. 77 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Exercise problem: Modify the above program for a given input x(n) =[1 2 3 4]

Computation of Autocorrelation of noise corrupted sinusoidal sequence

N = 30; %number of points of input

n = 1:N;

Generate the sinusoidal sequence x =


cos(pi*0.25*n);

Generate the noise sequence

d = rand(1,N) - 0.5;

%Generate the noise-corrupted sinusoidal sequence y


= x + d;

Compute the autocorrelation sequence of corrupted signal r =


conv(y, fliplr(y));

[ry,lag]=xcorr(y);

Compute the autocorrelation sequence of noise


[rn,lag1]=xcorr(d);

%plot the sequences


figure(1) subplot(3,1,1)
stem(x);

title('reference sinusoidal sequence');


subplot(3,1,2)

stem(y)

title('noise corrupted signal');


subplot(3,1,3)

stem(lag, r);

Department of ETE, BMSIT & M. 78 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

title('autocorrelation seq of corrupted signal');


xlabel('Lag index'); ylabel('Amplitude');
figure(2);

stem(lag1,rn); title('autocorrelation seq of


noise'); xlabel('Lag index');
ylabel('Amplitude');

Result: Plots in Fig.5.2 & 5.3

Department of Telecommunication Page 35

Department of ETE, BMSIT & M. 79 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Fig. 5.2 Auto-correlation of noise sequence

Fig. 5.3 Auto-correlation of sequences

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

Department of ETE, BMSIT & M. 80 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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.

Department of Telecommunication Page 36

Department of ETE, BMSIT & M. 81 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

3.2 Cross Correlation Of A Given Sequence And verification of Its Properties


Aim: To obtain cross correlation of the given sequence and verify its properties.
Theory:

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=−∞

reference signal with a folded version of sequence to be shifted (y[n]) results in


cross correlation output. (Use ‘fliplr’ function for folding the sequence for
correlation).

The properties of cross correlation are 1) the cross correlation sequence

sample values are upper bounded by the inequality

rxx [l] ≤ rxx [0]ryy [0] = ε xε y

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

obtained by time reversing the sequence rxy[l].


%Verification of the properties

%Small OCTAVE code

Department of ETE, BMSIT & M. 82 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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:

Strong peak of 15 at lag = -2 implies the delay between xi and x2 is 2.

Department of Telecommunication Page 37

Department of ETE, BMSIT & M. 83 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Also peak =15=energy of xi (1+22+32+(-1)2) implies that both xi and x2 are strongly
correlated.

%consider the below sequences

xr=[1,2,3,-1];

x2=[3,-1,1,2];

xcorr(xr,x2) ans = {2,5,7, 2,2,10,−3}


xcorr(x2,xr) ans = {−3,10,2, 2,7,5,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:

Input the sequence as x and y.

Use the ‘xcorr’ function to get cross correlated output r.

Plot the sequences.

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.

Department of ETE, BMSIT & M. 84 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 38

Department of ETE, BMSIT & M. 85 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

OCTAVE Programs

Computation of Cross-correlation Sequence using folded sequence


and convolution [ without in-built function]

x = input('Type in the reference sequence = '); y =


input('Type in the second sequence = ');
Compute the correlation sequence

n1 = length(y)-1;

n2 = length(x)-1;

r = conv(x,fliplr(y));

disp(‘Cross correlation output is =’);

disp(r);

k = (-n1):n2; %time vector for plotting

stem(k,r);

xlabel('Lag index');

ylabel('Amplitude');

Result:

Type the reference sequence = [1 -2 6 1]

Type the second sequence = [1 2 3 4]

Cross correlation output is =

4 -5 20 19 13 8 1

20

15

10

Amplitude

Department of ETE, BMSIT & M. 86 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

0
-10 -2 -1 0 1 2 3
-3
-5
Lag
index

Department of Telecommunication Page 39

Department of ETE, BMSIT & M. 87 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Fig.6.1 Cross correlation output using Convolution

Computation of Cross-correlation Sequence using xcorr function

x = input('Type in the reference sequence = '); y =


input('Type in the second sequence = ');
Compute the correlation sequence

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');

Verification: with xcorr(x,y), the output is

Type the reference sequence = [1 3 -2 1 2 -1 4 4 2]

Type the second sequence = [2 -1 4 1 -2 3]


Cross correlation output is =

Columns 1 through 9

-0.0000 -0.0000 0.0000 3.0000 7.0000 -11.0000 14.0000 13.0000 - 15.0000

Columns 10 through 17

28.0000 6.0000 -2.0000 21.0000 12.0000 12.0000 6.0000 4.0000

Department of ETE, BMSIT & M. 88 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 40

Department of ETE, BMSIT & M. 89 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Fig.6.2 Cross correlation output using xcorr

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

Explain the differences between convolution and correlation

List the methods for performing correlation

Which are the OCTAVE functions used to perform correlation

List out the properties of Auto-correlation.

Explain the following functions

rand(1,N) - 0.5

fliplr(y)

xcorr(y)

Explain the differences between cross correlation and autocorrelation

List the methods for performing cross correlation

List out the applications of cross correlation.

Which are the OCTAVE functions used to perform cross correlation

Department of ETE, BMSIT & M. 90 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 41

Department of ETE, BMSIT & M. 91 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 4

SOLVING A GIVEN DIFFERENCE EQUATION

Aim: To obtain the impulse response/step response/steady state response/response to an


arbitrary input of a system described by the given difference equation

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.

The output depends on the input x[n]

With x[n]= δ[n], an impulse, the computed output y[n] is the impulse

response.

If x[n]=u[n], a step response is obtained.

If x[n] = cos(wn) is a sinusoidal sequence, a steady state response is obtained


(wherein y[n] is of the same frequency as x[n], with only an

amplitude gain and phase shift-refer Fig.7.3).

Similarly for any arbitrary sequence of x[n], the corresponding output


response y[n] is computed.

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

Department of ETE, BMSIT & M. 92 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of Telecommunication Page 42

Department of ETE, BMSIT & M. 93 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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:

Input the two sequences as a and b representing the coefficients of y and x.

If IIR response, then input the length of the response required (say 100, which can be
made constant).

Compute the output response using the ‘filter’ command.

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).

Also remember a0, the coefficient of y[n] should always be 1.

For impulse response x[n] = {1,0,0,0,....} the number of zeros = the length of the IIR

response required (say 100 implemented by function zeros(1,99)).

For step response x[n] = {1,1,1,1,1,....} the number of ones = the length of the IIR

response required-1 (say 100 implemented by function ones(1,100).

Similarly for any given x[n] sequence, the response y[n] can be calculated.

Department of ETE, BMSIT & M. 94 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 43

Department of ETE, BMSIT & M. 95 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

To find Impulse Response

N=input('Length of response required=');

b=[2]; %x[n] coefficient

a=[1,0.5]; %y coefficients

%impulse input

x=[1,zeros(1,N-1)];

%time vector for plotting

n=0:1:N-1;

%impulse response

h=filter(b,a,x);

%plot the waveforms

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:

Length of response required=8

h= 2.0000 -1.0000 0.5000 -0.2500 0.1250 -0.0625 0.0313 -0.0156

Department of Telecommunication Page 44

Department of ETE, BMSIT & M. 97 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Fig.7.1 Impulse Response

To find step response

N=input('Length of response required=');

b=[2]; %x[n] coefficient

a=[1,0.5]; %y coefficients

x=[ones(1,N)]; %step input

n=0:1:N-1; %time vector for plotting

y=filter(b,a,x) %step response

%plot the waveforms

subplot(2,1,1);

stem(n,x);

title('step input');

xlabel('n');

ylabel('u(n)');

subplot(2,1,2);

Department of ETE, BMSIT & M. 98 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

stem(n,y);

title('step response');

xlabel('n');

ylabel('y(n)');

Department of Telecommunication Page 45

Department of ETE, BMSIT & M. 99 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result:

Length of response required=10

y = 2.0000 1.0000 1.5000 1.2500 1.3750 1.3125 1.3438 1.3281 1.3359


1.3320

Plots in Fig. 7.2

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

Fig.7.2 Step Response

To find response of the system for the given input

N=input('Length of response required=');

b=[2]; %x[n] coefficient

a=[1,0.5]; %y coefficients

n=0:1:N-1; %time vector for plotting

x= 2.^n ; %Arbitrary input


Department of ETE, BMSIT & M. 100 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

y=filter(b,a,x) %step response

%plot the waveforms

subplot(2,1,1);

stem(n,x);

title('input sequence');

xlabel('n');

Department of Telecommunication Page 46

Department of ETE, BMSIT & M. 101 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

ylabel('x(n)');

subplot(2,1,2);

stem(n,y);

title('system response');

xlabel('n');

ylabel('y(n)');

Result:

Length of response required=5

y=

2.0000 3.0000 6.5000 12.7500 25.6250

Plots in Fig. 7.3

input sequence
20
15

10

x(n) 5

0.5 1 1.5 2 2.5 3 3.5 4


00

system response
30
20

y(n
)
10
0.5 1 1.5 2 2.5 3 3.5 4
00

Fig. 7.3 System response for a given input

To find steady state response

N=input('Length of response required=');

b=[2]; % x(n) coefficient


Department of ETE, BMSIT & M. 102 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

a=[1,0.5]; % y(n) coefficients

n=0:1:N-1; %time vector

x=cos(0.05*pi*n); % sinusoidal input

y=filter(b,a,x) %steady state response

Department of Telecommunication Page 47

Department of ETE, BMSIT & M. 103 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%plot the waveforms

subplot(2,1,1);

stem(n,x);

title('input sequence');

xlabel('n');

ylabel('x(n)');

subplot(2,1,2);

stem(n,y);

title('steady state response');

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

Plots in Fig. 7.4


input sequence
1
0.5

0
x(n)
-0.5

5 10 15 20 25
-10

n
steady state response

y(n)

Department of ETE, BMSIT & M. 104 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

2 -1
1
5 10 15 20 25
0 -20

Fig. 7.4 Steady state response to a Cosine input

Department of Telecommunication Page 48

Department of ETE, BMSIT & M. 105 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Further Self Study:

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).

Solve the difference equation with initial conditions.

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⎠

and the initial conditions are

y[-1]=-2; y[-2]=-3; x[-1]=1; x[-2]=1.

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.

Department of ETE, BMSIT & M. 106 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 49

Department of ETE, BMSIT & M. 107 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

The sample program for the above problem is:

b=[1/3 1/3 1/3];

a=[1 -0.95 0.9025]; %difference equation coefficient arrays

Y=[-2 -3]; X=[1 1]; %initial condition arrays

xic=filtic(b,a,Y,X);

n=0:7; %For 8 output samples using filter

function, the input should also be of size 8

x=cos(pi*n/3); %input sequence

y=filter (b,a,x,xic); %output response

Answer: y = 1.8075 4.3555 2.8398 -1.5664 -4.7176 -3.4014

1.3596 5.0281

Questions

Explain the OCTAVE function filter

How to find various responses using filter function.

3. Given y(n) – 0.25y(n-1) – 0.375y(n-2) = -x(n) + 2x(n-1) find

Impulse response

Step response

Response of the system to the input given by x(n) = 2n u(n).

Steady state response.

Department of ETE, BMSIT & M. 108 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 50

Department of ETE, BMSIT & M. 109 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 5

COMPUTATION OF N POINT DFT OF A GIVEN SEQUENCE AND TO

PLOT MAGNITUDE AND PHASE SPECTRUM.

Aim: To compute DFT of the given sequence.

Algorithm:

Input the sequence for which DFT is to be computed.

Input the length of the DFT required (say 4, 8, >length of the sequence).

Compute the DFT using the function dft.

Plot the magnitude & phase spectra.

OCTAVE Implementation:

Create a OCTAVE function dft. The procedure is as follows.

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).

Department of ETE, BMSIT & M. 110 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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.

Department of Telecommunication Page 51

Department of ETE, BMSIT & M. 111 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

OCTAVE PROGRAMS --

Program:

Program 1: N point DFT and IDFT of a given sequence

clc;

clear all;

xn=input('enter the sequence x(n)');

N=input('enter the number of points of computation ');

y=fft(xn,N);

disp(y);

mag=abs(y);

phase=angle(y);

subplot(2,2,1);

stem(xn);

title('Input Sequence x(n)');

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);

Department of ETE, BMSIT & M. 112 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

stem(mag1);

title('signal sequence constituted from spectrum');

Department of Telecommunication Page 52

Department of ETE, BMSIT & M. 113 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

RESULT:

enter the sequence x(n)[1 1 1 1 0 0 0 0]


enter the number of points of computation 8

Columns 1 through 6

4.0000 1.0000 - 2.4142i 0 1.0000 - 0.4142i 0 1.0000 +


0.4142i

Columns 7 through 8

0 1.0000 + 2.4142i

Plots in Fig. 8.1

Input Sequence x(n) magnitude plot


1 4
M 3
A
0.5 G 2
NI
T
U
D
E
1

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

Fig.8.1 DFT and IDFT

Department of ETE, BMSIT & M. 114 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 53

Department of ETE, BMSIT & M. 115 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Program : N point DFT of a given sequence using a function file dft

clc;

clear all;

xn=input('enter the sequence x(n) = ');

N=input('enter the number of points of computation = ');

y=dft(xn,N);

disp(y);

mag=abs(y);

phase=angle(y);

subplot(2,2,1);

stem(xn);

title('Input Sequence x(n)');

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

name as: function name with dot m (.m) extension.

function[xk]=dft(xn,N)

n=[0:1:N-1];
k=[0:1:N-1];

Department of ETE, BMSIT & M. 116 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

WN=exp(-j*2*pi/N);
nk=n'*k;

WNnk=WN.^nk;
xk=xn*WNnk;

Here name of the function is dft and save it as dft.m

Department of Telecommunication Page 54

Department of ETE, BMSIT & M. 117 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

RESULT:

enter the sequence x(n) = [1 1 1 1 0 0 0 0] enter the


number of points of computation = 8

Columns 1 through 3

4.0000 1.0000 - 2.4142i -0.0000 - 0.0000i

Columns 4 through 6
1.0000 - 0.4142i 0 - 0.0000i 1.0000 + 0.4142i

Columns 7 through 8

0.0000 - 0.0000i 1.0000 + 2.4142i

Plots in Fig. 8.2

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

Department of ETE, BMSIT & M. 118 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Fig. 8.2 DFT

Program 3 : N point IDFT of a given sequence using a function file idft

clc;

clear all;
XK=input('enter the sequence X(K) = ');

N=input('enter the number of points of computation = ');


y=idft(XK,N);

disp(y);
mag=abs(y);

phase=angle(y);
subplot(2,1,1);

stem(mag);

Department of Telecommunication Page 55

Department of ETE, BMSIT & M. 119 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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 name with dot m (.m) extension

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;

Here name of the function is idft and save it as idft.m

RESULT:

enter the sequence X(K) = [4 0-2i 0 0+2i]


enter the number of points of computation = 4

Columns 1 through 3

1.0000 2.0000 - 0.0000i 1.0000

Column 4
0 + 0.0000i

Plots in Fig. 8.3


phase

MAGNIT
UDE

Department of ETE, BMSIT & M. 120 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of Telecommunication Page 56

Department of ETE, BMSIT & M. 121 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Inputs -

Run1: Give 4 point sequence and find 4 point DFT

Run2: Give 8 point sequence and find 8 point DFT

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

What are the methods of finding DFT

Explain differences between DFT and FFT

Explain how functions can be declared in MATLB.

Explain OCTAVE function fft(x)

How can user create a OCTAVE function.

Department of ETE, BMSIT & M. 122 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 57

Department of ETE, BMSIT & M. 123 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 6

(1 ) VERIFICATION OF DFT PROPERTIES (LIKE LINEARITY &


PARSEVALS THEOREM, etc .)

(2) DFT COMPUTATION OF SQUARE PULSE AND SINC FUNCTION etc.

6. 1 VERIFICATION OF DFT PROPERTIES (LIKE LINEARITY &


PARSEVALS THEOREM, etc .)

Aim: To verify following properties of DFT

Linearity

Parseval’s Theorem

Convolution Property

Linearity Property :

Linearity Property

clc;

clear all;

close all;

LINEARITY PROPERTY

a1*x1(n) + a2*x2(n)<-->a1*X1(K) + a2*X2(K).

x1=[1,2,3,4];

x2=[1,1,1,1];

a1=1;

a2=1;

term1= a1.*x1 + a2.*x2;

LHS=fft(term1);

term2= fft(a1.*x1);

term3 = fft(a2.*x2);

Department of ETE, BMSIT & M. 124 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

RHS = term2 + term3;

if (LHS==RHS)

disp('Linearity property proved');

disp('LHS=');

disp(LHS);

Department of Telecommunication Page 58

Department of ETE, BMSIT & M. 125 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

disp('RHS=');

disp(RHS);

else

disp ('DFTs are not linear');

end

Result :

Linearity property proved

LHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i

RHS=

14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i

(ii) Parsevals Theorem :

Parsevals Property

clc;

clear all;

close all;

Parseval's theoerm
x=[1,2,3,4];

XK =fft (x);

en1 = sum(x.^2);

en2= (1/4)* sum(abs(XK).^2);

if (en1==en2)

disp('Parevals theorem proved');

disp('Energy=');
Department of ETE, BMSIT & M. 126 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

disp(en2);

end

Department of Telecommunication Page 59

Department of ETE, BMSIT & M. 127 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result :

Parevals theorem proved

Energy = 30

(iii) Circular Convolution Property:

Theory:

By multiplying two N-point DFTs in the frequency domain, we get the circular
convolution in the time domain.

y[n] = x[n] * h[n]←⎯N−DFT⎯→Y (k ) = X (k) × H (k )

Circular Convolution is made identical to linear convolution by choosing the number


of points in DFT as N ≥ xlength + hlength -1.

For Circular Convolution, N = max (xlength, length).

Algorithm:

Input the two sequences as x1, x2

Compute N=max(xlength + hlength))

Obtain N-point DFTs (X1, X2) of both the sequences.

Multiply both the DFTs (Y=X1×X2).

Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time domain.

Verify using the ‘conv’ command.

Plot the sequences.

Circular Convolution Program using DFT

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);

%Perform vector multiplication

y= X1.*X2;

Department of Telecommunication Page 60

Department of ETE, BMSIT & M. 129 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%ifft to get y[n]

yn= ifft(y,N);

disp('circular convolution of x1 &x2 is yn=');

disp(yn);

%plot

stem(yn);

title('Circular convolution output y(n)');

Result:
circular convolution of x1 &x2 is yn= 26 28 26 20
Circular convolution output y(n)

30
25

20

15

10

01

1.5 2 2.5 3 3.5 4

Fig.10.1 Circular convolution using DFT

(iv) circular shift Property :

Circular shift Property

Circular shift Property x =

[3 1 5 2 4]';

N = length(x); % N: data length

= exp(2*pi/N*sqrt(-1));

= fft(x);
Department of ETE, BMSIT & M. 130 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

= [0:N-1]'; % frequency index m =


2; % shift

G = W.^(-m*k) .* X;

Department of Telecommunication Page 61

Department of ETE, BMSIT & M. 131 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

g = ifft(G);

disp ('the circular shifted version of x = ');

disp(g);

Result :

the circular shifted version of x =

2.0000 - 0.0000i

4.0000 + 0.0000i

3.0000 - 0.0000i

1.0000 - 0.0000i

5.0000 + 0.0000i

(v) Time Reversal Property:

Time Reversal Property

%Time reversal Property

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);

disp ('the Time reverse of x = ');


disp(g);

Result :

Department of ETE, BMSIT & M. 132 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

the Time reverse of x =

Department of Telecommunication Page 62

Department of ETE, BMSIT & M. 133 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

(vi) Complex Conjugation Property of The DFT

COMPLEX CONJUGATION PROPERTY OF THE DFT

clc; clear all; close all

N=5;

n = 0:N-1;

i = sqrt(-1);

= [3+2*i 5-3*i 6+i 4-7*i 8+9*i]; Xk


= fft(x);

Gk = conj(Xk(mod(-n,N)+1)); g
= ifft(Gk);

disp('X(n) =');
disp(x);

disp ('X*(n)=');
disp(g);

Result :
X(n) =

3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +


9.0000i

X*(n)=

3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -


9.0000i

(Vii) Real X[N] Property of The DFT :

COMPLEX CONJUGATION PROPERTY OF THE DFT

clc; clear all; close all

N=5;

Department of ETE, BMSIT & M. 134 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

n = 0:N-1;

i = sqrt(-1);

= [3+2*i 5-3*i 6+i 4-7*i 8+9*i]; Xk


= fft(x);

Gk = conj(Xk(mod(-n,N)+1)); g
= ifft(Gk);

Department of Telecommunication Page 63

Department of ETE, BMSIT & M. 135 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

disp('X(n) =');

disp(x);

disp ('X*(n)=');

disp(g);

Result :

X(n) =

3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +

9.0000i

X*(n)=

3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -

9.0000i

(vii) circular symmetric property of real signals

circular symmetries present in the DFT of a real signal.

In this case, the signal is of odd length. clc;


clear all; close all

x1 = [3 7 5 2 4 1 5]';
Xk1 = fft(x1);

y1= [real(Xk1) imag(Xk1)];

disp(' circular symmetries present in the DFT of a real odd signal= ');
disp(y1);

the signal is of even length.

x2 = [3 7 5 2 4 1 3 2]';

Xk2 = fft(x2);
Department of ETE, BMSIT & M. 136 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

y2= [real(Xk2) imag(Xk2)];

disp(' circular symmetries present in the DFT of a real even signal= ');
disp(y2);

Department of Telecommunication Page 64

Department of ETE, BMSIT & M. 137 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

6.2 DFT COMPUTATION OF SQUARE PULSE AND SINC FUNCTION

Aim: To obtain DFT computation of square pulse and sinc function

OCTAVE Program:

To find DFT of Square pulse

clc;

clear all;

close all;

generating square pulse


x1=[zeros(1,32),ones(1,64),zeros(1,32)];
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');

Department of ETE, BMSIT & M. 138 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result :

Department of Telecommunication Page 65

Department of ETE, BMSIT & M. 139 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

To find DFT of SINC function

generating a sinc function x= -


pi : 0.05 : pi; x1=sinc(x);

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');

Department of ETE, BMSIT & M. 140 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 66

Department of ETE, BMSIT & M. 141 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Viva Questions

State all properties of DFT.

Define sinc function

Define pulse signals

Department of ETE, BMSIT & M. 142 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication Page 67

Department of ETE, BMSIT & M. 143 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 7

DESIGN AND IMPLEMENTATION OF FIR FILTER TO MEET GIVEN

SPECIFICATIONS

Aim: To design and implement a FIR filter for given 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.

DESIGNING AN FIR FILTER (using window method):

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

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 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

OCTAVE inbuilt command ‘fir1’ as

h =fir1(N, wc , 'high', boxcar(N+1));

Note: In theory we would have calculated h[n]=hd[n]×w[n], where hd[n] is the


desired impulse response (low pass/ high pass,etc given by the sinc function) and

Department of ETE, BMSIT & M. 144 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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).

Department of Telecommunication, BMSIT Page 68

Department of ETE, BMSIT & M. 145 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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 1: Select the window depending on the stop-band attenuation required.


Generally if

As>40 dB, choose Hamming window. (Refer table )

Step 2: Compute order N based on the edge frequencies as Transition bandwidth =


tb=ws-wp; N=ceil (6.6*pi/tb);

Step 3: Compute the digital cut-off frequency Wc as Wc=(wp+ws)/2. Now

compute the normalized frequency in the range 0 to 1 for OCTAVE as wc=Wc/pi;

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.

IMPLEMENTATION OF THE FIR FILTER

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

Department of ETE, BMSIT & M. 146 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of Telecommunication, BMSIT Page 69

Department of ETE, BMSIT & M. 147 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

linear phase, i.e., even symmetric coefficients obeying B(k) = B(N+2-k), k = 1,2,...,N+1.

If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter


with passband W1 < W < W2. B = FIR1(N,Wn,'high') designs a highpass filter. B =
FIR1(N,Wn,'stop') is a bandstop filter if Wn = [W1 W2]. If Wn is a multi-element

vector, Wn = [W1 W2 W3 W4 W5 ... WN], FIR1 returns an order N multiband

filter with bands

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

Commonly used window function characteristics


Window Transition Width Δω Min. Stop band
OCTAVE
Name Approximate Exact values Attenuation Command
Rectangular 4Π 1.8Π 21db B

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

Department of ETE, BMSIT & M. 148 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

FIR1(N,Wn)

Department of Telecommunication, BMSIT Page 70

Department of ETE, BMSIT & M. 149 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Blackman 12Π 11Π 74db


M M

B=FIR1(N,Wn,blackman)

RECTANGULAR WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:'); %bandpass filter
wp=2*fp/f;
ws=2*fs/f; wn=[wp,ws];
num=-20*log10(sqrt(lp*ls))-13; b=fir1(n,wn,y);
sum=14.6*(fs-fp)/f; [h,o]=freqz(b,1,256);
n=ceil(num/sum); m=20*log10(abs(h));
n1=n+1; subplot(2,2,3);
if(rem(n,2)) plot(o/pi,m)
N=0; ylabel('gain in db');
n1=n; xlabel('(c)normalised freq');
n=n-1; grid on;
end %band stop filter
y=boxcar(n1); b=fir1(n,wn,'stop',y);
%low pass filter [h,o]=freqz(b,1,256);
m=20*log10(abs(h));
b=fir1(n,wp,y); subplot(2,2,4);
[h,o]= freqz(b,1,256); plot(o/pi,m)
m=20*log10(abs(h)); ylabel('gain in db');
subplot(2,2,1); xlabel('(d)normalised freq');
plot(o/pi,m) grid on;
ylabel('gain in db');
xlabel('(a)normalised freq');
grid on;
RESULT:

enter the passband ripple:0.05


enter the stopband ripple:0.04
enter the passband frequency:1500
Department of ETE, BMSIT & M. 150 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

enter the stopband frequency:2000


enter the sampling frequency:9000

Department of Telecommunication, BMSIT Page 71

Department of ETE, BMSIT & M. 151 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

50 50
ga 0 gain in 0
in db
in
db -50 -50

-100 0 0.5 1 -100 0 0.5 1

(a)normalised freq (b)normalised freq

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

(c)normalised freq (d)normalised freq

Fig 11.1 Frequency response of rectangular window

HAMMING WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:'); %bandpass filter
wp=2*fp/f;
ws=2*fs/f; wn=[wp,ws];
num=-20*log10(sqrt(lp*ls))-13; b=fir1(n,wn,y);
sum=14.6*(fs-fp)/f; [h,o]=freqz(b,1,256);
n=ceil(num/sum); m=20*log10(abs(h));
n1=n+1; subplot(2,2,3);
if(rem(n,2)) plot(o/pi,m)
N=0; ylabel('gain in db');
n1=n; xlabel('(c)normalised freq');
n=n-1; grid on;
end
y=hamming(n1); %band stop filter
Department of ETE, BMSIT & M. 152 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

%low pass filter b=fir1(n,wn,'stop',y);

[h,o]=freqz(b,1,256);
Department of Telecommunication, BMSIT Page 72

Department of ETE, BMSIT & M. 153 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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:

enter the passband ripple:0.05


enter the stopband ripple:0.001
enter the passband frequency:1200
enter the stopband frequency:1700
enter the sampling frequency:9000

50 50
0 in 0
db
gain
in db -50
ga
-100 -50
in

0.5 1 -100 0 0.5 1


-1500

(a)normalised freq (b)normalised freq

0 5
0

db -50 db -5
in in
-100
ga gai -10
in n
-15

0.5 1 0.5 1
-1500 -200

(c)normalised freq (d)normalised freq

Fig 11.2 Frequency response of hamming window

Department of ETE, BMSIT & M. 154 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 73

Department of ETE, BMSIT & M. 155 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

BARTLETT WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:');
wp=2*fp/f; %bandpass filter
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13; wn=[wp,ws];
sum=14.6*(fs-fp)/f; b=fir1(n,wn,y);
n=ceil(num/sum); [h,o]=freqz(b,1,256);
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=bartlett(n1);
%band stop filter
%low pass filter
b=fir1(n,wn,'stop',y);
b=fir1(n,wp,y); [h,o]=freqz(b,1,256);
[h,o]= freqz(b,1,256); m=20*log10(abs(h));
m=20*log10(abs(h)); subplot(2,2,4);
subplot(2,2,1); plot(o/pi,m)
plot(o/pi,m) ylabel('gain in db');
ylabel('gain in db'); xlabel('(d)normalised freq');
xlabel('(a)normalised freq'); grid on;
grid on;

RESULT:

enter the passband ripple:0.05


enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000

Department of ETE, BMSIT & M. 156 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 74

Department of ETE, BMSIT & M. 157 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

(c)normalised freq (d)normalised freq

Fig 11.3 Frequency response of barlet window

HANNING WINDOW

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband ripple:'); [h,o]=freqz(b,1,256);
ls=input('enter the stopband ripple:'); m=20*log10(abs(h));
fp=input('enter the passband subplot(2,2,2);
frequency:'); plot(o/pi,m)
fs=input('enter the stopband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
f=input('enter the sampling grid on;
frequency:');
wp=2*fp/f; %bandpass filter
ws=2*fs/f;
num=-20*log10(sqrt(lp*ls))-13; wn=[wp,ws];
sum=14.6*(fs-fp)/f; b=fir1(n,wn,y);
n=ceil(num/sum); [h,o]=freqz(b,1,256);
Department of ETE, BMSIT & M. 158 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

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);

Department of Telecommunication, BMSIT Page 75

Department of ETE, BMSIT & M. 159 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

%low pass filter %band stop filter

b=fir1(n,wp,y); b=fir1(n,wn,'stop',y);

[h,o]= freqz(b,1,256); [h,o]=freqz(b,1,256);


m=20*log10(abs(h)); m=20*log10(abs(h));
subplot(2,2,1); subplot(2,2,4);
plot(o/pi,m) plot(o/pi,m)
ylabel('gain in db'); ylabel('gain in db');
xlabel('(a)normalised freq'); xlabel('(d)normalised freq');
grid on; grid on;

RESULT:

enter the passband ripple:0.03


enter the stopband ripple:0.01

enter the passband frequency:1400


enter the stopband frequency:2000

enter the sampling frequency:8000


50 50
0

in in 0
db db
-50

gai gai -50


n n
-100
0.5 1 0.5 1
-1500 -1000

(a)normalised freq (b)normalised freq

0 5
-50 0

in in db
db
-5

ga -100 gain
in
-10
0.5 1 0.5 1
-1500 -150

(c)normalised freq (d)normalised freq

Fig 11.4 Frequency response of hanning window

Department of ETE, BMSIT & M. 160 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 76

Department of ETE, BMSIT & M. 161 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

KAISER WINDOW (optional)

clc; %high pass filter


clear all;
close all; b=fir1(n,wp,'high',y);
lp=input('enter the passband [h,o]=freqz(b,1,256);
ripple:'); m=20*log10(abs(h));
ls=input('enter the stopband subplot(2,2,2);
ripple:'); plot(o/pi,m)
fp=input('enter the passband ylabel('gain in db');
frequency:'); xlabel('(b)normalised freq');
fs=input('enter the stopband grid on;
frequency:');
f=input('enter the sampling %bandpass filter
frequency:');
beta=input('enter the beta value'); wn=[wp,ws];
wp=2*fp/f; b=fir1(n,wn,y);
ws=2*fs/f; [h,o]=freqz(b,1,256);
num=-20*log10(sqrt(lp*ls))-13; m=20*log10(abs(h));
sum=14.6*(fs-fp)/f; subplot(2,2,3);
n=ceil(num/sum); plot(o/pi,m)
n1=n+1; ylabel('gain in db');
if(rem(n,2)) xlabel('(c)normalised freq');
N=0; grid on;
n1=n;
n=n-1; %band stop filter
end
y=kaiser(n1,beta); b=fir1(n,wn,'stop',y);
[h,o]=freqz(b,1,256);
%low pass filter m=20*log10(abs(h));
subplot(2,2,4);
b=fir1(n,wp,y); plot(o/pi,m)
[h,o]= freqz(b,1,256); ylabel('gain in db');
m=20*log10(abs(h)); xlabel('(d)normalised freq');
subplot(2,2,1); grid on;
plot(o/pi,m)
ylabel('gain in db');
xlabel('(a)normalised freq');
grid on;
RESULT:

enter the passband ripple:0.05


enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
enter the beta value5.8

Department of ETE, BMSIT & M. 162 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 77

Department of ETE, BMSIT & M. 163 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

50 50
0 ga 0
in
in in
db db -50
-50

gai
n
-100 -100 0 0.5 1
0.5 1
-1500

(a)normalised freq (b)normalised freq

0 2
-20 0

db -40 in
db
in -2

gai -60 gai


n n
-80 -4
0.5 1
0.5 1 -60
-1000

(c)normalised freq (d)normalised freq

Fig 11.5 Frequency response of kaiser window

Questions

What is a Filter? What are its specifications?

Mention the types of filters.

Compare analog and digital filters.

Define a FIR filter.

What are the important properties of FIR filter?

What are the window techniques used?

Why the Kaiser window is important?

What is Gibb’s Phenomenon?

How do you calculate the length of Kaiser Window?

What are the functions used in designing a FIR filter in OCTAVE?

Department of ETE, BMSIT & M. 164 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 78

Department of ETE, BMSIT & M. 165 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 8

DESIGN AND IMPLEMENTATION OF IIR FILTER TO MEET GIVEN

SPECIFICATIONS

Aim: To design and implement an IIR filter for given 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

OCTAVE inbuilt command ‘butter’ as [b,a] =butter(N, wc , 'high');

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.

Department of ETE, BMSIT & M. 166 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Step 1: Since the frequencies are in radians divide by π to obtain normalized


frequencies to get wp=Wp/pi and ws=Ws/pi. If the frequencies are in Hz (note: in
this case the sampling frequency should be given), then obtain

Department of Telecommunication, BMSIT Page 79

Department of ETE, BMSIT & M. 167 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

normalized frequencies as wp=fp/(fs/2), ws=fstop/(fs/2), where fp, fstop and fs


are the passband, stop band and sampling frequencies in Hz

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');

IMPLEMENTATION OF THE IIR FILTER

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,

Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1

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.

Department of ETE, BMSIT & M. 168 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Department of Telecommunication, BMSIT Page 80

Department of ETE, BMSIT & M. 169 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

lp=input('Enter the passband attenuation:');

ls=input('Enter the stopband attenuation:');

wp=input('Enter the passband frequency:');

ws=input('Enter the stopband frequency:');

fs=input('Enter the sampling frequency:');

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));

Department of ETE, BMSIT & M. 170 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

an=angle(h);

figure(1);

subplot(2,1,1);

title('Butterworth Low Pass Filter');

plot(om/pi,m);

Department of Telecommunication, BMSIT Page 81

Department of ETE, BMSIT & M. 171 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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

Enter the sampling frequency:10000

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--
>

Fig 12.1 Magnitude and phase plot of Butterworth LFP

Department of ETE, BMSIT & M. 172 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Butterworth HPF clc;

clear all; close


all; format
long
lp=input('Enter the passband attenuation:');

Department of Telecommunication, BMSIT Page 82

Department of ETE, BMSIT & M. 173 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

ls=input('Enter the stopband attenuation:');

wp=input('Enter the passband frequency:');

ws=input('Enter the stopband frequency:');

fs=input('Enter the sampling 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);

title('Butterworth Low Pass Filter');

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

Department of ETE, BMSIT & M. 174 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Enter the stopband attenuation: 50


Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000

Department of Telecommunication, BMSIT Page 83

Department of ETE, BMSIT & M. 175 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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-->

Fig 12.2 Magnitude and phase plot of Butterworth HFP

Butter worth BPF clc;

close all; clear


all; format
long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;

w2=2*ws/fs;

[n]=buttord(w1,w2,rp,rs);

wn=[w1,w2];

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

w=0:0.01:pi;

Department of ETE, BMSIT & M. 176 |


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-->');

Department of Telecommunication, BMSIT Page 84

Department of ETE, BMSIT & M. 177 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

xlabel('(a) normalised frequency-->');

subplot(2,1,2);plot(om/pi,an);

xlabel('(b) normalised frequency-->');

ylabel('phase in radians-->');

RESULT:
enter the passband ripple 0.3
enter the stopband ripple 40

enter the passband frequency 1500

enter the stopband frequency 2000

enter the sampling frequency 9000

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

(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-->

Fig 12.3 Magnitude and phase plot of Butterworth BPF

Department of ETE, BMSIT & M. 178 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 85

Department of ETE, BMSIT & M. 179 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Butter worth BSF clc;

clear all;
format long

rp=input('enter the passband ripple');


rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;

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

Department of ETE, BMSIT & M. 180 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

enter the stopband ripple 46


enter the passband frequency 1100
enter the stopband frequency 2200

Department of Telecommunication, BMSIT Page 86

Department of ETE, BMSIT & M. 181 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

enter the sampling frequency 6000


100
d 0
b
-- -100
>
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
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-->

Fig 12.4 Magnitude and phase plot of Butterworth BSF

CHEBYSHEV FILTERS

Chebyshev LPF clc;

close all; clear


all; format
long

rp=input('enter the passband ripple');


rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;

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-->');

Department of Telecommunication, BMSIT Page 87

Department of ETE, BMSIT & M. 183 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

xlabel('(a) normalised frequency-->');

subplot(2,1,2);plot(om/pi,an);

xlabel('(b) normalised frequency-->');

ylabel('phase in radians-->');

RESULT:
enter the passband ripple 0.2
enter the stopband ripple 45

enter the passband frequency 1300

enter the stopband frequency 1500

enter the sampling frequency 10000

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

(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-->

Fig 12.5 Magnitude and phase plot of Chebyshev LPF

Chebyshev HPF clc;

close all; clear


all; format
long

Department of ETE, BMSIT & M. 184 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

rp=input('enter the passband ripple');


rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');

Department of Telecommunication, BMSIT Page 88

Department of ETE, BMSIT & M. 185 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

fs=input('enter the sampling 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-->');

xlabel('(a) normalised frequency-->');

subplot(2,1,2);plot(om/pi,an);

xlabel('(b) normalised frequency-->');

ylabel('phase in radians-->');

RESULT:

enter the passband ripple0.3

enter the stopband ripple60

enter the passband frequency1500

enter the stopband frequency2000

enter the sampling frequency9000


0
d -100
b-
-> -200
in
g -300
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

Department of ETE, BMSIT & M. 186 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

--> 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-->

Fig 12.6 Magnitude and phase plot of Chebyshev HPF

Department of Telecommunication, BMSIT Page 89

Department of ETE, BMSIT & M. 187 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Chebyshev BPF clc;

clear all; close


all; format
long

lp=input('Enter the passband attenuation:');


ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;

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);

title('Butterworth Low Pass Filter');


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-
->');

Department of ETE, BMSIT & M. 188 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

grid on

Department of Telecommunication, BMSIT Page 90

Department of ETE, BMSIT & M. 189 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result:

Enter the passband attenuation:0.4

Enter the stopband attenuation:35

Enter the passband frequency:2000

Enter the stopband frequency:2500

Enter the sampling frequency:10000

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-->

Fig 12.7 Magnitude and phase plot of Chebyshev BPF

Chebyshev BSF clc;

clear all; close


all; format
long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');

Department of ETE, BMSIT & M. 190 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

fs=input('Enter the sampling frequency:');


w1=2*wp/fs;

w2=2*ws/fs;

[n]=cheb1ord(w1,w2,lp,ls);

Department of Telecommunication, BMSIT Page 91

Department of ETE, BMSIT & M. 191 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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);

title('Chebyshev bandstop Filter');

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.4

Enter the stopband attenuation:46

Enter the passband frequency:1100

Enter the stopband frequency:2200

Enter the sampling frequency:6000

wn = 0.36666666666667 0.73333333333333

Department of ETE, BMSIT & M. 192 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 92

Department of ETE, BMSIT & M. 193 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

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-->

Fig 12.8 Magnitude and phase plot of Chebyshev BSF

Viva Questions:

Define an IIR filter.

Compare Impulse invariance and bilinear transformations.

How do you convert analog filter prototype to a digital filter?

What are the functions used in OCTAVE for designing a digital


Butterworth and Chebyshev low pass filter using BLT?

Draw typical responses of Chebyshev filter for order odd & even.

Compare FIR & IIR filters.

Department of ETE, BMSIT & M. 194 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 93

Department of ETE, BMSIT & M. 195 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

PART II
EXPERIMENTS USING DSP
PROCESSOR

Department of ETE, BMSIT & M. 196 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 94

Department of ETE, BMSIT & M. 197 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

INTRODUCTION to CCS

Procedure

Double click on CCS icon

🡪
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 \ c6000 \ cgtools \ lib \ rts6700.lib

<Select type of file as: library>

c: \ ti \ tutorial \ dsk6713 \ hello1 \ hello.cmd

Select type of file as: Linker Command file>

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)

Department of ETE, BMSIT & M. 198 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

🡪
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)

Department of Telecommunication, BMSIT Page 95

Department of ETE, BMSIT & M. 199 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 9

LINEAR CONVOLUTION OF TWO SEQUENCES

Aim: To perform linear convolution for the given sequences


+∞ +∞
The linear convolution sum is y[n] = x[n] * h[n] = ∑ x[k ]h[n − k ] = ∑ x[n − k ]h[k ]
k =−∞ k =−∞

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 outer for loop

} //end of main

Result

6.000000 12.000000 20.000000 18.000000 14.000000 8.000000

Department of ETE, BMSIT & M. 200 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 96

Department of ETE, BMSIT & M. 201 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 10

CIRCULAR CONVOLUTION OF TWO GIVEN SEQUENCES

Aim: To perform circular convolution of two given sequences


+∞ N

The circular convolution sum is y[n] = x[n](N )h[n] = ∑h[k ]x[ n − k ]


k =−∞

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

Department of ETE, BMSIT & M. 202 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Result

41.000000 51.000000 51.000000 46.000000 36.000000

Department of Telecommunication, BMSIT Page 97

Department of ETE, BMSIT & M. 203 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 11

COMPUTATION OF N- POINT DFT OF A GIVEN SEQUENCE

Aim: To compute the N (=4/8/16) point DFT of the given sequence

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

Where N is chosen such that N ≥ L , where L=length of x[n]. To implement using C


− j 2πkn
⎛⎛ 2πkn
2πkn ⎞
N
j sin⎜ ⎟ and
⎞ allot memory ⎝ N ⎠
program we use the expression e = cos⎜ ⎟ −

⎝ N ⎠

space for real and imaginary parts of the


DFT X(k)

C Program

#include <stdio.h>

#include <math.h>

main()

float y[16]; //for 8 point DFT to store real & imaginary


float x[4]={1,3,2,5}; //input only real sequence float w;

int n,k,k1,N=8,xlen=4;

for(k=0;k<2*N;k=k+2)

y[k]=0; y[k+1]=0; //initialize real & imag parts

k1=k/2; //actual k index

Department of ETE, BMSIT & M. 204 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

for(n=0;n<xlen;n++)

w= -2*3.14*k1*n/N; //careful about minus sign

y[k]=y[k]+x[n]*cos(w);

y[k+1]=y[k+1]+x[n]*sin(w);

Department of Telecommunication, BMSIT Page 98

Department of ETE, BMSIT & M. 205 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

printf("%f+j%f \n",y[k],y[k+1]);

}//end of main

Result: (on std out)


11.000000+j0.000000 -0.407551+j-7.660230 -
1.009554+j1.996801
2.424618+j-3.646700 -4.999950+j-0.022297 2.396780+j3.673694
-0.971311+j-2.009436 -0.460726+j7.633037

Department of ETE, BMSIT & M. 206 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 99

Department of ETE, BMSIT & M. 207 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 12

IMPULSE RESPONSE OF THE GIVEN FIRST ORDER / SECOND ORDER

SYSTEM

Aim: To find the Impulse response of the given first order / second order system

Theory:

A linear constant coefficient difference equation representing a second order system is


given by y[n] + a1 y[n −1] + a2 y[n − 2] = b0 x[n] + b1 x[n −1] + b2 x[n − 2];

For a first order system the coefficients a2 and b2 are zero.

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.

(refer expt 2 &7of part A - h=impz(b,a,N); %impulse response verification in

OCTAVE)

To implement in C the following algorithm is used.

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)

Generate the impulse input of size n (1st element=1, rest all=0)

Compute the output as

y[n] = −a1 y[n −1] − a2 y[n − 2] + b0 x[n] + b1 x[n −1] + b2 x[n − 2];

4. Repeat step 3 for n=0 through N (say 50,100, etc)

Department of ETE, BMSIT & M. 208 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 100

Department of ETE, BMSIT & M. 209 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Program #include
<stdio.h> float
x[60],y[60]; main()
{

float a1,a2,b0,b1,b2; int


i,j,N=20;

a1= -1.1430; a2= 0.4128;


b0=0.0675;b1=0.1349;b2=0.0675;
//generate impulse input x[0]=1;

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

Department of ETE, BMSIT & M. 210 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 101

Department of ETE, BMSIT & M. 211 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

EXPERIMENT NO. 13

REALIZATION OF AN FIR FILTER ( ANY TYPE ) TO MEET GIVEN

SPECIFICATIONS

Aim: Realization of an FIR filter ( any type ) to meet given specifications. The input

can be a signal from function generator / speech signal .

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

Before compiling, Copy header files “dsk6713.h” and “dsk6713_aic23.h” from


C:\CCStudio_v3.1\c6000\dsk6713\include and paste it in the current project folder.

C Program

#include "FIRcfg.h"

#include "dsk6713.h"

#include "dsk6713_aic23.h"

float filter_Coeff[] ={0.000000,-0.001591,-0.002423,0.000000,0.005728, 0.011139,


0.010502,-0.000000, -0.018003,-0.033416,-0.031505,0.000000,0.063010, 0.144802,

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 = { \

0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */ \

0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume

*/\

0x00d8, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */

0x00d8, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume


Department of ETE, BMSIT & M. 212 |
Page
EMBEDDED CONTROLLER LAB 18ECL46

*/ \
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 */ \

Department of Telecommunication, BMSIT Page 102

Department of ETE, BMSIT & M. 213 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

0x0081, /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */ \


0x0001 /* 9 DSK6713_AIC23_DIGACT Digital interface activation */ \

};

/* main() - Main code routine, initializes BSL and generates tone */


void main()

{ DSK6713_AIC23_CodecHandle hCodec;
Uint32 l_input, r_input,l_output, r_output;

/* Initialize the board support library, must be called first */


DSK6713_init();

hCodec = DSK6713_AIC23_openCodec(0, &config); /* Start the codec */


DSK6713_AIC23_setFreq(hCodec, 1);

while(1)

{ /* Read a sample to the left channel */

while (!DSK6713_AIC23_read(hCodec, &l_input)); /*


Read a sample to the right channel */

while (!DSK6713_AIC23_read(hCodec, &r_input));

l_output=(Int16)FIR_FILTER(&filter_Coeff

,l_input);

r_output=l_output;

while (!DSK6713_AIC23_write(hCodec, l_output)); /* Send o/p to the left channel */


while (!DSK6713_AIC23_write(hCodec, r_output)); /* Send o/p to the right channel */

DSK6713_AIC23_closeCodec(hCodec); /* Close the codec */

signed int FIR_FILTER(float * h, signed int x)

Department of ETE, BMSIT & M. 214 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

{ int i=0; signed long output=0;


in_buffer[0] = x; /* new input at buffer[0] */
for(i=30;i>0;i--)
in_buffer[i] = in_buffer[i-1]; /* shuffle the buffer */
for(i=0;i<32;i++)

Department of Telecommunication, BMSIT Page 103

Department of ETE, BMSIT & M. 215 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

output = output + h[i] * in_buffer[i];

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)

Department of ETE, BMSIT & M. 216 |


Page
EMBEDDED CONTROLLER LAB 18ECL46

Department of Telecommunication, BMSIT Page 104

Department of ETE, BMSIT & M. 217 |


Page

You might also like