Add Matlab files

This commit is contained in:
Imants Pulkstenis
2019-07-22 22:06:56 +03:00
parent f5014dd232
commit 033bb04bf6
6 changed files with 192 additions and 27 deletions
+70
View File
@@ -0,0 +1,70 @@
%% converting decimal to binary floting point
%
% | Sign | Exponent | Mantissa |
% | 1bit | 8bits | 18bits |
%
% 2^0 => Exponent = 'd127 = 'h7f
%
% Mantissa = [ ( 1-2^(-126) ) ; 0.5 ]
%
%
clear all;
x = 4/320 ; % number to convert
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
exponent = 127;
%
% calculating exponent and mantissa
%
x_new = abs(x);
while (x_new >= 1 && exponent <255 && exponent >0)
x_new = x_new / 2;
exponent = exponent + 1;
end
while (x_new < 0.5 && exponent <255 && exponent >0)
x_new = x_new * 2;
exponent = exponent -1;
end
% covertin x and exponent to fixed point number
disp( ' ' )
print = ['Converting decimal number ' , num2str(x) , ' to floting point number:'];
disp( print );
if (x < 0)
print = ['-'];
else
print = [' '];
end
print = [ ' ' , print , num2str(x_new) , '*2^' , num2str(exponent - 127)];
disp( print );
% if number are out of boundaries pront overflow
if (exponent == 255 || exponent == 0)
disp( ' ' );
disp( '************overflow*************' );
disp( ' ' );
end
%% Convering to binary
% Sign
if (x < 0 )
string_bin = ['1'];
else
string_bin = ['0'];
end
% Exponent
exp_ufi = ufi(exponent,8,0);
temp = [exp_ufi.bin];
for n=7:-1:0
string_bin = [string_bin, temp(end-n)];
end
% Mantissa
x_ufi = ufi(x_new,19,18);
temp = [x_ufi.bin];
for n=18:-1:0
string_bin = [string_bin, temp(end-n)];
end
string_hex = dec2hex(bin2dec(string_bin),7);
%% printing out result
disp( ' ' );
print = [ ' 27`b_ = ' , string_bin];
disp( print );
print = [ ' 27`h_ = ' , string_hex];
disp( print );
disp( ' ' );
+36
View File
@@ -0,0 +1,36 @@
%
%
% Clar figure 1 and workspace
clear all
%figure(1); clf;
% Image size
columns = 320;
rows = 240;
% Terminate after n cycles
termination = 1000;
x = linspace(-2, 2, columns); %[-2,1]
y = linspace(-1.5, 1.5, rows); %[-1,1]
%
% make blank(white) matrix
x_index = 1:length(x) ;
y_index = 1:length(y) ;
img = ones(length(y),length(x));
for k=x_index
for j=y_index
z = 0;
n = 0;
c = x(k)+ y(j)*i ;%complex number
while (abs(z)<2 && n<termination)
z = z^2 + c;
n = n + 1;
end
img(j,k) = fix(log2(n));
end
end
imagesc(img)
imwrite(img,'mandelbrot.jpeg','JPEG');
+32
View File
@@ -0,0 +1,32 @@
%
% Source:
% http://people.ece.cornell.edu/land/courses/ece5760/LABS/s2019/lab3_mandelbrot.html
%
clear all
figure(1); clf;
termination = 100;
x = linspace(-1.45, -1.3, 640); %[-2,1]
y = linspace(-0.07, 0.07, 480); %[-1,1]
x_index = 1:length(x) ;
y_index = 1:length(y) ;
img = zeros(length(y),length(x));
for k=x_index
for j=y_index
z = 0;
n = 0;
c = x(k)+ y(j)*i ;%complex number
while (abs(z)<2 && n<termination)
z = z^2 + c;
n = n + 1;
end
img(j,k) = fix(log2(n));
end
end
imagesc(img)
colormap(summer)