//line = N/2.56 /// <summary> /// range of f : -Line*df ~ Line*df where df = Fs/length /// </summary> /// <param name="f">frequency 배열. length: 2*line + 1</param> /// <param name="y">스펙트럼값 배열. length: 2*line + 1</param> /// <param name="Fs"></param> public static void FullSpectrum_RMS(data_type[] x1, data_type[] x2, double[] f, double[] y, double Fs, Unit2Type Unit2Type, WindowFunction windowFunc)//length = 2^m, the length of h and x = length, the length of f and y = length/2 { int length = x1.Length; int i; double sum, rms; int k1, k2, N, half_length; var XH = new Complex[65536]; double scale_rms_vec; int line, count; var h = GetWindow(windowFunc, length); line = (int)Math.Round((double)length / 2.56); if (length != length_saved) { df = Fs / ((double)length); scale = 0.0; for (i = 0; i < length; i++) { scale = scale + h[i] * h[i]; } scale = ((double)length) * scale; scale = 1.0 / scale; length_saved = length; } // k1 = floor(f1/df) - 1; // k2 = ceil(f2/df) + 1; half_length = length >> 1; // if(k1 < 0) // k1 = 0; // else if(k1 > half_length) // k1 = half_length; // // if(k2 < 0) // k2 = 0; // else if(k2 > half_length) // k2 = half_length; for (i = 0; i < length; i++) { XH[i].Real = (data_type)(h[i] * x1[i]); XH[i].Imag = (data_type)(h[i] * x2[i]); } N = (int)Math.Round(Math.Log10((double)length) / Math.Log10(2.0)); FFT(XH, N); //RMS scalar generation // sum = 0.0; // for(i = k1; i <= k2; i++) // { // sum = sum + XH[i].real*XH[i].real + XH[i].imag*XH[i].imag; // } // // rms = sqrt(scale*2.0*sum); //RMS vector generation scale = 0.0; for (i = 0; i < length; i++) { scale = scale + h[i]; } scale = 1.0 / 1.414214 / (scale); switch (Unit2Type) { case Unit2Type.pk: scale = scale * 1.414213; break; case Unit2Type.pp: scale = scale * 2.828427; break; default: break; } count = 0; for (i = -line; i <= line; i++) { f[count] = df * ((double)i); count++; } count = 0; for (i = length - line; i <= length - 1; i++) { y[count] = scale * Math.Sqrt(XH[i].Real * XH[i].Real + XH[i].Imag * XH[i].Imag); count++; } for (i = 0; i <= line; i++) { y[count] = scale * Math.Sqrt(XH[i].Real * XH[i].Real + XH[i].Imag * XH[i].Imag); count++; } }
//used in DspAnalyzer public static double BPF_RMS(data_type[] x, data_type[] f, data_type[] y, double f1, double f2, double Fs, Unit2Type?unit2Type = null) //length = 2^m, the length of h and x = length, the length of f and y = length/2 { int length = x.Length; int i; double df, sum, rms; int k1, k2, N, half_length; var XH = new Complex[65536]; double scale; var h = Hann(x.Length); df = Fs / ((double)length); k1 = (int)Math.Floor(f1 / df) - 1; k2 = (int)Math.Ceiling(f2 / df) + 1; half_length = length >> 1; scale = 0.0; for (i = 0; i < length; i++) { scale = scale + h[i] * h[i]; } scale = ((double)length) * scale; scale = 1.0 / scale; if (k1 < 0) { k1 = 0; } else if (k1 > half_length) { k1 = half_length; } if (k2 < 0) { k2 = 0; } else if (k2 > half_length) { k2 = half_length; } for (i = 0; i < length; i++) { XH[i].Real = (data_type)(h[i] * x[i]); XH[i].Imag = 0.0f; } N = (int)Math.Round(Math.Log10((double)length) / Math.Log10(2.0)); FFT(XH, N); //RMS scalar generation sum = 0.0; for (i = k1; i <= k2; i++) { sum = sum + XH[i].Real * XH[i].Real + XH[i].Imag * XH[i].Imag; } rms = Math.Sqrt(scale * 2.0 * sum); //RMS vector generation scale = 0.0; for (i = 0; i < length; i++) { scale = scale + h[i]; } scale = 1.414214 / scale; //*((double)length); if (unit2Type.HasValue) { switch (unit2Type.Value) { case Unit2Type.pk: scale = scale * 1.414213; break; case Unit2Type.pp: scale = scale * 2.828427; break; default: break; } } for (i = 0; i < half_length; i++) { f[i] = (data_type)(df * ((double)i)); y[i] = (data_type)(scale * Math.Sqrt(XH[i].Real * XH[i].Real + XH[i].Imag * XH[i].Imag)); } RMS = rms; return(rms); }