Wednesday, October 7, 2009

Filtering an Input signal using Digital Filter

Hi,
This a program in MATLAB. This program s
hows you, how the digital filter filters a desired frequency from an input signal.
I had written two methods for doing this.One methods is using Convolution (filtering takes place in time domain)and the other is using the property of convolu
tion (filtering action takes place in frequency domain). In this program the input signal is selected such that it contains two frequencies and both are present all the time. Since this is a Linear time invariant signal we can do the filtering in frequency domain too.
The basic algorithm of this program is as follows:
1) sample the input signal
2) plot the signal, its sampled version, and its frequency components
3) finding the FFT
4) Plot it .
5) designing a low pass filter
6) displaying filter response


7) In Frequency domain method
a) multiply both responses
b) displaying the resultant response
c) finding inverse fft
d) displaying it;
In Time domain method
a) finding the impulse response of filter

b) convoluting it with input signal
c) displaying it.
8) END


DOWNLOAD
Program using Convolution

%% filtering of an input signal using convolution
% vipin.p.nair
% 28-sept-2009

%% observations
% 1) when we increse the signal duration the power
% content will also increase.

% 2) anplitude of power also increases with increase
% of N

% 3) we need to display only the the frequency up to FS/2

% bcz the values beyond it will be a mirror image of the
% first. ie we are displaying 0 to pi.
%% program


clear all
f1 = 2000;
f2 = 1000;

t=.002; % input signal duration in seconds.
Ts = 1/50000;
n = 0: Ts :10*t;
x = 5*sin(2*pi*f2*n) + 5*cos(2*pi*f1*n);
figure(1);
subplot(2,2,1), plot(n*1000, x);
title 'Input signal';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
subplot(2,2,2), plot(n*1000, x, '.');
title 'Input signal samples';
xlabel 'time (ms)'
ylabel 'voltage';
grid;
subplot(2,2,3), plot(n*1000,5*cos(2*pi*f1*n));
title 'Input signal frequency -1';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
subplot(2,2,4), plot(n*1000,5*sin(2*pi*f2*n));
title 'Input signal frequency -2';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
% frequency spectrum of input signal
N =1024*1;
Xk = fft(x,N);
Pxx = Xk .* conj(Xk)/N;
figure(2);
plot((0:N/2)/(N*Ts),(Pxx(1:N/2+1)));
title 'Power spectrum'
xlabel 'Frequency (Hz) upto Fs/2'
grid;

% filter
filter_order = 10;
Fs = 1/Ts;
fc = 1500 / ( Fs/2) ;
[b,a] = butter(filter_order, fc);
w = 0: 2*pi/(N-1) :pi;
[H,wn2] = freqz(b,a,w);

figure(3)
plot(w/pi*Fs/2,(abs(H)));

h = ifft(H,length(n));
out = cconv(abs(h),x);
ln=length(n);
figure(4);
plot(n*1000,out(1:ln));grid;
title 'Signal after filtering'

OUTPUT FIGURES








Program using filtering in frequency domain

%% filter
% vipin.p.nair

% sept- 28- 2009
% observations

% 1) when we increse the signal duration the power

% content will also increase.

% 2) anplitude of power also increases with increase
% of N
% 3) we need to display only the the frequency up to FS/2

% bcz the values beyond it will be a mirror image of the
% first. ie we are displaying 0 to pi.


clear all
f1 = 2000;
f2 = 500;

t=0.06; % input signal duration in seconds.
Ts = 1/10000;
n = 0: Ts :t;
x = 5*sin(2*pi*f2*n) + 5*cos(2*pi*f1*n);
figure(1);
subplot(2,2,1), plot(n*1000, x);
title 'Input signal';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
subplot(2,2,2), plot(n*1000, x, '.');
title 'Input signal samples';
xlabel 'time (ms)'
ylabel 'voltage';
grid;
subplot(2,2,3), plot(n*1000,5*cos(2*pi*f1*n));
title 'Input signal frequency -1';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
subplot(2,2,4), plot(n*1000,5*sin(2*pi*f2*n));
title 'Input signal frequency -2';
xlabel 'time (ms)'
ylabel 'voltage'
grid;
% frequency spectrum of input signal
N =1024*1;
Xk = fft(x,N);
Pxx = Xk .* conj(Xk)/N;
figure(2);
plot((0:N/2)/(N*Ts),(Pxx(1:N/2+1)));
title 'Power spectrum'
xlabel 'Frequency (Hz) upto Fs/2'
grid;

% filter
filter_order = 10;
Fs = 1/Ts;
fc = 1500 / ( Fs/2) ;
[b,a] = butter(filter_order, fc);

%'syms z
%v=tf (b,a);
w = 0: 2*pi/(N-1) :pi;
[h,wn] = freqz(b,a,w);
[h2,wn2] = freqz(b,a,0: 2*pi/(N-1): 2*pi);
figure(3)
plot(wn/pi*Fs/2,(abs(h)));
title 'Filter response'
xlabel 'Frequency (Hz) upto Fs/2'
figure(4)
Yk = h.*Xk(1:N/2);
Yk2 = h2.*Xk;
plot(wn/pi*Fs/2,(abs(Yk)));
xlabel 'Frequency (Hz) upto Fs/2'
title ' frequency spectrum after filtering'
grid;
%figure(5)
%plot(wn/pi*Fs/2,(abs(Xk(1:N/2))));
%grid;
figure(6)
plot(abs(ifft(Yk2,N)));

OUTPUT:






Output signal waveform

Wednesday, May 27, 2009

Tic - Tac - Toe in C



This is a famous game. Here it is written in Turbo c. In this game one player is the user and the other is the computer.

The important feature in this is that the user can use the mouse to select the cells. I wrote this program mainly to introduce how to use mouse in your c programs.

1) For using mouse you must declare the header file ‘bios.h’.


2) Another thing is a union should be declared as follows:

union REGS regs;


3) The mouse can be initialized as follows

regs.x.ax=0;

int86(0x33,&regs,&regs);


4) To show the mouse

regs.x.ax=1;

int86(0x33,&regs,&regs);


5) To hide the mouse

regs.x.ax=2;

int86(0x33,&regs,&regs);


6) To read the co ordinate (position)

regs.x.ax=3;

int86(0x33,&regs,&regs);

left = regs.x.bx&1; //to get left click

right = regs.x.bx&2;// right click

x = regs.x.cx; // x –coordinate

y= regs.x.dx; // y –coordinate

Here left, right, x, y is in integer data type.

These are the main instructions use full for writing mouse using programs. If the graphics is initialized in your program then the mouse pointer will have an arrow head. Otherwise it will be a rectangular block.

The following strategy can be used while writing these programs.

Coordinate ()

{

Readmouse();

If(x>50 && x<150>25 &&y<50)

Click(1);

...

}

Click(int z)

{

Readmouse();

While(left)

{

Readmouse();

Switch(z)

{

}

}

}

Download and Enjoy the game.

Tic-Tac- Toe :- Program

/* Tic Tac Toe - Game
* Vipin p nair *
* 25-5-09 *
* College of Engineering Kallooppara *
* vipinpn@gmail.com *
* Kerala *
*/


#include "graphics.h"
#include "bios.h"
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#define CELL1 (x_cord>100 && x_cord<150>100 && y_cord<150)
#define CELL2 (x_cord>150 && x_cord<200>100 && y_cord<150)
#define CELL3 (x_cord>200 && x_cord<250>100 && y_cord<150)

#define CELL4 (x_cord>100 && x_cord<150>150 && y_cord<200)
#define CELL5 (x_cord>150 && x_cord<200>150 && y_cord<200)
#define CELL6 (x_cord>200 && x_cord<250>150 && y_cord<200)

#define CELL7 (x_cord>100 && x_cord<150>200 && y_cord<250)
#define CELL8 (x_cord>150 && x_cord<200>200 && y_cord<250)
#define CELL9 (x_cord>200 && x_cord<250>200 && y_cord<250)

#define EXIT (x_cord>230 && x_cord<290>260 && y_cord<290)
#define NEW (x_cord>160 && x_cord<220>260 && y_cord<290)
#define ABOUT (x_cord>60 && x_cord<130>260 && y_cord<290)

void PlyrComp();
union REGS regs;

int left_click,
x_cord,
y_cord,
select[9],
selectcmp[9];

char pass,pass1,pass2,win=1;
// mouse functions
void InitMouse()
{
// initialize
regs.x.ax=0;
int86(0x33,&regs,&regs);
}
void showmouse()
{
// show mouse
regs.x.ax=1;
int86(0x33,&regs,&regs);

}
void hidemouse()
{
regs.x.ax=2;
int86(0x33,&regs,&regs);
}

void readmouse()
{
regs.x.ax = 3;
int86(0x33,&regs,&regs);
left_click = regs.x.bx & 1;
// right_click = regs.x.bx & 2
x_cord = regs.x.cx;
y_cord = regs.x.dx;
}
// graphics functions
void InitGraphics()
{
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
}



void MainScreen()
{
setbkcolor(CYAN);
setcolor(WHITE);
rectangle(100,100,250,250);
setfillstyle(1,BLUE);
floodfill(150,150,WHITE);
line(100,150,250,150);
line(100,200,250,200);
line(150,100,150,250);
line(200,100,200,250);
rectangle(50,50,300,300);
rectangle(230,260,290,290);
rectangle(160,260,220,290);
rectangle(50,300,300,330);
rectangle(60,260,130,290);
setfillstyle(9,LIGHTBLUE);
floodfill(240,270,WHITE);
floodfill(170,270,WHITE);
floodfill(70,270,WHITE);
setfillstyle(9,LIGHTMAGENTA);
floodfill(350,350,WHITE);
settextstyle(1,0,2);
setcolor(RED);
outtextxy(115,60,"Tic-Tac-Toe");
outtextxy(240,260,"Exit");
outtextxy(170,260,"New");
outtextxy(66,260,"About");
setcolor(WHITE);
select[0]=0;select[1]=0;select[2]=0;
select[3]=0;select[4]=0;select[5]=0;
select[6]=0;select[7]=0;select[8]=0;
selectcmp[0]=0;selectcmp[1]=0;selectcmp[2]=0;
selectcmp[3]=0;selectcmp[4]=0;selectcmp[5]=0;
selectcmp[6]=0;selectcmp[7]=0;selectcmp[8]=0;
pass=0;
pass2=0;
PlyrComp();
}
//*************************************************************
void Comptr_click(int a)
{
switch(a)
{
case 0:
case 1: outtextxy(120,115,"O");selectcmp[0]=1;
break;
case 2: outtextxy(170,115,"O");selectcmp[1]=1;
break;
case 3: outtextxy(220,115,"O");selectcmp[2]=1;
break;
case 4: outtextxy(120,165,"O");selectcmp[3]=1;
break;
case 5: outtextxy(170,165,"O");selectcmp[4]=1;
break;
case 6: outtextxy(220,165,"O");selectcmp[5]=1;
break;
case 7: outtextxy(120,215,"O");selectcmp[6]=1;
break;
case 8: outtextxy(170,215,"O");selectcmp[7]=1;
break;
case 9: outtextxy(220,215,"O");selectcmp[8]=1;
break;
}
pass1=1;
}

//**************************************************************
void PlyrComp()
{
int x=0;
// getch();

if(!pass)
{
randomize();
Comptr_click(rand()%10);
pass=1;
return;
}
// final step for computer win
// horiz check
pass1=0;
if ((selectcmp[0]+selectcmp[1]+selectcmp[2])==2)
{
if (((selectcmp[0]+selectcmp[1])==2) && !select[2])
Comptr_click(3);
if (((selectcmp[0]+selectcmp[2])==2) && !select[1])
Comptr_click(2);
if (((selectcmp[1]+selectcmp[2])==2) && !select[0])
Comptr_click(1);
}
if(pass1)return;
pass1=0;
if ((selectcmp[3]+selectcmp[4]+selectcmp[5])==2)
{
if (((selectcmp[3]+selectcmp[4])==2) && !select[5])
Comptr_click(6);
if (((selectcmp[3]+selectcmp[5])==2) && !select[4])
Comptr_click(5);
if (((selectcmp[4]+selectcmp[5])==2) && !select[3])
Comptr_click(4);
}
if(pass1)return;
pass1=0;
if ((selectcmp[6]+selectcmp[7]+selectcmp[8])==2)
{
if (((selectcmp[6]+selectcmp[7])==2) && !select[8])
Comptr_click(9);
if (((selectcmp[6]+selectcmp[8])==2) && !select[7])
Comptr_click(8);
if (((selectcmp[7]+selectcmp[8])==2) && !select[6])
Comptr_click(7);
}
if(pass1)return;

// vertical check
pass1=0;
if ((selectcmp[0]+selectcmp[3]+selectcmp[6])==2)
{
if (((selectcmp[0]+selectcmp[3])==2) && !select[6])
Comptr_click(7);
if (((selectcmp[0]+selectcmp[6])==2) && !select[3])
Comptr_click(4);
if (((selectcmp[3]+selectcmp[6])==2) && !select[0])
Comptr_click(1);
}
if(pass1)return;
pass1=0;
if ((selectcmp[1]+selectcmp[4]+selectcmp[7])==2)
{
if (((selectcmp[1]+selectcmp[4])==2) && !select[7])
Comptr_click(8);
if (((selectcmp[1]+selectcmp[7])==2) && !select[4])
Comptr_click(5);
if (((selectcmp[4]+selectcmp[7])==2) && !select[1])
Comptr_click(2);
}
if(pass1)return;
pass1=0;
if ((selectcmp[2]+selectcmp[5]+selectcmp[8])==2)
{
if (((selectcmp[2]+selectcmp[5])==2) && !select[8])
Comptr_click(9);
if (((selectcmp[2]+selectcmp[8])==2) && !select[5])
Comptr_click(6);
if (((selectcmp[5]+selectcmp[8])==2) && !select[2])
Comptr_click(3);
}
if(pass1)return;
pass1=0;
if ((selectcmp[0]+selectcmp[4]+selectcmp[8])==2)
{
if (((selectcmp[0]+selectcmp[4])==2) && !select[8])
Comptr_click(9);
if (((selectcmp[0]+selectcmp[8])==2) && !select[4])
Comptr_click(5);
if (((selectcmp[8]+selectcmp[4])==2) && !select[0])
Comptr_click(1);
}
if(pass1)return;
pass1=0;
if ((selectcmp[2]+selectcmp[4]+selectcmp[6])==2)
{
if (((selectcmp[2]+selectcmp[4])==2) && !select[6])
Comptr_click(7);
if (((selectcmp[2]+selectcmp[6])==2) && !select[4])
Comptr_click(5);
if (((selectcmp[6]+selectcmp[4])==2) && !select[2])
Comptr_click(3);
}
if(pass1)return;

// blocking plyr winning conditions
// horiz check
pass1=0;
if ((select[0]+select[1]+select[2])==2)
{
if (((select[0]+select[1])==2) && !selectcmp[2])
Comptr_click(2+1);
if (((select[0]+select[2])==2) && !selectcmp[1])
Comptr_click(1+1);
if (((select[1]+select[2])==2) && !selectcmp[0])
Comptr_click(0+1);
}
if(pass1)return;
pass1=0;
if ((select[3]+select[4]+select[5])==2)
{
if (((select[3]+select[4])==2) && !selectcmp[5])
Comptr_click(5+1);
if (((select[3]+select[5])==2) && !selectcmp[4])
Comptr_click(4+1);
if (((select[4]+select[5])==2) && !selectcmp[3])
Comptr_click(3+1);
}
if(pass1)return;
pass1=0;
if ((select[6]+select[7]+select[8])==2)
{
if (((select[6]+select[7])==2) && !selectcmp[8])
Comptr_click(8+1);
if (((select[6]+select[8])==2) && !selectcmp[7])
Comptr_click(7+1);
if (((select[7]+select[8])==2) && !selectcmp[6])
Comptr_click(6+1);
}
if(pass1)return;

// vertical check
pass1=0;
if ((select[0]+select[3]+select[6])==2)
{
if (((select[0]+select[3])==2) && !selectcmp[6])
Comptr_click(6+1);
if (((select[0]+select[6])==2) && !selectcmp[3])
Comptr_click(3+1);
if (((select[3]+select[6])==2) && !selectcmp[0])
Comptr_click(0+1);
}
if(pass1)return;
pass1=0;
if ((select[1]+select[4]+select[7])==2)
{
if (((select[1]+select[4])==2) && !selectcmp[7])
Comptr_click(7+1);
if (((select[1]+select[7])==2) && !selectcmp[4])
Comptr_click(4+1);
if (((select[4]+select[7])==2) && !selectcmp[1])
Comptr_click(1+1);
}
if(pass1)return;
pass1=0;
if ((select[2]+select[5]+select[8])==2)
{
if (((select[2]+select[5])==2) && !selectcmp[8])
Comptr_click(8+1);
if (((select[2]+select[8])==2) && !selectcmp[5])
Comptr_click(5+1);
if (((select[5]+select[8])==2) && !selectcmp[2])
Comptr_click(2+1);
}
if(pass1)return;
pass1=0;
if ((select[0]+select[4]+select[8])==2)
{
if (((select[0]+select[4])==2) && !selectcmp[8])
Comptr_click(8+1);
if (((select[0]+select[8])==2) && !selectcmp[4])
Comptr_click(4+1);
if (((select[8]+select[4])==2) && !selectcmp[0])
Comptr_click(0+1);
}
if(pass1)return;
pass1=0;
if ((select[2]+select[4]+select[6])==2)
{
if (((select[2]+select[4])==2) && !selectcmp[6])
Comptr_click(6+1);
if (((select[2]+select[6])==2) && !selectcmp[4])
Comptr_click(4+1);
if (((select[6]+select[4])==2) && !selectcmp[2])
Comptr_click(2+1);
}
if(pass1)return;
pass1=0;
while(pass2 && (checkwin()==3) && !pass1)
{
x=rand()%10;
if( (!select[x-1]) && (!selectcmp[x-1]) )
{
Comptr_click(x);
return;
}
}

}



//****************************************************************

int checkwin()
{
int i;
// return 1 for player win , 2 for computer & 0 for draw
// horiz
if (select[0] && select[1] && select[2]) return 1;
if (select[3] && select[4] && select[5]) return 1;
if (select[6] && select[7] && select[8]) return 1;
// vert
if (select[0] && select[3] && select[6]) return 1;
if (select[1] && select[4] && select[7]) return 1;
if (select[2] && select[5] && select[8]) return 1;
// diag
if (select[0] && select[4] && select[8]) return 1;
if (select[6] && select[4] && select[2]) return 1;
// comp
// horiz
if (selectcmp[0] && selectcmp[1] && selectcmp[2]) return 2;
if (selectcmp[3] && selectcmp[4] && selectcmp[5]) return 2;
if (selectcmp[6] && selectcmp[7] && selectcmp[8]) return 2;
// vert
if (selectcmp[0] && selectcmp[3] && selectcmp[6]) return 2;
if (selectcmp[1] && selectcmp[4] && selectcmp[7]) return 2;
if (selectcmp[2] && selectcmp[5] && selectcmp[8]) return 2;
// diag
if (selectcmp[0] && selectcmp[4] && selectcmp[8]) return 2;
if (selectcmp[6] && selectcmp[4] && selectcmp[2]) return 2;
for (i=0;i<9;i++)
if(!(select[i]||selectcmp[i]))
return 3;
return 0;
}

//****************************************************************8
int check(int a)
{
if (a==9 || a==10) return 0;
if (select[a] || selectcmp[a])
return 1;
return 0;
}
//****************************************************************
result()
{
switch(checkwin())
{
case 1: outtextxy(100,300,"Player Wins");return 0;
case 2: outtextxy(100,300,"Computer Wins"); return 0;
case 0: outtextxy(100,300,"Game Draw"); return 0;
}
}
//******************************************************************
void click(int z)
{
readmouse();
while(left_click)
{
left_click=0;
// readmouse();
if(check(z-1))return;
if (z<10)
pass2=z;
else pass2=0;
hidemouse();
switch(z)
{
case 1: outtextxy(120,115,"X");select[0]=1;
break;
case 2: outtextxy(170,115,"X");select[1]=1;
break;
case 3: outtextxy(220,115,"X");select[2]=1;
break;
case 4: outtextxy(120,165,"X");select[3]=1;
break;
case 5: outtextxy(170,165,"X");select[4]=1;
break;
case 6: outtextxy(220,165,"X");select[5]=1;
break;
case 7: outtextxy(120,215,"X");select[6]=1;
break;
case 8: outtextxy(170,215,"X");select[7]=1;
break;
case 9: outtextxy(220,215,"X");select[8]=1;
break;
case 10: cleardevice();
exit(0);
case 11: cleardevice();
MainScreen();
pass=0;
break;
case 12: outtextxy(70,330,"Vipin P Nair");
outtextxy(70,360,"College Of Engineering Kallooppara") ;
outtextxy(70,390,"Kerala, India");
break;
}
// delay(450);
showmouse();
result();
PlyrComp();
win=result();
}
}

//****************************************************************
void cordinate()
{
readmouse();
if (win)
{
if(CELL1)click(1);
if(CELL2)click(2);
if(CELL3)click(3);

if(CELL4)click(4);
if(CELL5)click(5);
if(CELL6)click(6);

if(CELL7)click(7);
if(CELL8)click(8);
if(CELL9)click(9);
}
if(EXIT) click(10);
if(NEW) click(11);
if(ABOUT)click(12);
}


//**************************************************************
void main()
{
InitGraphics();
MainScreen();
InitMouse();
showmouse();
while(1)
cordinate();
}


Saturday, May 2, 2009

Image Hider 2.0 -MATLAB Program

Hide an image file inside another image file :-high quality version


This is a MATLAB based program. Image hider 2.0 is the second part of Image Hider 1.0. Image hider 2.0 is a high quality version. This software helps you to hide one image (photo) in to another image (photo). Suppose image2 is the image to be hid on image1 and the encoded image is image3. Then all the pixel details present in the image2 were encoded in to the image1. So the quality of the image2 will not be affected. Only two bits in every pixels of the image1 is used for encoding. And so the encoded image do not show any patches of the image2. So others will never suspect that image3 as encoded or it may contain any secrets.


The bits in every pixel of the image2 is split into four and placed in four different locations in image1. So it provides some sort of encryption too. So it will be hard for others to decode the hidden image.It supports formats like JPG, BMP etc



    Example: Encoding section

    Image1.jpg :- image on which the encoding to be done

    Image2: Image to be hid

    Image3: the encoded image

    Example: Decoding section - using the above encoded image

    Decoded image1: extracted image from image3.bmp

    Decoded image2: hidden image in image3.bmp



MATLAB Program:

% Program for hiding an image inside the other

% high quality version

%By vipin.p.nair

% College of Engineering Kallooppara

% Kerala, India

% vipinpn@yahoo.com

%01-may-2009

%*****************************************************

% ---Only two bit in pixel of the image 1 is affected

% ---image2 is split & store in two diagonally opposite quadrants

% Algorithm

% the fist image is resize to double its original size

% logically the image is divided to four partitions

% the bits of the image to be hid is stored in the first image as

% shown below

% |-------------|

% |d7,d6 |d3,d2 |

% |-------------|

% |d1,d0 |d5,d4 |

% |____________|

% ****************program********************

clc; % clear the command window

clear; % clear the workspace

disp(' ');

disp(' ***** IMAGE HIDER 2.0 *****');

disp('___Program for hidimg one image inside the other image___');

disp(' ');

disp('_________________________________________________________');

task = input('---Encode :- 1 \n---Decode :- 2\n Enter your task:');

% select task

if isempty(task)

task=1;

end

if task == 1

% reads two image files

x = imread(input(' Welcome to Encoder\n Enter the first image file name: ','s'));

y = imread(input(' Enter the second image file name: ','s'));

% check compatibility

sx = size(x);

sy = size(y);

%if (sx(1) ~= sy(1))|(sx(2)~=sy(2))

x=imresize(x,[2*sy(1),2*sy(2)]);

% end

%sy=2*sy;

%

% clearing Ist files last two lsb bits & moving IInd files msb bits to lsb bits

x1 = bitand(x,uint8(252));

y1 = bitshift(y,-4);

y1_= bitand(y1,12);

y1_= bitshift(y1_,-2); % y1_ has D6 & D7

y1 = bitand(y1,3); % Y1 HAS D4,D5

% clearing II image's msb bits

y_lsb1 = bitshift(bitand(y,12),-2);

y_lsb2 = bitand(y,3);

% inserting IInd to Ist file

z=x1;

for j=1:sy(2) % y variation

for i=1:sy(1) % x variation

for k=1:3

% IInd quadrent

z(i,j,k) = bitor(x1(i,j,k), y1_(i,j,k));

% IV th quadrent

z(i+sy(1),j+sy(2),k) = bitor(x1(i+sy(1),j+sy(2),k), y1(i,j,k));

% I st quadrent

z(i+sy(1),j,k) = bitor(x1(i+sy(1),j,k), y_lsb1(i,j,k));

% IIIrd quadrent

z(i,j+sy(2),k) = bitor(x1(i,j+sy(2),k), y_lsb2(i,j,k));

end

end

end

% display the first image

figure(1)

image(x);

xlabel(' Ist Image ');

% display IInd image

figure(2);

image(y);

xlabel(' IInd Image ');

% display encoded image

figure(3);

image(z);

xlabel(' Encoded Image ');

% saving file

sav=input('Do you want to save the file y/n [y] ','s');

if isempty(sav)

sav='y';

end

if sav == 'y'

name=input('Enter a name for the encoded image: ','s');

if isempty(sav)

name='encoded_temp';

end

name=[name,'.bmp']; % concatination

imwrite(z,name,'bmp');

end

else

% Decoding encoded image

clear;

z=imread(input(' Welcome to Decoder\n Enter the image file to be decoded:','s'));

sy = size(z)/2; % take the size of input file

% xo is fist file- obtained by clearing lsb bits, yo is IInd file right

% shifting z by 4 bits

xo=bitand(z,uint8(252));

xo=imresize(xo,[sy(1),sy(2)]); % reduce the resolution to half so

%that it becoms the original image's resolution

for j=1:sy(2) % y variation

for i=1:sy(1) % x variation

for k=1:3

zout1(i,j,k) = bitshift(bitand(z(i,j,k),uint8(3)),2);

zout2(i,j,k) = bitand(z(i+sy(1),j+sy(2),k), uint8(3));

zout3(i,j,k) = bitshift(bitand(z(i+sy(1),j,k),uint8(3)),2);

zout4(i,j,k) = bitand(z(i,j+sy(2),k),uint8(3));

end

end

end

zout = bitshift((zout1+zout2),4)+zout3+zout4;

yo = zout;

% display Ist & IInd image from encoded image

figure(4);

image(xo);

xlabel('Ist Decoded Image ');

figure(5);

image(yo);

xlabel('IInd Decoded Image');

% saving file

sav=input('Do you want to save the file y/n [y] ','s');

if isempty(sav)

sav='y';

end

if sav == 'y'

name1=input('Enter a name for the first image: ','s');

name2=input('Enter a name for the second image: ','s');

if isempty(name1)

name1 = 'Ist_temp';

end

if isempty(name2)

name2 = 'IInd_temp';

end

name1 = [name1,'.bmp'];

name2 = [name2,'.bmp'];

imwrite(xo,name1,'bmp');

imwrite(yo,name2,'bmp');

end

end