How to calculate FFT of real even signal of N points with just N/2 points? - algorithm

Suppose the given signal is x = [0 1 2 3 0 3 2 1]
The FFT of this real even signal is also real even
fft(x) = X = [12.0000 -2.8284 -4.0000 2.8284 -4.0000 2.8284 -4.0000 -2.8284];
Let y = x(1:length(x)/2); i.e. x = [y 0 fliplr(y(2:end)]
I can use fft(y) to calculate X = fft(x)?

Related

How divide pie with constraints

How divide a pie with constraints?
Hi I have rounded pie and would like to divide it, but am not able to figure out how to do it.
I have four friends: A,B,C,D
I want to divide pie based on how I like them, so based on opinion.
Slice sizes:
A= 1/22 of pie
B= 10/22 of pie
C= 1/22 of pie
D= 10/22 of pie.
How to divide the pie when there are some constrains?
Like B will tell me he wants < 10% of whole pie and
D must have at least 85% of pie.
A,C don't care.
In this case I can say ok, so D wants at least 85% 22*0.85= 18.7 so he will get this.
Now I have only rest of the pie 22-18.7= 3.3 = 15% to divide and I don't want to give bigger slice than 10% to B. And I want still apply the ratios I proposed but only on rest of the pie as D must have at least 85%.
I think the ratios should be now applied after constrains are resolved.
A has no constraint so he can get from 0-100%
B wants 0-10%
C has no constraints 0-100%
D wants at least 85-100%
I can apply the ratios on slices under constrains like
when B wants 0-10% then I can say the ratio will have influence to the size between 0-10%
and for D will the ratio influence size (85%-100%).
| friends: | A | B | C | D |
|constraints:| | <=0.1 | >=0.85 | |
|ranges: | 0 to 1 | 0 to 0.1 | 0.85 to 1 | 0 to 1 |
|ratios: | 1/22 | 10/22 | 1/22 | 10/22 |
Hopefully is the problem understandable. At the end I want to have whole pie divided among ABCD with not violated constrains, and with somehow applied ratios.
Let me propose a formalization of this problem as a quadratic program.
Let x be the desired result. We want to minimize the L2 norm of
x/ratio (element-wise) subject to lower ≤ x ≤ upper
(element-wise) and x·1 = 1.
The idea behind this objective is that, by examining the optimality
conditions, we can show that there exists some scalar z such that x
= median(lower, ratio z, upper).
Below is some very poorly tested Python 3 code to approximately solve
this quadratic program.
from fractions import Fraction
ratio = [1, 10, 2, 10]
lower = [0, 0, 0, 85]
upper = [100, 10, 100, 100]
# Want to minimize the L2 norm of x / ratio subject to lower <= x <= upper and
# sum(x) == 100
# Validation
assert 0 < len(ratio) == len(lower) == len(upper)
assert all(0 < r for r in ratio)
assert all(0 <= l <= u <= 100 for (l, u) in zip(lower, upper))
assert sum(lower) <= 100 <= sum(upper)
# Binary search
n = len(ratio)
critical = sorted(
{Fraction(bound[i], ratio[i]) for bound in [lower, upper] for i in range(n)}
)
a = 0
b = len(critical)
while b - a > 1:
m = (a + b) // 2
z = critical[m]
if sum(sorted([lower[i], ratio[i] * z, upper[i]])[1] for i in range(n)) <= 100:
a = m
else:
b = m
x = [0] * n
z = critical[a]
divisor = 0
for i in range(n):
value = ratio[i] * z
if value < lower[i]:
x[i] = lower[i]
elif upper[i] <= value:
x[i] = upper[i]
else:
divisor += ratio[i]
dividend = 100 - sum(x)
for i in range(n):
if lower[i] <= ratio[i] * z < upper[i]:
x[i] = Fraction(ratio[i], divisor) * dividend
print(x)
Output:
[Fraction(5, 3), 10, Fraction(10, 3), 85]

Reshaping matrix in N dimensions octave

would appreciate feedback on how to reshape general ND matrix from a file in octave. To set the problem,
if I read data like for 2D case x y z
0 10 13
0 11 -1
0 12 12
1 10 5
1 11 4
1 12 3
2 10 1
2 11 6
2 12 2
reading Z values
matrix = dlmread('data.dat');
z = matrix(:,3);
z = reshape(z, 3, 3).';
x = [0 1 2];
y = [10 11 12];
xi = linspace (min (x), max (x), 200);
yi = linspace (min (y), max (y), 300);
fcn = interpn (x, y, A, xi, yi, "spline");
Now, transposing a matrix doesn't work in ND which here needs to be done. How would one do a ND case pulling from data so to format this for interpn,
thanks, Damir

Generate a random number which is far enough from another number

Let x, range, d be integers. We'd like to generate a number y, such that
1 <= y <= range
abs(x-y) >= d
One idea I came up with is to generate some smaller range and then make some adjustments to handle the numbers which too close to x. But that's really tedious.
Is there any better way to do it?
Here is a Python function that you should be able to adapt to the language of your choice:
import random
def distantRand(a,b,x,d):
#returns a random integer in range a ... b
#which is greater than or equal to d units from x
lb = max(a,x-d+1)
ub = min(b,x+d-1)
k = ub-lb+1 #number of numbers ruled out
if b-k < a:
return None
else:
y = random.randint(a,b-k)
if y > x - d:
y = y + k
return y
For example, distantRand(1,10,5,3) should return a number in the range 1 to 10 which is at least units away from 5. This rules out 3,4,5,6,7 as return values, leaving 10-5 = 5 valid numbers. The function picks one such in the range 1 to 5. If the number chosen is >2, 5 is added to it to make it a number which is >7 (but still <= 10). For example:
>>> for i in range(20):
print(distantRand(1,10,5,3))
1
1
1
8
2
9
10
8
1
10
10
2
8
10
8
8
8
2
1
2
I have done it like this in Python.
import random
range=100
d=20
x=115
while(True):
y=random.randint(1,range)
if abs(x-y)>=d:
print abs(x-y)
print y
break
And here it is as a def
import random
r=100
d=20
x=115
def yourandom (x,d,r):
while(True):
y=random.randint(1,r)
if abs(x-y)>=d:
print "abs(x-y)=",abs(x-y)
print "y=",y
break
yourandom(x,d,r)

3D plotting from an m by n matrix

I have a large file (over 2 million cells) and I need the value of each individual cell to be read as the z coordinate of a 3D plot and the original x,y coordinates of the cell to be the x and y coordinates in the graph. For example:
M =
1 2 3
4 5 6
7 8 9
I need it to be read as (1,1,1) (2,1,2) (3,1,3)
(1,2,4) (2,2,5) (3,2,6)
(1,3,7) (2,3,8) (3,3,9)
And from there I need to create a 3D plot. Please help!
Pseudocode:
size = 3 # set this to the maximum value for x
x = 1
y = 1
z = readvalue()
while read succeeded
plot(x, y, z)
x = x + 1
if x > size
x = 1
y = y + 1
z = readvalue()

Matrix linear indexing

I need to traverse a rectangular grid in continuous manner. Here is an example of what I want, the number means sequence:
+ x
y 0 1 2
5 4 3
6 7 8
At each step I know the index in matrix. Is there any way to calculate the coordinates? The inverse mapping for [x + y * width] doesn't help, beacuse it creates "steps" or "jumps". Is there any solution?
Here is explanation for "steps" mentioned above:
+ x
y 0 1 2
3 4 5 //at this moment the X coordinate changes by 3, thus create step
6 7 8
y = index / width
if( y % 2 == 0 )
x = index % width
else
x = width - index % width - 1
I think that should do it. It's a single modification of the standard way of calculating with "steps" as you call them. You are only changing the way the calculation is done based upon the row.
so you need to first increase the "x" component and then decrease right - so that you get a kind of snake-behavior? You will need an if statement (or some kind of modulo - magic). Let my try the magic:
y := floor(i/columnCount)
x = (y mod 2)*(i - y*columCount) + ((y+1) mod 2)*((columnCount -1) - (i - y*columnCount))

Resources