public static double[] getFFTResult(int[] data, int dx, int len = 1024) { double[] result = new double[len]; double[] resulti = new double[len]; for (int i = 0; i < len; i++) { int index = i + dx; if (index >= 0 && index < data.Length) { result[i] = data[index]; } else { result[i] = 0; } resulti[i] = 0; } result = addHanning(result); TWFFT.FFT(result, resulti); double[] result_len = new double[len]; for (int i = 0; i < len; i++) { result_len[i] = Math.Sqrt(result[i] * result[i] + resulti[i] * resulti[i]); } return(result); }
/// <summary> /// 用倒谱获得频谱包络 /// </summary> /// <param name="data"></param> /// <returns></returns> private static double[] GetEnvelopeFD(double[] data) { int n = data.Length; double[] data1 = new double[n]; double[] data1i = new double[n]; for (int i = 0; i < n; i++) { data1[i] = Math.Log(data[i]); } for (int i = 0; i < n; i++) { data1i[i] = 0; } TWFFT.FFT(data1, data1i); double therehold = 100; double[] data2 = new double[n]; double[] data2i = new double[n]; // 低通滤波 for (int i = 0; i < n; i++) { if (i < therehold || i > n - therehold) { data2[i] = data1[i]; data2i[i] = data1i[i]; } else { data2[i] = 0; data2i[i] = 0; } } //for (int i = 0; i < n; i++) data2i[i] = 0; TWFFT.IFFT(data2, data2i); var res = FToneAnalysis.MiddleFilter(data2, 3); for (int i = 0; i < n; i++) { res[i] = Math.Pow(Math.E, res[i]); } return(res); }
private ArrayList FFTList(ArrayList lfft) { float[] resource = new float[max]; int x = 0; foreach (string fft in lfft) { if (x == max) { break; } float f = float.Parse(fft); resource[x] = f; x++; } ArrayList time_out = TWFFT.getArrayWithDivision(512.0f, 0.0f, 1024.0f); ArrayList result = TWFFT.FFTMagic(time_out, TWFFT.FFTAbs(TWFFT.FFT(resource, getZero())), 1025); return(result); }
public static Image getFFTImage(int[] data) { int w = 2000; int h = 1000; Bitmap bitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //draw fft int n = 4096; double[] datas = new double[n]; double[] datas2 = new double[n]; double[] datamod = new double[n]; //if (data.Length <= n) return null; for (int i = 0; i < n; i++) { datas2[i] = 0; if (i > data.Length - 1) { datas[i] = 0; } else { datas[i] = data[i]; } } TWFFT.FFT(datas, datas2); for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(datas[i] * datas[i] + datas2[i] * datas2[i]); } int time = 2; for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Black, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } double[] datad = new double[n]; double[] datad2 = new double[n]; for (int i = 0; i < n; i++) { datad2[i] = 0; } for (int i = 0; i < n; i++) { datad[i] = Math.Log(datamod[i]); } for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Red, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datad[i - 1] * 50)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datad[i] * 50))); } TWFFT.FFT(datad, datad2); for (int i = 0; i < n; i++) { datamod[i] = Math.Log(Math.Sqrt(datad[i] * datad[i] + datad2[i] * datad2[i])); } double therehold = 100; double[] rdata = new double[n]; double[] rdata2 = new double[n]; for (int i = 0; i < n; i++) { if (i < therehold || i > n - therehold) { rdata[i] = datad[i]; } else { rdata[i] = 0; } } for (int i = 0; i < n; i++) { rdata2[i] = datad2[i]; } TWFFT.IFFT(rdata, rdata2); for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Blue, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - rdata[i - 1] * 50)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - rdata[i] * 50))); } return(bitmap); }
public static Image getDFFTImage(int[] data, int beginx, int len, double pitch = 1) { int w = 2000; int h = 1000; Bitmap bitmap = new Bitmap(w, h); Graphics g = Graphics.FromImage(bitmap); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //draw fft int n = 4096; double[] datas = new double[n]; double[] datasi = new double[n]; double[] datamod = new double[n]; //if (data.Length <= n) return null; for (int i = 0; i < n; i++) { datasi[i] = 0; if (i > data.Length - 1) { datas[i] = 0; } else { datas[i] = data[i]; } } TWFFT.FFT(datas, datasi); for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(datas[i] * datas[i] + datasi[i] * datasi[i]); } var ddataenv = GetEnvelopeFD(datamod); int time = 2; for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Black, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } double[] ddatabase = new double[n]; double[] ddataibase = new double[n]; for (int i = 0; i < n; i++) { ddatabase[i] = datas[i] / ddataenv[i]; } for (int i = 0; i < n; i++) { ddataibase[i] = datasi[i] / ddataenv[i]; } double[] ddatabaser = new double[n]; double[] ddataibaser = new double[n]; // 对称共轭,所以分两半分别压缩 ddatabaser[0] = datas[0]; ddataibaser[0] = datasi[0]; pitch = 2.4; for (int i = 1; i <= n / 2; i++) { ddatabaser[i] = getResampleValue(ddatabase, (double)i / pitch); ddatabaser[n - i] = ddatabaser[i]; ddataibaser[i] = getResampleValue(ddataibase, (double)i / pitch); ddataibaser[n - i] = -ddataibaser[i]; } for (int i = 0; i < n; i++) { ddatabaser[i] = ddatabaser[i] * ddataenv[i]; } for (int i = 0; i < n; i++) { ddataibaser[i] = ddataibaser[i] * ddataenv[i]; } for (int i = 0; i < n; i++) { datamod[i] = Math.Sqrt(ddatabaser[i] * ddatabaser[i] + ddataibaser[i] * ddataibaser[i]); } for (int i = 1; i < n / time; i++) { g.DrawLine(new Pen(Color.Red, 1), new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)), new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w))); } return(bitmap); }