Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. /**
  2. * complexData is overlapped 50%.
  3. * @param complexData
  4. * @return
  5. */
  6. private Double[] autoCorrelation(final Complex[] complexData) {
  7. Complex[] toFFT = doubleAndPad(complexData);
  8.  
  9. Double[] autoCorrelationAbsolute = new Double[toFFT.length];
  10.  
  11. FFT.fftForward(toFFT);
  12.  
  13. for(int j = 0; j < toFFT.length; j++) {
  14. //Same as Complex.mult(toFFT[j], toFFT[j].conjugate()) but simpler
  15. double square = toFFT[j].absoluteSquare();
  16. toFFT[j] = new Complex(square);
  17. }
  18.  
  19. //Effective inverse FFT
  20. //forward FFT == inverse FFT for real numbers.
  21. FFT.fftForward(toFFT);
  22.  
  23. for(int i = 0; i < toFFT.length; i++) {
  24. autoCorrelationAbsolute[i] = toFFT[i].absolute();
  25. }
  26. return autoCorrelationAbsolute;
  27. }
  28.  
  29. /**
  30. * Returns a copy of data with the size doubled and the
  31. * second half set to zero.
  32. * @param data
  33. * @return
  34. */
  35. private Complex[] doubleAndPad(Complex[] data) {
  36. Complex[] doubledData = Arrays.copyOf(data, data.length * 2);
  37. //Set the second half to zero
  38. for(int i = data.length; i < doubledData.length; i++) {
  39. doubledData[i] = new Complex(0);
  40. }
  41. return doubledData;
  42. }
  43.  
  44. /**
  45. * Computes the frequency based off of the padded FFT in autoCorrelation
  46. * @param bin
  47. * @param data
  48. * @return
  49. */
  50. public static double computeFrequency(int bin, AudioData data) {
  51. return bin * data.getFormat().getSampleRate() / data.getFftLength();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement