Exemplo n.º 1
0
        public void readFile(List <ComplexNumber> mInputNum, long mStartPos = 0)
        {
            long       offset = 0;
            FileStream fs     = new FileStream(fFileName, FileMode.Open);



            if (mStartPos != 0)
            {
                if (fDataWidth == 8 && fDataNum == 1)
                {
                    fFilePos = mStartPos;
                }
                if (fDataWidth == 12 && fDataNum == 1)
                {
                    fFilePos = mStartPos * 3 / 2;
                }
                if (fDataWidth == 12 && fDataNum == 2)
                {
                    fFilePos = mStartPos * 3;
                }
                if (fDataWidth == 16 && fDataNum == 2)
                {
                    fFilePos = mStartPos * 4;
                }
                if (fDataWidth == 16 && fDataNum == 1)
                {
                    fFilePos = mStartPos * 2;
                }
            }
            offset = fs.Seek(fFilePos, SeekOrigin.Begin);
            if (offset > fs.Length - 5)
            {
                return;
            }

            BinaryReader  br = new BinaryReader(fs);
            ComplexNumber c  = new ComplexNumber(0, 0);



            if (fDataWidth == 8 && fDataNum == 1)
            {
                for (long i = 0; i < fMaxNumCount; i++)
                {
                    c.real = (double)(br.ReadByte() - 128);
                    c.imag = 0;
                    mInputNum.Add(c);
                    fFilePos += 1;
                }
            }



            //位宽12,没有虚部
            if (fDataWidth == 12 && fDataNum == 1)
            {
                for (long i = 0; i < fMaxNumCount / 2; i++)
                {
                    byte[] rd   = br.ReadBytes(3);
                    int    real = (int)rd[1] & 0x0f;
                    c.real = ((real << 8) | (rd[0])) - 2048;
                    c.imag = 0;
                    mInputNum.Add(c);

                    real   = (int)rd[1] & 0xf0;
                    c.real = ((real >> 4) | (((int)rd[2]) << 4)) - 2048;
                    c.imag = 0;
                    mInputNum.Add(c);
                    fFilePos += 3;
                }

                ////判断是否有符号
                //int mLow = 0;
                //int mMid = 0;
                //int mHigh = 0;

                //for (int i = 0; i < mInputNum.Count; i++)
                //{
                //    if (mInputNum[i].real < 1365)
                //        mLow++;

                //    if (mInputNum[i].real > 1365 && mInputNum[i].real < 2730)
                //        mMid++;

                //    if (mInputNum[i].real > 2730)
                //        mHigh++;

                //}

                ////if (mMid <= mLow || mMid <= mHigh)
                //{
                //    for (int i = 0; i < mInputNum.Count; i++)
                //    {
                //        double tmpR = mInputNum[i].real;
                //        double tmpI = mInputNum[i].imag;
                //        //if (mInputNum[i].real > 2048)
                //            tmpR = mInputNum[i].real-2048;
                //        //if (mInputNum[i].imag > 2047)
                //        //    tmpI = 2048 - mInputNum[i].imag;
                //        mInputNum[i] = new ComplexNumber(tmpR, tmpI);
                //    }
                //}
            }


            //位宽12,有虚部,无符号
            if (fDataWidth == 12 && fDataNum == 2)
            {
                for (long i = 0; i < fMaxNumCount; i++)
                {
                    byte[] rd   = br.ReadBytes(3);
                    int    real = (int)rd[1] & 0x0f;
                    c.real = ((real << 8) | (rd[0])) - 2048;

                    real   = (int)rd[1] & 0xf0;
                    c.imag = ((real >> 4) | (((int)rd[2]) << 4)) - 2048;

                    mInputNum.Add(c);
                    fFilePos += 3;
                }


                ////判断是否有符号
                //int mLow = 0;
                //int mMid = 0;
                //int mHigh = 0;

                //for (int i = 0; i < mInputNum.Count; i++)
                //{
                //    if (mInputNum[i].real < 1365)
                //        mLow++;

                //    if (mInputNum[i].real > 1365 && mInputNum[i].real < 2730)
                //        mMid++;

                //    if (mInputNum[i].real > 2730)
                //        mHigh++;

                //}

                //if (mMid <= mLow || mMid <= mHigh)
                //{
                //    for (int i = 0; i < mInputNum.Count; i++)
                //    {
                //        double tmpR = mInputNum[i].real;
                //        double tmpI = mInputNum[i].imag;
                //        //if (mInputNum[i].real > 2047)
                //            tmpR = 2048 - mInputNum[i].real;
                //        //if (mInputNum[i].imag > 2047)
                //            tmpI = 2048 - mInputNum[i].imag;
                //        mInputNum[i] = new ComplexNumber(tmpR, tmpI);
                //    }
                //}
            }

            //位宽16,有虚部,有符号
            if (fDataWidth == 16 && fDataNum == 2)
            {
                for (long i = 0; i < fMaxNumCount; i++)
                {
                    c.real    = br.ReadInt16();
                    c.imag    = br.ReadInt16();
                    fFilePos += 4;
                    // long pos = fs.Seek(offset, SeekOrigin.Current);
                    mInputNum.Add(c);
                }
            }


            //位宽16,没有虚部,有符号
            if (fDataWidth == 16 && fDataNum == 1)
            {
                for (long i = 0; i < fMaxNumCount; i++)
                {
                    int tmp = br.ReadInt16();
                    //if (tmp >= 2047)
                    tmp = tmp - 2048;


                    c.real    = tmp;
                    c.imag    = 0;
                    fFilePos += 2;
                    // long pos = fs.Seek(offset, SeekOrigin.Current);
                    mInputNum.Add(c);
                }
            }



/*
 *
 *          nNumChars = (fDataWidth * fDataNum) / 8;
 *
 *          offset = fs.Length / fMaxNumCount;
 *          if (offset % 4 != 0)
 *              offset = offset + (offset % 4);
 *
 *
 *          for (long i = 0; i < fMaxNumCount; i++)
 *              {
 *                  c.real = br.ReadInt16();
 *                  c.imag = br.ReadInt16();
 *                  fFilePos += 4;
 *                 // long pos = fs.Seek(offset, SeekOrigin.Current);
 *                  mInputNum.Add(c);
 *                 // if (pos >= fs.Length)
 *                 //     break;
 *
 *
 *              }
 * */
            br.Close();
            fs.Close();
        }
Exemplo n.º 2
0
        /// <summary>
        /// FFT变换
        /// </summary>
        ///
        private void FFT()
        {
            int fftLen;
            int len;
            int nLen = fInputEnd - fInputStart;

            if (fFFTCount == 0)
            {
                for (len = 0; len < 20; len++)
                {
                    if (System.Math.Pow((double)2, (double)len) > nLen)
                    {
                        break;
                    }
                }
                fftLen = (int)System.Math.Pow((double)2, (double)(len - 1));
            }
            else
            {
                fftLen = fFFTCount;
            }
            fFFTNum.Clear();


            for (int i = 0; i < fftLen; i++)
            {
                if (i >= nLen)
                {
                    fFFTNum.Add(new ComplexNumber(0, 0));
                }
                else
                {
                    fFFTNum.Add(fInputNum[i + fInputStart]);
                }
            }

            BitReOrder();
            int           n     = fFFTNum.Count;
            double        angle = -2 * Math.PI / n;
            ComplexNumber comp  = new ComplexNumber(Math.Cos(angle), Math.Sin(angle));

            ComplexNumber[] Wcomplex = new ComplexNumber[n / 2];
            Wcomplex[0].real = 1;
            Wcomplex[0].imag = 0;
            for (int i = 1; i < n / 2; i++)//建立权向量索引表
            {
                Wcomplex[i] = Wcomplex[i - 1] * comp;
            }
            int wei = (int)Math.Log(n, 2);//求位数
            //ComplexNumber[] newW = new ComplexNumber[n / 2];
            List <ComplexNumber> newW = new List <ComplexNumber>(n / 2);

            for (int i = 0; i < n / 2; i++)
            {
                newW.Add(new ComplexNumber(0, 0));
            }
            ComplexNumber temp;
            int           t;

            for (int i = 0; i < wei; i++)
            {
                for (int l = 0; l < n / 2;) //每级权数组
                {
                    for (int k = 0; k < Math.Pow(2, i); k++)
                    {
                        // newW.Add( Wcomplex[(int)(k * Math.Pow(2, (wei - i - 1)))] );
                        newW[l] = Wcomplex[(int)(k * Math.Pow(2, (wei - i - 1)))];
                        l++;
                    }
                }
                int j = 0, a = 0;
                for (int m = 0; m < Math.Pow(2, (wei - i - 1)); m++)               //每级分块
                {
                    for (int o = 0; o < (n / 2) / Math.Pow(2, (wei - i - 1)); o++) //每块蝶形运算次数
                    {
                        t          = (int)(j + Math.Pow(2, i));
                        temp       = fFFTNum[j] + fFFTNum[t] * newW[a];
                        fFFTNum[t] = fFFTNum[j] - fFFTNum[t] * newW[a];
                        fFFTNum[j] = temp;
                        a++; j++;
                    }
                    j = j - (int)((n / 2) / Math.Pow(2, (wei - i - 1))) + (int)Math.Pow(2, (i + 1));
                }

                update((int)((float)i / wei * 100), "正在进行快速傅立叶变换(FFT)");
            }

            for (int i = 0; i < n; i++)
            {
                fFFTNum[i] = fFFTNum[i];
            }
            update(100, "快速傅立叶变换(FFT)完成");
        }