public static unsafe double[] IirCreateHpCheby(double cutoff, int order, double ripple) { IppStatus st; double[] coeff = new double[2 * (order + 1)]; int sz = 0; st = Ipps.ippsIIRGenGetBufferSize(order, ref sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("ippsIIRGenGetBufferSize error {0}", st)); } byte[] buf = new byte[sz]; fixed(byte *pbuf = &buf[0]) { st = Ipps.ippsIIRGenHighpass_64f(cutoff, ripple, order, coeff, IppsIIRFilterType.ippChebyshev1, pbuf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("IIRGenHighpass error {0}", st)); } } return(coeff); }
public static double[] IirFwdRev(double[] src, double[] coeff, int initCount) { //Filter double[] dst = Iir(src, coeff, initCount); double[] dst1 = new double[dst.Length]; IppStatus st; //Reverse st = Ipps.ippsFlip_64f(dst, dst1, dst.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Flip error {0}", st)); } //Filter again dst = Iir(dst1, coeff, initCount); //Reverse again st = Ipps.ippsFlip_64f(dst, dst1, dst.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Flip error {0}", st)); } //Final return(dst1); }
public static unsafe double[] Iir(double[] src, double[] coeff, int initCount) { int order = coeff.Length / 2 - 1; if (src.Length < 5) { throw new Exception("Invalid IIR src length"); } if (order < 1) { throw new Exception("Invalid IIR coeff length"); } double[] dst = new double[src.Length]; double[] delLine = new double[order]; IppStatus st = 0; //Get buffer size for filter int sz = 0; st = Ipps.ippsIIRGetStateSize_64f(order, ref sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("IIRGetStateSize error {0}", st)); } //Buffer for IIR state information. //This is used between calls to ipp and so must be fixed byte[] buf = new byte[sz]; fixed(byte *pbuf = &buf[0]) { IntPtr pstate = IntPtr.Zero; st = Ipps.ippsIIRInit_64f(ref pstate, coeff, order, delLine, pbuf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("IIRInit error {0}", st)); } //Initialize the filter to minimize settling for (int i = 0; i < initCount; ++i) { st = Ipps.ippsIIR_64f(src, dst, 1, pstate); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("IIR error {0}", st)); } } //Filter all the data st = Ipps.ippsIIR_64f(src, dst, src.Length, pstate); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("IIR error {0}", st)); } } return(dst); }
public unsafe static void FftFwd(double[] x, int order, out double[] re, out double[] im) { IppStatus st; int sz = 1 << order; if (sz > 1 << 23 || sz < 4) { throw new Exception("Invalid FftFwd order"); } double[] rw = new double[sz]; double[] iw = new double[sz]; double[] frw = new double[sz]; double[] fiw = new double[sz]; Array.Copy(x, rw, Math.Min(x.Length, rw.Length)); int specSz = 0; int specBufSz = 0; int bufSz = 0; st = Ipps.ippsFFTGetSize_C_64f(order, Ipps.IppFftFlag.IPP_FFT_DIV_FWD_BY_N, IppHintAlgorithm.ippAlgHintNone, ref specSz, ref specBufSz, ref bufSz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTGetSize returned error {0}", st)); } byte[] spec = new byte[specSz]; byte[] specBuf = new byte[specBufSz]; byte[] buf = new byte[bufSz]; IntPtr pspec = IntPtr.Zero; fixed(byte *ps = &spec[0]) { //Spec needs to be fixed as used between ipp calls st = Ipps.ippsFFTInit_C_64f(ref pspec, order, Ipps.IppFftFlag.IPP_FFT_DIV_FWD_BY_N, IppHintAlgorithm.ippAlgHintNone, ps, specBuf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTInit returned error {0}", st)); } st = Ipps.ippsFFTFwd_CToC_64f(rw, iw, frw, fiw, pspec, buf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTFwd returned error {0}", st)); } re = frw; im = fiw; } }
public static double[] FirCreateBsHamming(double lowCutoff, double highCutoff, int numTaps) { double[] taps = new double[numTaps]; IppStatus st = Ipps.ippsFIRGenBandstop_64f(lowCutoff, highCutoff, taps, numTaps, IppWinType.ippWinHamming, IppBool.ippTrue); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIRGenBandstop error {0}", st)); } return(taps); }
public static double[] FirCreateLpHamming(double cutoff, int numTaps) { double[] taps = new double[numTaps]; IppStatus st = Ipps.ippsFIRGenLowpass_64f(cutoff, taps, numTaps, IppWinType.ippWinHamming, IppBool.ippTrue); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIRGenLowpass error {0}", st)); } return(taps); }
public static double[] HanningWindow(int length) { double[] src = new double[length]; double[] dst = new double[length]; IppStatus st; st = Ipps.ippsSet_64f(1.0, src, src.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Set error {0}", st)); } st = Ipps.ippsWinHann_64f(src, dst, length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("WinHann error {0}", st)); } return(dst); }
public unsafe static double[] SpectrumBlockAverage(double[] x, int order, double percOverlap, bool power, bool zeroMean, double windowCorrection) { IppStatus st; int sz = 1 << order; if (sz > 1 << 23 || sz < 4) { throw new Exception("Invalid SpectrumBlockAverage order"); } if (percOverlap < 0 || percOverlap >= 100) { throw new Exception("Invalid SpectrumBlockAverage percOverlap"); } if (x == null || x.Length < 5) { throw new Exception("Invalid SpectrumBlockAverage data length"); } double[] rw = new double[sz]; double[] iw = new double[sz]; double[] frw = new double[sz]; double[] fiw = new double[sz]; double[] psp = new double[sz]; double[] sum = new double[sz]; double[] res = new double[sz / 2]; int specSz = 0; int specBufSz = 0; int bufSz = 0; st = Ipps.ippsFFTGetSize_C_64f(order, Ipps.IppFftFlag.IPP_FFT_DIV_FWD_BY_N, IppHintAlgorithm.ippAlgHintNone, ref specSz, ref specBufSz, ref bufSz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTGetSize returned error {0}", st)); } byte[] spec = new byte[specSz]; byte[] specBuf = new byte[specBufSz]; byte[] buf = new byte[bufSz]; IntPtr pspec = IntPtr.Zero; fixed(byte *ps = &spec[0]) { //Spec needs to be fixed as used between ipp calls st = Ipps.ippsFFTInit_C_64f(ref pspec, order, Ipps.IppFftFlag.IPP_FFT_DIV_FWD_BY_N, IppHintAlgorithm.ippAlgHintNone, ps, specBuf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTInit returned error {0}", st)); } int count = 0; int step = (int)(0.01 * (100.0 - percOverlap) * sz); double[] win = HanningWindow(sz); for (int ix = 0; ix < x.Length - sz; ix += step, ++count) { //Prepare input block st = Ipps.ippsZero_64f(iw, iw.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Zero returned error {0}", st)); } //Window block Array.Copy(x, ix, rw, 0, sz); if (zeroMean) { //Subtract mean from block double m; st = Ipps.ippsMean_64f(rw, rw.Length, out m); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Mean returned error {0}", st)); } st = Ipps.ippsAddC_64f_I(-m, rw, rw.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Mean returned error {0}", st)); } } st = Ipps.ippsMul_64f(rw, win, rw, sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Mul returned error {0}", st)); } //Correct for window st = Ipps.ippsMulC_64f_I(windowCorrection, rw, sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("MulC returned error {0}", st)); } //Calculate FFT st = Ipps.ippsFFTFwd_CToC_64f(rw, iw, frw, fiw, pspec, buf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FFTFwd returned error {0}", st)); } if (power) { //Calc power spectrum st = Ipps.ippsPowerSpectr_64f(frw, fiw, psp, sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("PowerSpectr returned error {0}", st)); } } else { //Calculate magnitude st = Ipps.ippsMagnitude_64f(frw, fiw, psp, psp.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Mag returned error {0}", st)); } } //Add result st = Ipps.ippsAdd_64f(psp, sum, sum, psp.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("Add returned error {0}", st)); } } if (count > 0) { //Scale average st = Ipps.ippsMulC_64f_I(1.0 / count, sum, sum.Length); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("MulC returned error {0}", st)); } //Calc result discarding upper half samples res[0] = sum[0]; for (int i = 1; i < sum.Length / 2; ++i) { res[i] = 2 * sum[i]; } } return(res); } }
public static unsafe double[] Fir(double[] src, double[] taps, bool shift) { double[] dst = new double[src.Length]; double[] delLine = new double[taps.Length]; IppStatus st = 0; if (src.Length < 5) { throw new Exception("Invalid FIR src length"); } if (taps.Length < 1) { throw new Exception("Invalid FIR win length"); } //Initialize delay line from initial sample and sample gradient double dd = src[1] - src[0]; delLine[0] = src[0] - dd; for (int i = 1; i < delLine.Length; ++i) { delLine[i] = delLine[i - 1] - dd; } //Get buffer size for filter int sz = 0; st = Ipps.ippsFIRGetStateSize_64f(taps.Length, ref sz); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIRGetStateSize error {0}", st)); } //Buffer for FIR state information. //This is used between calls to ipp and so must be fixed byte[] buf = new byte[sz]; fixed(byte *pbuf = &buf[0]) { IntPtr pstate = IntPtr.Zero; //pstate is initialized to point to somewhere in pbuf st = Ipps.ippsFIRInit_64f(ref pstate, taps, taps.Length, delLine, pbuf); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIRInit error {0}", st)); } st = Ipps.ippsFIR_64f(src, dst, src.Length, pstate); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIR error {0}", st)); } if (shift) { //Move the samples forward in the dst vector by half the filter length //to compensate for filter phase delay and pad at final sample value and gradient int w2 = taps.Length / 2; Array.Copy(dst, w2, dst, 0, dst.Length - w2); dd = src[src.Length - 1] - src[src.Length - 2]; double g = src[src.Length - 1] + dd; double d = 0; for (int i = dst.Length - w2; i < dst.Length; ++i) { st = Ipps.ippsFIROne_64f(g, ref d, pstate); if (st != IppStatus.ippStsNoErr) { throw new Exception(string.Format("FIROne error {0}", st)); } dst[i] = d; g += dd; } } } return(dst); }