Пример #1
0
 private void SetPrePostWindow(int length)
 {
     if (window == null || window.Length != length)
     {
         var preWindow  = FHTArrays.GetPreWindow(length);
         var postWindow = FHTArrays.GetPostWindow(length);
         window = new double[length];
         for (int i = 0; i < length; i++)
         {
             window[i] = preWindow[i] * postWindow[i] * length * 2 * Math.Sqrt(2);
         }
     }
 }
Пример #2
0
        public void Process(short[] datain, out short[] dataout)
        {
            var length       = datain.Length;
            var mBitRev      = FHTArrays.GetBitRevTable(length);
            var mPreWindow   = FHTArrays.GetPreWindow(length);
            var mPostWindow  = FHTArrays.GetPostWindow(length);
            var hopanal      = length / kOverlapCount;
            var hopsyn       = (int)(hopanal * ShiftRate);
            var overlapCount = (int)Math.Ceiling((double)length / hopsyn);
            var temp         = new double[length];

            if (shiftChanged || mLastPhase == null || mLastPhase.Length != length / 2)
            {
                mLastPhase   = new double[length / 2];
                mSumPhase    = new double[length / 2];
                overlap      = new Overlap(overlapCount, hopsyn);
                shiftChanged = false;
            }

            for (var i = 0; i < length; ++i)
            {
                var j = mBitRev[i];
                var k = (uint)(j & (length - 1));
                temp[i] = datain[k] * mPreWindow[k];
            }

            double[] re, im;
            fht.ComputeFHT(temp, out re, out im);
            var temp_shift = new double[length];

            AnalysisAndSynthesis(ref re, ref im);

            ifht.ComputeFHT(re, im, out temp_shift, false);
            temp = temp_shift;

            var window = FHTArrays.GetPostWindow(length);

            if (overlapCount >= kOverlapCount)
            {
                for (var i = 0; i < length; i++)
                {
                    temp_shift[i] *= window[i] * shiftRate / 2;
                }
            }
            overlap.AddOverlap(ref temp_shift);
            temp = new double[hopsyn];
            Array.Copy(temp_shift, temp, hopsyn);
            temp = Stretch(temp, 1 / ShiftRate, length);

            dataout = ToShort(temp, length);
        }
Пример #3
0
        public void Process(short[] datain, out short[] dataout, bool isLeft)
        {
            FHTransform fht, ifht;

            if (isLeft)
            {
                fht = fhtl; ifht = ifhtl;
            }
            else
            {
                fht = fhtr; ifht = ifhtr;
            }

            var length     = datain.Length;
            var mBitRev    = FHTArrays.GetBitRevTable(length);
            var mPreWindow = FHTArrays.GetPreWindow(length);
            var temp       = new double[length];
            var length2    = length / 2;
            var cutoffDown = (int)((CutoffFrequencyDown / ((double)mSampleRate / length2)) + 0.5);
            var cutoffUp   = (int)((CutoffFrequencyUp / ((double)mSampleRate / length2)) + 0.5);

            for (var i = 0; i < length; ++i)
            {
                var j = mBitRev[i];
                var k = (uint)(j & (length - 1));
                temp[i] = datain[k] * mPreWindow[k];
            }

            double[] re, im;
            fht.ComputeFHT(temp, out re, out im);

            for (var i = 0; i < cutoffDown; i++)
            {
                re[i] = im[i] = 0;
            }
            for (var i = cutoffUp; i < length2; i++)
            {
                re[i] = im[i] = 0;
            }

            ifht.ComputeFHT(re, im, out temp, true);

            dataout = ToShort(temp, length);
        }
Пример #4
0
        private void Process(double[] leftin, double[] rightin, out double[] leftout, out double[] rightout)
        {
            int length           = leftin.Length;
            int freqBelowToSides = (int)((200.0 / ((double)mSampleRate / length)) + 0.5);
            int freqAboveToSides = (int)((300.0 / ((double)mSampleRate / length)) + 0.5);
            var mBitRev          = FHTArrays.GetBitRevTable(length);
            var mPreWindow       = FHTArrays.GetPreWindow(length);
            var mPostWindow      = FHTArrays.GetPostWindow(length);

            double[] tempLeft  = new double[length];
            double[] tempRight = new double[length];

            for (uint i = 0; i < length; ++i)
            {
                uint j = (uint)(mBitRev[i]);
                uint k = (uint)(j & (length - 1));
                tempLeft[i]  = leftin[k] * mPreWindow[k];
                tempRight[i] = rightin[k] * mPreWindow[k];
            }

            fhtl.ComputeFHT(ref tempLeft, length);
            fhtr.ComputeFHT(ref tempRight, length);

            double[] tempC = new double[length];
            for (uint i = 0; i < length / 2; i++)
            {
                double lR = tempLeft[i] + tempLeft[length - 1 - i];
                double lI = tempLeft[i] - tempLeft[length - 1 - i];
                double rR = tempRight[i] + tempRight[length - 1 - i];
                double rI = tempRight[i] - tempRight[length - 1 - i];

                double sumR  = lR + rR;
                double sumI  = lI + rI;
                double diffR = lR - rR;
                double diffI = lI - rI;

                double sumSq  = sumR * sumR + sumI * sumI;
                double diffSq = diffR * diffR + diffI * diffI;
                double alpha  = 0.0;

                if (sumSq > FHTransform.nodivbyzero)
                {
                    alpha = 0.5 - Math.Sqrt(diffSq / sumSq) * 0.5;
                }

                double cR = sumR * alpha;
                double cI = sumI * alpha;


                if (mBassToSides && ((i < freqBelowToSides)))// && (i < freqAboveToSides)))
                {
                    cR = cI = 0.0;
                }

                tempC[mBitRev[i]] = cR + cI;
                tempC[mBitRev[length - 1 - i]] = cR - cI;
            }
            fhtc.ComputeFHT(ref tempC, length, true);
            for (var i = 0; i < length; i++)
            {
                tempLeft[i]  = leftin[i] - tempC[i];
                tempRight[i] = rightin[i] - tempC[i];
            }

            if (vocalExtract)
            {
                leftout  = tempC;
                rightout = tempC;
            }
            else
            {
                leftout  = tempLeft;
                rightout = tempRight;
            }
        }