Reputation: 19
I have one signal y. The signal gets values from 0 to 1. The signal is actually derived from the red channel (from RGB) from a smartphone's camera, normalized by mapping all values from the space [0,1,... 255] to the space [0 - 1] by dividing by 255. I have another array x for storing the camera frames 0,1,2,3,... What I want to do is to convert the signal y from time domain to the frequency domain using Java. I have tried to use the class provided here.
My code is
fftsize = powtwo(y.size()); //keep samples of a size of power of 2
Float[] rp = new Float[fftsize]; //rp is real part of signal (= y)
for(int i=0; i<fftsize; i++){
rp[i]=y.get(i);
}
Float[] ip = new Float[fftsize]; //ip is the imaginary part of signal = 0
for(int i=0; i<fftsize;i++){
ip[i]=0.0f;
}
fft(rp,ip);
where
public int powtwo(int size){ //this function estimates the next power of 2 (below the given int value).
while(!(Math.sqrt((double)size)-(int)Math.sqrt((double)size) == 0)){
size--;
}
return size;
}
I'm not sure if I'm doing it right and I don't know the next steps for turning FFT to frequency domain signal.
Upvotes: 1
Views: 601
Reputation: 212929
You seem to have started off OK, apart from the fact that powtwo
is horribly inefficient. (See this question for various alternatives which are much more efficient.)
The FFT you are using is "in-place", so the frequency domain real/imaginary parts are returned in the input arrays. What you do with this data depends on your specific requirements, e.g. if you want to generate a power spectrum (one of the most common uses of an FFT) you would do something like this:
// ...
fft(rp,ip);
Float[] spectrum = new Float[fftsize / 2];
for (int i = 0; i < fftsize / 2; i++)
{
spectrum[i] = Math.sqrt(rp[i] * rp[i] + ip[i] * ip[i]);
}
Upvotes: 1