抑制FFT的频率

时间:2013-07-28 20:11:02

标签: c# .net audio fft

我要做的是从某些歌曲中检索频率并抑制所有未出现在人类声音范围或一般任何范围内的频率。这是我的抑制功能。

    public void SupressAndWrite(Func<FrequencyUnit, bool> func)
    {
        this.WaveManipulated = true;
        while (this.mainWave.WAVFile.NumSamplesRemaining > 0)
        {
            FrequencyUnit[] freqUnits = this.mainWave.NextFrequencyUnits();

            Complex[] compUnits = (from item
                                   in freqUnits
                                   select (func(item)
                                    ? new Complex(item.Frequency, 0) :Complex.Zero))    
                                   .ToArray();

            FourierTransform.FFT(compUnits, FourierTransform.Direction.Backward);

            short[] shorts = (from item
                              in compUnits
                              select (short)item.Real).ToArray();

            foreach (short item in shorts)
            {
                this.ManipulatedFile.AddSample16bit(item);
            }
        }
        this.ManipulatedFile.Close();
    }

这是我的浪潮课程。

public sealed class ComplexWave
{
    public readonly WAVFile WAVFile;
    public readonly Int32 SampleSize;

    private FourierTransform.Direction fourierDirection { get; set; }

    private long position;
    /// <param name="file"></param>
    /// <param name="sampleSize in BLOCKS"></param>
    public ComplexWave(WAVFile file, int sampleSize)
    {
        file.NullReferenceExceptionCheck();

        this.WAVFile = file;
        this.SampleSize = sampleSize;

        if (this.SampleSize % 8 != 0)
        {
            if (this.SampleSize % 16 != 0)
            {
                throw new ArgumentException("Sample Size");
            }
        }
        if (!MathTools.IsPowerOf2(sampleSize))
        {
            throw new ArgumentException("Sample Size");
        }
        this.fourierDirection = FourierTransform.Direction.Forward;
    }
    public Complex[] NextSampleFourierTransform()
    {
        short[] newInput = this.GetNextSample();
        Complex[] data = newInput.CopyToComplex();

        if (newInput.Any((x) => x != 0))
        {
            Debug.Write("done");
        }
        FourierTransform.FFT(data, this.fourierDirection);

        return data;
    }
    public FrequencyUnit[] NextFrequencyUnits()
    {
        Complex[] cm = this.NextSampleFourierTransform();
        FrequencyUnit[] freqUn = new FrequencyUnit[(cm.Length / 2)];
        int max = (cm.Length / 2);
        for (int i = 0; i < max; i++)
        {
            freqUn[i] = new FrequencyUnit(cm[i], this.WAVFile.SampleRateHz, i, cm.Length);
        }
        Array.Sort(freqUn);
        return freqUn;
    }
    private short[] GetNextSample()
    {
        short[] retval = new short[this.SampleSize];

        for (int i = 0; i < this.SampleSize; i++)
        {
            if (this.WAVFile.NumSamplesRemaining > 0)
            {
                retval[i] = this.WAVFile.GetNextSampleAs16Bit();
                this.position++;
            }
        }
        return retval;
    }
}

FFT前向和FFT后向都能正常工作。你能否告诉我我的错误是什么。

1 个答案:

答案 0 :(得分:2)

不幸的是,即使在唱歌时,人声也不在“频率范围”。它通常有一个主频率和多个谐波跟随它,具体取决于音素。

使用此https://play.google.com/store/apps/details?id=radonsoft.net.spectralview&hl=en或类似应用查看我的意思 - 然后重新定义您的策略。还有谷歌'卡拉OK'效应。

NEXT:

从您的示例中可以看出这一点并不明显,但您应该在Windows中扫描整个文件(google'ftft windowing')以完整处理它。