Im trying to implement something like discribed here and here, Specifically i want to be able to perform the following operation as in the following image :
That is, given N discrete points with constant time interval, i want to create a function that converges to those points as in the image...
So far what i did was :
imported jtransform
used it
private double[] doDFT(double[] data, int start, int end) {
DoubleFFT_1D doubleFFT_1D = new DoubleFFT_1D(end-start);
double[] array = new double[(end-start)*2];
for (int i=0;i<end-start;i++) {
array[i] = data[i+start];
array[i+1] = data[i+start+1];
}
doubleFFT_1D.complexForward(array);
return array;
}
and Now im stuck, how do i use the output array to produce the function that converges to the points in the original data array?
Just to clearify what i want : for example in the image the data array that is inputted to doDFT is the blue line plot, and what i want is to produce a function f that its image is the red line plot.
You probably want to set the imaginary component of your complex input to zero, not to the next point.
The functions you want are sinusoids. Each sinusoid will have a frequency of an FFT result bin index * Fs/N. The magnitude and phase of each sinusoid will be given by the complex value corresponding to its FFT result bin.
You can sum an increasing number of these sinusoids, starting with 1, to get your converging waveforms.
Related
I'm trying to get the most representative frequency (or first harmonic) from an audio file using the Noise FFT library (https://github.com/paramsen/noise). I have an array with the values of size x and the output array's size is x+2. I'm not familiar with Fourier Transform, so maybe I'm missing something, but from my understanding I should have something that represents the frequencies and stores the magnitude (or in this case a complex number from with to calculate it) of each one.
The thing is: since each position in the array should be a frequency, how can I know the range of the output frequencies, what frequency is each position or something like that?
Edit: This is part of the code I'm using
float[] mono = new float[size];
// I fill the array with the appropiate values
Noise noise = Noise.real(size);
float[] dst = new float[size + 2];
float[] fft = noise.fft(mono, dst);
// The result array has the pairs of real+imaginary floats in a one dimensional array; even indices
// are real, odd indices are imaginary. DC bin is located at index 0, 1, nyquist at index n-2, n-1
double greatest = 0;
int greatestIdx = 0;
for(int i = 0; i < fft.length / 2; i++) {
float real = fft[i * 2];
float imaginary = fft[i * 2 + 1];
double magnitude = Math.sqrt(real*real+imaginary*imaginary);
if (magnitude > greatest) {
greatest = magnitude;
greatestIdx = i;
}
System.out.printf("index: %d, real: %.5f, imaginary: %.5f\n", i, real, imaginary);
}
I just noticed something I had overlooked. When reading the comment just before the for loop (which is from the sample code provided in GitHub) it says that nyquist is located at the last pair of values of the array. From what I searched, nyquist is 22050Hz, so... To know the frequency corresponding to greatestIdx I should map the range [0,size+2] to the range [0,22050] and calculate the new value? It seems like a pretty unprecise measure.
Taking the prior things into account, maybe I should use another library for more precision? If that is the case, what would be one that let me specify the output frequency range or that gives me approximately the human hearing range by default?
I believe that the answer to your question is here if I understand it correctly https://stackoverflow.com/a/4371627/9834835
To determine the frequency for each FFT bin you may use the formula
F = i * sample / nFFt
where:
i = the FFT index
sample = the sample rate
nFft = your FFT size
I have a very long array in a Java program (300 000+ unsorted integers) and need to calculate the minimum absolute difference between any two numbers inside the array, and display the absolute difference and the corresponding pair of numbers as an output. The whole calculation should happen very quickly.
I have the following code, which would usually work:
private static void calcMinAbsDiff(int[] inputArray)
{
Arrays.sort(inputArray);
int minimum = Math.abs(inputArray[1] - inputArray[0]);
int firstElement = inputArray[0];
int secondElement = inputArray[1];
for (int i = 2; i < inputArray.length; i++)
{
if(Math.abs(inputArray[i] - inputArray[i-1]) < minimum)
{
minimum = Math.abs(inputArray[i] - inputArray[i-1]);
firstElement = inputArray[i-1];
secondElement = inputArray[i];
}
}
System.out.println("Minimum Absolute Difference : "+minimum);
System.out.println("Pair of Elements : ("+firstElement+", "+secondElement+")");
}
However, the output I receive is all 0s. I believe this is because the array is way too long.
If you have two or more zeros and no negative integers in your dataset, then your output is expected. After sorting, then inputArray[0] and inputArray[1] would both be 0, and the difference would be 0. No other pair of adjacent elements would have an absolute difference less than 0, so minimum, firstElement, and second Element would all be 0 at the end of the algorithm.
If you really have no zeros in your dataset, or if you do have negative integers, then you may have an initialization problem. Check this thread:
Why is my simple Array only printing zeros in java?
If that's not it, then only other thing I can think of is that you have a problem in the previous scope causing the data to get zeroed out.
I would try printing samples of your dataset at various points to see exactly where/when it's getting zeroed.
If you still have trouble, then post more info on the dataset and the scope which calls this function to help us see what's going on. Let us know how you make out!
I wanted to evaluate a certain sum needed for a monte carlo method in N dimensions. In N dimensions I have N variables with M data points, meaning that I basically have M N-D vectors representing M sampling points. I have to hold one variable fixed while I sum over all other variables for M values. Currently my code is (assuming my mesh which has N columns and M rows is static):
public static double myFunction(int row, int col){
double checkMesh = mesh[row][col];
return myFunctionHelper(0,col,checkMesh);
}
public static double myFunctionHelper(int j, int col, double checkMesh){
//function is initialized with j=0
if (j >= mesh[0].length){
return 0;
}
double sum = 0;
for (int i=0 ; i<mesh.length ; i++){
sum += myFunctionHelper(j+1,col,checkMesh);
if (j==mesh[0].length-1){
if (mesh[0][col]==checkMesh){
sum += function(mesh[0]);
}
mesh = rotate(mesh,j);
}
}
if (j>=1){
mesh = rotate(mesh,j-1);
}
return sum;
}
Now this function works but it doesn't hold any variables fixed, instead it just sums over all possible combinations of variables. I was wondering if anyone had any suggestions on how I can alter this to make it work as I want it to. Currently the only workaround that I can think of is to remove the fixed column from my matrix, sum over the combinations of the altered matrix, and when the function gets evaluated make sure the fixed variable is included in the right spot of an altered list, except I want to avoid doing this as it requires more memory and isn't as clean. I'd appreciate the help :).
EDIT: The rotate function takes the mesh and rotates the j-th column by one vertically upwards movement. Also function takes a vector as an argument, where the vector is represented by an array.
(1) You are computing the sum recursively, through myFunctionHelper calling myFunctionHelper. This may work, but it makes the code more involved than necessary. Recursion is not really not suited for this kind of numeric computation. Rewrite your algorithm without recursion, as a double loop over row and columnd indices, and things will get much clearer.
(2) With mesh[0][col]==checkMesh you try to identify the fixed entry by value. Why not by column and row index?
I have a question.
I've made a Pascal Triangle Generator program in Java using 2-D arrays and it was generally simple.
However, I can't seem to figure out how to program the same thing using only a 1-D array.
Does anyone have any suggestions? I will try to insert code once when I can, but I'm asking this in a general sense; what should you think about when converting 2-D arrays to 1-D array?
Thanks.
All the answers posted thus far have given cookie-cutter solutions to turning a 2D array into a 1D array. As far as I can tell, storing a Pascal's triangle in a statically dimensioned 2D array is pretty inefficient since around half the entries are unused.
The main issue is that the triangle is ... well, triangular, whereas a rectangular 2D matrix is relatively easy to unroll.
However, a triangle is still quite simple to unroll. You just need to figure out where each offset is. Consider storing your pascal's triangle as follows in a 1-D array:
1,1,1,1,2,1,1,3,3,1
Consider the ith row in the Pascal's triangle. Its first entry is in the (1+2+...+i-1)th entry in the 1-dimensional array. This is a simple arithmetic progression sum which evaluates to (i-1)(i)/2. So, write some functions like:
int getArrayOffset(int row, int offset) {
// assert(row>0);
return (row*(row-1))/2 + offset - 1;
}
int calculateEntry(int row, int offset, int[] triangle) {
triangle[getArrayOffset(row,offset)] = triangle[getArrayOffset(row-1,offset-1)]+triangle[getArrayOffset(row-1,offset)];
}
void calculatePascal(int n) {
int [] triangle = new int[getArrayOffset(n+1,1)];
for (int row=1; row <=n; row++) {
triangle[getArrayOffset(row,1)]=1;
triangle[getArrayOffset(row,row)]=1;
for (int offset=2; offset < row; offset++) {
calculateEntry(row,offset,triangle);
}
}
}
You just have to replace all accesse operation to your array from data[x][y] to data[x+y*width] or data[x*height+y].
If you have only given the index of a field and you want to calculate the coordinates that would be
x=index%width;
y=index/width; //be sure to use integer division here
or
x=index/width; //be sure to use integer division here
y=index%width;
You can find out more about pairing functions here:
http://en.wikipedia.org/wiki/Cantor_pairing_function
I am not sure until I see your code, but generally -
you can try different addressing mode in your code.
2D case: array is mxn... Addressing goes [i,j]
same data can be stored in 1D, row after another / column wise. Lets say it was row wise. Then the addressing mode will be -[i*(n) + j]
where i=0..m & j=0..n
Cheers,
Rishikesh
Let's say that you have an arbitrarily large sized two-dimensional array with an even amount of items in it. Let's also assume for clarity that you can only choose between two things to put as a given item in the array. How would you go about putting a random choice at a given index in the array but once the array is filled you have an even split among the two choices?
If there are any answers with code, Java is preferred but other languages are fine as well.
You could basically think about it in the opposite way. Rather than deciding for a given index, which value to put in it, you could select n/2 elements from the array and place the first value in them. Then place the 2nd value in the other n/2.
A 2-D A[M,N] array can be mapped to a vector V[M*N] (you can use a row-major or a column-major order to do the mapping).
Start with a vector V[M*N]. Fill its first half with the first choice, and the second half of the array with the second choice object. Run a Fisher-Yates shuffle, and convert the shuffled array to a 2-D array. The array is now filled with elements that are evenly split among the two choices, and the choices at each particular index are random.
The below creates a List<T> the size of the area of the matrix, and fills it half with the first choice (spaces[0]) and half with the second (spaces[1]). Afterward, it applies a shuffle (namely Fisher-Yates, via Collections.shuffle) and begins to fill the matrix with these values.
static <T> void fill(final T[][] matrix, final T... space) {
final int w = matrix.length;
final int h = matrix[0].length;
final int area = w * h;
final List<T> sample = new ArrayList<T>(area);
final int half = area >> 1;
sample.addAll(Collections.nCopies(half, space[0]));
sample.addAll(Collections.nCopies(half, space[1]));
Collections.shuffle(sample);
final Iterator<T> cursor = sample.iterator();
for (int x = w - 1; x >= 0; --x) {
final T[] column = matrix[x];
for (int y = h - 1; y >= 0; --y) {
column[y] = cursor.next();
}
}
}
Pseudo-code:
int trues_remaining = size / 2;
int falses_remaining = size / 2;
while (trues_remaining + falses_remaining > 0)
{
if (trues_remaining > 0)
{
if (falses_remaining > 0)
array.push(getRandomBool());
else
array.push(true);
}
else
array.push(false);
}
Doesn't really scale to more than two values, though. How about:
assoc_array = { 1 = 4, 2 = 4, 3 = 4, 4 = 4 };
while (! assoc_array.isEmpty())
{
int index = rand(assoc_array.getNumberOfKeys());
int n = assoc_array.getKeyAtIndex(index);
array.push(n);
assoc_array[n]--;
if (assoc_array[n] <= 0) assoc_array.deleteKey(n);
}
EDIT: just noticed you asked for a two-dimensional array. Well it should be easy to adapt this approach to n-dimensional.
EDIT2: from your comment above, "school yard pick" is a great name for this.
It doesn't sound like your requirements for randomness are very strict, but I thought I'd contribute some more thoughts for anyone who may benefit from them.
You're basically asking for a pseudorandom binary sequence, and the most popular one I know of is the maximum length sequence. This uses a register of n bits along with a linear feedback shift register to define a periodic series of 1's and 0's that has a perfectly flat frequency spectrum. At least it is perfectly flat within certain bounds, determined by the sequence's period (2^n-1 bits).
What does that mean? Basically it means that the sequence is guaranteed to be maximally random across all shifts (and therefore frequencies) if its full length is used. When compared to an equal length sequence of numbers generated from a random number generator, it will contain MORE randomness per length than your typical randomly generated sequence.
It is for this reason that it is used to determine impulse functions in white noise analysis of systems, especially when experiment time is valuable and higher order cross effects are less important. Because the sequence is random relative to all shifts of itself, its auto-correlation is a perfect delta function (aside from qualifiers indicated above) so the stimulus does not contaminate the cross correlation between stimulus and response.
I don't really know what your application for this matrix is, but if it simply needs to "appear" random then this would do that very effectively. In terms of being balanced, 1's vs 0's, the sequence is guaranteed to have exactly one more 1 than 0. Therefore if you're trying to create a grid of 2^n, you would be guaranteed to get the correct result by tacking a 0 onto the end.
So an m-sequence is more random than anything you'll generate using a random number generator and it has a defined number of 0's and 1's. However, it doesn't allow for unqualified generation of 2d matrices of arbitrary size - only those where the total number of elements in the grid is a power of 2.