/* * Computes the discrete Fourier transform (DFT) of the given vector. * All the array arguments must have the same length. */ public static ImaginaryNumber[] computeDft(ImaginaryNumber[] signal) { int n = signal.Length; ImaginaryNumber[] dftResult = new ImaginaryNumber[n]; for(int i = 0; i < dftResult.Length; i++) { dftResult[i] = new ImaginaryNumber(0, 0); } for (int k = 0; k < n; k++) { // For each output element double sumreal = 0; double sumimag = 0; for (int t = 0; t < n; t++) { // For each input element double angle = 2 * Math.PI * t * k / n; sumreal += signal[t].Real * Math.Cos(angle) + signal[t].Img * Math.Sin(angle); sumimag += -signal[t].Real * Math.Sin(angle) + signal[t].Img * Math.Cos(angle); } dftResult[k].Real = (float)sumreal; dftResult[k].Img = (float)sumimag; } return dftResult; }
static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines(@"C:\Users\IngMarIntern\Desktop\Signal_IN.txt"); StreamWriter FrequencyRFileWriter = new StreamWriter("C:\\Users\\IngMarIntern\\Desktop\\Freq_R.txt", false); StreamWriter FrequencyIFileWriter = new StreamWriter("C:\\Users\\IngMarIntern\\Desktop\\Freq_I.txt", false); ImaginaryNumber[] signalValues = new ImaginaryNumber[lines.Length/2]; ImaginaryNumber[] dftValues = new ImaginaryNumber[lines.Length/2]; for(int i = 0; i < lines.Length/2; i++) { signalValues[i] = new ImaginaryNumber((float)double.Parse(lines[i]), 0); } dftValues = DFTCalculator.computeDft(signalValues); foreach(ImaginaryNumber i in dftValues) { FrequencyRFileWriter.WriteLine(i.Real.ToString()); FrequencyIFileWriter.WriteLine(i.Img.ToString()); } }