首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单DFT低通

简单DFT低通
EN

Stack Overflow用户
提问于 2013-01-29 12:19:17
回答 1查看 1.7K关注 0票数 0

我在制作一个带有DFT的简单低通滤波器时遇到了一些麻烦。最后,我希望能够实时地进行音调转换,但就目前的情况而言,我甚至做不到这一点。我没有接受过这方面的训练,我只知道FFT将波转换为频率,iFFT也是这样做的,还有一些我读过的其他东西。坦率地说,我很惊讶它到目前为止工作得这么好。不管怎样,下面是代码:

代码语言:javascript
复制
        byte[] samples = new byte[20000000];
        int spos = 0;

samples在这里用8位无符号PCM填充。spos <-样本数

代码语言:javascript
复制
        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;

所以现在我已经计算了128个输入样本部分的DFT。kick是(我希望)离散傅立叶变换中跨越1000 the的样本数量。也就是说,由于frequencies.Length / 2包含高达rate/2 Hz的频率幅度数据,因此frequencies[0].Length / 2 * shiftUp / (rate / 2) = frequencies[0].Length * shiftUp / rate应该会给出正确的值

代码语言:javascript
复制
        for (int i = 0; i < sampleCount; i++)
        {

这就是我遇到麻烦的地方。没有它,输出听起来很棒!这将跳过索引0和索引64。这两个变量都有一个复数分量0,我记得在某处读到索引0处的值很重要……

代码语言:javascript
复制
            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;
                    }
                }
            }

最后,它会撤消转换

代码语言:javascript
复制
            dft.Radix2Inverse(frequencies[i], MathNet.Numerics.IntegralTransforms.FourierOptions.Default);

将其放回...tosses数组中

代码语言:javascript
复制
            for (int j=0; j<samplesize; j++)
                samples[i * samplesize + j] = (byte)(frequencies[i][j].Real * 128.0f + 128.0f);
        }

将其...chucks到一个文件中

代码语言:javascript
复制
        BinaryWriter bw = new BinaryWriter(File.OpenWrite("sound"));
        for (int i = 0; i < spos; i++)
        {
            bw.Write(samples[i]);
        }
        bw.Close();

...then我把它导入到Audacity中,用手工艺品杀死我的耳朵。

光谱显示显示代码在一定程度上是有效的

然而,在整首歌中都会出现这些恼人的高音爆裂声。我听说过一些关于Gibbs现象和窗口函数的事情,但我真的不知道如何在这里应用它们。fade变量是我对窗口函数的最好尝试:超过1000 in标记的所有内容在两个样本中都会淡入淡出为0。

有什么想法吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-29 18:30:52

所以事实证明我是对的(耶):每1024个样本中我就会听到一声滴答声,这使得音频听起来很糟糕。为了解决这个问题,我在许多短小重叠的过滤音频之间进行了淡入。它不是很快,但很有效,我很确定这就是他们所说的“窗口”。

代码语言:javascript
复制
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();
    }
}

如果你想编译它,你需要DragonOgg (http://sourceforge.net/projects/dragonogg/)和MathNet.Numerics (http://mathnetnumerics.codeplex.com/)

我希望它能对某些人有所帮助--我不知道StackOverflow是如何默认许可的,但这段代码目前是公共领域的。

经过进一步的思考,我决定我可以通过简单地“模糊”样本来获得一个基本的低通滤波器,从而更容易地达到近似的效果。高通滤波器可以通过减去低通的结果来实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14575593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档