C# 简单DFT低通

C# 简单DFT低通,c#,fft,dft,C#,Fft,Dft,我在用DFT制作一个简单的低通滤波器时遇到了一些问题。最后,我希望能够实时改变音高,但目前我甚至无法做到这一点。我在这方面没有受过训练,我只知道FFT会把波转换成频率,IFFT也会这样做,还有一些我读过的其他东西。老实说,我很惊讶它能像现在这样工作。无论如何,代码如下: byte[] samples = new byte[20000000]; int spos = 0; 示例在此处用8位无符号PCM填充spos结果证明我是对的(耶):每1024个样本我都会听到一

我在用DFT制作一个简单的低通滤波器时遇到了一些问题。最后,我希望能够实时改变音高,但目前我甚至无法做到这一点。我在这方面没有受过训练,我只知道FFT会把波转换成频率,IFFT也会这样做,还有一些我读过的其他东西。老实说,我很惊讶它能像现在这样工作。无论如何,代码如下:

        byte[] samples = new byte[20000000];
        int spos = 0;

示例
在此处用8位无符号PCM填充<代码>spos结果证明我是对的(耶):每1024个样本我都会听到一声咔哒声,这让音频听起来很糟糕。为了解决这个问题,我在许多短重叠的过滤音频块之间进行了淡入。虽然速度不快,但很有效,我很确定这就是他们所说的“窗口化”

公共类OggDFT
{
int样本长度;
字节[]样本;
DragonOgg.MediaPlayer.OggFile f;
积分率=0;
系统。数字。复[]频率;
离散傅里叶变换dft=新的离散傅里叶变换();
int-samplespace=128;
int samplesize=1024;
整数抽样计数;
public void示例低通()
{
int shiftUp=1000;//1khz
int fade=2;//8样本淡入度。
int halfsize=采样大小/2;
int kick=频率[0]。长度*移位/速率;
对于(int i=0;i半码-1-踢)
{
频率[i][j]=0;
}
}
}
BluesteinInverse(频率[i],MathNet.Numerics.IntegralTransforms.FourierOptions.Default);
}
}
公共OggDFT(dragongogg.MediaPlayer.OggFile f)
{
络合物[]c=新络合物[10];
对于(int i=0;i<10;i++)
c[i]=i;
移位复合体(-2,c,5,10);
这个。f=f;
//制作一个20MB的缓冲区。
样本=新字节[20000000];
int sample_length=0;
//这里的这个块只是将ogg文件中未压缩的数据加载到一个漂亮的、大的20MB缓冲区中。如果您想使用与我使用的相同的库,它被称为DragonOgg(如果您不能通过名称空间来判断)
while(样本长度<样本长度)
{
var bs=f.GetBufferSegment(4096);//获取~4096字节(不保证返回4096字节)。
如果(bs.ReturnValue==0)
break;//文件结束
//设定汇率
速率=bs.RateHz;
//显示一些加载信息:
控制台写入线(“秒:+样本长度/速率”);
//它是立体声的,所以我们需要一半的数据。
int max=bs.ReturnValue/2;
//缓冲区溢出保护。
if(样本长度-样本长度<最大值)
最大值=样本长度-样本长度;
//复印机。
对于(int j=0;j样本长度)
样本=新系统.数值.复数[样本长度-i];
其他的
样本=新系统.Numerics.Complex[samplesize];
对于(int j=0;j对于(int i=0;在我的区域内……但吉布斯不是一个事实吗?你不能达到你的目标水平而不超过或低于它吗?@Ritch Melton我也不太确定……我想我现在离得越来越近了:问题似乎在样本之间:它在两者之间跳跃时发出轻微的咔嗒声。我想我现在知道窗口在哪里了……山姆pleSize!=采样间隔,然后窗口插值…..是的,这让事情变得更好。好多了。我会清理我的窗口处理工具中可能存在的一些小故障,然后发布代码。无需重新发明轮子。查找重叠添加和重叠保存使用FFT的快速卷积滤波。这在许多DSP技术教科书中都有描述。
        int samplesize = 128;
        int sampleCount = spos / samplesize;
        frequencies = new System.Numerics.Complex[sampleCount][];
        for (int i = 0; i < sampleCount; i++)
        {
            Console.WriteLine("Sample " + i + " / " + sampleCount);
            frequencies[i] = new System.Numerics.Complex[samplesize];
            for (int j = 0; j < samplesize; j++)
            {
                frequencies[i][j] = (float)(samples[i * samplesize + j] - 128) / 128.0f;
            }
            dft.Radix2Forward(frequencies[i], MathNet.Numerics.IntegralTransforms.FourierOptions.Default);
        }

        int shiftUp = 1000; //1khz
        int fade = 2; //8 sample fade.
        int kick = frequencies[0].Length * shiftUp / rate;
        for (int i = 0; i < sampleCount; i++)
        {
            for (int j = 0; j < frequencies[i].Length; j++)
            {
                if (j == 0 || j == 64)
                    continue;
                if (j < 64)
                {
                    if (!(j < kick + 1))
                    {
                        frequencies[i][j] = 0;
                    }
                }
                else
                {
                    if (!(j - 64 > 63 - kick))
                    {
                        frequencies[i][j] = 0;
                    }
                }
            }
            dft.Radix2Inverse(frequencies[i], MathNet.Numerics.IntegralTransforms.FourierOptions.Default);
            for (int j=0; j<samplesize; j++)
                samples[i * samplesize + j] = (byte)(frequencies[i][j].Real * 128.0f + 128.0f);
        }
        BinaryWriter bw = new BinaryWriter(File.OpenWrite("sound"));
        for (int i = 0; i < spos; i++)
        {
            bw.Write(samples[i]);
        }
        bw.Close();
public class OggDFT
{
    int sample_length;
    byte[] samples;
    DragonOgg.MediaPlayer.OggFile f;
    int rate = 0;
    System.Numerics.Complex[][] frequencies;
    DiscreteFourierTransform dft = new DiscreteFourierTransform();
    int samplespacing = 128;
    int samplesize = 1024;
    int sampleCount;

    public void ExampleLowpass()
    {
        int shiftUp = 1000; //1khz
        int fade = 2; //8 sample fade.
        int halfsize = samplesize / 2;
        int kick = frequencies[0].Length * shiftUp / rate;
        for (int i = 0; i < sampleCount; i++)
        {
            for (int j = 0; j < frequencies[i].Length; j++)
            {
                if (j == 0 || j == halfsize)
                    continue;
                if (j < halfsize)
                {
                    if (!(j < kick + 1))
                    {
                        frequencies[i][j] = 0;
                    }
                }
                else
                {
                    if (!(j - halfsize > halfsize - 1 - kick))
                    {
                        frequencies[i][j] = 0;
                    }
                }
            }
            dft.BluesteinInverse(frequencies[i], MathNet.Numerics.IntegralTransforms.FourierOptions.Default);
        }
    }

    public OggDFT(DragonOgg.MediaPlayer.OggFile f)
    {
        Complex[] c = new Complex[10];
        for (int i = 0; i < 10; i++)
            c[i] = i;
        ShiftComplex(-2, c, 5, 10);


        this.f = f;

        //Make a 20MB buffer.
        samples = new byte[20000000];
        int sample_length = 0;

        //This block here simply loads the uncompressed data from the ogg file into a nice n' large 20MB buffer. If you want to use the same library as I've used, It's called DragonOgg (If you cant tell by the namespace)
        while (sample_length < samples.Length)
        {
            var bs = f.GetBufferSegment(4096); //Get ~4096 bytes (does not gurantee that 4096 bytes will be returned.
            if (bs.ReturnValue == 0)
                break; //End of file

            //Set the rate
            rate = bs.RateHz;

            //Display some loading info:
            Console.WriteLine("seconds: " + sample_length / rate);

            //It's stereo so we want half the data.
            int max = bs.ReturnValue / 2;

            //Buffer overflow care.
            if (samples.Length - sample_length < max)
                max = samples.Length - sample_length;

            //The copier.
            for (int j = 0; j < max; j++)
            {
                //I'm using j * 2 here because I know that the input audio is 8Bit Stereo, and we want just one mono channel. So we skip every second one.
                samples[sample_length + j] = bs.Buffer[j * 2];
            }

            sample_length += max;
            if (max == 0)
                break;
        }
        sampleCount = (sample_length - 1) / samplespacing + 1;
        frequencies = new System.Numerics.Complex[sampleCount][];
        for (int i = 0; i < sample_length; i += samplespacing)
        {
            Console.WriteLine("Sample---" + i + " / " + sample_length);
            System.Numerics.Complex[] sample;
            if (i + samplesize > sample_length)
                sample = new System.Numerics.Complex[sample_length - i];
            else
                sample = new System.Numerics.Complex[samplesize];
            for (int j = 0; j < sample.Length; j++)
            {
                sample[j] = (float)(samples[i + j] - 128) / 128.0f;
            }
            dft.BluesteinForward(sample, MathNet.Numerics.IntegralTransforms.FourierOptions.Default);
            frequencies[i / samplespacing] = sample;
        }

        //Perform the filters to the frequencies
        ExampleLowpass();

        //Make window kernel thingy
        float[] kernel = new float[samplesize / samplespacing * 2];
        for (int i=0; i<kernel.Length; i++)
        {
            kernel[i] = (float)((1-Math.Cos(2*Math.PI*i/(kernel.Length - 1)))/2);
        }

        //Apply window kernel thingy
        for (int i = 0; i < sample_length; i++)
        {
            int jstart = i / samplespacing - samplesize / samplespacing + 1;
            int jend = i / samplespacing;
            if (jstart < 0) jstart = 0;
            float ktotal = 0;
            float stotal = 0;
            for (int j = jstart; j <= jend; j++)
            {
                float kernelHere = 1.0f;
                if (jstart != jend)
                    kernelHere = kernel[(j - jstart) * kernel.Length / (jend + 1 - jstart)];
                int index = i - j * samplespacing;
                stotal += (float)frequencies[j][index].Real * kernelHere;
                ktotal += kernelHere;
            }
            if (ktotal != 0)
            {
                stotal /= ktotal;
                samples[i] = (byte)(stotal * 128 * 0.9f + 128);
            }
            else
            {
                Console.WriteLine("BAD " + jstart + " " + jend + " sec: " + ((float)i / rate));
                samples[i] = (byte)(stotal * 128 * 0.9f + 128);
            }
        }

        BinaryWriter bw = new BinaryWriter(File.OpenWrite("sound"));
        for (int i = 0; i < sample_length; i++)
        {
            bw.Write(samples[i]);
        }
        bw.Close();
    }
}