アットウィキロゴ

Java > 計算機科学 > その他 > FFT

高速フーリエ変換

言語:Java
参照ライブラリ:Complex

  1. public class FFT {
  2. /**
  3. * 順変換。規格化は1/sqrt(n)
  4. * @param f 変換前
  5. * @return 変換後
  6. */
  7. public static Complex[] exec(Complex[] f){
  8. int n = f.length;
  9. Complex[] ret = new Complex[n];
  10. Complex[] w = new Complex[n];
  11. for(int i=0; i<n; i++){
  12. w[i] = new Complex(Math.cos(2.0*Math.PI*i/(double)n), Math.sin(2.0*Math.PI*i/(double)n));
  13. // System.out.println(w[i]);
  14. }
  15.  
  16. //ならびかえ
  17. for(int i=0; i<n; i++){
  18. int k = 0;
  19. for(int j=1; j<n; j*=2){
  20. k*=2;
  21. if((j&i)!=0) k+=1;
  22. }
  23. ret[i] = f[k];
  24. }
  25.  
  26. Complex[] tmp = new Complex[n];
  27. for(int i=1; i<n; i*=2){
  28. for(int j=0; j<n; j+=(i*2)){
  29. for(int k=j; k<i+j; k++){
  30. tmp[k] = ret[k].add( ret[k+i].mul(w[(n/i/2)*(k-j)]) );
  31. tmp[k+i] = ret[k].add( ret[k+i].mul(w[(n/i/2)*(k-j)+(n/2)]) );
  32. }
  33. }
  34. for(int j=0; j<n; j++){
  35. ret[j] = tmp[j];
  36. }
  37. }
  38.  
  39. for(int i=0; i<n; i++){
  40. ret[i].muleq(1.0/Math.sqrt(n));
  41. }
  42. return ret;
  43. }
  44.  
  45. /**
  46. * 逆変換。規格化は1/sqrt(n)
  47. * @param f 変換前
  48. * @return 変換後
  49. */
  50. public static Complex[] inv(Complex[] f){
  51. int n = f.length;
  52. Complex[] ret = new Complex[n];
  53. Complex[] w = new Complex[n];
  54. for(int i=0; i<n; i++){
  55. w[i] = new Complex(Math.cos(2.0*Math.PI*(-i)/(double)n), Math.sin(2.0*Math.PI*(-i)/(double)n));
  56. }
  57.  
  58. //ならびかえ
  59. for(int i=0; i<n; i++){
  60. int k = 0;
  61. for(int j=1; j<n; j*=2){
  62. k*=2;
  63. if((j&i)!=0) k+=1;
  64. }
  65. ret[i] = f[k];
  66. }
  67.  
  68. Complex[] tmp = new Complex[n];
  69. for(int i=1; i<n; i*=2){
  70. for(int j=0; j<n; j+=(i*2)){
  71. for(int k=j; k<i+j; k++){
  72. tmp[k] = ret[k].add( ret[k+i].mul(w[(n/i/2)*(k-j)]) );
  73. tmp[k+i] = ret[k].add( ret[k+i].mul(w[(n/i/2)*(k-j)+(n/2)]) );
  74. }
  75. }
  76. for(int j=0; j<n; j++){
  77. ret[j] = tmp[j];
  78. }
  79. }
  80.  
  81. for(int i=0; i<n; i++){
  82. ret[i].muleq(1.0/Math.sqrt(n));
  83. }
  84. return ret;
  85. }
  86.  
  87. }
  88.  
  89.  

タグ:

+ タグ編集
  • タグ:
最終更新:2011年06月04日 21:18