private unsafe float Utility(float phase, float gain) { byte * ptr = stackalloc byte[(int)(uint)(1024 * sizeof(Complex) + 16)]; Complex *ptr2 = (Complex *)((long)ptr + 15 & -16); byte * ptr3 = stackalloc byte[4112]; float * ptr4 = (float *)((long)ptr3 + 15 & -16); Utils.Memcpy(ptr2, this._iqPtr, 1024 * sizeof(Complex)); IQBalancer.Adjust(ptr2, 1024, phase, gain); Fourier.ApplyFFTWindow(ptr2, this._windowPtr, 1024); Fourier.ForwardTransform(ptr2, 1024, true); Fourier.SpectrumPower(ptr2, ptr4, 1024, 0f, false); float num = 0f; for (int i = 0; i < 512; i++) { int num2 = 512 - i; if ((float)num2 > 25.6f && (float)num2 < 486.4f) { int num3 = 1022 - i; if (ptr4[i] - this._averagePower > 20f || ptr4[num3] - this._averagePower > 20f) { float num4 = ptr4[i] - ptr4[num3]; num += num4 * num4; } } } return(num); }
private float Utility(float phase, float gain) { var rawFFTPtr = stackalloc byte[FFTBins * sizeof(Complex) + 16]; var fftPtr = (Complex *)(((long)rawFFTPtr + 15) & ~15); var rawSpectrumPtr = stackalloc byte[FFTBins * sizeof(float) + 16]; var spectrumPtr = (float *)(((long)rawSpectrumPtr + 15) & ~15); Utils.Memcpy(fftPtr, _iqPtr, FFTBins * sizeof(Complex)); Adjust(fftPtr, FFTBins, phase, gain); Fourier.ApplyFFTWindow(fftPtr, _windowPtr, FFTBins); Fourier.ForwardTransform(fftPtr, FFTBins); Fourier.SpectrumPower(fftPtr, spectrumPtr, FFTBins); var result = 0.0f; const int halfBins = FFTBins / 2; for (var i = 0; i < halfBins; i++) { var distanceFromCenter = halfBins - i; if (distanceFromCenter > 0.05f * halfBins && distanceFromCenter < 0.95f * halfBins) { var j = FFTBins - 2 - i; if (spectrumPtr[i] - _averagePower > PowerThreshold || spectrumPtr[j] - _averagePower > PowerThreshold) { var distance = spectrumPtr[i] - spectrumPtr[j]; result += distance * distance; } } } return(result); }
public unsafe static void GetFilterSpecs(float[] kernel, double passband, double stopband, out double ripple, out double attenuation) { int num = 0; int num2 = 0; int num3 = 2; while (true) { if (num3 >= kernel.Length * 2 && num >= 4) { break; } num = (int)Math.Round((double)num3 * passband); num2 = (int)Math.Ceiling((double)num3 * stopband); num3 *= 2; } Complex[] obj = new Complex[num3]; float[] array = new float[num3 / 2]; Complex[] array2 = obj; fixed(Complex *ptr = array2) { float[] array3 = array; fixed(float *ptr2 = array3) { for (int i = 0; i < kernel.Length; i++) { ptr[i] = kernel[i]; } Fourier.ForwardTransform(ptr, num3, false); Fourier.SpectrumPower(ptr, ptr2, array.Length, 0f); float num4 = float.PositiveInfinity; float num5 = float.NegativeInfinity; for (int j = 0; j <= num; j++) { if (num4 > ptr2[j]) { num4 = ptr2[j]; } if (num5 < ptr2[j]) { num5 = ptr2[j]; } } ripple = (double)(num5 - num4); float num6 = float.NegativeInfinity; for (int k = num2; k < array.Length; k++) { if (num6 < ptr2[k]) { num6 = ptr2[k]; } } attenuation = Math.Max(0.0, (double)(num4 + num5) * 0.5 - (double)num6); } } }
private void EstimatePower() { var rawFFTPtr = stackalloc byte[FFTBins * sizeof(Complex) + 16]; var fftPtr = (Complex *)(((long)rawFFTPtr + 15) & ~15); var rawSpectrumPtr = stackalloc byte[FFTBins * sizeof(float) + 16]; var spectrumPtr = (float *)(((long)rawSpectrumPtr + 15) & ~15); Utils.Memcpy(fftPtr, _iqPtr, FFTBins * sizeof(Complex)); Fourier.ApplyFFTWindow(fftPtr, _windowPtr, FFTBins); Fourier.ForwardTransform(fftPtr, FFTBins); Fourier.SpectrumPower(fftPtr, spectrumPtr, FFTBins); const int halfBins = FFTBins / 2; var max = float.NegativeInfinity; var avg = 0f; var count = 0; for (var i = 0; i < halfBins; i++) { var distanceFromCenter = halfBins - i; if (distanceFromCenter > 0.05f * halfBins && distanceFromCenter < 0.95f * halfBins) { var j = FFTBins - 2 - i; if (spectrumPtr[i] > max) { max = spectrumPtr[i]; } if (spectrumPtr[j] > max) { max = spectrumPtr[j]; } avg += spectrumPtr[i] + spectrumPtr[j]; count += 2; } } avg /= count; _powerRange = max - avg; _averagePower = avg; }
private unsafe void EstimatePower() { byte * ptr = stackalloc byte[(int)(uint)(1024 * sizeof(Complex) + 16)]; Complex *ptr2 = (Complex *)((long)ptr + 15 & -16); byte * ptr3 = stackalloc byte[4112]; float * ptr4 = (float *)((long)ptr3 + 15 & -16); Utils.Memcpy(ptr2, this._iqPtr, 1024 * sizeof(Complex)); Fourier.ApplyFFTWindow(ptr2, this._windowPtr, 1024); Fourier.ForwardTransform(ptr2, 1024, true); Fourier.SpectrumPower(ptr2, ptr4, 1024, 0f, false); float num = float.NegativeInfinity; float num2 = 0f; int num3 = 0; for (int i = 0; i < 512; i++) { int num4 = 512 - i; if ((float)num4 > 25.6f && (float)num4 < 486.4f) { int num5 = 1022 - i; if (ptr4[i] > num) { num = ptr4[i]; } if (ptr4[num5] > num) { num = ptr4[num5]; } num2 += ptr4[i] + ptr4[num5]; num3 += 2; } } num2 /= (float)num3; this._powerRange = num - num2; this._averagePower = num2; }