public void DynamicParameters(List <double> d, WindowBase window, double fs, double f0, double fmin, double fmax, bool remove_dc, out double snr, out double sndr, out double enob) { double[] dd = new double[d.Count]; double dc_offset = 0; if (remove_dc) { dc_offset = ArrayMath.Sum(d.ToArray()) / d.Count; } if (dc_offset != 0) { for (int i = 0; i < d.Count; i++) { dd[i] = d[i] - dc_offset; } } Complex[] spec = DoFFT(dd, window); snr = SignalNoise(spec, dd.Length, fs, f0, 6, 3, fmin, fmax); sndr = SignalNoise(spec, dd.Length, fs, f0, 0, 3, fmin, fmax); enob = (sndr - 1.76) / 6.02; }
public double[] PowerSpectralDensity(double[] d, WindowBase window, double fs, double maxSignalAmpl) { Complex[] spec = DoFFT(d, window); double[] y = new double[spec.Length]; double max = Double.MinValue; for (int i = 0; i < spec.Length; i++) { y[i] = 20 * Math.Log10(spec[i].Abs); if (y[i] > max) { max = y[i]; } } if (double.NaN.CompareTo(maxSignalAmpl) == 0) { ArrayMath.Add(ref y, -max); } else { max = 20 * Math.Log10(maxSignalAmpl / 2); ArrayMath.Add(ref y, -max); } return(y); }
public List <ChartData> Plot(List <double> d) { if (!visible) { return(new List <ChartData>()); } dc_offset = 0; if (remove_dc) { dc_offset = ArrayMath.Sum(d.ToArray()) / d.Count; } FFT fft = new FFT(); ChartData cd; List <double> dd = new List <double>(); foreach (double da in d) { dd.Add(da - dc_offset); } List <ChartData> cds = new List <ChartData>(); fft.PowerSpectralDensity(dd.ToArray(), out cd, new Hanning(), fs, maxamplitude); cd.Title = this.Title; cd.ShowFullNameLegend = false; cds.Add(cd); return(cds); }
/* * The point on the Y - axis where the line Y = A + BX intercepts it is given by the equation: * A = [(SUM(Y))*(SUM(SQUARE(X))) - (SUM(X))*(SUM(X*Y))] / [N*SUM(SQUARE(X)) - SQUARE((SUM(X)))]. * * * * The slope of the line Y = A + BX is given by the equation: * B = N*SUM(X*Y) - (SUM(X))*(SUM(Y)) / [N*SUM(SQUARE(X)) - SQUARE((SUM(X))] * * * * The correlation coefficient of the line Y = A + BX where 0 means no correlation and 1 means perfect * correlation is given by the equation: * R = N*SUM(X*Y) - (SUM(X))*(SUM(Y)) / SQRT[N*SUM(SQUARE(X)) - SQUARE(SUM(X))]*SQRT[N*SUM(SQUARE(Y)) - SQUARE((SUM(Y)))] */ public void Coefficients(out double a, out double b, out double r) { double x2sum = ArrayMath.Sum(ArrayMath.Pow(X, 2)); double xsum2 = Math.Pow(ArrayMath.Sum(X), 2); double ysum = ArrayMath.Sum(Y); double y2sum = ArrayMath.Sum(ArrayMath.Pow(Y, 2)); double ysum2 = Math.Pow(ArrayMath.Sum(Y), 2); double xsum = ArrayMath.Sum(X); double xysum = ArrayMath.Sum(ArrayMath.Multiply(X, Y)); a = (ysum * x2sum - xsum * xysum) / (N * x2sum - xsum2); b = (N * xysum - xsum * ysum) / (N * x2sum - xsum2); r = (N * xysum - xsum * ysum) / (Math.Sqrt(N * x2sum - xsum2) * Math.Sqrt(N * y2sum - ysum2)); }
public Complex[] DoFFT(double[] d, WindowBase window) { double M = d.Length; double[] dwindow = window.Calc((int)M); ArrayMath.Multiply(ref d, ref dwindow); Complex[] fft = Run(d); Complex[] spec = new Complex[fft.Length / 2]; for (int i = 1; i < fft.Length / 2 + 1; i++) { spec[i - 1].Real = fft[i].Real * 2 / M; spec[i - 1].Imag = fft[i].Imag * 2 / M; } return(spec); }
public LinearRegression(double[] X, double[] Y) { N = ArrayMath.MakeEqualLength(ref X, ref Y); this.X = X; this.Y = Y; }