FFT 音频输入 [英] FFT audio input

查看:26
本文介绍了FFT 音频输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对由 AudioRecorder 记录并保存到 wav 文件的信号应用 FFT.我使用的 FFT 有一个 Complex[] 输入参数.我很困惑,从字节转换为复数除以 32768 与仅将虚部加 0 并将实部保留为字节进行转换是否有区别?

I want to apply FFT on a signal recorded by AudioRecorder and saved to a wav file. the FFT i am using has a Complex[] input parameter. I am confused, is there a difference between converting from bytes to comlex dividing by 32768, and converting by just adding 0 to the imaginary part and leaving the real part as a byte?

public Complex[] convertToComplex(byte[] file)
{


    int size= file.length;
    double[]x=new double[size];
    Complex[]data= new Complex[size];
    for(int i=0;i<size;i++)
    {
        x[i]=file[i]/32768.0;
        data[i]=new Complex(x[i],0);
        //  Log.d("tag", "indice"+i+":"+data[i]);
    }
    return data;
}

推荐答案

如果您正在处理位深度为 16 位的音频(每个样本有 16 位),那么每个字节将只有一个样本的一半.什么您需要做的是将您的字节转换为 16 位样本,然后将结果数字除以 32768(这是 2 的补码 16 位数字可以存储的最小数字的大小,即 2^15)以获得实际的音频样本,这是一个-1 和 1 之间的数字.然后,您将通过将其虚部设置为 0 将该数字转换为复数.

If you are working with audio with a bit depth of 16 bits (each sample has 16 bits), then each byte will only have half of a sample.What you need to do is cast your bytes to 16 bit samples then divide the resulting number by 32768 (This is the magnitude of the smallest number a 2's complement 16 bit number can store i.e 2^15) to get the actual audio sample which is a number between -1 and 1.You will then convert this number to a complex number by setting it's imaginary component to 0.

可以在下面看到一个小的 C# 示例(指示性代码):

A small C# sample can be seen below (indicative code):

    byte[] myAudioBytes = readAudio();
    int numBytes = myAudioBytes.Length;

    var myAudioSamples = new List<short>();

    for( int i = 0; i < numBytes; i = i + 2)
    {
      //Cast to 16 bit audio and then add sample
       short sample = (short) ((myAudioBytes[i] << 8 | myAudioBytes[i + 1]) / 32768 ); 
       myAudioSamples.Add(sample);
    }

    //Change real audio to Complex audio

    var complexAudio = new Complex[myAudioSamples.Length];

    int i = 0;
    foreach(short sample in myAudioSamples)
       complexAudio[i++] = new Complex(){ Real = sample, Imaginary = 0 };

   //Now you can proceed to getting the FFT of your Audio here

希望代码能指导您如何处理音频.

Hope the code has guided you on how you should handle your audio.

这篇关于FFT 音频输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆