..

...

...

## Monday 27 January 2014

### DIGITIZING AN ANALOG FILTER BY BILINEAR TRANSFORMATION METHOD (BLT) - MATLAB CODE - EXAMPLE

DIGITIZING AN ANALOG FILTER TO DIGITAL FILTER USING BILINEAR TRANSFORMATION METHOD

Here is a simple matlab code to digitize an anlog filter to a digial filter using bilinear transformation method, which is most commonly used technique.

The tranformation is achieved by the use of 'bilinear' function available in MATLAB toolset. It gets the analog filter co-effs & returns the digital filter co-eff.

PROGRAM:
clc;
clear all;
b=input('enter the analog numerator:');
a=input('enter the analog denominator:');
fs=1;
[bz,az]=bilinear(b,a,fs);
disp('num coeff');
disp(bz);
disp('deno coeff');
disp(az);

OUTPUT:
enter the analog numerator:8
enter the analog denominator:[6 4]
num coeff
0.5000 0.5000
deno coeff
1.0000 ‐0.5000

## Sunday 26 January 2014

### I2C header for AVR microcontrollers

This header contains function description for I2C module in AVR microcontrollers.
The Two-wire Serial Interface (TWI) is ideally suited for typical microcontroller applications. The TWI protocol allows the systems designer to interconnect up to 128 different devices using only two bi-directional bus lines, one for clock (SCL) and one for data (SDA). The only external hardware needed to implement the bus is a single pull-up resistor for each of the TWI bus lines. All devices connected to the bus have individual addresses, and mechanisms for resolving bus contention are inherent in the TWI protocol.

Features:
• Simple Yet Powerful and Flexible Communication Interface, Only Two Bus Lines Needed
• Both Master and Slave Operation Supported
• Device Can Operate as Transmitter or Receiver
• Multi-master Arbitration Support
• Up to 400 kHz Data Transfer Speed
• Slew-rate Limited Output Drivers
• Noise Suppression Circuitry Rejects Spikes on Bus Lines
• Fully Programmable Slave Address with General Call Support
• Address Recognition causes Wake-up when AVR is in Sleep Mode
Registers:
Clock frequency is set by TWBR register

TWI Bit Rate Register – TWBR
TWI Data Register      – TWDR
TWI (Slave) Address Register – TWAR
TWI Control Register – TWCR
TWI Status Register  – TWSR

THE FUNCTIONS USED IN I2C.H ARE...
void I2CInit();
To initialse I2C module with defined clock freq, mode of operation.

void I2CStart();
Sends a start signal on I2C bus

void I2CStop();
Sends a stop signal on I2C bus

unsigned char I2CWriteSLA(unsigned char sla);

unsigned char I2CWriteByte(unsigned char dat);
sends a data byte on I2C bus

Reads a data byte from I2C bus.

void I2CInit()
{
TWBR=0x04;
TWSR|=((1<<TWPS1) | (1<<TWPS0));
SETBIT(TWCR,TWEN)
// SDA & SCL PINS ARE OVERRIDE BY TWI & CAN'T BE USED AS I/O WHEN TWI ENABLED
// so no DDR & PULL-UP settings required.
}

void I2CStart()
{
TWCR=(1<<TWINT)| (1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}

#define I2CStop()      TWCR=(1<<TWINT)| (1<<TWEN)|(1<<TWSTO)

unsigned char I2CWriteByte(uint8_t dat)
{
unsigned char STATUS;
TWDR=dat;
TWCR=(1<<TWEN) | (1<<TWINT);
while(!(TWCR & (1<<TWINT)));

STATUS=TW_STATUS;

if(STATUS==TW_MT_DATA_ACK || STATUS==TW_MT_DATA_NACK || STATUS==TW_MT_SLA_ACK || STATUS==TW_MR_SLA_ACK)
return TRUE;
else
return FALSE;
}

{
unsigned char STATUS;
TWCR&=(~(1<<TWEA));
CLEARBIT(TWCR,TWINT)
while(!(TWCR & (1<<TWINT)));
STATUS=TW_STATUS;
if(STATUS==TW_MR_DATA_ACK || STATUS==TW_MR_DATA_NACK)
{
*data=TWDR;
return TRUE;
}
else
return FALSE;
}

## Friday 3 January 2014

### MATLAB SAMPLING THEOREM EXAMPLE

Here is a matlab code to demonstrate the sampling theorem.

CODE:

fig_size=[232 84 774 624];

Ts1=0.05;

Ts2=0.1;

Ts3=0.2;

ws1=2*pi/Ts1;

ws2=2*pi/Ts2;

ws3=2*pi/Ts3;

w1=7;

w2=23;

t=[0:0.005:2];

x=cos(w1*t)+cos(w2*t);

disp(‘X’);

disp(x);

subplot(2,3,1),plot(t,x),grid,xlabel(‘time(s)’),ylabel(‘amplitude’),….

title(‘continuous‐time signal; x(t)=cos(7t)+cos(23t)’),….

set(gcf,’position’,fig_size)

t1=[0:Ts1:2];

xs1=cos(w1*t1)+cos(w2*t1);

disp(‘xs1′);

disp(xs1);

subplot(2,3,2);

stem(t1,xs1);

grid,hold on,plot(t,x,’r'),hold off,….

xlabel(‘time(s)’),ylabel(‘amplitude’),….

title(‘sampled version of x(t) with Ts=0.05s’),….

set(gcf,’position’,fig_size)

t2=[0:Ts2:2];

xs2=cos(w1*t2)+cos(w2*t2);

subplot(2,3,3);

stem(t2,xs2);

grid,hold on,plot(t,x,’r'),hold off,….

xlabel(‘time(s)’),ylabel(‘amplitude’),….

title(‘sampled version of x(t) with Ts=0.1s’),….

set(gcf,’position’,fig_size)

t3=[0:Ts3:2];

xs3=cos(w1*t3)+cos(w2*t3);

subplot(2,3,4);

stem(t3,xs3);

grid,hold on,plot(t,x,’r'),hold off,….

xlabel(‘time(s)’),ylabel(‘amplitude’),….

title(‘sampled version of x(t) with Ts=0.2s’),….

set(gcf,’position’,fig_size)

w2s3=w2‐ws3;

x1=cos(w1*t)+cos(w2s3*t);

subplot(2,3,5);

stem(t3,xs3);

grid,hold on,plot(t,x,’k:’,t,x1,’r:’),hold off,….

xlabel(‘time(s)’),ylabel(‘amplitude’),….

title(‘sampled version of x(t) & x1(t) with Ts=0.2s’),….

set(gcf,’position’,fig_size)

text(1.13,1.2,’x(t)’),text(0.1,1.6,’x1(t)’)

n=[‐1 0 1];

wx=[‐w2 ‐w1 w1 w2];

wx1=[];

wx2=[];

wx3=[];

for i=1:length(n)

wx1=[wx1 (wx+n(i)*ws1)];

wx2=[wx2 (wx+n(i)*ws2)];

wx3=[wx3 (wx+n(i)*ws3)];

end

wx1=sort(wx1);

wx2=sort(wx2);

wx3=sort(wx3);

clear i

disp(wx1)

OUTPUT: