a combinational circuit that accepts a 4-bit number and generates a 3-bit binary number output that approximates the square root of the number - boolean

Design a combinational circuit that accepts a 4-bit number and generates a 3-bit binary number output that approximates the square root of the number. For example, if the square root is 3.5 or larger, give a result of 4. If the square root is < 3.5 and ≥ 2.5, give a result of 3.
Does my truth table on input goes this way? (I'm using A, B, C, D for my inputs)
INPUTS OUTPUTS Decimal - Square Root Value
__________ __________ ____________________________
A B C D W X Y Z
0 0 0 0 0 0 0 0 0 - 0
0 0 0 1 0 0 0 1 1 - 1
0 0 1 0 0 0 0 1 2 - 1.14
0 0 1 1 0 0 1 0 3 - 1.73
0 1 0 0 0 0 1 0 4 - 2
0 1 0 1 0 0 1 0 5 - 2.23
0 1 1 0 0 0 1 0 6 - 2.44
0 1 1 1 0 0 1 1 7 - 2.64
1 0 0 0 0 0 1 1 8 - 2.82
1 0 0 1 0 0 1 1 9 - 3
1 0 1 0 0 0 1 1 10 - 3.16
1 0 1 1 0 0 1 1 11 - 3.31
1 1 0 0 0 0 1 1 12 - 3.46
1 1 0 1 0 1 0 0 13 - 3.60
1 1 1 0 0 1 0 0 14 - 3.74
1 1 1 1 0 1 0 0 15 - 3.87
I'm having trouble generating the output table with "generates a 3-bit binary number output that approximates the square root of the number" Can someone help me with the outputs? Thank you.

Translate your input as decimal, get square root for each of them, and translate them in binary?
Exemple:
0000 => 0
Square root of 0 is 0
0 => 0000
So you have
A|B|C|D||W|X|Y|Z
0 0 0 0||0 0 0 0
And do the rest of your homework this way?

Related

Filter function for k elements larger than A

In R/Bioconductor's genefilter package, there is a nice function called kOverA (page 18 in this manual).
It's just a filter method that, given a numerical matrix, removes the rows of that matrix that do not have k-elements that are greater than or equal to A-value.
How can I do the same thing in MATLAB?
Examples (simplified. In R, kOverA returns a function, so the actual syntax is a bit different but this is the functionality that I want):
m = [1 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1];
kOverA(m, A=1, k=0) → m
kOverA(m, A=2, k=1) → empty
kOverA(m, A=1, k=1) → [1 0 0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
kOverA(m, A=1, k=4) → [1 0 0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
kOverA(m, A=1, k=5) → [1 1 1 1 1 1 1 1 1 1
0 1 0 1 0 1 0 1 0 1]
Requires relational operator >=, sum and logical indexing and this is it.
out = m(sum(m>=A,2) >= k,:);

Concatenating binary bits into a number

I want to concatenate last four bits of binary into a number i have tried the following code
x8=magic(4)
x8_n=dec2bin(x8)
m=x8_n-'0'
which gives me the following output
m =
1 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 0 1 0 0
0 0 0 1 0
0 1 0 1 1
0 0 1 1 1
0 1 1 1 0
0 0 0 1 1
0 1 0 1 0
0 0 1 1 0
0 1 1 1 1
0 1 1 0 1
0 1 0 0 0
0 1 1 0 0
0 0 0 0 1
now i want to take every last 4 bits it each row and convert it into an integer
n = 4; %// number of bits you want
result = m(:,end-n+1:end) * pow2(n-1:-1:0).'; %'// matrix multiplication
Anyway, it would be easier to use mod on x8 directly, without the intermediate step of m:
result = mod(x8(:), 2^n);
In your example:
result =
0
5
9
4
2
11
7
14
3
10
6
15
13
8
12
1
This could be another approach -
n = 4; %%// number of bits you want
out = bin2dec(num2str(m(:,end-n+1:end)))
Output -
out =
0
5
9
4
2
11
7
14
3
10
6
15
13
8
12
1

Calculating a partial cumulative sum for a square matrix

Let's say I have a square matrix M:
M = [0 0 0 0 0 1 9; 0 0 0 0 0 4 4; 0 0 1 1 6 1 1; 0 1 2 9 2 1 0; 2 1 8 3 2 0 0; 0 8 1 1 0 0 0; 14 2 0 1 0 0 0]
0 0 0 0 0 1 9
0 0 0 0 0 4 4
0 0 1 1 6 1 1
M = 0 1 2 9 2 1 0
2 1 8 3 2 0 0
0 8 1 1 0 0 0
14 2 0 1 0 0 0
Now I'd like to calculate two different cumulative sums: One that goes from the top of each column to the element of the column, that is a diagonal element of the matrix, and one that goes from the bottom of the column to the same diagonal element.
The resulting matrix M'should therefore be the following:
0 0 0 0 0 1 9
0 0 0 0 0 4 5
0 0 1 1 6 2 1
M' = 0 1 3 9 4 1 0
2 2 8 5 2 0 0
2 8 1 2 0 0 0
14 2 0 1 0 0 0
I hope the explanation of what I'm trying to achieve is comprehensible enough. Since my matrices are much larger than the one in this example, the calculation should be efficient as well...but so far I couldn't even figure out how to calculate it "inefficiently".
In one line using some flipping and the upper triangular function triu:
Mp = fliplr(triu(fliplr(cumsum(M)),1)) ...
+flipud(triu(cumsum(flipud(M)),1)) ...
+flipud(diag(diag(flipud(M))));
The following will do the job:
Mnew = fliplr(triu(cumsum(triu(fliplr(M)),1))) + flipud(triu(cumsum(triu(flipud(M)),1)));
Mnew = Mnew - fliplr(diag(diag(fliplr(Mnew)))) + fliplr(diag(diag(fliplr(M))));
But is it the fastest method?
I think logical indexing might get you there faster

Grouping logical data in Matlab

I have a matrix that contains data of 0 & 1. I want to find groups of ones (not a specific size) in that matrix. Is it possible somehow?
Thanks in advance!
If you mean that you want to find all the "connected components in the matrix, say BW, simply use:
BW = logical([1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0]);
L = bwlabel(BW,4) %Result
This would yeild:
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 3 3 0
1 1 1 0 0 0 0 0
Now if you want to find the size of various groups:
for ii=1:max(L(:))
length_vector(ii)=length(find(L==ii));
end
length_vector
This gives you:
length_vector =
24 4 5

profile of circular image - more efficient way to do it?

I need to get a 1-D profile of a circular image, for example 256x256 sin(R) image
I've written a matlab function for the task but it turns out to be very un-efficient.
the function averages over radius intervals of the original images.
matlab profiler reveals that the first line in the for-loop [indxs=find(...)]
takes ~86% of the running time.
i need to run the function on a some thousands of simulated images (some larger then 256x256) and it takes very long time to complete.
does anyone knows how can i make this code run faster?
maybe someone has another, more efficient way to do the task??
i also tried to convert to function into C++ & mex file using matlab coder
but it took longer (x3) to perform the task, might be because the sub-function- "findC"
uses some 2D-ffts to find the center of the image.
Thanks you All,
Dudas
My Matlab function:
function [sig R_axis Center]= Im2Polar (imR,ch,Center_Nblock)
% Converts Circular image to 1-D sig
% based on true image values w/o interpolation
% Input -
% imR - circular sinuns image
% ch - number of data-points in output signal (sig)
% Center_Nblock - a varible related to the image center finding method
% Output -
% sig - 1D vector of the circular image profile
% R_axis - axis data-points for sig
% Center - image center in pixels
[Mr Nr] = size(imR); % size of rectangular image
[Center]=findC(imR,Center_Nblock);
Xc=Center(1);
Yc=Center(2);
rMax=sqrt((Mr/2)^2 + (Nr/2)^2);
x=[0:1:Mr-1]-Xc+1;
y=[0:1:Nr-1]-Yc+1;
[X,Y]=meshgrid(x,y);
[TH,R] = cart2pol(X,Y);
% Assembling 1-D signal
sig=single([]);
ii=1;
dr=floor(rMax)/ch;
V=dr:dr:floor(rMax);
for v=V
indxs=find((v-dr)<=R & R<v);**
sig(ii)=mean(imR(indxs));
Nvals(ii)=length(indxs);
ii=ii+1;
end %for v
R_axis=V-dr/2;
end % of function
Following from the comments here's an example of something I might try. Let's work with a 9x9 example. Suppose you have the following annulus.
A =
0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 0 0
0 1 1 1 0 1 1 1 0
0 1 1 0 0 0 1 1 0
0 1 0 0 0 0 0 1 0
0 1 1 0 0 0 1 1 0
0 1 1 1 0 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0
Then the indices of your sort of mask are, lets say [k n]
>> [k n]
ans =
3 2
4 2
5 2
6 2
7 2
2 3
3 3
4 3
6 3
7 3
8 3
2 4
3 4
7 4
8 4
2 5
8 5
2 6
3 6
7 6
8 6
2 7
3 7
4 7
6 7
7 7
8 7
3 8
4 8
5 8
6 8
7 8
Now have a 9x9 matrix of zeroes on hand called B, we can shift the whole thing over to the left by one pixel as follows using the formula (i+9*(j-1)) to convert double index to a single index.
>> B=zeros(9,9);
>> B((k)+9*(n-2))=1
B =
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0
1 1 1 0 1 1 1 0 0
1 1 0 0 0 1 1 0 0
1 0 0 0 0 0 1 0 0
1 1 0 0 0 1 1 0 0
1 1 1 0 1 1 1 0 0
0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0
Or move down and to the right as follows
>> B=zeros(9,9);
>> B((k+1)+9*(n-0))=1
B =
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 1 0
0 0 1 1 1 0 1 1 1
0 0 1 1 0 0 0 1 1
0 0 1 0 0 0 0 0 1
0 0 1 1 0 0 0 1 1
0 0 1 1 1 0 1 1 1
0 0 0 1 1 1 1 1 0
As long as it doesn't go out of bounds you should be able to shift a single annular mask around with a simple addition to put the center at the image center.