Naudio中的逆FFT

时间:2016-04-14 14:11:02

标签: c# mp3 fft naudio steganography

目前,我正在使用C#,相位编码方法和Naudio Library构建音频隐写程序。到目前为止,我已经获得了新阶段(phi)和Magnitude(使用SampleAggregator方法)。示例数据: Example

问题是,如何使用Naudio库进行逆FFT(IFFT)以及如何使用Magnitude和新阶段重建音频(或音频缓冲区)?

FFT方法

private void FFT()
{
    using (var ms = File.OpenRead("C:/MP3_TEMP/temp.mp3"))
    {                
        using (Mp3FileReader rdr = new Mp3FileReader(ms))
        {                    
            byte[] buffer = new byte[4096]; //4096 buffer size

            int bytesRead;
            int total = 0;                    
            int count = 0;                

            sampleAggregator.PerformFFT = true;                    
            sampleAggregator.FftCalculated += new EventHandler<FftEventArgs>(FftCalculated);                                       

            do
            {                        
                bytesRead = rdr.Read(buffer, 0, buffer.Length);          

                count++;
                sampleAggregator.Add(BitConverter.ToInt32(buffer,0));                           

                total += bytesRead;
            } while (bytesRead > 0);                                      

            sampleAggregator.PerformFFT = false;
        }
    }
}

FFTCalculated Method

void FftCalculated(object sender, FftEventArgs e)
{           
    // Do something with e.result!
    // string dummy for saving temporary result
    string dummy = "";

    int n = e.Result.Length;

    float[] delta_phi = new float[n]; // difference between phases

    float[] X_array = new float[n]; // X real
    float[] Y_array = new float[n]; // Y imaginary
    float[] A_array = new float[n]; // magnitude
    float[] secret_msg = new float[n]; // secret text, in bit
    float[] phi_proc = new float[n]; // new phi


    for (var i = 0; i < e.Result.Length; i++)
    {
        float X = e.Result[i].X;
        float Y = e.Result[i].Y;

        X_array[i] = X;
        Y_array[i] = Y;

        // Magnitude = sqrt(X^2 + Y^2)
        float A = (float)Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));                
        A_array[i] = A;

        // Phase = ArcTan(Y/X)
        float phi_ = Convert.ToSingle(Math.Atan2(Y,X));
        phi[i] = phi_;              
    }

    for (int i = 0; i < e.Result.Length; i++)
    {
        if (i < secret_msg_len)
        {
            delta_phi[i] = Math.Abs(phi[i + secret_msg_len] - phi[i]);

            if (secret_msg_bool[i] == 0)
                phi_proc[i] = (float)Math.PI / -2;
            else if (secret_msg_bool[i] == 1)
                phi_proc[i] = (float)Math.PI / 2;

            secret_msg[i] = secret_msg_bool[i];
        }
        else
        {
            phi_proc[i] = delta_phi[i - secret_msg_len] + phi_proc[i - secret_msg_len];
        }

        // Write result in a text file
        dummy += "\t| X : " + X_array[i];
        dummy += "\t| Y : " + Y_array[i];
        dummy += "\t| A : " + A_array[i];
        dummy += "\t| phi : " + phi[i];
        dummy += "\t | delta_phi : " + delta_phi[i];
        dummy += "\t | secret_msg : " + secret_msg[i]; 
        dummy += "\t | phi_proc : " + phi_proc[i]+ "\n";
        //dummy += "\t | delta_phi_proc : " + delta_phi_proc[i] + " \n";
    }

        System.IO.File.WriteAllText("C:/MP3_TEMP/temp.txt", dummy);
}

0 个答案:

没有答案