initial point in CORDIC algorithm - matlab

I am trying to reduce number of iterations required to calculate multiplication using the CORDIC algorithm because I am using this algorithm in a continuous function to calculate square function. Here is the algorithm assuming -1<x<1'
function z=square(x)
y=x;
z=0;
for i=1:15
if (x > 0)
x = x - 2^(-i);
z = z + y*2^(-i);
else
x = x + 2^(-i);
z = z - y*2^(-i);
end
end
return
end
I already know the close value to multiplication result (from the previous result (call it pr)) and value of x (the value of x is continuous) . Does it help in anyway to decrease number of iterations?

If you are multiplying twice by the same constant, say a.x and a.x', then you can multiply and add with the delta a.(x' - x), which has less significant digits.
In case both factors vary, you can still use
x'.y' = (x'- x).(y' - y) + x.(y' - y) + (x' - x).y + x.y
where maybe the first term is neglectible.
For a square,
x'² = (x'- x)² + 2.x.(x' - x) + x²

Related

A little discrete math problem, not sure where to even start?

𝑓(𝑥) = 1/9𝑥^2, for 0 ≤ x ≤ 3, and
= 0, otherwise
**Determine both the expectation and variance of the continuous random variable defined above. **
My professor never addressed this topic and I just wanted a starting point.
Wasn't able to complete as I had no idea where to start.
The expected value of a random variable with probability density function f(x) is the indefinite integral from -infinity to +infinity of xf(x). For us, we find
S xf(x) dx
= S 1/9 x^3 dx
= 1/36 x^4 + C
Evaluating at x=3 and x=0 we get:
81/36 - 0/36 = 81/36 = 9/4
The variance is given by E[x^2] - E[x]^2; we already found E[x] above and we can find E[x^2] by integrating x^2 f(x):
S x^2 f(x) dx
= S 1/9 x^4 dx
= 1/45 x^5 + C
Evaluating at x=3 and x=0:
243/45 - 0/45 = 243/45 = 27/5
Now we can evaluate E[x^2] - E[x]^2:
27/5 - 81/16 = (432 - 405)/80 = 27/80
So, the variance is 27/80 and the expected value is 9/4.

Series expansion of a function about infinity - how to return coefficients of series as a Matlab array?

This question is connected to this one. Suppose again the following code:
syms x
f = 1/(x^2+4*x+9)
Now taylor allows the function f to be expanded about infinity:
ts = taylor(f,x,inf,'Order',100)
But the following code
c = coeffs(ts)
produces errors, because the series does not contain positive powers of x (it contains negative powers of x).
In such a case, what code should be used?
Since the Taylor Expansion around infinity was likely performed with the substitution y = 1/x and expanded around 0, I would explicitly make that substitution to make the power positive for use on coeffs:
syms x y
f = 1/(x^2+4x+9);
ts = taylor(f,x,inf,'Order',100);
[c,ty] = coeffs(subs(ts,x,1/y),y);
tx = subs(ty,y,1/x);
The output from taylor is not a multivariate polynomial, so coeffs won't work in this case. One thing you can try is using collect (you may get the same or similar result from using simplify):
syms x
f = 1/(x^2 + 4*x + 9);
ts = series(f,x,Inf,'Order',5) % 4-th order Puiseux series of f about 0
c = collect(ts)
which returns
ts =
1/x^2 - 4/x^3 + 7/x^4 + 8/x^5 - 95/x^6
c =
(x^4 - 4*x^3 + 7*x^2 + 8*x - 95)/x^6
Then you can use numden to extract the numerator and denominator from either c or ts:
[n,d] = numden(ts)
which returns the following polynomials:
n =
x^4 - 4*x^3 + 7*x^2 + 8*x - 95
d =
x^6
coeffs can then be used on the numerator. You may find other functions listed here helpful as well.

Optimization of function with 2 variables

I have this function with all variables in the unit square:
(x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2))
ezplot yields this figure:
I want to numerically find 2 points in this plot: First, the minimum value of x on the green line (that is, approx. (0.11, 0.4)) and second, the maximum value of y on the green line (that is, approx. (0.15, 0.43)). These approximate value are just rough estimates from the figure. How can I retrieve these 2 values numerically in matlab?
Ezplot. You need the handle.
h = ezplot('(x + y)^(1/2) - 6*y*(x + y)^5 - (x + y)^6 + (x - 1)/(2*(x + y)^(1/2))', [0 1]);
Get the data.
data = get(h, 'ContourMatrix')
Remove first column
data = data(:,2:end)
You have what you need. First row are 'x' values and second row are 'y' values.
x_min = min(data(1,:));
y_max = max(data(2,:));

What is wrong in this Discrete Integration with Galois Fields in Matlab

I want to implement Discrete Integration with Galois Fields in Matlab where the time step is not constant.
Assume that it is this:
My attempt
function [ int ] = integrate_matlab( YDataVector, a, b )
%integrate_matlab Calculate the discrete integral
% Discrete Matlab Integration
% int_1^N x(t_k) * (b-a)/N, where t_k = a + (b-a) k/N
%
% YDataVector - Galois vector (255 x 1 gf), this is signal,
% which values you can reach by YDataVector.x
%
% int - returns Galois vector (255 x 1 gf)
N = length(YDataVector);
for k=1:N
tk = a + (b - a) * k/N;
int = xtk(YDataVector, k) * (b - a) / N;
% How to implement the function xtk(YDataVector)?
end
and then the function xtk
function [ xtk_result ] = xtk( YDataVector, k )
%xkt Summary of this function goes here
% YDataVector - Galois vector (255 x 1 gf), this is signal
% xtk_result - Galois vector (255 x 1 gf)
% k - index, this must be here to be able calculate different xtk for different iterations
xtk_result = ; // I do not know what to fill here
end
I am confused by the mathematical series equation x(tk) for tk.
I know that I am doing now this wrong.
The writing x(tk) just confuses me, since I think it as a function that takes in the series.
I know that it is a signal at some time point, here the YDataVector, but how to implement it I have forgotten.
I should probably iterate the series first:
t_0 = a;
t_1 = a + (b - a) * 1/N;
This does not seem to help, since tk is not defined iteratively.
What am I thinking wrong when implementing the series x(tk)?
Assuming that t contains the time that corresponds to each element of x (stored in YDataVector.x). Then if I understood correctly your question you can get x_tk with something like this :
N = length(YDataVector.x) ;
k = 1 : N;
tk = a + (b-a)* k/N ;
x_tk = interp1(t,YDataVector.x,tk);

how to build an accumulator array in matlab

I'm very new to matlab so sorry if this is a dumb question. I have to following matrices:
im = imread('image.jpg'); %<370x366 double>
[y,x] = find(im); %x & y both <1280x1 double>
theta; %<370x366 double> computed from gradient of image
I can currently plot points one at a time like this:
plot(x(502) + 120*cos(theta(y(502),x(502))),y(502) + 120*sin(theta(y(502),x(502))));
But what I want to do is some how increment an accumulator array, I want to increment the location of acc by 1 for every time value for that location is found.
So if x(502) + 120*cos(theta(y(502),x(502))),y(502) + 120*sin(theta(y(502),x(502)) = (10,10) then acc(10,10) should be incremented by 1. I'm working with a very large data set so I want to avoid for-loops and use something like this:
acc = zeros(size(im));
%increment value when point lands in that location
acc(y,x) = acc(x + 120*cos(theta(y,x)),y + 120*sin(theta(y,x)),'*b')) + 1;
It would be nice if the 120 could actually be another matrix containing different radius values as well.
Do
i = find(im);
instead of
[y,x] = find(im)
wthis will give you linear indice of non zero values
Also, create a meshgrid
[x,y] = meshgrid(1:366,1:370)
Now you can index both coordinated and values linearly, for example
x(520) is 520-th point x coordinate
im(520) is 520-th point gray value
theta(520) is 520-th gradient value
So, now you can just plot it:
plot(x(i) + 120*cos(theta(i)),y(i) + 120*sin(theta(i)),'b*');
x(i) means a column of i-th values
x(i) + 120*cos(theta(i)) means a column of results
ACCUMULATING
I think in this case it is ok to loop for accumulating:
acc=zeros(size(im));
for ii=1:length(i)
xx=round(x(ii) + 120*cos(theta(ii)));
yy=round(y(ii) + 120*sin(theta(ii)));
acc(xx,yy)=acc(xx,yy)+1;
end;
The factor (120 in the example) can be matrix of size of im or scalar. The .* will do it.
im = imread('image.jpg');
[y, x] = meshgrid(1 : size(im, 1), 1 : size(im, 2));
factor = 120 * ones(size(im));
theta = gradient(double(image)); % just for example
acc = zeros(size(im));
increment = ((x + factor .* cos(theta)) == 10) & ((y + factor .* sin(theta)) == 10);
acc = acc + increment;
But the comparison to 10 will rarely be true, so you need to allow some range. For example (9,11).
e = 1;
increment = abs((x + factor .* cos(theta)) - 10) < e & abs((y + factor .* sin(theta)) - 10) < e;