Beispiel #1
0
        /// <summary>
        ///   Computes the signal energy.
        /// </summary>
        ///
        public double GetEnergy()
        {
            double e = 0, v;

            unsafe
            {
                if (format != SampleFormat.Format128BitComplex)
                {
                    // Iterate over all samples and compute energy
                    float *src = (float *)this.ptrData.ToPointer();
                    for (int i = 0; i < this.Samples; i++, src++)
                    {
                        v  = (*src);
                        e += v * v;
                    }
                }
                else
                {
                    // Iterate over all samples and compute energy
                    System.Numerics.Complex *src = (System.Numerics.Complex *) this.Data.ToPointer();
                    for (int i = 0; i < this.Samples; i++, src++)
                    {
                        double m = (*src).Magnitude;
                        e += m * m;
                    }
                }
            }

            return(e);
        }
Beispiel #2
0
        /// <summary>
        ///   Applies the filter to a signal.
        /// </summary>
        ///
        protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData)
        {
            SampleFormat format  = sourceData.SampleFormat;
            int          samples = sourceData.Samples;

            if (format == SampleFormat.Format32BitIeeeFloat)
            {
                float *src = (float *)sourceData.Data.ToPointer();
                float *dst = (float *)destinationData.Data.ToPointer();

                for (int i = 0; i < samples; i++, dst++, src++)
                {
                    *dst = System.Math.Abs(*src);
                }
            }
            else if (format == SampleFormat.Format128BitComplex)
            {
                System.Numerics.Complex *src = (System.Numerics.Complex *)sourceData.Data.ToPointer();
                System.Numerics.Complex *dst = (System.Numerics.Complex *)destinationData.Data.ToPointer();

                for (int i = 0; i < samples; i++, dst++, src++)
                {
                    *dst = new System.Numerics.Complex((*src).Magnitude, 0);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        ///   Generates the given number of samples.
        /// </summary>
        ///
        public Signal Generate(int samples)
        {
            Signal signal = new Signal(Channels, samples, SamplingRate, Format);

            int ti = interval * Channels;

            unsafe
            {
                if (Format == SampleFormat.Format32BitIeeeFloat)
                {
                    for (int c = 0; c < Channels; c++)
                    {
                        float *dst = (float *)signal.Data.ToPointer() + c;

                        for (int i = 0; i < samples; i += interval, dst += ti)
                        {
                            *dst = ampMax;

                            if (Pulses > 0 && i / interval >= Pulses)
                            {
                                break;
                            }
                        }
                    }
                }
                else if (Format == SampleFormat.Format128BitComplex)
                {
                    System.Numerics.Complex campMax = new System.Numerics.Complex(ampMax, 0);

                    for (int c = 0; c < Channels; c++)
                    {
                        System.Numerics.Complex *dst = (System.Numerics.Complex *)signal.Data.ToPointer() + c;

                        for (int i = 0; i < samples; i += interval, dst += ti)
                        {
                            *dst = campMax;

                            if (Pulses > 0 && i / interval >= Pulses)
                            {
                                break;
                            }
                        }
                    }
                }
                else
                {
                    throw new UnsupportedSampleFormatException("Sample format is not supported by the filter.");
                }
            }

            return(signal);
        }
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        ///
        protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int length = sourceData.Length;

            System.Numerics.Complex *src = (System.Numerics.Complex *)sourceData.Data.ToPointer();
            System.Numerics.Complex *dst = (System.Numerics.Complex *)destinationData.Data.ToPointer();

            for (int i = 0; i < length - 1; i++, src++, dst++)
            {
                double re = src[i + 1].Real - src[i].Real;

                // Retain only if difference is positive
                *dst = (re > 0) ? new System.Numerics.Complex(re, 0) : System.Numerics.Complex.Zero;
            }
        }
Beispiel #5
0
        /// <summary>
        /// Processes the filter.
        /// </summary>
        ///
        protected override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            int samples = sourceData.Samples;

            unsafe
            {
                System.Numerics.Complex *src  = (System.Numerics.Complex *)sourceData.Data.ToPointer();
                System.Numerics.Complex *dst  = (System.Numerics.Complex *)destinationData.Data.ToPointer();
                System.Numerics.Complex *comb = (System.Numerics.Complex *)combSignal.Data.ToPointer();

                for (int i = 0; i < samples; i++, src++, dst++, comb++)
                {
                    *dst = new System.Numerics.Complex((src[0] * comb[0]).Magnitude, 0);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        ///   Processes the filter.
        /// </summary>
        protected unsafe override void ProcessFilter(ComplexSignal sourceData, ComplexSignal destinationData)
        {
            if (sourceData.Status != ComplexSignalStatus.Analytic)
            {
                throw new ArgumentException("Signal must be in analytic form.", "sourceData");
            }

            int samples = sourceData.Samples;

            System.Numerics.Complex *src = (System.Numerics.Complex *)sourceData.Data.ToPointer();
            System.Numerics.Complex *dst = (System.Numerics.Complex *)destinationData.Data.ToPointer();

            for (int i = 0; i < samples; i++, src++, dst++)
            {
                *dst = new System.Numerics.Complex((*dst).Magnitude, 0);
            }
        }
Beispiel #7
0
        /// <summary>
        ///   Splits a signal using the window.
        /// </summary>
        ///
        public virtual unsafe ComplexSignal Apply(ComplexSignal complexSignal, int sampleIndex)
        {
            System.Numerics.Complex[,] resultData = new System.Numerics.Complex[Length, complexSignal.Channels];
            ComplexSignal result = ComplexSignal.FromArray(resultData, complexSignal.SampleRate);

            int channels  = result.Channels;
            int minLength = System.Math.Min(complexSignal.Length - sampleIndex, Length);

            for (int c = 0; c < complexSignal.Channels; c++)
            {
                System.Numerics.Complex *dst = (System.Numerics.Complex *)result.Data.ToPointer() + c;
                System.Numerics.Complex *src = (System.Numerics.Complex *)complexSignal.Data.ToPointer() + c + channels * sampleIndex;

                for (int i = 0; i < minLength; i++, dst += channels, src += channels)
                {
                    *dst = window[i] * (*src);
                }
            }

            return(result);
        }