Android使用JTransform库可视化PCM数据

时间:2018-05-28 07:41:41

标签: android audio fft visualization

我试图显示一些PCM数据的一些可视化效果。 目标是显示如下内容:

enter image description here

我搜索并发现JTransform是正确使用的库。但是,我找不到如何使用这个库的好指南。如何将PCM数据转换为可用于绘制条形的波段/频率数据?

非常感谢。

1 个答案:

答案 0 :(得分:1)

PCM音频是模拟音频曲线的数字化简化...这个时域信号可以输入离散傅里叶变换api调用,将数据转换为其频域等效...虚数和欧拉公式是你的朋友

简单的部分是调用fft,它更多地涉及解析其输出...... 填充一个缓冲区至少1024(确保它的2的幂)从你的PCM点,并将其馈入一些fft api调用...这将返回给你它的频域等效...在任何一个指甲文件离散傅立叶变换api调用你使用...查找Nyquist限制的概念...频率仓的主要想法...保持每个缓冲区的样本数量和PCM音频的采样率

当你增加音频样本的数量(音频曲线上的PCM点)时,请注意你输入傅立叶变换,从该调用返回的频率分辨率越精细,但是如果你的音频是一些动态信号,如音乐(反对静音)这会降低时间特异性

这是我在golang中编写的一个函数,它是一个围绕DFT调用的包装器,在那里我向它提供一个归一化为浮点的PCM原始音频缓冲区,它从-1到+1变化,在那里它进行离散傅立叶变换(fft)然后调用然后使用从DFT返回的复数数组计算每个频率仓的大小...通过观看视频(一次一个图像)合成音频的项目的一部分然后它可以收听该音频以合成输出图像。 ..实现目标,其中输出照片很大程度上匹配输入照片......输入图像 - >音频 - >输出图像

func discrete_time_fourier_transform(aperiodic_audio_wave []float64, flow_data_spec *Flow_Spec) ([]discrete_fft, float64, float64, []float64) {

    min_freq := flow_data_spec.min_freq
    max_freq := flow_data_spec.max_freq

    //      https://www.youtube.com/watch?v=mkGsMWi_j4Q
    //      Discrete Fourier Transform - Simple Step by Step 

    var complex_fft []complex128

    complex_fft = fft.FFTReal(aperiodic_audio_wave) // input time domain ... output frequency domain of equally spaced freqs

    number_of_samples := float64(len(complex_fft))

    nyquist_limit_index := int(number_of_samples / 2)

    all_dft := make([]discrete_fft, 0) // 20171008

    /*
       0th term of complex_fft is sum of all other terms
       often called the bias shift
    */

    var curr_real, curr_imag, curr_mag, curr_theta, max_magnitude, min_magnitude float64

    max_magnitude = -999.0
    min_magnitude = 999.0
    min_magnitude = 999.0

    all_magnitudes := make([]float64, 0)

    curr_freq := 0.0
    incr_freq := flow_data_spec.sample_rate / number_of_samples

    for index, curr_complex := range complex_fft { // we really only use half this range + 1

        // if index <= nyquist_limit_index {
        if index <= nyquist_limit_index && curr_freq >= min_freq && curr_freq < max_freq {

            curr_real = real(curr_complex) // pluck out real portion of imaginary number
            curr_imag = imag(curr_complex) // ditto for im

            curr_mag = 2.0 * math.Sqrt(curr_real*curr_real+curr_imag*curr_imag) / number_of_samples

            curr_theta = math.Atan2(curr_imag, curr_real)

            curr_dftt := discrete_fft{

                real:      2.0 * curr_real,
                imaginary: 2.0 * curr_imag,
                magnitude: curr_mag,
                theta:     curr_theta,
            }

            if curr_dftt.magnitude > max_magnitude {

                max_magnitude = curr_dftt.magnitude
            }

            if curr_dftt.magnitude < min_magnitude {

                min_magnitude = curr_dftt.magnitude
            }

            // ... now stow it

            all_dft = append(all_dft, curr_dftt) 

            all_magnitudes = append(all_magnitudes, curr_mag)
        }

        curr_freq += incr_freq
    }

    return all_dft, max_magnitude, min_magnitude, all_magnitudes
}

现在你有一个数组all_magnitudes,其中数组的每个元素都是该频率仓的大小...每个频率仓均匀地由上面的var incr_freq定义的频率增量...使用min和max_magnitude对幅度进行归一化...它准备好进入X,Y图,为您提供频谱图可视化

我建议打开一些书......观看我在上面的评论中提到的视频......我自己进入傅立叶变换奇迹的过程一直在进行,因为它是一个EE本科并且它充满了令人惊讶的应用及其理论继续是一个非常活跃的研究领域