I have an input signal that I want to store in an ArrayList then convert it into Complex, which goes something like this
-0.03480425839330703
0.07910192950176387
0.7233322451735928
0.1659819820667019
and this outputs its FFT like this
0.9336118983487516
-0.7581365035668999 + 0.08688005256493803i
0.44344407521182005
-0.7581365035668999 - 0.08688005256493803i
This is in a complex structure, I want to convert this into an ArrayList type. while dropping the + 0.08688005256493803i value.
So All I need are these values
0.9336118983487516
-0.7581365035668999
0.44344407521182005
-0.7581365035668999
What is the best way of going about this?
And this is the code that I am using
public static Complex[] fft(Complex[] x) {
int N = x.length;
// base case
if (N == 1) return new Complex[] { x[0] };
// radix 2 Cooley-Tukey FFT
if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }
// fft of even terms
Complex[] even = new Complex[N/2];
for (int k = 0; k < N/2; k++) {
even[k] = x[2*k];
}
Complex[] q = fft(even);
// fft of odd terms
Complex[] odd = even; // reuse the array
for (int k = 0; k < N/2; k++) {
odd[k] = x[2*k + 1];
}
Complex[] r = fft(odd);
// combine
Complex[] y = new Complex[N];
for (int k = 0; k < N/2; k++) {
double kth = -2 * k * Math.PI / N;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + N/2] = q[k].minus(wk.times(r[k]));
}
return y;
}
All you want to do is just drop imaginary part of your Complex data structure.
As you not show us Complex class assume it has member for real part (e.g double real;)
To drop imaginary part just call something like complex.getRealPart(), or access complex.real (substitute with your real member name).
To compose ArrayList<Double> use the following snippet:
ArrayList<Double> list = new ArrayList<Double>();
for (Complex c : complexes) { // complexes your array of complexes returned from for fft
list.add(c.getRealpart());
}
Note: Just in case, I can be wrong, but I assume that instead of real part you need absolute value of complex number. To calculate it use:
Math.sqrt(c.getRealPart() * c.getRealPart() + c.getImPart() * c.getImPart());
From what I understand you just want the real part of the complex value. If that's the case, presumably your Complex class also has getReal() and getImaginary() (or similar) methods - so just use getReal().
Related
I tried to calculate Tr(x) operation for x coordinate of the elliptical curve F2m (m = 163). For that, I used "Bouncy Castle" with corresponding types. Trace for my elliptical curve is equal to either 0 or 1 and my code is the following:
public int CalculateTrace_Test(byte[] array)
{
int m = 163;
BigInteger two = new BigInteger("2", 10);
BigInteger x = new BigInteger(array);
BigInteger xi = x;
BigInteger temp = x;
for (int i = 1; i < m; i++)
{
var next = xi.ModPow(two.Pow(i), fx);
temp = temp.Xor(next);
}
return temp.IntValue;
}
Here fx is an integer formed from the irreducible polynomial f(x) = x^163+x^7+x^6+x^3 + 1.
So my problem that it doesn't work and as result, I have everything but not 1 or 0. Could anyone please tell me what is wrong in my implementation of the trace?
It doesn't look like you are properly doing field arithmetic in GF(2m). The classes that support correct field arithmetic are in the package org.bouncycastle.math.ec. Take a look at ECFieldElement.F2m and ECCurve.F2m. Also, for your specific case which corresponds to the SECT163 reduction polynomial, the class SecT163FieldElement may be particularly useful.
Here some code copied directly from the class org.bouncycastle.math.ec.tools.TraceOptimizer. The code assumes the the finite field is of characteristic 2.
private static int calculateTrace(ECFieldElement fe) {
int m = fe.getFieldSize();
ECFieldElement tr = fe;
for (int i = 1; i < m; ++i) {
fe = fe.square();
tr = tr.add(fe);
}
BigInteger b = tr.toBigInteger();
if (b.bitLength() > 1) {
throw new IllegalStateException();
}
return b.intValue();
I am having problems with the logic behind this multiplication method. It is using a 1D array of LongNumber. We can't use the Math class, must write our own logic. The two previous methods were addition and subtraction, I am having issues with how to setup the for loop in order to move one num index as we stay at the index of the other, and then changing to the next index of the number that wasn't moved.
This logic has been more confusing for me, addition and subtraction were all in once simple for loop and it was easy, with multiplication I cant figure out how to setup the loop so it only moves num index and not num2, so we can get the correct product.
public static Long_Number multiply(String num,String num2)
{
//data
String resultString3 = "";
Long_Number result3 = new Long_Number("");
int total, carry = 0, temp;
int v1 = 0, v2 = 0;
//set longest length to indexrange
int indexrange = num.length();
if(num2.length()>indexrange)
indexrange = num2.length();
//logic
for(int i = 0; i < indexrange; i++)
{
if(num.length()-1-i >= 0)
v1 = (num.charAt(num.length()-1-i)-'0');
else
v1 = 0;
if(num2.length()-1-i >= 0)
v2 = (num2.charAt(num2.length()-1-i)-'0');
else
v2 = 0;
sumofdigits = v1 * v2 + carry;
carry = sumofdigits % 10;
System.out.println(sumofdigits + "hi" + carry); //test print
}
result3.setNumber(resultString3);
return result3;
}
I have a list of 10 probabilities (assume these are sorted in descending order): <p1, p2, ..., p10>. I want to sample (without replacement) 10 elements such that the probability of selecting i-th index is p_i.
Is there a ready to use Java method in common libraries like Random, etc that I could use to do that?
Example: 5-element list: <0.4,0.3,0.2,0.1,0.0>
Select 5 indexes (no duplicates) such that their probability of selection is given by the probability at that index in the list above. So index 0 would be selected with probability 0.4, index 1 selected with prob 0.3 and so on.
I have written my own method to do that but feel that an existing method would be better to use. If you are aware of such a method, please let me know.
This is how this is typically done:
static int sample(double[] pdf) {
// Transform your probabilities into a cumulative distribution
double[] cdf = new double[pdf.length];
cdf[0] = pdf[0];
for(int i = 1; i < pdf.length; i++)
cdf[i] += pdf[i] + cdf[i-1];
// Let r be a probability [0,1]
double r = Math.random();
// Search the bin corresponding to that quantile
int k = Arrays.binarySearch(cdf, random.nextDouble());
k = k >= 0 ? k : (-k-1);
return k;
}
If you want to return a probability do:
return pdf[k];
EDIT: I just noticed you say in the title sampling without replacement. This is not so trivial to do fast (I can give you some code I have for that). Anyhow, your question does not make any sense in that case. You cannot sample without replacement from a probability distribution. You need absolute frequencies.
i.e. If I tell you that I have a box filled with two balls: orange and blue with the proportions 20% and 80%. If you do not tell me how many balls you have of each (in absolute terms), I cannot tell you how many balls you will have in a few turns.
EDIT2: A faster version. This is not how it is typically but I have found this suggestion on the web, and I have used it in projects of mine as well.
static int sample(double[] pdf) {
double r = random.nextDouble();
for(int i = 0; i < pdf.length; i++) {
if(r < pdf[i])
return i;
r -= pdf[i];
}
return pdf.length-1; // should not happen
}
To test this:
// javac Test.java && java Test
import java.util.Arrays;
import java.util.Random;
class Test
{
static Random random = new Random();
public static void sample(double[] pdf) {
...
}
public static void main(String[] args) {
double[] pdf = new double[] { 0.3, 0.4, 0.2, 0.1 };
int[] counts = new int[pdf.length];
final int tests = 1000000;
for(int i = 0; i < tests; i++)
counts[sample(pdf)]++;
for(int i = 0; i < counts.length; i++)
System.out.println(counts[i] / (double)tests);
}
}
You can see we get output very similar to the PDF that was used:
0.3001356
0.399643
0.2001143
0.1001071
This are the times I get when running each version:
1st version: 0m0.680s
2nd version: 0m0.296s
Use sample[i] as index of your values array.
public static int[] WithoutReplacement(int m, int n) {
int[] perm = new int[n];
for (int i = 0; i < n; i++) {
perm[i] = i;
}
//take sample
for (int i = 0; i < m; i++) {
int r = i + (int) (Math.random() * (n - 1));
int tmp = perm[i];
perm[i] = perm[r];
perm[r] = tmp;
}
int[] sample = new int[m];
for (int i = 0; i < m; i++) {
sample[i] = perm[i];
}
return sample;
}
I want to translate a code from C++ to Java. The original code implements fast DTW algorithm. The piece of code I couldn't figure out was I attribute I'm not sure what it does hence, I can't convert it.
The error in Java is in statements l_buff+I & u_buff+I because the plus operator is not supported between int I & double[] l_buff,u_buff.
I have included all statements that involves I
int I;
for(i=0; i<ep; i++)
{
/// A bunch of data has been read and pick one of them at a time to use
d = buffer[i];
/// Calculate sum and sum square
ex += d;
ex2 += d*d;
/// t is a circular array for keeping current data
t[i%m] = d;
/// Double the size for avoiding using modulo "%" operator
t[(i%m)+m] = d;
/// Start the task when there are more than m-1 points in the current chunk
if( i >= m-1 )
{
mean = ex/m;
std = ex2/m;
std = Math.sqrt(std-mean*mean);
/// compute the start location of the data in the current circular array, t
j = (i+1)%m;
/// the start location of the data in the current chunk
I = i-(m-1);
lb_k2 = lb_keogh_data_cumulative(order, tz, qo, cb2, l_buff+I, u_buff+I, m, mean, std, bsf);
and the lb_data_cumlative method implementation is
public static double lb_keogh_data_cumulative(int[] order, double []tz, double []qo, double []cb, double []l, double []u, int len, double mean, double std, double best_so_far )
{
double lb = 0;
double uu,ll,d;
for (int i = 0; i < len && lb < best_so_far; i++)
{
uu = (u[order[i]]-mean)/std;
ll = (l[order[i]]-mean)/std;
d = 0;
if (qo[i] > uu)
d = dist(qo[i], uu);
else
{
if(qo[i] < ll)
d = dist(qo[i], ll);
}
lb += d;
cb[order[i]] = d;
}
return lb;
}
here is the paper on which the code relies SIGKDD TRILLION
l_buff+I and u_buff+I mean that you shift the beginning of the arrays to I elements. The lb_keogh_data_cumulative parameters l and u won't see the first I elements of the given arrays.
So you could write something like
lb_k2 = lb_keogh_data_cumulative(order, tz, qo, cb2, Arrays.copyOfRange(l_buff, I, l_buff.length), Arrays.copyOfRange(u_buff, I, u_buff.length), m, mean, std, bsf);
The arrays are not modified by the called method so you can pass a copy.
I'm trying to calculate the Mean Difference average of a set of data. I have two (supposedly equivalent) formulas which calculate this, with one being more efficient (O^n) than the other (O^n2).
The problem is that while the inefficient formula gives correct output, the efficient one does not. Just by looking at both formulas I had a hunch that they weren't equivalent, but wrote it off because the derivation was made by a statician in a scientific journal. So i'm assuming the problem is my translation. Can anyone help me translate the efficient function properly?
Inefficient formula:
Inefficient formula translation (Java):
public static double calculateMeanDifference(ArrayList<Integer> valuesArrayList)
{
int valuesArrayListSize = valuesArrayList.size();
int sum = 0;
for(int i = 0; i < valuesArrayListSize; i++)
{
for(int j = 0; j < valuesArrayListSize; j++)
sum += (i != j ? Math.abs(valuesArrayList.get(i) - valuesArrayList.get(j)) : 0);
}
return new Double( (sum * 1.0)/ (valuesArrayListSize * (valuesArrayListSize - 1)));
}
Efficient derived formula:
where (sorry, don't know how to use MathML on here):
x(subscript i) = the ith order statistic of the data set
x(bar) = the mean of the data set
Efficient derived formula translation (Java):
public static double calculateMean(ArrayList<Integer> valuesArrayList)
{
double sum = 0;
int valuesArrayListSize = valuesArrayList.size();
for(int i = 0; i < valuesArrayListSize; i++)
sum += valuesArrayList.get(i);
return sum / (valuesArrayListSize * 1.0);
}
public static double calculateMeanDifference(ArrayList<Integer> valuesArrayList)
{
double sum = 0;
double mean = calculateMean(valuesArrayList);
int size = valuesArrayList.size();
double rightHandTerm = mean * size * (size + 1);
double denominator = (size * (size - 1)) / 2.0;
Collections.sort(valuesArrayList);
for(int i = 0; i < size; i++)
sum += (i * valuesArrayList.get(i) - rightHandTerm);
double meanDifference = (2 * sum) / denominator;
return meanDifference;
}
My data set consists of a collection of integers each having a value bounded by the set [0,5].
Randomly generating such sets and using the two functions on them gives different results. The inefficient one seems to be the one producing results in line with what is being measured: the absolute average difference between any two values in the set.
Can anyone tell me what's wrong with my translation?
EDIT: I created a simpler implementation that is O(N) provided the all your data has values limited to a relatively small set.The formula sticks to the methodology of the first method and thus, gives identical results to it (unlike the derived formula). If it fits your use case, I suggest people use this instead of the derived efficient formula, especially since the latter seems to give negative values when N is small).
Efficient, non-derived translation (Java):
public static double calculateMeanDifference3(ArrayList<Integer> valuesArrayList)
{
HashMap<Integer, Double> valueCountsHashMap = new HashMap<Integer, Double>();
double size = valuesArrayList.size();
for(int i = 0; i < size; i++)
{
int currentValue = valuesArrayList.get(i);
if(!valueCountsHashMap.containsKey(currentValue))
valueCountsHashMap.put(currentValue, new Double(1));
else
valueCountsHashMap.put(currentValue, valueCountsHashMap.get(currentValue)+ 1);
}
double sum = 0;
for(Map.Entry<Integer, Double> valueCountKeyValuePair : valueCountsHashMap.entrySet())
{
int currentValue = valueCountKeyValuePair.getKey();
Double currentCount = valueCountKeyValuePair.getValue();
for(Map.Entry<Integer, Double> valueCountKeyValuePair1 : valueCountsHashMap.entrySet())
{
int loopValue = valueCountKeyValuePair1.getKey();
Double loopCount = valueCountKeyValuePair1.getValue();
sum += (currentValue != loopValue ? Math.abs(currentValue - loopValue) * loopCount * currentCount : 0);
}
}
return new Double( sum/ (size * (size - 1)));
}
Your interpretation of sum += (i * valuesArrayList.get(i) - rightHandTerm); is wrong, it should be sum += i * valuesArrayList.get(i);, then after your for, double meanDifference = ((2 * sum) - rightHandTerm) / denominator;
Both equations yields about the same value, but they are not equal. Still, this should help you a little.
You subtract rightHandTerm on each iteration, so it gets [over]multiplied to N.
The big Sigma in the nominator touches only (i x_i), not the right hand term.
One more note: mean * size == sum. You don't have to divide sum by N and then remultiply it back.