1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DFT 9 { 10 class FFT 11 { 12 int row; 13 int col; 14 double[,] FM; 15 double[,] FN; 16 double[,] X; 17 double[,] Original; 18 public FFT(String name) 19 { 20 Bitmap bitmap = new Bitmap(name); 21 this.col = bitmap.Width; 22 this.row = bitmap.Height; 23 Original=new double[row,col]; 24 FM=new double[row,row]; 25 FN=new double[col,col]; 26 X=new double[row,col]; 27 for (int i = 0; i < row; i++) 28 { 29 for (int j = 0; j < col; j++) 30 { 31 Original[i, j] = (bitmap.GetPixel(j,i).R + bitmap.GetPixel(j,i).G + bitmap.GetPixel(j,i).B)/3; 32 } 33 } 34 initFMN(); 35 36 } 37 public double[,] getFFT() 38 { 39 X = martiply(martiply(FM, Original), T(FN)); 40 return X; 41 } 42 43 public double[,] getIFFT() 44 { 45 return martiply(martiply(T(FM), X), FN); 46 } 47 private double[,] T(double[,] a) 48 { 49 int awidth = a.GetLength(0); 50 int aheight = a.GetLength(1); 51 double[,] reutrnT = new double[aheight, awidth]; 52 for (int i = 0; i < awidth; i++) 53 { 54 for (int j = 0; j < aheight; j++) 55 { 56 reutrnT[j, i] = a[i, j]; 57 } 58 } 59 return reutrnT; 60 } 61 private double[,] martiply(double[,]a,double[,]b) 62 { 63 int awidth = a.GetLength(0); 64 int aheight = a.GetLength(1); 65 int bheight = b.GetLength(1); 66 double[,] returnMaxtrix = new double[awidth, bheight]; 67 for (int i = 0; i < awidth; i++) 68 { 69 for (int j = 0; j < bheight; j++) 70 { 71 for (int k = 0; k < aheight; k++) 72 { 73 returnMaxtrix[i, j] += a[i, k] * b[k, j]; 74 } 75 } 76 } 77 return returnMaxtrix; 78 } 79 private void initFMN() 80 { 81 double firstm = 1/Math.Sqrt(row); 82 double firstn = 1 / Math.Sqrt(col); 83 for (int i = 0; i < row; i++) 84 { 85 for (int j = 0; j < row; j++) 86 { 87 if (i == 0) 88 { 89 FM[i, j] = firstm; 90 } 91 else 92 { 93 FM[i, j] = Math.Sqrt(2.0 / row) * Math.Cos(i * j * Math.PI / row); 94 } 95 } 96 } 97 for (int i = 0; i < col; i++) 98 { 99 for (int j = 0; j < col; j++) 100 { 101 if (i == 0) 102 { 103 FN[i, j] = firstn; 104 } 105 else 106 { 107 FN[i, j] = Math.Sqrt(2.0 / col) * Math.Cos(i * j * Math.PI / col); 108 } 109 } 110 } 111 } 112 } 113 }
时间: 2024-10-11 06:10:17