Monday, November 18, 2013

Write a program in MATLAB to generate a median of an image.

clc;
clear all;
close all;

A=imread('median.tif');
% Pad the matrix with zeros on all sides
modifyA=zeros(size(A)+2);
B=zeros(size(A));
% copy the original image matrix to the padded matrix
for x=1:size(A,1)
    for y=1:size(A,2)
        modifyA(x+1,y+1)=A(x,y);
    end
end
 
h = waitbar(0,'Calculating Median Operation please wait......');

% Let the window be an array store the 3-by-3 neighbour
% values in the array sort and find the middle element
     
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        window=zeros(9,1);
        inc=1;
        for x=1:3
            for y=1:3
                window(inc,1)=modifyA(i+x-1,j+y-1);
                inc=inc+1;
            end
        end
        med=sort(window);
        % Place the median element in the output matrix
        B(i,j)=med(5);
    end
    waitbar(i/size(modifyA,1));
end
% Convert the output matrix to 0-255 range image type
B=uint8(B);
figure('Position',[100  100 1200 550]);
subplot(1,2,1),imshow(A),
ylabel('Image with salt-papper noise','Color','r','FontSize', 20,'FontAngle', 'italic');
subplot(1,2,2),imshow(B),
ylabel('Image after median filtering','Color','r','FontSize', 20,'FontAngle', 'italic');
close(h);

Write a program in MATLAB to design a Mask to generate a mean image considering 8 neighborhood.

clc;
clear all;
close all;

A=imread('median.tif');
A = im2double(A);
G=A;
% Pad the matrix with zeros on all sides
modifyA=zeros(size(A)+2);
% copy the original image matrix to the padded matrix
for x=1:size(A,1)
    for y=1:size(A,2)
        modifyA(x+1,y+1)=A(x,y);
    end
end

h = waitbar(0,'Calculating Arithmetic Mean Operation please wait......');
m=3;n=3;
       
for i= 1:size(modifyA,1)-2
    for j=1:size(modifyA,2)-2
        s=0;
        kernel = ones(m, n) / 9; % mxn(8-neighbor) mean kernel
        for x = 1:m
            for y = 1:n
                s=s+modifyA(i+x-1,j+y-1)*kernel(x,y);
            end
        end
        G(i,j)=s;
    end
    waitbar(i/size(modifyA,1));
end
figure('Position',[100  100 1200 550]);
subplot(1,2,1),imshow(A);
ylabel('salt-papper noise Image','Color','r','FontSize', 20,'FontAngle', 'italic');
subplot(1,2,2),imshow(G);
ylabel('Arithmetic mean filter','Color','r','FontSize', 20,'FontAngle', 'italic');
close(h);

Tuesday, November 5, 2013

Implementation of Frequency Reuse Factor in Mobile Computing using C

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

#define SIDE 30
#define PI 3.14159
#define X 300
#define Y 220

void draw_hexagon(int,int);
void recur_draw(int,int,int);
void frequency(int,int);

char *name[]={"Blue","Green","Cyan","Red","Magenta","Brown","Lightgray"};
int count=1;

void main(){
int gd=DETECT,gm;
int k=0,f=0;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("Enter the value of Reuse Factor (k) ");
scanf("%d",&k);
printf("Enter the frequency range: ");
scanf("%d",&f);
draw_hexagon(X,Y);
recur_draw(X,Y,k);
frequency(f,k);
getch();
closegraph();
}
void draw_hexagon(int x,int y){
int x1,y1,x2,y2,i;
int degree=0;
x1=x+SIDE*cos((degree*PI)/180);
y1=y+SIDE*sin((degree*PI)/180);
setcolor(WHITE);
setlinestyle(0,0,3);
for(i=1;i<=6;i++){
degree=degree+60;
x2=x+SIDE*cos((degree*PI)/180);
y2=y+SIDE*sin((degree*PI)/180);
line(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
}
void recur_draw(int x,int y,int k){
int x1,y1,i;
int degree=30;
int COLOR=1;
count=count+1;
setfillstyle(SOLID_FILL,COLOR);
floodfill(x-1,y+1,WHITE);
if(COLOR==1 && count==2)
printf("1. %s\n",name[0]);
for(i=1;i<k;i++){
x1=x+1.732*SIDE*cos((degree*PI)/180);
y1=y+1.732*SIDE*sin((degree*PI)/180);
draw_hexagon(x1,y1);
if(COLOR==i && count==2)
printf("%d. %s\n",i+1,name[i]);
COLOR=COLOR+1;
setfillstyle(SOLID_FILL,COLOR);
floodfill(x1-1,y1+1,WHITE);
degree=degree+60;
}
}
void frequency(int f,int k){
int x1,y1,i;
int degree=3.2+(k-3)*1.75;
int D=1.732*sqrt(k)*SIDE;
int p=f/k;
for(i=1;i<p;i++){
x1=X+D*cos((degree*PI)/180);
y1=Y+D*sin((degree*PI)/180);
draw_hexagon(x1,y1);
recur_draw(x1,y1,k);
degree=degree+60;
}
}

Sunday, November 3, 2013

Implementation of Code Division Multiple Access in Mobile Computing using C

#include<stdio.h>

#define max_user 10
#define max_data_length 20
#define max_sig_length 20

int no_user=0,no_sig_bit=0,no_data_bit=0;
int sig_seq[max_user][max_sig_length];
int z[max_data_length][max_sig_length];
int data_stream[max_user][max_data_length];

void sender();
void receiver();

void main(){
    FILE *f = fopen("CDMA.txt", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }
    sender(f);
    receiver(f);
    fclose(f);
}
void sender(FILE *f){
    int i,j;
    int n=0;
    printf("===========================================================\n");
    printf("    Input Required Information                   \n");   
    printf("===========================================================\n");   
    fprintf(f,"===========================================================\n");
    fprintf(f,"            SENDER                    \n");   
    fprintf(f,"===========================================================\n");   
    printf("Enter number of user (<=%d)  ",max_user);       
    scanf("%d",&no_user);
    printf("Enter number of data bit (<=%d)  ",max_data_length);       
    scanf("%d",&no_data_bit);
    printf("Enter number of signature bit (<=%d)  ",max_sig_length);       
    scanf("%d",&no_sig_bit);
    printf("----------------------------------------------------\n");
    /* User */
    for(i=0;i<no_user;i++){
        printf("Enter data for User %d\n",i+1);       
        /* Data Stream */
        printf("Enter the Data Stream bit (+1 | -1)\n");       
        for(j=0;j<no_data_bit;j++){
            scanf("%d",&n);
            if(n==1 || n==-1)
                data_stream[i][j]=n;
            else{
                printf("Wrong data bit, Re-Enter\n");
                j--;
            }
        }       
        /* Signature Code */
        printf("Enter the Signature Code (+1 | -1)\n");       
        for(j=0;j<no_sig_bit;j++){
            scanf("%d",&n);
            if(n==1 || n==-1)
                sig_seq[i][j]=n;
            else{
                printf("Wrong Signature bit, Re-Enter\n");
                j--;
            }
        }
    }
    for(i=0;i<no_user;i++){
        fprintf(f,"\nSender %d\n",i+1);       
        fprintf(f,"---------------------\n");
        fprintf(f,"Data Stream:   ");
        for(j=0;j<no_data_bit;j++)
                fprintf(f,"%d ",data_stream[i][j]);
        fprintf(f,"\n");       
        fprintf(f,"Signature Sequence:   ");   
        for(j=0;j<no_sig_bit;j++)
                fprintf(f,"%d ",sig_seq[i][j]);
        fprintf(f,"\n");
    }
    fprintf(f,"_________________________________________________\n");
    /* Encoding */
    fprintf(f,"Encoded Data:\n");
    fprintf(f,"---------------------\n");
    for(i=0;i<no_data_bit;i++){
        fprintf(f,"|");
        for(j=0;j<no_sig_bit;j++){
            n=0;
            while(n<no_user){
                z[i][j]=z[i][j]+data_stream[n][i]*sig_seq[n][j];
                n++;
            }
            // Channel Output
            fprintf(f,"%d ",z[i][j]);
        }
        fprintf(f,"|");
    }
    fprintf(f,"\n");
}
void receiver(FILE *f){
    int i,j;
    int n=0,sum=0;
    fprintf(f,"===========================================================\n");
    fprintf(f,"            RECEIVER                    \n");   
    fprintf(f,"===========================================================\n");
    /* Decoding */   
    fprintf(f,"\nReceived Channel Output:\n");
    fprintf(f,"-----------------------------\n");
    for(i=0;i<no_data_bit;i++){
        fprintf(f,"|");
        for(j=0;j<no_sig_bit;j++)
            fprintf(f,"%d ",z[i][j]);
        fprintf(f,"|");
    }
    fprintf(f,"\n");
    fprintf(f,"\nSignature Code to be used:\n");
    fprintf(f,"-----------------------------\n");
    for(i=0;i<no_user;i++){
        fprintf(f,"User %d: ",i+1);
        for(j=0;j<no_sig_bit;j++)
            fprintf(f,"%d ",sig_seq[i][j]);
        fprintf(f,"\n");
    }
    fprintf(f,"\nDecoded data stream:\n");
    fprintf(f,"--------------------------------\n");
    while(n<no_user){
        fprintf(f,"\nReceiver %d\n",n+1);       
        fprintf(f,"---------------------\n");
        fprintf(f,"Data Stream: ");
        for(i=0;i<no_data_bit;i++){
            sum=0;
            for(j=0;j<no_sig_bit;j++)
                sum=sum+z[i][j]*sig_seq[n][j];       
            data_stream[n][i]=sum/no_sig_bit;
            fprintf(f,"%d ",data_stream[n][i]);
        }
        fprintf(f,"\n");
        n++;       
    }   
}