One-dimensional Convolution Example - fft

I'm trying to understand 1-dimensional convolution. Can someone explain--potentially step by step--how convolve(A,B) works for vectors A=[a,b,c] and B=[d,e,f]. Thanks!

I'm not sure what exacly you wan to do with this convolution, but here you can find amazing pdf about this. In first chapter there's example for 1-dimensional data. Later there are some examples and explanations form image processing.
Hope it will help you!

For the linear convolution result [v,w,x,y,z],
first reverse [d,e,f] to [f,e,d] , then input the first element of [a,b,c] :
v = f * 0 + e * 0 + d * a
for the next output, shift 0,0,a,b,c to the left by 1 and use the same f,e,d
w = f * 0 + e * a + d * b
for the next output, shift 0,a,b,c to the left by 1 and use the same f,e,d
x = f * a + e * b + d * c
for the next output, shift a,b,c to the left by 1 and use the same f,e,d
y = f * b + e * c + d * 0
for the next output, shift a,b,c to the left by 1 and use the same f,e,d
z = f * c + e * 0 + d * 0
For circular convolution replace the zeros with inputs that are wrapped around elements inside [a,b,c].

Related

What is the time complexity of the following pseudocode?

XYZ(a, b, c, m, n){
For p = 1 to m do
For q=p to n do
c[p,q] = a[p,q] + b[p,q];}
I think it is n + n-1 + n-2 +.....+(n-m+1). But I am not sure. Is it this or m*n?
Let's simplify your code :
For p from 1 to m
For q from p to n
Do something
Assuming the Do something part is done in constant time, what determines the time complexity of the code are the two loops. The outer loop runs m times, while the inner loop runs n-p, with p going from 1 to m.
If m >= n, the Do something part is repeated n+(n-1)+...+1 = n*(n+1)/2 = n²/2 + n/2 = O(n²) times.
Otherwise, if n > m, it's repeated n+(n-1)+...+(n-m+1) = (n*(n+1) - (n-m)*(n-m+1))/2 = 1/2 * (n² + n - n² + 2*n*m - n - m² + m) = O(2*n*m - m²) = O(n²) times.
In any case, O(n²) is a right answer, but if n >> m, a more precise answer is O(n*m).

Maxima - Substitutions in Equations - let() and letsimp() without effect

My own equation is a bit longer, but the following example shows perfectly where I struggle at the moment.
So far I have been using the let() and letsimp() function
to substitute longer terms in an equation,
but in this example they have no effect:
(%i1) eq: ((2*u+a^2+d) * y+x)/2*a = x;
2
a ((2 u + d + a ) y + x)
(%o1) ------------------------ = x
2
(%i2) let(2*u+a^2+d, %beta);
2
(%o2) 2 u + d + a --> %beta
(%i3) letsimp(eq);
2
a ((2 u + d + a ) y + x)
(%o3) ------------------------ = x
2
What is the preferred way to replace 2*u+a^2+d with %beta in this sample equation?
And why has letsimp() no effect?
Thank you very much!
letsimp applies only to "*" expressions. You could try subst.

Octave division returning zero

I'm writing a simple octave program that calculate similarity between two images.
The relevant part for this question can be found here:
http://pastebin.com/gBxN7VbP
The function struct_comp was supposed to return some number between zero and one. But the command
ec = (corr + C) / (dp1 * dp2 + C);
is setting ec as 0 even when it should not be zero.
The disp commands are showing the values of all variables involved in this division. The output is:
C:
6.5536
dp1
97.663
dp2
47.686
corr
-290
(corr + C)
-283
(dp1 * dp2 + C)
4663.7
(corr + C) / (dp1 * dp2 + C)
0
Struct comp:
0
As you can see, the partial values (numerator and denominator) are right, but the division is returning zero.
Somebody knows what is happening here?
Thank you.
EDIT:
The two images used to generate this output were these:
http://lasca.ic.unicamp.br/~hilder/tux.jpg
http://lasca.ic.unicamp.br/~hilder/monalisa.jpg
But you can use any grayscale image to test, just change the name in the code.
i replaced the line
ec = (corr + C) / (dp1 * dp2 + C);
by
ec = double(corr + C) / double(dp1 * dp2 + C);
and it worked.

Mathcad 14: "pattern match exception" when solving equation with more unknowns

I'm trying to solve an equation with 5 unknowns in Mathcad 14. My equations look like this:
Given
0 = e
1 = d
0 = c
-1 = 81a + 27b + 9c + 3d + e
0 = 108a + 27b + 6c + d
Find(a,b,c,d,e)
Find(a,b,c,d,e) is marked as red and says "pattern match exception". What is the problem?
In mathcad you need to do something similar to:
c:=0
d:=1
e:=0
a:=1
b:=1
Given
81*a + 27*b + 9*c + 3*d + e = -1
108*a + 27*b + 6*c + d = 0
Find(a,b,c,d,e) = (0,0,0,0,-1)
Now, what I have done here is to define the variables BEFORE the Solve Block (Given...Find), you have to give initial values which you think are close to the solution you require in order for the iteration to succeed.
Tips: To get the equals sign in the Solve Block, use ctrl and '='. If your looking to solve for 5 unknowns then you need 5 equations, the original post looked like you knew 3 of the variables and were looking for a and b, in this case you would do the following:
c:=0
d:=1
e:=0
a:=1
b:=1
Given
81*a + 27*b + 9*c + 3*d + e = -1
108*a + 27*b + 6*c + d = 0
Find(a,b) = (0.111,-0.481)
This has held c, d and e to their original values and iterated to solve for a and b only.
Hope this helps.

Math - Get x & y coordinates at intervals along a line

I'm trying to get x and y coordinates for points along a line (segment) at even intervals. In my test case, it's every 16 pixels, but the idea is to do it programmatically in ActionScript-3.
I know how to get slope between two points, the y intercept of a line, and a2 + b2 = c2, I just can't recall / figure out how to use slope or angle to get a and b (x and y) given c.
Does anyone know a mathematical formula to figure out a and b given c, y-intercept and slope (or angle)? (AS3 is also fine.)
You have a triangle:
|\ a^2 + b^2 = c^2 = 16^2 = 256
| \
| \ c a = sqrt(256 - b^2)
a | \ b = sqrt(256 - a^2)
| \
|__________\
b
You also know (m is slope):
a/b = m
a = m*b
From your original triangle:
m*b = a = sqrt(256 - b^2)
m^2 * b^2 = 256 - b^2
Also, since m = c, you can say:
m^2 * b^2 = m^2 - b^2
(m^2 + 1) * b^2 = m^2
Therefore:
b = m / sqrt(m^2 + 1)
I'm lazy so you can find a yourself: a = sqrt(m^2 - b^2)
Let s be the slop.
we have: 1) s^2 = a^2/b^2 ==> a^2 = s^2 * b^2
and: 2) a^2 + b^2 = c^2 = 16*16
substitute a^2 in 2) with 1):
b = 16/sqrt(s^2+1)
and
a = sqrt((s^2 * 256)/(s^2 + 1)) = 16*abs(s)/sqrt(s^2+1)
In above, I assume you want to get the length of a and b. In reality, your s is a signed value, so a could be negative. Therefore, the incremental value of a will really be:
a = 16s/sqrt(s^2+1)
The Point class built in to Flash has a wonderful set of methods for doing exactly what you want. Define the line using two points and you can use the "interpolate" method to get points further down the line automatically, without any of the trigonometry.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Point.html#interpolate()
The Slope is dy/dx. Or in your terms A/B.
Therefore you can step along the line by adding A to the Y coordinate, and B to the X coordinate. You can Scale A and B to make the steps bigger or smaller.
To Calculate the slope and get A and B.
Take two points on the line (X1,Y1) , (X2,Y2)
A= (Y2-Y1)
B= (X2-X1)
If you calculate this with the two points you want to iterate between simply divide A and B by the number of steps you want to take
STEPS=10
yStep= A/STEPS
xStep= B/STEPS
for (i=0;i<STEPS;i++)
{
xCur=x1+xStep*i;
yCur=y1+yStep*i;
}
Given the equation for a line as y=slope*x+intercept, you can simply plug in the x-values and read back the y's.
Your problem is computing the step-size along the x-axis (how big a change in x results from a 16-pixel move along the line, which is b in your included plot). Given that you know a^2 + b^2 = 16 (by definition) and slope = a/b, you can compute this:
slope = a/b => a = b * slope [multiply both sides by b]
a^2 + b^2 = 16 => (b * slope)^2 + b^2 = 16 [by substitution from the previous step]
I'll leave it to you to solve for b. After you have b you can compute (x,y) values by:
for x = 0; x += b
y = slope * x + intercept
echo (x,y)
loop