## Saturday, 16 November 2013

### Study and Implementation of Looping Statements (for loop and while loop), Implementation of Peak Signal to Noise Ratio and Factorial of Number.....

AIM: Study and Implementation of Looping Statements (for loop and while loop).

THEORY:
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: while loops and for loops. The major difference between these two types of loops is in how the repetition is controlled. The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied. By contrast, the code in a “for loop” is repeated a specified number of times, and the number of repetitions is known before the loops starts.

1. For Loop:
The for loop is a loop that executes a block of statements a specified number of times.

Syntax:
For index = expression
Statement;
Statement;
End
where index is the loop variable (also known as the loop index) and expr is the loop control expression, whose result is an array. The columns in the array produced by expr are stored one at a time in the variable index, and then the loop body is executed, so that the loop is executed once for each column in the array produced by expr. The expression usually takes the form of a vector in shortcut notation first:incr:last.

2. While Loop:
A while loop is a block of statements that are repeated indefinitely as long as some condition is satisfied.
Syntax:
While (Control Expression)
Statement;
End

Fig: Flow Diagram of While Loop

The controlling expression produces a logical value. If the expression is true, the code block will be executed, and then control will return to the while statement. If the expression is still true, the statements will be executed again. This process will be repeated until the expression becomes false. When control returns to the while statement and the expression is false, the program will execute the first statement after the end.

PROCEDURE:
1. Program for finding factorial of given number using while loop:
Using while loop just implement factorial of a given number. First assign 0! Is 1 and 1! Is also 1.
2. Program for finding PSNR using for loop:
The PSNR is calculated by using following formula.

MAXI=Maximum value of pixel in Original image
m=No. of Row in Original image
n= No. of Column in Original image
1.      If Original Image is equal to Noisy Image then PSNR is 100%.
2.      Find out difference between Original Image & Noisy Image.
3.      Find out Mean Square Error by using above formula.
4.      Find out maximum value of Pixel in Original Image.
5.      Find out Peak Signal to Noise Ratio.

1. Program for Finding Factorial using While loop:
Program:
% Factorial finding
a=input('Enter value for factorial = ');
fact=1;
while(a>1)
fact=fact*a;
a=a-1;
end
disp('Factorial of given value is');fact

Output:
Enter value for factorial = 6
Factorial of given value is
fact =720

2. Program for PSNR using for loop
Program:
% PSNR using foe loop

ori=input('Enter Original Matrix = ');
noi=input('Enter Noisy Matrix = ');

if (ori==noi)
PSNR=100;
else
[r c]=size(ori);
d = 0 ;
for i = 1:r-1
for j = 1:c-1
d = d + (ori(i,j) - noi(i,j))^2 ;
end
end
MSE=d/(r*c);
MAX = max( abs(ori(:)) );

PSNR= 10*log10(MAX^2/MSE);
end
disp('PSNR is');PSNR

Output:
Enter Original Matrix = [2 3 4;4 5 6;1 2 3]
Enter Noisy Matrix = [6 7 8;4 5 6;2 3 4]
PSNR is
PSNR =10.0540